Some fixes
This commit is contained in:
@@ -5,12 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||||
"github.com/CalebQ42/darkstorm-server/internal/blog"
|
"github.com/CalebQ42/darkstorm-server/internal/blog"
|
||||||
"github.com/google/uuid"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -166,9 +166,10 @@ func editorEdit(w http.ResponseWriter, r *http.Request) {
|
|||||||
if blogID == "new" {
|
if blogID == "new" {
|
||||||
bl = &blog.Blog{}
|
bl = &blog.Blog{}
|
||||||
} else {
|
} else {
|
||||||
bl, err = blogApp.Blog(r.Context(), r.URL.Query().Get("blog"), false)
|
bl, err = blogApp.Blog(r.Context(), blogID, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error getting blog for editor:", err)
|
log.Println("error getting blog for editor:", err)
|
||||||
|
log.Println(blogID)
|
||||||
sendContent(w, r, "ERROR", "", "")
|
sendContent(w, r, "ERROR", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -210,14 +211,11 @@ func editorPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if newBlog.ID == "" {
|
if newBlog.ID == "" {
|
||||||
var id uuid.UUID
|
newBlog.ID = strings.TrimSpace(strings.ToLower(strings.ReplaceAll(newBlog.ID, " ", "-")))
|
||||||
id, err = uuid.NewV7()
|
if blogApp.Contains(r.Context(), newBlog.ID) {
|
||||||
if err != nil {
|
sendContent(w, r, "<p>Title is not unique!</p>", "", "")
|
||||||
log.Println("error creating new blog ID:", err)
|
|
||||||
sendContent(w, r, "<p>Server error</p>", "", "")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newBlog.ID = id.String()
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
newBlog.CreateTime = now.Unix()
|
newBlog.CreateTime = now.Unix()
|
||||||
newBlog.Author = usr.Username
|
newBlog.Author = usr.Username
|
||||||
|
|||||||
@@ -109,6 +109,11 @@ func (b *BlogApp) Blog(ctx context.Context, ID string, useCache bool) (*Blog, er
|
|||||||
return &blog, nil
|
return &blog, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BlogApp) Contains(ctx context.Context, ID string) bool {
|
||||||
|
res := b.blogCol.FindOne(ctx, bson.M{"_id": ID, "draft": false})
|
||||||
|
return res.Err() == nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BlogApp) CleanCache(ID string) {
|
func (b *BlogApp) CleanCache(ID string) {
|
||||||
time.Sleep(5 * time.Minute)
|
time.Sleep(5 * time.Minute)
|
||||||
b.cacheMutex.Lock()
|
b.cacheMutex.Lock()
|
||||||
|
|||||||
Reference in New Issue
Block a user