Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonQiu21 committed Oct 8, 2023
2 parents f2b7f63 + 8375a6a commit 39350e8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
20 changes: 9 additions & 11 deletions backend/controllers/repos_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ func CreateRepo(w http.ResponseWriter, r *http.Request) {
repo.LastUpdated = utils.GetCurrentTime()
}


filter := utils.ConstructFilters(r, types.Repo{})

// Get data from database
data, err := db.GetReposByFilters(filter)
if err != nil {
log.Println(err)
}
if len(data) > 0 {
http.Error(w, "Error - duplicate entry", http.StatusBadRequest);
return
// 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
Expand Down
31 changes: 31 additions & 0 deletions backend/db/repos_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
26 changes: 13 additions & 13 deletions backend/types/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 39350e8

Please sign in to comment.