From 9edd91148a09eb06971fb3ec636e7768ddd111c7 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sat, 16 Nov 2024 07:19:16 -0600 Subject: [PATCH] Fixing some stuff --- blog.go | 2 +- editor.go | 4 ++-- internal/blog/blog.go | 37 +++++++++++++++++++++++++------------ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/blog.go b/blog.go index c5607e3..e3cd79b 100644 --- a/blog.go +++ b/blog.go @@ -29,7 +29,7 @@ func latestBlogsHandle(w http.ResponseWriter, r *http.Request) { } func blogHandle(w http.ResponseWriter, r *http.Request, blog string) { - bl, err := blogApp.Blog(r.Context(), blog, true) + bl, err := blogApp.Blog(r.Context(), blog) if err != nil { if err == backend.ErrNotFound { w.WriteHeader(404) diff --git a/editor.go b/editor.go index f2f1edd..192fb4c 100644 --- a/editor.go +++ b/editor.go @@ -166,7 +166,7 @@ func editorEdit(w http.ResponseWriter, r *http.Request) { if blogID == "new" { bl = &blog.Blog{} } else { - bl, err = blogApp.Blog(r.Context(), blogID, false) + bl, err = blogApp.AnyBlog(r.Context(), blogID) if err != nil { log.Println("error getting blog for editor:", err) log.Println(blogID) @@ -250,7 +250,7 @@ func editorPost(w http.ResponseWriter, r *http.Request) { sendContent(w, r, "

Server error updating blog

", "", "") return } - old, err := blogApp.Blog(r.Context(), newBlog.ID, false) + old, err := blogApp.AnyBlog(r.Context(), newBlog.ID) if err != nil { log.Println("error getting old blog to be updated:", err) sendContent(w, r, "

Updated!

", "", "") diff --git a/internal/blog/blog.go b/internal/blog/blog.go index ed93b0d..6bd8e04 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -79,16 +79,12 @@ func (b *BlogApp) GetAuthor(ctx context.Context, blog *Blog) (*Author, error) { return &author, err } -func (b *BlogApp) Blog(ctx context.Context, ID string, useCache bool) (*Blog, error) { - var blog Blog - if useCache { - b.cacheMutex.RLock() - var has bool - blog, has = b.blogCache[ID] - b.cacheMutex.RUnlock() - if has { - return &blog, nil - } +func (b *BlogApp) Blog(ctx context.Context, ID string) (*Blog, error) { + b.cacheMutex.RLock() + blog, has := b.blogCache[ID] + b.cacheMutex.RUnlock() + if has { + return &blog, nil } res := b.blogCol.FindOne(ctx, bson.M{"_id": ID, "draft": false}) if res.Err() != nil { @@ -109,8 +105,25 @@ func (b *BlogApp) Blog(ctx context.Context, ID string, useCache bool) (*Blog, er return &blog, nil } +func (b *BlogApp) AnyBlog(ctx context.Context, ID string) (*Blog, error) { + res := b.blogCol.FindOne(ctx, bson.M{"_id": ID}) + if res.Err() != nil { + if res.Err() == mongo.ErrNoDocuments { + return nil, backend.ErrNotFound + } + return nil, res.Err() + } + var blog Blog + err := res.Decode(&blog) + if err != nil { + return nil, err + } + b.ConvertBlog(&blog) + return &blog, nil +} + func (b *BlogApp) Contains(ctx context.Context, ID string) bool { - res := b.blogCol.FindOne(ctx, bson.M{"_id": ID, "draft": false}) + res := b.blogCol.FindOne(ctx, bson.M{"_id": ID}) return res.Err() == nil } @@ -127,7 +140,7 @@ func (b *BlogApp) reqBlog(w http.ResponseWriter, r *http.Request) { backend.ReturnError(w, http.StatusBadRequest, "badRequest", "Must provide a blogID") return } - blog, err := b.Blog(r.Context(), blogID, true) + blog, err := b.Blog(r.Context(), blogID) if err != nil { if err == backend.ErrNotFound { backend.ReturnError(w, http.StatusNotFound, "notFound", "Not blog found with the given ID")