161 lines
3.4 KiB
Go
161 lines
3.4 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"gitea.deepak.science/deepak/gogmagog/models"
|
|
"gitea.deepak.science/deepak/gogmagog/tokens"
|
|
"github.com/go-chi/chi"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
// NewCurrentPlanRouter returns a new primary plan router
|
|
func NewCurrentPlanRouter(m *models.Model) http.Handler {
|
|
router := chi.NewRouter()
|
|
router.Get("/", getCurrentPlanFunc(m))
|
|
router.Post("/", postCurrentPlanFunc(m))
|
|
router.Put("/", putCurrentPlanFunc(m))
|
|
return router
|
|
}
|
|
|
|
func getCurrentPlanFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID, userErr := tokens.GetUserID(r.Context())
|
|
if userErr != nil {
|
|
unauthorizedHandler(w, r)
|
|
return
|
|
}
|
|
|
|
pp, err := m.CurrentPlan(userID)
|
|
if err != nil {
|
|
if models.IsNotFoundError(err) {
|
|
notFoundHandler(w, r)
|
|
return
|
|
}
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(pp); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type createCurrentPlanResponse struct {
|
|
CreatedCurrentPlan *models.CurrentPlan `json:"created_current_plan"`
|
|
}
|
|
|
|
func postCurrentPlanFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID, err := tokens.GetUserID(r.Context())
|
|
if err != nil {
|
|
unauthorizedHandler(w, r)
|
|
return
|
|
}
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
var pp models.CurrentPlan
|
|
err = dec.Decode(&pp)
|
|
if err != nil {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
err = dec.Decode(&struct{}{})
|
|
if err != io.EOF {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
|
|
newPP := &models.CurrentPlan{
|
|
PlanID: pp.PlanID,
|
|
UserID: int64(userID),
|
|
}
|
|
err = m.AddCurrentPlan(newPP, userID)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
finishedPP, err := m.CurrentPlan(userID)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
|
|
response := &createCurrentPlanResponse{
|
|
CreatedCurrentPlan: finishedPP,
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
type updateCurrentPlanResponse struct {
|
|
UpdatedCurrentPlan *models.CurrentPlan `json:"updated_current_plan"`
|
|
}
|
|
|
|
func putCurrentPlanFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID, err := tokens.GetUserID(r.Context())
|
|
if err != nil {
|
|
unauthorizedHandler(w, r)
|
|
return
|
|
}
|
|
|
|
_, err = m.CurrentPlan(userID)
|
|
if models.IsNotFoundError(err) {
|
|
notFoundHandler(w, r)
|
|
return
|
|
}
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
var pp models.CurrentPlan
|
|
err = dec.Decode(&pp)
|
|
if err != nil {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
err = dec.Decode(&struct{}{})
|
|
if err != io.EOF {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
|
|
newPP := &models.CurrentPlan{
|
|
PlanID: pp.PlanID,
|
|
UserID: int64(userID),
|
|
}
|
|
err = m.SaveCurrentPlan(newPP, userID)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
newPP, err = m.CurrentPlan(userID)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
|
|
response := &updateCurrentPlanResponse{
|
|
UpdatedCurrentPlan: newPP,
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
|
|
}
|
|
}
|