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 // } // } // }