All checks were successful
gitea-deepak/gogmagog/pipeline/pr-master This commit looks good
187 lines
4.2 KiB
Go
187 lines
4.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"gitea.deepak.science/deepak/gogmagog/models"
|
|
"github.com/go-chi/chi"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func newActionRouter(m *models.Model) http.Handler {
|
|
router := chi.NewRouter()
|
|
router.Get("/", getActionsFunc(m))
|
|
router.Post("/", postActionFunc(m))
|
|
router.Get("/{actionid}", getActionByIDFunc(m))
|
|
router.Put("/{actionid}", putActionFunc(m))
|
|
return router
|
|
}
|
|
|
|
func getActionsFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var (
|
|
actions []*models.Action
|
|
err error
|
|
)
|
|
planIDString := r.URL.Query().Get("plan_id")
|
|
if planIDString == "" {
|
|
actions, err = m.Actions()
|
|
} else {
|
|
planID, convErr := strconv.ParseInt(planIDString, 10, 64)
|
|
if convErr != nil {
|
|
actions = []*models.Action{}
|
|
err = nil
|
|
} else {
|
|
plan := &models.Plan{PlanID: planID}
|
|
actions, err = m.GetActions(plan)
|
|
}
|
|
}
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(actions); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func getActionByIDFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
|
if err != nil {
|
|
notFoundHandler(w, r)
|
|
return
|
|
}
|
|
action, err := m.Action(id)
|
|
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(action); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type createActionResponse struct {
|
|
CreatedAction *models.Action `json:"created_action"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func postActionFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
var a models.Action
|
|
err := dec.Decode(&a)
|
|
if err != nil {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
err = dec.Decode(&struct{}{})
|
|
if err != io.EOF {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
|
|
action := &models.Action{
|
|
ActionDescription: a.ActionDescription,
|
|
EstimatedChunks: a.EstimatedChunks,
|
|
CompletedChunks: a.CompletedChunks,
|
|
CompletedOn: a.CompletedOn,
|
|
PlanID: a.PlanID,
|
|
}
|
|
id, err := m.AddAction(action)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
action, err = m.Action(id)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
|
|
response := &createActionResponse{
|
|
CreatedAction: action,
|
|
ID: int64(id),
|
|
}
|
|
w.WriteHeader(http.StatusCreated)
|
|
w.Header().Add("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
type updateActionResponse struct {
|
|
UpdatedAction *models.Action `json:"updated_action"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func putActionFunc(m *models.Model) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
|
if err != nil {
|
|
notFoundHandler(w, r)
|
|
return
|
|
}
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
var a models.Action
|
|
err = dec.Decode(&a)
|
|
if err != nil {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
err = dec.Decode(&struct{}{})
|
|
if err != io.EOF {
|
|
badRequestError(w, err)
|
|
return
|
|
}
|
|
|
|
action := &models.Action{
|
|
ActionDescription: a.ActionDescription,
|
|
EstimatedChunks: a.EstimatedChunks,
|
|
CompletedChunks: a.CompletedChunks,
|
|
CompletedOn: a.CompletedOn,
|
|
PlanID: a.PlanID,
|
|
ActionID: int64(id),
|
|
}
|
|
err = m.SaveAction(action)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
action, err = m.Action(id)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
|
|
response := &updateActionResponse{
|
|
UpdatedAction: action,
|
|
ID: int64(id),
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Header().Add("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
|
|
}
|
|
}
|