probo/server.go

151 lines
3.9 KiB
Go
Raw Normal View History

2022-06-23 11:25:35 +02:00
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
2022-06-24 18:56:06 +02:00
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/logger"
"git.andreafazzi.eu/andrea/probo/models"
"git.andreafazzi.eu/andrea/probo/store"
2022-06-28 13:49:35 +02:00
"github.com/julienschmidt/httprouter"
2022-06-23 11:25:35 +02:00
)
2022-06-29 16:08:55 +02:00
type ProboCollectorServer struct {
store store.ProboCollectorStore
2022-06-23 11:25:35 +02:00
http.Handler
}
2022-06-29 16:08:55 +02:00
func NewProboCollectorServer(store store.ProboCollectorStore) *ProboCollectorServer {
ps := new(ProboCollectorServer)
2022-06-23 11:25:35 +02:00
ps.store = store
2022-06-28 13:49:35 +02:00
router := httprouter.New()
2022-06-23 11:25:35 +02:00
2022-06-28 13:49:35 +02:00
router.GET("/quizzes", httprouter.Handle(ps.readAllQuizzesHandler))
router.POST("/quizzes/create", httprouter.Handle(ps.createQuizHandler))
2022-06-29 16:08:55 +02:00
router.PUT("/quizzes/update/:id", httprouter.Handle(ps.updateQuizHandler))
2022-06-23 11:25:35 +02:00
2022-06-29 16:08:55 +02:00
ps.Handler = logger.WithLogging(ps.jsonHeaderMiddleware(router))
2022-06-23 11:25:35 +02:00
return ps
}
2022-06-29 16:08:55 +02:00
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)
})
2022-06-28 13:49:35 +02:00
}
2022-06-23 11:25:35 +02:00
2022-06-29 16:08:55 +02:00
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)
2022-06-24 18:56:06 +02:00
2022-06-28 13:49:35 +02:00
quiz, err := ps.createQuiz(w, r)
if err != nil {
2022-06-29 16:08:55 +02:00
response = &client.CreateQuizResponse{
BaseResponse: client.BaseResponse{Status: "error", Message: err.Error()},
Content: nil,
}
2022-06-28 13:49:35 +02:00
}
2022-06-24 18:56:06 +02:00
2022-06-29 16:08:55 +02:00
response = &client.CreateQuizResponse{
BaseResponse: client.BaseResponse{Status: "success", Message: ""},
Content: quiz,
}
2022-06-24 18:56:06 +02:00
2022-06-28 13:49:35 +02:00
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(response)
}
2022-06-29 16:08:55 +02:00
func (ps *ProboCollectorServer) updateQuizHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
response := new(client.UpdateQuizResponse)
2022-06-28 13:49:35 +02:00
quiz, err := ps.updateQuiz(w, r, params.ByName("id"))
if err != nil {
2022-06-29 16:08:55 +02:00
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,
}
2022-06-23 11:25:35 +02:00
}
2022-06-28 13:49:35 +02:00
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(response)
2022-06-23 11:25:35 +02:00
}
2022-06-29 16:08:55 +02:00
func (ps *ProboCollectorServer) readAllQuiz(w http.ResponseWriter, r *http.Request) ([]*models.Quiz, error) {
quizzes, err := ps.store.ReadAllQuizzes()
2022-06-23 11:25:35 +02:00
if err != nil {
2022-06-29 16:08:55 +02:00
return nil, err
2022-06-23 11:25:35 +02:00
}
2022-06-29 16:08:55 +02:00
return quizzes, nil
2022-06-23 11:25:35 +02:00
}
2022-06-29 16:08:55 +02:00
func (ps *ProboCollectorServer) updateQuiz(w http.ResponseWriter, r *http.Request, id string) (*models.Quiz, error) {
2022-06-28 13:49:35 +02:00
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
2022-06-29 16:08:55 +02:00
updateQuizReq := new(client.CreateUpdateQuizRequest)
2022-06-28 13:49:35 +02:00
err = json.Unmarshal(body, &updateQuizReq)
if err != nil {
return nil, err
}
2022-06-29 16:08:55 +02:00
updatedQuiz, err := ps.store.UpdateQuiz(updateQuizReq, id)
2022-06-28 13:49:35 +02:00
if err != nil {
return nil, err
}
return updatedQuiz, nil
}
2022-06-29 16:08:55 +02:00
func (ps *ProboCollectorServer) createQuiz(w http.ResponseWriter, r *http.Request) (*models.Quiz, error) {
2022-06-23 11:25:35 +02:00
body, err := ioutil.ReadAll(r.Body)
if err != nil {
2022-06-24 18:56:06 +02:00
return nil, err
2022-06-23 11:25:35 +02:00
}
2022-06-24 18:56:06 +02:00
2022-06-29 16:08:55 +02:00
createQuizReq := new(client.CreateUpdateQuizRequest)
2022-06-24 18:56:06 +02:00
2022-06-23 11:25:35 +02:00
err = json.Unmarshal(body, &createQuizReq)
if err != nil {
2022-06-24 18:56:06 +02:00
return nil, err
2022-06-23 11:25:35 +02:00
}
2022-06-24 18:56:06 +02:00
createdQuiz, err := ps.store.CreateQuiz(createQuizReq)
if err != nil {
return nil, err
}
2022-06-23 11:25:35 +02:00
2022-06-24 18:56:06 +02:00
return createdQuiz, nil
2022-06-23 11:25:35 +02:00
}