-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |