fix: more refactoring #181

This commit is contained in:
2024-10-20 15:02:39 +02:00
parent 38d530a3c9
commit 77bf66a4fc
6 changed files with 153 additions and 56 deletions

View File

@@ -3,18 +3,16 @@ package service
import (
"me-fit/db"
"me-fit/types"
"me-fit/utils"
"database/sql"
"errors"
"log/slog"
"net/http"
"strconv"
"time"
)
type ServiceWorkout interface {
AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error)
DeleteWorkout(user *User, rowId int) error
GetWorkouts(user *User) ([]*WorkoutDto, error)
}
type ServiceWorkoutImpl struct {
@@ -25,9 +23,9 @@ type ServiceWorkoutImpl struct {
serverSettings *types.ServerSettings
}
func NewServiceWorkoutImpl(dbAuth db.DbAuth, randomGenerator RandomGenerator, clock Clock, mailService MailService, serverSettings *types.ServerSettings) *ServiceAuthImpl {
return &ServiceAuthImpl{
dbAuth: dbAuth,
func NewServiceWorkoutImpl(dbWorkout db.DbWorkout, randomGenerator RandomGenerator, clock Clock, mailService MailService, serverSettings *types.ServerSettings) ServiceWorkout {
return ServiceWorkoutImpl{
dbWorkout: dbWorkout,
randomGenerator: randomGenerator,
clock: clock,
mailService: mailService,
@@ -66,7 +64,7 @@ var (
ErrInputValues = errors.New("Invalid input values")
)
func (service ServiceWorkoutImpl) AddWorkout(user *User, workoutDto WorkoutDto) (*WorkoutDto, error) {
func (service ServiceWorkoutImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
if workoutDto.Date == "" || workoutDto.Type == "" || workoutDto.Sets == "" || workoutDto.Reps == "" {
return nil, ErrInputValues
@@ -97,45 +95,34 @@ func (service ServiceWorkoutImpl) AddWorkout(user *User, workoutDto WorkoutDto)
return NewWorkoutDtoFromDb(workout), nil
}
func HandleWorkoutDeleteComp(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := utils.GetUser(r)
if user == nil {
utils.DoRedirect(w, r, "/auth/signin")
return
}
rowId := r.PathValue("id")
if rowId == "" {
http.Error(w, "Missing required fields", http.StatusBadRequest)
slog.Warn("Missing required fields for workout delete")
utils.TriggerToast(w, r, "error", "Missing ID field")
return
}
res, err := db.Exec("DELETE FROM workout WHERE user_id = ? AND rowid = ?", user.Id, rowId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
utils.LogError("Could not delete workout", err)
utils.TriggerToast(w, r, "error", "Internal Server Error")
return
}
rows, err := res.RowsAffected()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
utils.LogError("Could not get rows affected", err)
utils.TriggerToast(w, r, "error", "Internal Server Error")
return
}
if rows == 0 {
http.Error(w, "Not found", http.StatusNotFound)
slog.Warn("Could not find workout to delete")
utils.TriggerToast(w, r, "error", "Not found. Refresh the page.")
return
}
func (service ServiceWorkoutImpl) DeleteWorkout(user *User, rowId int) error {
if user == nil {
return types.ErrInternal
}
return service.dbWorkout.DeleteWorkout(user.Id, rowId)
}
func (service ServiceWorkoutImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
if user == nil {
return nil, types.ErrInternal
}
workouts, err := service.dbWorkout.GetWorkouts(user.Id)
if err != nil {
return nil, err
}
// for _, workout := range workouts {
// workout.Date = renderDate(workout.Date)
// }
workoutsDto := make([]*WorkoutDto, len(workouts))
for i, workout := range workouts {
workoutsDto[i] = NewWorkoutDtoFromDb(&workout)
}
return workoutsDto, nil
}
func renderDateStr(date string) (string, error) {