package api import ( "arimelody-web/controller" "arimelody-web/log" "arimelody-web/model" "encoding/json" "fmt" "net/http" "os" "strings" "time" ) func ServeAllBlogs(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) onlyVisible := true if session != nil && session.Account != nil { onlyVisible = false } posts, err := controller.GetBlogPosts(app.DB, onlyVisible, -1, 0) if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog posts: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } type ( BlogAuthor struct { ID string `json:"id"` Avatar string `json:"avatar"` } BlogPost struct { ID string `json:"id"` Title string `json:"title"` Description string `json:"description"` Author BlogAuthor `json:"author"` CreatedAt time.Time `json:"created_at"` } ) resPosts := []*BlogPost{} for _, post := range posts { author, err := controller.GetAccountByID(app.DB, post.AuthorID) if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to fetch author for blog post %s: %v\n", post.ID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } resPosts = append(resPosts, &BlogPost{ ID: post.ID, Title: post.Title, Description: post.Description, Author: BlogAuthor{ ID: author.Username, Avatar: author.AvatarURL.String, }, CreatedAt: post.CreatedAt, }) } err = json.NewEncoder(w).Encode(resPosts) if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to serve blog posts: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } }) } func ServeBlog(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) privileged := session != nil && session.Account != nil blogID := r.PathValue("id") blog, err := controller.GetBlogPost(app.DB, blogID) if err != nil { if strings.Contains(err.Error(), "no rows") { http.NotFound(w, r) return } fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } if !blog.Visible && !privileged { http.NotFound(w, r) return } err = json.NewEncoder(w).Encode(blog) if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to serve blog post %s: %v\n", blogID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } }) } func CreateBlog(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) var blog model.BlogPost err := json.NewDecoder(r.Body).Decode(&blog) if err != nil { http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } if blog.ID == "" { http.Error(w, "Post ID cannot be empty", http.StatusBadRequest) return } if blog.Title == "" { blog.Title = blog.ID } if !blog.CreatedAt.Equal(time.Unix(0, 0)) { blog.CreatedAt = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC) } blog.AuthorID = session.Account.ID err = controller.CreateBlogPost(app.DB, &blog) if err != nil { if strings.Contains(err.Error(), "duplicate key") { http.Error(w, fmt.Sprintf("Post %s already exists", blog.ID), http.StatusBadRequest) return } fmt.Printf("WARN: Failed to create blog %s: %s\n", blog.ID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" created by \"%s\".", blog.ID, session.Account.Username) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) err = json.NewEncoder(w).Encode(blog) if err != nil { fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %s\n", blog.ID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } }) } func UpdateBlog(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) blogID := r.PathValue("id") blog, err := controller.GetBlogPost(app.DB, blogID) if err != nil { if strings.Contains(err.Error(), "no rows") { http.NotFound(w, r) return } fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } err = json.NewDecoder(r.Body).Decode(blog) if err != nil { fmt.Printf("WARN: Failed to update blog %s: %s\n", blog.ID, err) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } err = controller.UpdateBlogPost(app.DB, blogID, blog) if err != nil { if strings.Contains(err.Error(), "no rows") { http.NotFound(w, r) return } fmt.Printf("WARN: Failed to update release %s: %s\n", blogID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" updated by \"%s\".", blog.ID, session.Account.Username) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) err = json.NewEncoder(w).Encode(blog) if err != nil { fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %s\n", blog.ID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } }) } func DeleteBlog(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) blogID := r.PathValue("id") rowsAffected, err := controller.DeleteBlogPost(app.DB, blogID) if err != nil { fmt.Printf("WARN: Failed to delete blog post %s: %s\n", blogID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } if rowsAffected == 0 { http.NotFound(w, r) return } app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" deleted by \"%s\".", blogID, session.Account.Username) }) }