diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..787b44f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/backend/controllers/repos-controller.go b/backend/controllers/repos-controller.go deleted file mode 100644 index 04073bf..0000000 --- a/backend/controllers/repos-controller.go +++ /dev/null @@ -1,24 +0,0 @@ -package controllers - -import ( - "context" - "encoding/json" - "log" - "net/http" - - "github.com/g00gol/frieren/backend/db" - "go.mongodb.org/mongo-driver/bson" -) - -func GetRepos(w http.ResponseWriter, r *http.Request) { - reposColl := db.GetRepos() - var data bson.M - reposColl.FindOne(context.TODO(), bson.D{}).Decode(&data) - - // Return data as JSON - w.Header().Set("Content-Type", "application/json") - err := json.NewEncoder(w).Encode(data) - if err != nil { - log.Println(err) - } -} diff --git a/backend/controllers/repos_controller.go b/backend/controllers/repos_controller.go new file mode 100644 index 0000000..5cc7ce9 --- /dev/null +++ b/backend/controllers/repos_controller.go @@ -0,0 +1,41 @@ +package controllers + +import ( + "encoding/json" + "log" + "net/http" + + "github.com/g00gol/frieren/backend/db" + "github.com/g00gol/frieren/backend/types" + "github.com/g00gol/frieren/backend/utils" +) + +func GetRepos(w http.ResponseWriter, r *http.Request) { + // Get the filters from the request + filter := utils.ConstructFilters(r, types.Repo{}) + + // Get data from database + data, err := db.GetReposByFilters(filter) + if err != nil { + log.Println(err) + } + + // Return data as JSON + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(data) + if err != nil { + log.Println(err) + } +} + +func CreateRepo(w http.ResponseWriter, r *http.Request) { + var repo types.Repo + + // Decode the request body into repo + err := json.NewDecoder(r.Body).Decode(&repo) + if err != nil { + log.Println(err) + } + + // Insert repo into database +} diff --git a/backend/db/repos_db.go b/backend/db/repos_db.go index 5c67e73..54f75b3 100644 --- a/backend/db/repos_db.go +++ b/backend/db/repos_db.go @@ -1,10 +1,34 @@ package db import ( - "go.mongodb.org/mongo-driver/mongo" + "context" + "log" + + "github.com/g00gol/frieren/backend/types" + "go.mongodb.org/mongo-driver/mongo/options" ) -func GetRepos() *mongo.Collection { +func GetReposByFilters(filter any) ([]types.Repo, error) { + log.Println("Getting repos by filters", filter) collection := GetCollection("repos") - return collection + + // Specify options to use for string comparison + opts := options.Find().SetCollation(&options.Collation{ + Locale: "en", + Strength: 1, + }) + cursor, err := collection.Find(context.TODO(), filter, opts) + if err != nil { + log.Println("Error finding repos:", err) + return nil, err + } + defer cursor.Close(context.TODO()) + + var data []types.Repo + if err := cursor.All(context.Background(), &data); err != nil { + log.Println("Error decoding cursor results:", err) + return nil, err + } + + return data, err } diff --git a/backend/types/repo.go b/backend/types/repo.go new file mode 100644 index 0000000..ea2a702 --- /dev/null +++ b/backend/types/repo.go @@ -0,0 +1,19 @@ +package types + +import "time" + +type Repo struct { + Hash string `bson:"hash"` + Name string `bson:"name"` + Description string `bson:"description"` + RepoOrigin string `bson:"repo_origin"` + FernBranch string `bson:"fern_branch"` + Languages []string `bson:"languages"` + Technologies []string `bson:"technologies"` + RecommendedIssueLabels []string `bson:"recommended_issue_labels"` + RecommendedIssuesCount int `bson:"recommended_issues_count"` + Difficulty int `bson:"difficulty"` + LastUpdated time.Time `bson:"last_updated"` // MongoDB Datetime + DateCreated time.Time `bson:"date_created"` // MongoDB Datetime + Stars int `bson:"stars"` +} diff --git a/backend/utils/construct_filters.go b/backend/utils/construct_filters.go new file mode 100644 index 0000000..9b1d081 --- /dev/null +++ b/backend/utils/construct_filters.go @@ -0,0 +1,33 @@ +package utils + +import ( + "net/http" + "reflect" + "strings" + + "go.mongodb.org/mongo-driver/bson" +) + +func ConstructFilters(r *http.Request, model interface{}) bson.D { + val := reflect.ValueOf(model) + typ := val.Type() + + filter := bson.D{} + + for i := 0; i < typ.NumField(); i++ { + field := typ.Field(i) + param := strings.ToLower(field.Name) + + if values, ok := r.URL.Query()[param]; ok && len(values) > 0 { + if field.Type.Kind() == reflect.Slice { + // Split the comma-delimited string into a slice of strings + elements := strings.Split(values[0], ",") + filter = append(filter, bson.E{Key: param, Value: bson.D{{Key: "$in", Value: elements}}}) + } else { + filter = append(filter, bson.E{Key: param, Value: values[0]}) + } + } + } + + return filter +}