Skip to content

Commit

Permalink
Merge branch 'main' into frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
g00gol committed Oct 8, 2023
2 parents 0773869 + 39350e8 commit 10121d8
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 90 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"`
}
6 changes: 5 additions & 1 deletion frieren-cli/frieren-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

fern = {"name": re.search(r"/([^/]*/[^/]*)$","https://github.com/g00gol/frieren").group(1), "technologies": technologies, "difficulty": difficulty, "description": desc, "recommended_issue_labels": recommended_issue_labels}

fern['repo_origin'] = origin
fern['repo_origin'] = origin
fern['fern_branch'] = repo.active_branch.name

print(fern)

# Make api call
r = requests.post("http://127.0.0.1:8080/repos", json=fern)
Expand All @@ -41,4 +44,5 @@
else:
with open("open-source.fern", "w+") as f:
del fern['repo_origin']
del fern['fern_branch']
json.dump(fern, f)
2 changes: 1 addition & 1 deletion open-source.fern
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name": "g00gol/frieren", "technologies": ["MongoDB", "Rust", "Go", "Python", "JavaScript"], "difficulty": 3, "description": "Open source project finder", "recommended_issue_labels": ["easy", "start-here", "Good First Issue"]}
{"name": "g00gol/frieren", "technologies": ["MongoDB"], "difficulty": 3, "description": "test", "recommended_issue_labels": ["good first issue"]}
114 changes: 114 additions & 0 deletions server-update-tool/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion server-update-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ chrono = "0.4"
serde = "1"
url = "2.4"
reqwest = { version = "0.11", features = ["json"] }
serde_json = "1.0.107"
serde_json = "1.0.107"
futures = "0.3"
log = "0.4"
env_logger = "0.10"
more-asserts = "0.3"
11 changes: 8 additions & 3 deletions server-update-tool/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use chrono::{DateTime, Utc};
use std::env;
use std::error::Error;
use serde::{Serialize, Deserialize};
use log::{debug};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Repo {
Expand Down Expand Up @@ -48,20 +49,24 @@ async fn get_repos_collection() -> Result<Collection<Repo>, Box<dyn Error>> {
}

pub async fn get_repos() -> Result<Cursor<Repo>, Box<dyn Error>> {
debug!("Attempting to query DB");
let collection = get_repos_collection().await?;
let cursor = collection.find(None, None).await?;

debug!("Successully queried DB");
return Ok(cursor);
}

pub async fn update_repo(id: &ObjectId, new_repo: &Repo) -> Result<(), Box<dyn Error>> {
pub async fn update_repo(new_repo: &Repo) -> Result<(), Box<dyn Error>> {
debug!("Attempting to update DB");
let col = get_repos_collection().await?;
let update_result = col.find_one_and_replace(
col.find_one_and_replace(
doc!{
"_id": id
"_id": new_repo._id
},
new_repo,
FindOneAndReplaceOptions::builder().build()
).await?;
debug!("Successfully updated DB");
return Ok(());
}
Loading

0 comments on commit 10121d8

Please sign in to comment.