From 7610e6f05246726a7a780fd639385240fbad1ed9 Mon Sep 17 00:00:00 2001 From: g00gol Date: Sun, 8 Oct 2023 01:51:29 -0400 Subject: [PATCH 1/2] adjusted repo type to support json input --- backend/types/repo.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/backend/types/repo.go b/backend/types/repo.go index ea2a702..ee6554f 100644 --- a/backend/types/repo.go +++ b/backend/types/repo.go @@ -3,17 +3,17 @@ 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"` + Hash string `bson:"hash" json:"hash"` + Name string `bson:"name" json:"name"` + Description string `bson:"description" json:"description"` + RepoOrigin string `bson:"repo_origin" json:"repo_origin"` + FernBranch string `bson:"fern_branch" json:"fern_branch"` + Languages []string `bson:"languages" json:"languages"` + Technologies []string `bson:"technologies" json:"technologies"` + RecommendedIssueLabels []string `bson:"recommended_issue_labels" json:"recommended_issue_labels"` + RecommendedIssuesCount int `bson:"recommended_issues_count" json:"recommended_issues_count"` + Difficulty int `bson:"difficulty" json:"difficulty"` + LastUpdated time.Time `bson:"last_updated" json:"last_updated"` + DateCreated time.Time `bson:"date_created" json:"date_created"` + Stars int `bson:"stars" json:"stars"` } From ff90550f54b82077fc44d3c91377a86fc208b479 Mon Sep 17 00:00:00 2001 From: g00gol Date: Sun, 8 Oct 2023 02:07:04 -0400 Subject: [PATCH 2/2] backend now handles duplicate repo name entries --- backend/controllers/repos_controller.go | 11 +++++++++ backend/db/repos_db.go | 31 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/backend/controllers/repos_controller.go b/backend/controllers/repos_controller.go index 819a5d9..2f7685a 100644 --- a/backend/controllers/repos_controller.go +++ b/backend/controllers/repos_controller.go @@ -44,6 +44,17 @@ func CreateRepo(w http.ResponseWriter, r *http.Request) { repo.LastUpdated = utils.GetCurrentTime() } + // If the repo already exists, remove the old repo and insert the new one + oldRepo, err := db.GetRepoByName(repo.Name) + if err == nil { + // Delete old repo + _, err = db.DeleteRepoByName(oldRepo.Name) + if err != nil { + http.Error(w, "Error deleting old repo", http.StatusInternalServerError) + return + } + } + // Insert repo into database _, err = db.GetCollection("repos").InsertOne(context.TODO(), repo) if err != nil { diff --git a/backend/db/repos_db.go b/backend/db/repos_db.go index a91f380..1d70343 100644 --- a/backend/db/repos_db.go +++ b/backend/db/repos_db.go @@ -5,6 +5,7 @@ import ( "log" "github.com/g00gol/frieren/backend/types" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -31,3 +32,33 @@ func GetReposByFilters(filter any) ([]types.Repo, error) { return data, err } + +func GetRepoByName(name string) (types.Repo, error) { + collection := GetCollection("repos") + + var data types.Repo + + filter := bson.D{{Key: "name", Value: name}} + err := collection.FindOne(context.TODO(), filter).Decode(&data) + + if err != nil { + log.Println("Error finding repo:", err) + return types.Repo{}, err + } + + log.Println("Found repo:", data) + return data, err +} + +func DeleteRepoByName(name string) (int64, error) { + collection := GetCollection("repos") + + filter := bson.D{{Key: "name", Value: name}} + result, err := collection.DeleteOne(context.TODO(), filter) + if err != nil { + log.Println("Error deleting repo:", err) + return 0, err + } + + return result.DeletedCount, err +}