added comments
This commit is contained in:
@ -12,6 +12,7 @@ import (
|
||||
"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 {
|
||||
@ -45,6 +46,7 @@ func (s Service) SavePost(w http.ResponseWriter, r *http.Request) {
|
||||
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
|
||||
|
||||
@ -63,6 +65,7 @@ func (s Service) GetAllPosts(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
|
@ -2,10 +2,12 @@ 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