Skip to content

Commit

Permalink
WIP: Add equipment (or gear)
Browse files Browse the repository at this point in the history
Fixes #135

Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed May 29, 2024
1 parent 7d9a2b4 commit ceaa7a4
Show file tree
Hide file tree
Showing 26 changed files with 926 additions and 24 deletions.
93 changes: 93 additions & 0 deletions assets/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,71 @@ table {
}
}

.selectable-pill {
margin: 0.5rem;
margin-top: 0.75rem;
margin-bottom: 0.75rem;
display: inline-block;
min-width: 150px;
border-radius: 9999px;
border-width: 1px;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1.25rem;
padding-right: 1.25rem;
text-align: center;
--tw-border-opacity: 1;
border-color: rgb(115 115 115 / var(--tw-border-opacity));
--tw-bg-opacity: 1;
background-color: rgb(212 212 212 / var(--tw-bg-opacity));
}

.selectable-pill:hover {
--tw-bg-opacity: 1;
background-color: rgb(163 163 163 / var(--tw-bg-opacity));
}

@media (prefers-color-scheme: dark) {
.selectable-pill {
--tw-border-opacity: 1;
border-color: rgb(115 115 115 / var(--tw-border-opacity));
--tw-bg-opacity: 1;
background-color: rgb(64 64 64 / var(--tw-bg-opacity));
}

.selectable-pill:hover {
--tw-bg-opacity: 1;
background-color: rgb(82 82 82 / var(--tw-bg-opacity));
}
}

.selectable-pill:hover {
--tw-brightness: brightness(1.25);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}

.selectable-pill {
--tw-bg-opacity: 1;
background-color: rgb(248 113 113 / var(--tw-bg-opacity));
}

.peer:checked ~ .selectable-pill {
--tw-bg-opacity: 1;
background-color: rgb(74 222 128 / var(--tw-bg-opacity));
}

@media (prefers-color-scheme: dark) {
.selectable-pill {
--tw-bg-opacity: 1;
background-color: rgb(153 27 27 / var(--tw-bg-opacity));
}

.peer:checked ~ .selectable-pill {
--tw-bg-opacity: 1;
background-color: rgb(22 101 52 / var(--tw-bg-opacity));
}
}

.user-pill {
margin: 0.5rem;
margin-top: 0.75rem;
Expand Down Expand Up @@ -1821,6 +1886,10 @@ table {
display: block;
}

.inline {
display: inline;
}

.flex {
display: flex;
}
Expand Down Expand Up @@ -2380,6 +2449,14 @@ table {
content: "\f021";
}

.icon-bicycle::before {
content: "\f206";
}

.icon-bicycle::after {
content: "\f206";
}

.icon-bookmark::before {
content: "\f02e";
}
Expand All @@ -2388,6 +2465,14 @@ table {
content: "\f02e";
}

.icon-calculator::before {
content: "\f1ec";
}

.icon-calculator::after {
content: "\f1ec";
}

.icon-calendar::before {
content: "\f133";
}
Expand Down Expand Up @@ -2692,6 +2777,14 @@ table {
content: "\e445";
}

.icon-square-check::before {
content: "\f14a";
}

.icon-square-check::after {
content: "\f14a";
}

.icon-stopwatch::before {
content: "\f2f2";
}
Expand Down
8 changes: 8 additions & 0 deletions main.css
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@
}
}
}

.selectable-pill {
@apply user-pill;
@apply hover:brightness-125;
@apply bg-red-400 peer-checked:bg-green-400;
@apply dark:bg-red-800 dark:peer-checked:bg-green-800;
}

.user-pill {
@apply inline-block rounded-full my-3 m-2 min-w-[150px] py-2 px-5 border text-center;
@apply bg-neutral-300 hover:bg-neutral-400 border-neutral-500;
Expand Down
29 changes: 29 additions & 0 deletions pkg/app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,32 @@ func (a *App) getWorkout(c echo.Context) (*database.Workout, error) {

return w, nil
}

func (a *App) addAllEquipment(u *database.User, data map[string]interface{}) error {
if u == nil {
return nil
}

w, err := u.GetAllEquipment(a.db)
if err != nil {
return err
}

data["equipment"] = w

return nil
}

func (a *App) getEquipment(c echo.Context) (*database.Equipment, error) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return nil, err
}

w, err := a.getCurrentUser(c).GetEquipment(a.db, id)
if err != nil {
return nil, err
}

return w, nil
}
108 changes: 108 additions & 0 deletions pkg/app/equipment_handlers.go
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)
}
9 changes: 9 additions & 0 deletions pkg/app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,14 @@ func (a *App) secureRoutes(e *echo.Group) *echo.Group {
workoutsGroup.POST("/:id/refresh", a.workoutsRefreshHandler).Name = "workout-refresh"
workoutsGroup.GET("/add", a.workoutsAddHandler).Name = "workout-add"

equipmentGroup := secureGroup.Group("/equipment")
equipmentGroup.GET("", a.equipmentHandler).Name = "equipment"
equipmentGroup.POST("", a.addEquipment).Name = "equipment-create"
equipmentGroup.GET("/:id", a.equipmentShowHandler).Name = "equipment-show"
equipmentGroup.POST("/:id", a.equipmentUpdateHandler).Name = "equipment-update"
equipmentGroup.GET("/:id/edit", a.equipmentEditHandler).Name = "equipment-edit"
equipmentGroup.POST("/:id/delete", a.equipmentDeleteHandler).Name = "equipment-delete"
equipmentGroup.GET("/add", a.equipmentAddHandler).Name = "equipment-add"

return secureGroup
}
17 changes: 17 additions & 0 deletions pkg/app/workouts_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,27 @@ func (a *App) workoutsUpdateHandler(c echo.Context) error {
workout.Notes = c.FormValue("notes")
workout.Type = database.WorkoutType(c.FormValue("type"))

var equipmentIDS struct {
EquipmentIDs []uint `form:"equipment"`
}

if err := c.Bind(&equipmentIDS); err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-edit", c.Param("id")), err)
}

equipment, err := database.GetEquipmentByIDs(a.db, a.getCurrentUser(c).ID, equipmentIDS.EquipmentIDs)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-edit", c.Param("id")), err)
}

if err := workout.Save(a.db); err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}

if err := a.db.Model(&workout).Association("Equipment").Replace(equipment); err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}

a.setNotice(c, "The workout '%s' has been updated.", workout.Name)

return c.Redirect(http.StatusFound, a.echo.Reverse("workout-show", c.Param("id")))
Expand Down
Loading

0 comments on commit ceaa7a4

Please sign in to comment.