// API function to update a post. func patchApiPostHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) { userName := authentication.GetUserName(r) if userName != "" { userId, err := getUserId(userName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } userRole, err := getUserRole(userName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // Update post decoder := json.NewDecoder(r.Body) var json JsonPost err = decoder.Decode(&json) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var postSlug string // Get current slug of post post, err := database.RetrievePostById(json.Id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if post.Author.Id != userId && userRole != 4 { http.Error(w, "Not your post", http.StatusInternalServerError) return } if json.Slug != post.Slug { // Check if user has submitted a custom slug postSlug = slug.Generate(json.Slug, "posts") } else { postSlug = post.Slug } currentTime := time.Now() *post = structure.Post{Id: json.Id, Title: []byte(json.Title), Slug: postSlug, Markdown: []byte(json.Markdown), Html: conversion.SanitizeHtml([]byte(json.Html)), IsFeatured: json.IsFeatured, IsPage: json.IsPage, IsPublished: json.IsPublished, Image: []byte(json.Image), Date: ¤tTime, Tags: methods.GenerateTagsFromCommaString(json.Tags), Author: &structure.User{Id: userId}} err = methods.UpdatePost(post) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) w.Write([]byte("Post updated!")) return } else { http.Error(w, "Not logged in!", http.StatusInternalServerError) return } }
// API function to create a post func postApiPostHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) { userName := authentication.GetUserName(r) if userName != "" { userId, err := getUserId(userName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // Create post decoder := json.NewDecoder(r.Body) var json JsonPost err = decoder.Decode(&json) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var postSlug string if json.Slug != "" { // Ceck if user has submitted a custom slug postSlug = slug.Generate(json.Slug, "posts") } else { postSlug = slug.Generate(json.Title, "posts") } currentTime := time.Now() post := structure.Post{Title: []byte(json.Title), Slug: postSlug, Markdown: []byte(json.Markdown), Html: conversion.SanitizeHtml([]byte(json.Html)), IsFeatured: json.IsFeatured, IsPage: json.IsPage, IsPublished: json.IsPublished, Image: []byte(json.Image), Date: ¤tTime, Tags: methods.GenerateTagsFromCommaString(json.Tags), Author: &structure.User{Id: userId}} err = methods.SavePost(&post) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) w.Write([]byte("Post created!")) return } else { http.Error(w, "Not logged in!", http.StatusInternalServerError) return } }