better hierarchy and better way to make a CRUD
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blog/driver"
|
||||
models "blog/models"
|
||||
repository "blog/repository"
|
||||
post "blog/repository/post"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
// NewPostHandler ...
|
||||
func NewPostHandler(db *driver.DB) *Post {
|
||||
return &Post{
|
||||
repo: post.NewSQLPostRepo(db.SQL),
|
||||
}
|
||||
}
|
||||
|
||||
// Post ...
|
||||
type Post struct {
|
||||
repo repository.PostRepo
|
||||
}
|
||||
|
||||
// Fetch ...
|
||||
func (p *Post) Fetch(w http.ResponseWriter, r *http.Request) {
|
||||
payload, _ := p.repo.Fetch(r.Context(), 25)
|
||||
|
||||
respondwithJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
||||
// Create a new post
|
||||
func (p *Post) Create(w http.ResponseWriter, r *http.Request) {
|
||||
post := models.Post{}
|
||||
json.NewDecoder(r.Body).Decode(&post)
|
||||
|
||||
newID, err := p.repo.Create(r.Context(), &post)
|
||||
fmt.Println(newID)
|
||||
if err != nil {
|
||||
respondWithError(w, http.StatusInternalServerError, "Server Error")
|
||||
}
|
||||
respondwithJSON(w, http.StatusCreated, map[string]string{"message": "Successfully Created"})
|
||||
}
|
||||
|
||||
// Update a post by id
|
||||
func (p *Post) Update(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
data := models.Post{ID: int(id)}
|
||||
json.NewDecoder(r.Body).Decode(&data)
|
||||
payload, err := p.repo.Update(r.Context(), &data)
|
||||
|
||||
if err != nil {
|
||||
respondWithError(w, http.StatusInternalServerError, "Server Error")
|
||||
}
|
||||
|
||||
respondwithJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
||||
// GetByID returns a post details
|
||||
func (p *Post) GetByID(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
payload, err := p.repo.GetByID(r.Context(), int64(id))
|
||||
|
||||
if err != nil {
|
||||
respondWithError(w, http.StatusNoContent, "Content not found")
|
||||
}
|
||||
|
||||
respondwithJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
||||
// Delete a post
|
||||
func (p *Post) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
_, err := p.repo.Delete(r.Context(), int64(id))
|
||||
|
||||
if err != nil {
|
||||
respondWithError(w, http.StatusInternalServerError, "Server Error")
|
||||
}
|
||||
|
||||
respondwithJSON(w, http.StatusMovedPermanently, map[string]string{"message": "Delete Successfully"})
|
||||
}
|
||||
|
||||
// respondwithJSON write json response format
|
||||
func respondwithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
||||
response, _ := json.Marshal(payload)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(response)
|
||||
}
|
||||
|
||||
// respondwithError return error message
|
||||
func respondWithError(w http.ResponseWriter, code int, msg string) {
|
||||
respondwithJSON(w, code, map[string]string{"message": msg})
|
||||
}
|
||||
Reference in New Issue
Block a user