140 lines
3.1 KiB
Go
140 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"me-fit/db"
|
|
"me-fit/types"
|
|
|
|
"errors"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type WorkoutService interface {
|
|
AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error)
|
|
DeleteWorkout(user *User, rowId int) error
|
|
GetWorkouts(user *User) ([]*WorkoutDto, error)
|
|
}
|
|
|
|
type WorkoutServiceImpl struct {
|
|
dbWorkout db.WorkoutDb
|
|
randomGenerator RandomService
|
|
clock ClockService
|
|
mailService MailService
|
|
serverSettings *types.ServerSettings
|
|
}
|
|
|
|
func NewWorkoutServiceImpl(dbWorkout db.WorkoutDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) WorkoutService {
|
|
return WorkoutServiceImpl{
|
|
dbWorkout: dbWorkout,
|
|
randomGenerator: randomGenerator,
|
|
clock: clock,
|
|
mailService: mailService,
|
|
serverSettings: serverSettings,
|
|
}
|
|
}
|
|
|
|
type WorkoutDto struct {
|
|
RowId string
|
|
Date string
|
|
Type string
|
|
Sets string
|
|
Reps string
|
|
}
|
|
|
|
func NewWorkoutDtoFromDb(workout *db.Workout) *WorkoutDto {
|
|
return &WorkoutDto{
|
|
RowId: strconv.Itoa(workout.RowId),
|
|
Date: renderDate(workout.Date),
|
|
Type: workout.Type,
|
|
Sets: strconv.Itoa(workout.Sets),
|
|
Reps: strconv.Itoa(workout.Reps),
|
|
}
|
|
}
|
|
func NewWorkoutDto(rowId string, date string, workoutType string, sets string, reps string) *WorkoutDto {
|
|
return &WorkoutDto{
|
|
RowId: rowId,
|
|
Date: date,
|
|
Type: workoutType,
|
|
Sets: sets,
|
|
Reps: reps,
|
|
}
|
|
}
|
|
|
|
var (
|
|
ErrInputValues = errors.New("Invalid input values")
|
|
)
|
|
|
|
func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
|
|
|
if workoutDto.Date == "" || workoutDto.Type == "" || workoutDto.Sets == "" || workoutDto.Reps == "" {
|
|
return nil, ErrInputValues
|
|
}
|
|
|
|
date, err := time.Parse("2006-01-02", workoutDto.Date)
|
|
if err != nil {
|
|
return nil, ErrInputValues
|
|
}
|
|
|
|
sets, err := strconv.Atoi(workoutDto.Sets)
|
|
if err != nil {
|
|
return nil, ErrInputValues
|
|
}
|
|
|
|
reps, err := strconv.Atoi(workoutDto.Reps)
|
|
if err != nil {
|
|
return nil, ErrInputValues
|
|
}
|
|
|
|
workoutInsert := db.NewWorkoutInsert(date, workoutDto.Type, sets, reps)
|
|
|
|
workout, err := service.dbWorkout.InsertWorkout(user.Id, workoutInsert)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewWorkoutDtoFromDb(workout), nil
|
|
}
|
|
|
|
func (service WorkoutServiceImpl) DeleteWorkout(user *User, rowId int) error {
|
|
if user == nil {
|
|
return types.ErrInternal
|
|
}
|
|
|
|
return service.dbWorkout.DeleteWorkout(user.Id, rowId)
|
|
}
|
|
|
|
func (service WorkoutServiceImpl) 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) {
|
|
t, err := time.Parse("2006-01-02 15:04:05-07:00", date)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return renderDate(t), nil
|
|
}
|
|
|
|
func renderDate(date time.Time) string {
|
|
return date.Format("2006-01-02")
|
|
}
|