Skip to content

Commit

Permalink
New stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Aug 18, 2024
1 parent 7bad4ff commit 89aebd7
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 20 deletions.
93 changes: 92 additions & 1 deletion controllers/exercise.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ func APIStravaCombine(context *gin.Context) {
context.Abort()
return
} else if exerciseDay == nil {
log.Println("Failed to verify exercise day. Error: " + err.Error())
log.Println("Failed to verify exercise day.")
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to verify exercise day."})
context.Abort()
return
Expand Down Expand Up @@ -1361,3 +1361,94 @@ func APIStravaCombine(context *gin.Context) {

context.JSON(http.StatusCreated, gin.H{"message": "Exercises combined."})
}

func APIStravaDivide(context *gin.Context) {
var exerciseIDString = context.Param("exercise_id")
exerciseID, err := uuid.Parse(exerciseIDString)
if err != nil {
log.Println("Failed to verify exercise ID. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to verify exercise ID."})
context.Abort()
return
}

return

// Get user ID
userID, err := middlewares.GetAuthUsername(context.GetHeader("Authorization"))
if err != nil {
log.Println("Failed to verify user ID. Error: " + "Failed to verify user ID.")
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to verify user ID."})
context.Abort()
return
}

exercise, err := database.GetExerciseByIDAndUserID(exerciseID, userID)
if err != nil {
log.Println("Failed to get exercise. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get exercise."})
context.Abort()
return
} else if exercise == nil {
log.Println("Failed to verify exercise.")
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to verify exercise."})
context.Abort()
return
}

exerciseObject, err := ConvertExerciseToExerciseObject(*exercise)
if err != nil {
log.Println("Failed to get exercise object. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get exercise object."})
context.Abort()
return
}

exerciseDay, err := database.GetExerciseDayByID(exercise.ExerciseDayID)
if err != nil {
log.Println("Failed to get exercise day. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get exercise day."})
context.Abort()
return
} else if exerciseDay == nil {
log.Println("Failed to verify exercise day.")
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to verify exercise day."})
context.Abort()
return
}

now := time.Now()
nowYear, nowWeek := now.ISOWeek()
exerciseDayYear, exerciseDayWeek := exerciseDay.Date.ISOWeek()

if (nowYear != exerciseDayYear) || (nowWeek != exerciseDayWeek) {
context.JSON(http.StatusBadRequest, gin.H{"error": "You can no longer divide exercises for this day."})
context.Abort()
return
}

newExercises := []models.Exercise{}
for index, stravaID := range exerciseObject.StravaID {
var currentExercise *models.Exercise
if index != 0 {
currentExercise = &models.Exercise{}
currentExercise.ID = uuid.New()
currentExercise.CreatedAt = now
currentExercise.UpdatedAt = now
currentExercise.ExerciseDayID = exerciseDay.ID
} else {
currentExercise = exercise
}

currentExercise.StravaID = &stravaID

// REMOVE LATER
newExercises = append(newExercises, *currentExercise)

for _, exerciseOperation := range exerciseObject.Operations {
if exerciseOperation.StravaID == &stravaID {

}
}
}
}
7 changes: 5 additions & 2 deletions controllers/strava.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,12 @@ func SyncStravaOperationsToExerciseSession(exerciseID uuid.UUID, userID uuid.UUI
if err != nil {
log.Println("Failed to get exercise object. Error: " + err.Error())
return errors.New("Failed to get exercise object.")
} else if exercise == nil {
log.Println("Failed to find exercise object.")
return errors.New("Failed to find exercise object.")
}

exerciseObject, err := ConvertExerciseToExerciseObject(exercise)
exerciseObject, err := ConvertExerciseToExerciseObject(*exercise)
if err != nil {
log.Println("Failed to convert exercise to exercise object. Error: " + err.Error())
return errors.New("Failed to convert exercise to exercise object.")
Expand All @@ -531,7 +534,7 @@ func SyncStravaOperationsToExerciseSession(exerciseID uuid.UUID, userID uuid.UUI
exercise.Duration = &newDuration
exercise.Note = newNote

_, err = database.UpdateExerciseInDB(exercise)
_, err = database.UpdateExerciseInDB(*exercise)
if err != nil {
log.Println("Failed to update exercise. Error: " + err.Error())
return errors.New("Failed to update exercise.")
Expand Down
8 changes: 4 additions & 4 deletions database/exercise.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func UpdateExerciseByTurningOffByExerciseID(exerciseID uuid.UUID) error {
}

// Return exercises that are enabled and on
func GetExerciseByIDAndUserID(exerciseID uuid.UUID, userID uuid.UUID) (models.Exercise, error) {
var exercise models.Exercise
func GetExerciseByIDAndUserID(exerciseID uuid.UUID, userID uuid.UUID) (*models.Exercise, error) {
var exercise *models.Exercise

record := Instance.Where("`exercises`.enabled = ?", 1).
Where("`exercises`.id = ?", exerciseID).
Expand All @@ -77,9 +77,9 @@ func GetExerciseByIDAndUserID(exerciseID uuid.UUID, userID uuid.UUID) (models.Ex
Find(&exercise)

if record.Error != nil {
return models.Exercise{}, record.Error
return exercise, record.Error
} else if record.RowsAffected != 1 {
return models.Exercise{}, errors.New("No exercise found.")
return exercise, nil
}

return exercise, nil
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func initRouter() *gin.Engine {
auth.GET("/exercises/week", controllers.APIGetWeek)
auth.POST("/exercises", controllers.APICreateExercise)
auth.PUT("/exercises/:exercise_id", controllers.APIUpdateExercise)
auth.PUT("/exercises/:exercise_id/strava-divide", controllers.APIStravaDivide)
auth.POST("/exercises/strava-combine", controllers.APIStravaCombine)

auth.GET("/operations", controllers.APIGetOperationsForUser)
Expand Down
1 change: 1 addition & 0 deletions web/assets/scissors.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ input[type=number]

input[type=checkbox]
{
margin: 1em;
margin: 0.5em;
}

.modules {
Expand Down Expand Up @@ -1490,7 +1490,7 @@ input[type=checkbox]
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: center;
align-content: space-between;
justify-content: space-evenly;
align-items: center;
box-sizing: border-box;
Expand Down Expand Up @@ -1518,9 +1518,6 @@ input[type=checkbox]
box-sizing: inherit;
padding: 0 0.5em;
margin: 0;
height: 100%;
height: -webkit-fill-available;
height: -moz-available;
}

.exercise-date {
Expand All @@ -1538,6 +1535,12 @@ input[type=checkbox]
font-size: 0.8em;
}

.exercise-date-string {
width: 100%;
width: -webkit-fill-available;
width: -moz-available;
}

.exercise-week {
font-size: 1em;
margin: 0.5em 1em;
Expand Down
54 changes: 47 additions & 7 deletions web/js/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function load_page(result) {
<img src="/assets/plus.svg" class="button-icon" style="height: 100%; margin: 1em;">
</div>
<div class="top-row" style="margin-top: 5em; display: none;" id="stravaCombineButtonWrapper">
<button type="submit" class="btn btn-primary" style="width: 15em; background-color: salmon;" id="" onclick="combineStravaExercises(); return false;">
<div class="" style="margin-top: 5em; display: none;" id="stravaCombineButtonWrapper">
<button type="submit" class="btn btn-primary" style="width: 15em; background-color: salmon; font-size:0.75em;" id="" onclick="combineStravaExercises(); return false;">
Combine Strava exercises
</button>
</div>
Expand Down Expand Up @@ -171,6 +171,7 @@ function generateExerciseHTML(exercise, count) {

stravaHTML = ""
stravaCombineHTML = ""
stravaDivide = ""
if(exercise.strava_id && exercise.strava_id.length > 0) {
stravaHTML += `<div class="strava-stack">`
stravaIDString = ""
Expand All @@ -188,13 +189,17 @@ function generateExerciseHTML(exercise, count) {
stravaIDString += exercise.strava_id[i]
}
stravaHTML += `</div>`

stravaCombineHTML += `
<div class="top-row">
<input style="margin: 1.2em;" id="${stravaIDString}" class="clickable stravaCombineCheck" type="checkbox" name="" value="">
</div>
<input style="margin: 0.5em;" id="${stravaIDString}" class="clickable stravaCombineCheck" type="checkbox" name="" value="">
`;

if(exercise.strava_id.length > 1) {
stravaDivide += `
<img src="/assets/scissors.svg" style="height: 1em; width: 1em; padding: 1em;" onclick="divideStravaExercises('${exercise.id}')" class="btn_logo clickable color-invert">
`;
}

try {
document.getElementById('stravaCombineButtonWrapper').style.display = "flex";
} catch(e) {
Expand All @@ -204,9 +209,10 @@ function generateExerciseHTML(exercise, count) {

exerciseHTML = `
<div class="top-row">
${stravaDivide}
${stravaCombineHTML}
<img src="/assets/trash-2.svg" style="height: 1em; width: 1em; padding: 1em;" onclick="updateExercise('${exercise.id}', false, ${count})" class="btn_logo clickable color-invert">
</div>
${stravaCombineHTML}
<div class="exerciseSubWrapper" id="exercise-sub-${exercise.id}">
${stravaHTML}
Expand Down Expand Up @@ -1207,4 +1213,38 @@ function combineStravaExercisesAPI(stravaIDArray) {
xhttp.setRequestHeader("Authorization", jwt);
xhttp.send(form_data);
return false;
}

function divideStravaExercises(exerciseID) {
if(!confirm("Are you sure you want to divide all these Strava exercises?")) {
return;
}

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {

try {
result = JSON.parse(this.responseText);
} catch(e) {
console.log(e +' - Response: ' + this.responseText);
error("Could not reach API.");
return;
}

if(result.error) {
error(result.error);
} else {
alert(result.message)
location.reload();
}

}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "auth/exercises/" + exerciseID + "/strava-divide");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", jwt);
xhttp.send();
return false;
}
4 changes: 3 additions & 1 deletion web/js/exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ function place_exercises(exercise_array, year) {
// parse date object
try {
var date = new Date(Date.parse(exercise_array[i].date));
var dateFullString = GetDateString(date, false);
var date_string = GetDayOfTheWeek(date)
var dateStringDetailed = GetDateString(date, false)
var week = date.getWeek(1);
Expand All @@ -311,7 +312,8 @@ function place_exercises(exercise_array, year) {
<div class="exercise-date">
<img style="width: 100%;" src="assets/calendar.svg" class="btn_logo"></img>
${date_string}
<div class="exercise-date-string">${dateFullString}</div>
<div class="exercise-date-string"><b>${date_string}</b></div>
</div>
<div class="exercise-details" id="">
Expand Down

0 comments on commit 89aebd7

Please sign in to comment.