probo/server.go
2023-06-28 17:21:59 +02:00

150 lines
3.9 KiB
Go

package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/logger"
"git.andreafazzi.eu/andrea/probo/models"
"git.andreafazzi.eu/andrea/probo/store"
"github.com/julienschmidt/httprouter"
)
type ProboCollectorServer struct {
store store.ProboCollectorStore
http.Handler
}
func NewProboCollectorServer(store store.ProboCollectorStore) *ProboCollectorServer {
ps := new(ProboCollectorServer)
ps.store = store
router := httprouter.New()
router.GET("/quizzes", httprouter.Handle(ps.readAllQuizzesHandler))
router.POST("/quizzes/create", httprouter.Handle(ps.createQuizHandler))
router.PUT("/quizzes/update/:id", httprouter.Handle(ps.updateQuizHandler))
ps.Handler = logger.WithLogging(ps.jsonHeaderMiddleware(router))
return ps
}
func (ps *ProboCollectorServer) jsonHeaderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
}
func (ps *ProboCollectorServer) readAllQuizzesHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
response := new(client.ReadAllQuizResponse)
quizzes, err := ps.readAllQuiz(w, r)
if err != nil {
response = &client.ReadAllQuizResponse{
BaseResponse: client.BaseResponse{Status: "error", Message: err.Error()},
Content: nil,
}
} else {
response = &client.ReadAllQuizResponse{
BaseResponse: client.BaseResponse{Status: "success", Message: ""},
Content: quizzes,
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
func (ps *ProboCollectorServer) createQuizHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
response := new(client.CreateQuizResponse)
quiz, err := ps.createQuiz(w, r)
if err != nil {
response = &client.CreateQuizResponse{
BaseResponse: client.BaseResponse{Status: "error", Message: err.Error()},
Content: nil,
}
}
response = &client.CreateQuizResponse{
BaseResponse: client.BaseResponse{Status: "success", Message: ""},
Content: quiz,
}
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(response)
}
func (ps *ProboCollectorServer) updateQuizHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
response := new(client.UpdateQuizResponse)
quiz, err := ps.updateQuiz(w, r, params.ByName("id"))
if err != nil {
response = &client.UpdateQuizResponse{
BaseResponse: client.BaseResponse{Status: "error", Message: err.Error()},
Content: nil,
}
} else {
response = &client.UpdateQuizResponse{
BaseResponse: client.BaseResponse{Status: "success", Message: ""},
Content: quiz,
}
}
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(response)
}
func (ps *ProboCollectorServer) readAllQuiz(w http.ResponseWriter, r *http.Request) ([]*models.Quiz, error) {
quizzes, err := ps.store.ReadAllQuizzes()
if err != nil {
return nil, err
}
return quizzes, nil
}
func (ps *ProboCollectorServer) updateQuiz(w http.ResponseWriter, r *http.Request, id string) (*models.Quiz, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
updateQuizReq := new(client.CreateUpdateQuizRequest)
err = json.Unmarshal(body, &updateQuizReq)
if err != nil {
return nil, err
}
updatedQuiz, err := ps.store.UpdateQuiz(updateQuizReq, id)
if err != nil {
return nil, err
}
return updatedQuiz, nil
}
func (ps *ProboCollectorServer) createQuiz(w http.ResponseWriter, r *http.Request) (*models.Quiz, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
createQuizReq := new(client.CreateUpdateQuizRequest)
err = json.Unmarshal(body, &createQuizReq)
if err != nil {
return nil, err
}
createdQuiz, err := ps.store.CreateQuiz(createQuizReq)
if err != nil {
return nil, err
}
return createdQuiz, nil
}