From a77d55924de2b9236f3f43f470448181f5d956f9 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Tue, 2 Jul 2024 20:52:55 -0500 Subject: [PATCH] Finished blog stuff --- internal/backend/darkstorm.go | 2 +- internal/backend/{embed => }/robots.txt | 0 internal/blog/README.md | 2 +- internal/blog/author.go | 64 ++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 4 deletions(-) rename internal/backend/{embed => }/robots.txt (100%) diff --git a/internal/backend/darkstorm.go b/internal/backend/darkstorm.go index 0a264e8..72fc864 100644 --- a/internal/backend/darkstorm.go +++ b/internal/backend/darkstorm.go @@ -11,7 +11,7 @@ import ( "time" ) -//go:embed embed/* +//go:embed robots.txt var robotEmbed embed.FS // A simple backend that handles user authentication, user count, and crash reports. diff --git a/internal/backend/embed/robots.txt b/internal/backend/robots.txt similarity index 100% rename from internal/backend/embed/robots.txt rename to internal/backend/robots.txt diff --git a/internal/blog/README.md b/internal/blog/README.md index cdbe1e1..8277f22 100644 --- a/internal/blog/README.md +++ b/internal/blog/README.md @@ -41,7 +41,7 @@ Must have a auth token for a user with the `"blog": "admin"` permission. ```json { - id: "authorID", + name: "author name", about: "about", picurl: "picture URL" } diff --git a/internal/blog/author.go b/internal/blog/author.go index 8fd2052..66133c5 100644 --- a/internal/blog/author.go +++ b/internal/blog/author.go @@ -5,6 +5,8 @@ import ( "encoding/json" "log" "net/http" + "strconv" + "strings" "github.com/CalebQ42/darkstorm-server/internal/backend" "go.mongodb.org/mongo-driver/bson" @@ -71,7 +73,35 @@ func (b *BlogApp) addAuthorInfo(w http.ResponseWriter, r *http.Request) { backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application is unauthorized") return } - //TODO + var newAuth Author + err = json.NewDecoder(r.Body).Decode(&newAuth) + r.Body.Close() + if err != nil { + backend.ReturnError(w, http.StatusBadRequest, "badRequest", "Invalid request") + return + } + for i := 1; ; i++ { + newID := strings.ReplaceAll(newAuth.Name, " ", "-") + if i != 1 { + newID += strconv.Itoa(i) + } + collisionCheck := b.authCol.FindOne(context.Background(), bson.M{"name": newAuth.Name}) + if collisionCheck.Err() == mongo.ErrNoDocuments { + newAuth.ID = newID + break + } else if collisionCheck.Err() != nil { + log.Println("error checking for new author ID collisions:", err) + backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error") + return + } + } + _, err = b.authCol.InsertOne(context.Background(), newAuth) + if err != nil { + log.Println("error inserting new author:", err) + backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error") + return + } + w.WriteHeader(http.StatusCreated) } func (b *BlogApp) updateAuthorInfo(w http.ResponseWriter, r *http.Request) { @@ -89,5 +119,35 @@ func (b *BlogApp) updateAuthorInfo(w http.ResponseWriter, r *http.Request) { backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application is unauthorized") return } - //TODO + var rawUpd map[string]string + err = json.NewDecoder(r.Body).Decode(&rawUpd) + r.Body.Close() + if err != nil { + backend.ReturnError(w, http.StatusBadRequest, "badRequest", "Invalid request") + return + } + actlUpd := make(map[string]string) + if rawUpd["name"] != "" { + actlUpd["name"] = rawUpd["name"] + } + if rawUpd["about"] != "" { + actlUpd["about"] = rawUpd["about"] + } + if rawUpd["picurl"] != "" { + actlUpd["picurl"] = rawUpd["picurl"] + } + res, err := b.authCol.UpdateByID(context.Background(), r.PathValue("authorID"), actlUpd) + if err != nil { + if err == mongo.ErrNoDocuments { + backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found") + } else { + backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error") + } + return + } + if res.MatchedCount == 0 { + backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found") + return + } + w.WriteHeader(http.StatusCreated) }