Create and update Blogs

Started work on author info
This commit is contained in:
Caleb Gardner
2024-07-02 01:45:07 -05:00
parent 5af5de7719
commit 68edbca349
5 changed files with 160 additions and 33 deletions
+46 -8
View File
@@ -2,6 +2,7 @@ package blog
import (
"context"
"encoding/json"
"log"
"net/http"
@@ -35,21 +36,58 @@ func (b *BlogApp) AboutMe() (*Author, error) {
return &aboutMe, nil
}
func (b *BlogApp) GetAuthorInfo(w http.ResponseWriter, r *http.Request) {
func (b *BlogApp) reqAuthorInfo(w http.ResponseWriter, r *http.Request) {
res := b.authCol.FindOne(context.Background(), r.PathValue("authorID"))
if res.Err() == mongo.ErrNoDocuments {
backend.ReturnError(w, http.StatusNotFound, "notFound", "Author with ID "+r.PathValue("authorID")+" not found")
return
} else if res.Err() != nil {
log.Println("error getting author info:", res.Err())
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
return
}
var auth Author
err := res.Decode(&auth)
if err != nil {
log.Println("error decoding author info:", err)
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
return
}
json.NewEncoder(w).Encode(auth)
}
func (b *BlogApp) addAuthorInfo(w http.ResponseWriter, r *http.Request) {
hdr, err := b.back.VerifyHeader(w, r, "blogManagement", false)
if hdr == nil {
if err != nil {
log.Println("request key parsing error:", err)
}
return
} else if hdr.Key.AppID != "blog" {
backend.ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application is unauthorized")
return
}
if hdr.User == nil || hdr.User.Perm["blog"] != "admin" {
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application is unauthorized")
return
}
//TODO
}
func (b *BlogApp) SetAuthorInfo(w http.ResponseWriter, r *http.Request) {
hdr, err := b.back.VerifyHeader(w, r, "managment", true)
func (b *BlogApp) updateAuthorInfo(w http.ResponseWriter, r *http.Request) {
hdr, err := b.back.VerifyHeader(w, r, "blogManagement", false)
if hdr == nil {
if err != nil {
log.Println("error verifying apiKey:", err)
log.Println("request key parsing error:", err)
}
return
}
if hdr.Key.AppID != "blog" {
backend.ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
} else if hdr.Key.AppID != "blog" {
backend.ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application is unauthorized")
return
}
if hdr.User == nil || hdr.User.Perm["blog"] != "admin" {
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application is unauthorized")
return
}
//TODO
}