Skip to content

Commit

Permalink
progress on GET
Browse files Browse the repository at this point in the history
  • Loading branch information
g00gol committed Oct 8, 2023
1 parent 9b7aae0 commit d672b56
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 27 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
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": []
}
24 changes: 0 additions & 24 deletions backend/controllers/repos-controller.go

This file was deleted.

41 changes: 41 additions & 0 deletions backend/controllers/repos_controller.go
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
}
30 changes: 27 additions & 3 deletions backend/db/repos_db.go
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
}
19 changes: 19 additions & 0 deletions backend/types/repo.go
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"`
}
33 changes: 33 additions & 0 deletions backend/utils/construct_filters.go
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
}

0 comments on commit d672b56

Please sign in to comment.