probo/server.go

120 lines
3.1 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
)
const jsonContentType = "application/json"
type QuizHubCollectorServer struct {
store store.QuizHubCollectorStore
http.Handler
}
func NewQuizHubCollectorServer(store store.QuizHubCollectorStore) *QuizHubCollectorServer {
ps := new(QuizHubCollectorServer)
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))
router.POST("/quizzes/:id/update", httprouter.Handle(ps.updateQuizHandler))
// router.Handle("/quizzes", logger.WithLogging(http.HandlerFunc(ps.testHandler)))
2022-06-23 11:25:35 +02:00
2022-06-28 13:49:35 +02:00
ps.Handler = logger.WithLogging(router)
2022-06-23 11:25:35 +02:00
return ps
}
2022-06-28 13:49:35 +02:00
func (ps *QuizHubCollectorServer) readAllQuizzesHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
w.Header().Set("content-type", jsonContentType)
json.NewEncoder(w).Encode(ps.readAllQuizzes(w, r))
}
2022-06-23 11:25:35 +02:00
2022-06-28 13:49:35 +02:00
func (ps *QuizHubCollectorServer) createQuizHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
response := new(client.Response)
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 {
response = &client.Response{Status: "error", Content: err.Error()}
}
2022-06-24 18:56:06 +02:00
2022-06-28 13:49:35 +02:00
response = &client.Response{Status: "success", 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)
}
func (ps *QuizHubCollectorServer) updateQuizHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
response := new(client.Response)
quiz, err := ps.updateQuiz(w, r, params.ByName("id"))
if err != nil {
response = &client.Response{Status: "error", Content: err.Error()}
2022-06-23 11:25:35 +02:00
}
2022-06-28 13:49:35 +02:00
response = &client.Response{Status: "success", Content: quiz}
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(response)
2022-06-23 11:25:35 +02:00
}
func (ps *QuizHubCollectorServer) readAllQuizzes(w http.ResponseWriter, r *http.Request) *client.Response {
tests, err := ps.store.ReadAllQuizzes()
if err != nil {
return &client.Response{Status: "error", Content: err.Error()}
}
return &client.Response{Status: "success", Content: tests}
}
2022-06-28 13:49:35 +02:00
func (ps *QuizHubCollectorServer) 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.UpdateQuizRequest)
err = json.Unmarshal(body, &updateQuizReq)
if err != nil {
return nil, err
}
updatedQuiz, err := ps.store.UpdateQuiz(updateQuizReq)
if err != nil {
return nil, err
}
return updatedQuiz, nil
}
2022-06-24 18:56:06 +02:00
func (ps *QuizHubCollectorServer) 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-23 11:25:35 +02:00
createQuizReq := new(client.CreateQuizRequest)
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
}