serve frontend from go
This commit is contained in:
93
internal/posts/controller.go
Normal file
93
internal/posts/controller.go
Normal file
@ -0,0 +1,93 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/auth"
|
||||
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// SavePost handles saving a new post to the database after extracting user claims and decoding the request body.
|
||||
func (s Service) SavePost(w http.ResponseWriter, r *http.Request) {
|
||||
claims, ok := auth.ExtractClaims(r.Context())
|
||||
if !ok {
|
||||
log.Println("Err could not ExtractClaims")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var post model.Post
|
||||
if err := json.NewDecoder(r.Body).Decode(&post); err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
post.UserID = claims.UserID
|
||||
|
||||
if err := s.db.Save(&post).Error; err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := json.Marshal(&post)
|
||||
if err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(res)
|
||||
}
|
||||
|
||||
// GetAllPosts retrieves all posts from the database, eager-loads comments, orders them by creation time, and returns them as JSON.
|
||||
func (s Service) GetAllPosts(w http.ResponseWriter, r *http.Request) {
|
||||
var posts []model.Post
|
||||
|
||||
if err := s.db.Preload("Comments").Order("created_at DESC").Find(&posts).Error; err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := json.Marshal(&posts)
|
||||
if err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(res)
|
||||
}
|
||||
|
||||
// DeletePost handles deleting a post from the database based on its ID and user authentication.
|
||||
func (s Service) DeletePost(w http.ResponseWriter, r *http.Request) {
|
||||
idStr, ok := mux.Vars(r)["postID"]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
claims, ok := auth.ExtractClaims(r.Context())
|
||||
if !ok {
|
||||
log.Println("Err could not ExtractClaims")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.db.Where("user_id = ?", claims.UserID).Delete(&model.Post{}, id).Error
|
||||
if err != nil {
|
||||
fmt.Fprint(w, err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
13
internal/posts/resource.go
Normal file
13
internal/posts/resource.go
Normal file
@ -0,0 +1,13 @@
|
||||
package posts
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Service Represents a service with a database connection.
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// Service New creates a new Service instance, initializing it with a GORM database connection.
|
||||
func New(db *gorm.DB) *Service {
|
||||
return &Service{db: db}
|
||||
}
|
Reference in New Issue
Block a user