This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/template/account"
|
||||
"spend-sparrow/utils"
|
||||
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Account interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type AccountImpl struct {
|
||||
service service.Account
|
||||
auth service.Auth
|
||||
render *Render
|
||||
}
|
||||
|
||||
func NewAccount(service service.Account, auth service.Auth, render *Render) Account {
|
||||
return AccountImpl{
|
||||
service: service,
|
||||
auth: auth,
|
||||
render: render,
|
||||
}
|
||||
}
|
||||
|
||||
func (handler AccountImpl) Handle(router *http.ServeMux) {
|
||||
router.Handle("/account", handler.handleAccountPage())
|
||||
// router.Handle("POST /account", handler.handleAddAccount())
|
||||
// router.Handle("GET /account", handler.handleGetAccount())
|
||||
// router.Handle("DELETE /account/{id}", handler.handleDeleteAccount())
|
||||
}
|
||||
|
||||
func (handler AccountImpl) handleAccountPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
comp := account.AccountListComp(nil)
|
||||
handler.render.RenderLayout(r, w, comp, user)
|
||||
}
|
||||
}
|
||||
|
||||
// func (handler AccountImpl) handleAddAccount() http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {
|
||||
// user := middleware.GetUser(r)
|
||||
// if user == nil {
|
||||
// utils.DoRedirect(w, r, "/auth/signin")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// var dateStr = r.FormValue("date")
|
||||
// var typeStr = r.FormValue("type")
|
||||
// var setsStr = r.FormValue("sets")
|
||||
// var repsStr = r.FormValue("reps")
|
||||
//
|
||||
// wo := service.NewAccountDto("", dateStr, typeStr, setsStr, repsStr)
|
||||
// wo, err := handler.service.AddAccount(user, wo)
|
||||
// if err != nil {
|
||||
// utils.TriggerToast(w, r, "error", "Invalid input values", http.StatusBadRequest)
|
||||
// http.Error(w, "Invalid input values", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
// wor := account.Account{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps}
|
||||
//
|
||||
// comp := account.AccountItemComp(wor, true)
|
||||
// handler.render.Render(r, w, comp)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func (handler AccountImpl) handleGetAccount() http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {
|
||||
// user := middleware.GetUser(r)
|
||||
// if user == nil {
|
||||
// utils.DoRedirect(w, r, "/auth/signin")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// workouts, err := handler.service.GetAccounts(user)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// wos := make([]*types.Account, 0)
|
||||
// for _, wo := range workouts {
|
||||
// wos = append(wos, *types.Account{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps})
|
||||
// }
|
||||
//
|
||||
// comp := account.AccountListComp(wos)
|
||||
// handler.render.Render(r, w, comp)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func (handler AccountImpl) handleDeleteAccount() http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {
|
||||
// user := middleware.GetUser(r)
|
||||
// if user == nil {
|
||||
// utils.DoRedirect(w, r, "/auth/signin")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// rowId := r.PathValue("id")
|
||||
// if rowId == "" {
|
||||
// utils.TriggerToast(w, r, "error", "Missing ID field", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// rowIdInt, err := strconv.Atoi(rowId)
|
||||
// if err != nil {
|
||||
// utils.TriggerToast(w, r, "error", "Invalid ID", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// err = handler.service.DeleteAccount(user, rowIdInt)
|
||||
// if err != nil {
|
||||
// utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -1,129 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/template/workout"
|
||||
"spend-sparrow/utils"
|
||||
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Workout interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type WorkoutImpl struct {
|
||||
service service.Workout
|
||||
auth service.Auth
|
||||
render *Render
|
||||
}
|
||||
|
||||
func NewWorkout(service service.Workout, auth service.Auth, render *Render) Workout {
|
||||
return WorkoutImpl{
|
||||
service: service,
|
||||
auth: auth,
|
||||
render: render,
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutImpl) Handle(router *http.ServeMux) {
|
||||
router.Handle("/workout", handler.handleWorkoutPage())
|
||||
router.Handle("POST /api/workout", handler.handleAddWorkout())
|
||||
router.Handle("GET /api/workout", handler.handleGetWorkout())
|
||||
router.Handle("DELETE /api/workout/{id}", handler.handleDeleteWorkout())
|
||||
}
|
||||
|
||||
func (handler WorkoutImpl) handleWorkoutPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
currentDate := time.Now().Format("2006-01-02")
|
||||
comp := workout.WorkoutComp(currentDate)
|
||||
handler.render.RenderLayout(r, w, comp, user)
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutImpl) handleAddWorkout() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
var dateStr = r.FormValue("date")
|
||||
var typeStr = r.FormValue("type")
|
||||
var setsStr = r.FormValue("sets")
|
||||
var repsStr = r.FormValue("reps")
|
||||
|
||||
wo := service.NewWorkoutDto("", dateStr, typeStr, setsStr, repsStr)
|
||||
wo, err := handler.service.AddWorkout(user, wo)
|
||||
if err != nil {
|
||||
utils.TriggerToast(w, r, "error", "Invalid input values", http.StatusBadRequest)
|
||||
http.Error(w, "Invalid input values", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
wor := workout.Workout{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps}
|
||||
|
||||
comp := workout.WorkoutItemComp(wor, true)
|
||||
handler.render.Render(r, w, comp)
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutImpl) handleGetWorkout() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
workouts, err := handler.service.GetWorkouts(user)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
wos := make([]workout.Workout, 0)
|
||||
for _, wo := range workouts {
|
||||
wos = append(wos, workout.Workout{Id: wo.RowId, Date: wo.Date, Type: wo.Type, Sets: wo.Sets, Reps: wo.Reps})
|
||||
}
|
||||
|
||||
comp := workout.WorkoutListComp(wos)
|
||||
handler.render.Render(r, w, comp)
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutImpl) handleDeleteWorkout() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := middleware.GetUser(r)
|
||||
if user == nil {
|
||||
utils.DoRedirect(w, r, "/auth/signin")
|
||||
return
|
||||
}
|
||||
|
||||
rowId := r.PathValue("id")
|
||||
if rowId == "" {
|
||||
utils.TriggerToast(w, r, "error", "Missing ID field", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
rowIdInt, err := strconv.Atoi(rowId)
|
||||
if err != nil {
|
||||
utils.TriggerToast(w, r, "error", "Invalid ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = handler.service.DeleteWorkout(user, rowIdInt)
|
||||
if err != nil {
|
||||
utils.TriggerToast(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user