Skip to content

Commit 5cf92d6

Browse files
authored
Small changes for cosmetic purposes
2 parents a709dcb + ec114ef commit 5cf92d6

File tree

6 files changed

+84
-84
lines changed

6 files changed

+84
-84
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func main() {
4242
log.Fatal("Couldn't load .env variables")
4343
}
4444

45-
router := handlers.SetUpRouter(domain, csrf_key, db)
45+
handler := handlers.SetUpRouter(domain, csrf_key, db)
4646

47-
http.ListenAndServe(":8080", router)
47+
http.ListenAndServe(":8080", handler)
4848
}

models/create_plan_table.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ CREATE TABLE workout_plan (
99
FOREIGN KEY(creator) REFERENCES users(id)
1010
);
1111

12-
INSERT INTO workout_plan (name, creator, description) VALUES ('/', 1, '');
13-
1412

1513

1614
DROP TABLE IF EXISTS exercise_day;

models/create_track_table.sql

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CREATE TABLE workout_track_data (
77
weight FLOAT, -- NOTE: This doesn't neccessarily have to be the weight that is specified in the ex_day, unless it's null
88
set_num INTEGER,
99
rep_num INTEGER DEFAULT 0,
10+
time_recorded DATE DEFAULT NULL,
1011
FOREIGN KEY(ex_day) REFERENCES exercise_day(id),
1112
FOREIGN KEY(track) REFERENCES workout_track(id)
1213
);
@@ -26,16 +27,3 @@ CREATE TABLE workout_track (
2627
FOREIGN KEY(usr) REFERENCES users(id),
2728
UNIQUE(plan, usr, workout_date)
2829
);
29-
30-
31-
32-
DROP TABLE IF EXISTS track_exercise;
33-
34-
-- NOTE: this table is used to track which ex_days are for which track since ex days can be changed in a plan, but the track needs to remember them
35-
-- CREATE TABLE track_exercise (
36-
-- track INTEGER,
37-
-- ex_day INTEGER,
38-
-- PRIMARY KEY(track, ex_day),
39-
-- FOREIGN KEY(track) REFERENCES workout_track(id),
40-
-- FOREIGN KEY(ex_day) REFERENCES exercise_day(id)
41-
-- );

models/populate_exercise_table.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
---------------------
2+
--- WOKROUT PLANS ---
3+
---------------------
4+
5+
DELETE from workout_plan;
6+
DELETE FROM sqlite_sequence WHERE name='workout_plan';
7+
8+
INSERT INTO workout_plan (name, creator, description) VALUES ('/', 1, '');
9+
110
-----------------
211
--- EXERCISES ---
312
-----------------

models/user.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type User struct {
2727
func (Db *DataBase) CreateUser(usr User) (int, error) {
2828

2929
if usr.Email == "" {
30-
return 0, errors.New("Cannot store a user without their email")
30+
return defs.NO_USER_ID, errors.New("Cannot store a user without their email")
3131
}
3232

3333
// used to check if the user already has an account
@@ -39,7 +39,7 @@ func (Db *DataBase) CreateUser(usr User) (int, error) {
3939
var stmt *sql.Stmt
4040
stmt, err = Db.Data.Prepare(statement)
4141
if err != nil {
42-
return 0, err
42+
return defs.NO_USER_ID, err
4343
}
4444

4545
defer stmt.Close()
@@ -48,7 +48,7 @@ func (Db *DataBase) CreateUser(usr User) (int, error) {
4848

4949
encrypted_pass, err := bcrypt.GenerateFromPassword([]byte(usr.Password), bcrypt.DefaultCost)
5050
if err != nil {
51-
return 0, err
51+
return defs.NO_USER_ID, err
5252
}
5353

5454
err = stmt.QueryRow(
@@ -63,19 +63,19 @@ func (Db *DataBase) CreateUser(usr User) (int, error) {
6363
time.Now().Format("2006-01-02")).Scan(&usr_id)
6464

6565
if err != nil {
66-
return 0, err
66+
return defs.NO_USER_ID, err
6767
}
6868

6969
err = Db.AddUserToGym(usr.CurrentGym.Id, usr_id)
7070
if err != nil {
71-
return 0, err
71+
return defs.NO_USER_ID, err
7272
}
7373

7474
return usr_id, nil
7575
}
7676

7777
// user already has an account
78-
return 0, errors.New("ERROR: user already has an account") // NOTE: handle this better, lead the user to the login page
78+
return defs.NO_USER_ID, errors.New("ERROR: user already has an account")
7979
}
8080

8181
func (Db *DataBase) ReadUser(usr_id int) (*User, error) {

models/workout_track.go

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package models
22

33
import (
4-
// "database/sql"
4+
"database/sql"
55
"errors"
66
"fitness_app/defs"
77
"log"
@@ -20,11 +20,12 @@ type WorkoutTrack struct {
2020
}
2121

2222
type TrackData struct {
23-
Id int `json:"id"`
24-
ExDayId int `json:"ex_day_id"`
25-
Weight float32 `json:"weight"`
26-
SetNum int `json:"set_num"`
27-
RepNum int `json:"rep_num"`
23+
Id int `json:"id"`
24+
ExDayId int `json:"ex_day_id"`
25+
Weight float32 `json:"weight"`
26+
SetNum int `json:"set_num"`
27+
RepNum int `json:"rep_num"`
28+
TimeRecorded sql.NullTime `json:"time_recorded"`
2829
}
2930

3031
func (Db *DataBase) CreateWorkoutTrack(wt *WorkoutTrack) (int, error) {
@@ -235,41 +236,6 @@ func (Db *DataBase) UpdateWorkoutTrackPrivacy(wt *WorkoutTrack) (bool, error) {
235236
return true, nil
236237
}
237238

238-
// func (Db *DataBase) CreateTrackData(td *TrackData) (int, error) {
239-
//
240-
// if td.Track <= 1 {
241-
// return 0, errors.New("Invalid workout plan used in track")
242-
// }
243-
//
244-
// if td.ExDay <= 0 {
245-
// return 0, errors.New("Invalid user used in track")
246-
// }
247-
//
248-
// statement := "insert into workout_track_data (track, ex_day, weight, set_num) values (?, ?, ?, ?) returning id"
249-
// var stmt *sql.Stmt
250-
// stmt, err := Db.Data.Prepare(statement)
251-
// if err != nil {
252-
// return 0, err
253-
// }
254-
//
255-
// defer stmt.Close()
256-
//
257-
// var track_data_id int
258-
//
259-
// err = stmt.QueryRow(
260-
// td.Track,
261-
// td.ExDay,
262-
// td.Weight,
263-
// td.SetNum,
264-
// ).Scan(&track_data_id)
265-
//
266-
// if err != nil {
267-
// return 0, err
268-
// }
269-
//
270-
// return track_data_id, nil
271-
// }
272-
273239
func (Db *DataBase) CreateTrackDataForTrack(wt *WorkoutTrack) error {
274240

275241
if wt.Plan.Id <= defs.PLACEHOLDER_PLAN_ID {
@@ -310,7 +276,7 @@ func (Db *DataBase) CreateTrackDataForTrack(wt *WorkoutTrack) error {
310276

311277
func (Db *DataBase) ReadTrackDataForTrack(wt_id int) ([]TrackData, error) {
312278
query_str := `
313-
select workout_track_data.id, ex_day, workout_track_data.weight, set_num, rep_num
279+
select workout_track_data.id, ex_day, workout_track_data.weight, set_num, rep_num, time_recorded
314280
from workout_track_data inner join exercise_day on ex_day = exercise_day.id
315281
where track = ?
316282
order by day_order asc, exercise_order asc
@@ -334,6 +300,7 @@ func (Db *DataBase) ReadTrackDataForTrack(wt_id int) ([]TrackData, error) {
334300
&td.Weight,
335301
&td.SetNum,
336302
&td.RepNum,
303+
&td.TimeRecorded,
337304
)
338305
if err != nil {
339306
return nil, err
@@ -345,23 +312,49 @@ func (Db *DataBase) ReadTrackDataForTrack(wt_id int) ([]TrackData, error) {
345312
return data, nil
346313
}
347314

348-
// func (Db *DataBase) UpdateTrackData(td *TrackData) (bool, error) {
349-
//
350-
// statement := "UPDATE workout_track_data SET weight = ?, rep_num = ? WHERE id = ?"
351-
// stmt, err := Db.Data.Prepare(statement)
352-
// if err != nil {
353-
// return false, err
354-
// }
355-
//
356-
// defer stmt.Close()
357-
//
358-
// _, err = stmt.Exec(td.Weight, td.RepNum, td.Id)
359-
// if err != nil {
360-
// return false, err
361-
// }
362-
//
363-
// return true, nil
364-
// }
315+
func (Db *DataBase) GetAllDataForExerciseAndUser(user_id, exercise_id int) ([]*TrackData, error) {
316+
query := `
317+
select wtd.weight, wtd.set_num, wtd.rep_num, wtd.time_recorded
318+
from workout_track_data wtd inner join workout_track wt on wt.id = wtd.track inner join exercise_day ed on wtd.ex_day = ed.id
319+
where wt.usr = ? and ed.exercise = ? and wtd.time_recorded is not null
320+
order by wt.workout_date asc, wtd.time_recorded asc, wtd.set_num asc
321+
`
322+
rows, err := Db.Data.Query(query, user_id, exercise_id)
323+
if err != nil {
324+
return nil, err
325+
}
326+
327+
defer rows.Close()
328+
329+
res := make([]*TrackData, 0)
330+
331+
for rows.Next() {
332+
tmp := &TrackData{}
333+
err = rows.Scan(
334+
&tmp.Weight,
335+
&tmp.SetNum,
336+
&tmp.RepNum,
337+
&tmp.TimeRecorded,
338+
)
339+
340+
if err != nil {
341+
return nil, err
342+
}
343+
344+
res = append(res, tmp)
345+
}
346+
347+
return res, nil
348+
}
349+
350+
func CalcExerciseProgressFromTrackData(data []*TrackData) ([]float32, error) { // TODO: implement
351+
352+
for td := range data {
353+
_ = td
354+
}
355+
356+
return nil, nil
357+
}
365358

366359
func (Db *DataBase) UpdateMultipleTrackData(tds []TrackData) (bool, error) {
367360

@@ -376,15 +369,27 @@ func (Db *DataBase) UpdateMultipleTrackData(tds []TrackData) (bool, error) {
376369

377370
defer tx.Rollback()
378371

379-
stmt, err := tx.Prepare("UPDATE workout_track_data SET weight = ?, rep_num = ? WHERE id = ?")
372+
statement := `
373+
update workout_track_data
374+
set
375+
weight = ?,
376+
rep_num = ?,
377+
time_recorded = case
378+
when time_recorded is null then ?
379+
else time_recorded
380+
end
381+
where id = ?
382+
`
383+
384+
stmt, err := tx.Prepare(statement)
380385
if err != nil {
381386
return false, err
382387
}
383388

384389
defer stmt.Close()
385390

386391
for _, td := range tds {
387-
_, err = stmt.Exec(td.Weight, td.RepNum, td.Id)
392+
_, err = stmt.Exec(td.Weight, td.RepNum, time.Now(), td.Id)
388393
if err != nil {
389394
return false, err
390395
}

0 commit comments

Comments
 (0)