-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jo Vandeginste <[email protected]>
- Loading branch information
1 parent
7d9a2b4
commit c0dccda
Showing
22 changed files
with
756 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package app | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/jovandeginste/workout-tracker/pkg/database" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (a *App) addEquipment(c echo.Context) error { | ||
u := a.getCurrentUser(c) | ||
p := database.Equipment{} | ||
|
||
if err := c.Bind(&p); err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("add-equipment"), err) | ||
} | ||
|
||
p.UserID = u.ID | ||
|
||
if err := p.Save(a.db); err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("add-equipment"), err) | ||
} | ||
|
||
return c.Redirect(http.StatusFound, a.echo.Reverse("equipment")) | ||
} | ||
|
||
func (a *App) equipmentHandler(c echo.Context) error { | ||
data := a.defaultData(c) | ||
|
||
if err := a.addAllEquipment(a.getCurrentUser(c), data); err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("dashboard"), err) | ||
} | ||
|
||
return c.Render(http.StatusOK, "equipment_list.html", data) | ||
} | ||
|
||
func (a *App) equipmentShowHandler(c echo.Context) error { | ||
data := a.defaultData(c) | ||
|
||
id, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
return a.redirectWithError(c, "/equipment", err) | ||
} | ||
|
||
e, err := database.GetEquipment(a.db, id) | ||
if err != nil { | ||
return a.redirectWithError(c, "/equipment", err) | ||
} | ||
|
||
data["equipment"] = e | ||
|
||
return c.Render(http.StatusOK, "equipment_show.html", data) | ||
} | ||
|
||
func (a *App) equipmentAddHandler(c echo.Context) error { | ||
data := a.defaultData(c) | ||
return c.Render(http.StatusOK, "equipment_add.html", data) | ||
} | ||
|
||
func (a *App) equipmentDeleteHandler(c echo.Context) error { | ||
equipment, err := a.getEquipment(c) | ||
if err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("equipment-show", c.Param("id")), err) | ||
} | ||
|
||
if err := equipment.Delete(a.db); err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("equipment-show", c.Param("id")), err) | ||
} | ||
|
||
a.setNotice(c, "The equipment '%s' has been deleted.", equipment.Name) | ||
|
||
return c.Redirect(http.StatusFound, a.echo.Reverse("equipment")) | ||
} | ||
|
||
func (a *App) equipmentUpdateHandler(c echo.Context) error { | ||
equipment, err := a.getEquipment(c) | ||
if err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("equipment-edit", c.Param("id")), err) | ||
} | ||
|
||
if err := c.Bind(equipment); err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("equipment-edit", c.Param("id")), err) | ||
} | ||
|
||
equipment.Active = (c.FormValue("active") == "true") | ||
|
||
if err := equipment.Save(a.db); err != nil { | ||
return a.redirectWithError(c, a.echo.Reverse("equipment-edit", c.Param("id")), err) | ||
} | ||
|
||
a.setNotice(c, "The equipment '%s' has been updated.", equipment.Name) | ||
|
||
return c.Redirect(http.StatusFound, a.echo.Reverse("equipment-show", c.Param("id"))) | ||
} | ||
|
||
func (a *App) equipmentEditHandler(c echo.Context) error { | ||
data := a.defaultData(c) | ||
|
||
equipment, err := a.getEquipment(c) | ||
if err != nil { | ||
return a.redirectWithError(c, "/equipment", err) | ||
} | ||
|
||
data["equipment"] = equipment | ||
|
||
return c.Render(http.StatusOK, "equipment_edit.html", data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package database | ||
|
||
import ( | ||
"slices" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
type Equipment struct { | ||
gorm.Model | ||
Name string `gorm:"not null;uniqueIndex" json:"name" form:"name"` // The name of the gear | ||
UserID uint `gorm:"not null;index"` // The ID of the user who owns the workout | ||
Description string `gorm:"" json:"description" form:"description"` // More information about the equipment | ||
Active bool `gorm:"default:true" json:"active" form:"active"` // Whether this equipment is active | ||
DefaultFor []WorkoutType `gorm:"serializer:json;column:default_for" form:"default_for"` // Which workout types to add this equipment by default | ||
|
||
User User | ||
Workouts []Workout `gorm:"many2many:workout_equipment"` | ||
} | ||
|
||
type WorkoutEquipment struct { | ||
gorm.Model | ||
WorkoutID uint `gorm:"not null;uniqueIndex:idx_workout_equipment"` // The ID of the workout | ||
Workout Workout | ||
EquipmentID uint `gorm:"not null;uniqueIndex:idx_workout_equipment"` // The ID of the equipment | ||
Equipment Equipment | ||
} | ||
|
||
func GetAllEquipment(db *gorm.DB) ([]*Equipment, error) { | ||
var e []*Equipment | ||
|
||
if err := db.Order("date DESC").Find(&e).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return e, nil | ||
} | ||
|
||
func GetEquipment(db *gorm.DB, id int) (*Equipment, error) { | ||
var e Equipment | ||
|
||
if err := db.Preload("User").First(&e, id).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return &e, nil | ||
} | ||
|
||
func (e *Equipment) ValidFor(wt *WorkoutType) bool { | ||
return slices.Contains(e.DefaultFor, *wt) | ||
} | ||
|
||
func (e *Equipment) Delete(db *gorm.DB) error { | ||
return db.Unscoped().Delete(e).Error | ||
} | ||
|
||
func (e *Equipment) Save(db *gorm.DB) error { | ||
return db.Omit(clause.Associations).Save(e).Error | ||
} |
Oops, something went wrong.