Skip to content

Commit

Permalink
Merge pull request #45 from freekode/tr-copy-workout
Browse files Browse the repository at this point in the history
Copy workouts from TrainerRoad
  • Loading branch information
freekode authored Mar 21, 2024
2 parents 4a67451 + 4288e8f commit 85ea873
Show file tree
Hide file tree
Showing 92 changed files with 1,356 additions and 841 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.freekode.tp2intervals.app.plan

import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.plan.Plan
import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainer

data class CopyLibraryRequest(
val plan: Plan,
val libraryContainer: LibraryContainer,
val newName: String,
val sourcePlatform: Platform,
val targetPlatform: Platform,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package org.freekode.tp2intervals.app.plan

import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.plan.Plan
import org.freekode.tp2intervals.domain.plan.PlanRepository
import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainer
import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainerRepository
import org.freekode.tp2intervals.domain.workout.WorkoutRepository
import org.freekode.tp2intervals.infrastructure.utils.Date
import org.springframework.stereotype.Service

@Service
class LibraryService(
workoutRepositories: List<WorkoutRepository>,
planRepositories: List<PlanRepository>,
planRepositories: List<LibraryContainerRepository>,
) {
private val workoutRepositoryMap = workoutRepositories.associateBy { it.platform() }
private val planRepositoryMap = planRepositories.associateBy { it.platform() }

fun getLibraries(platform: Platform): List<Plan> {
fun getLibraryContainers(platform: Platform): List<LibraryContainer> {
val repository = planRepositoryMap[platform]!!
return repository.getLibraries()
return repository.getLibraryContainer()
}

fun copyLibrary(request: CopyLibraryRequest): CopyPlanResponse {
val targetPlanRepository = planRepositoryMap[request.targetPlatform]!!
val sourceWorkoutRepository = workoutRepositoryMap[request.sourcePlatform]!!
val targetWorkoutRepository = workoutRepositoryMap[request.targetPlatform]!!

val workouts = sourceWorkoutRepository.getWorkoutsFromLibrary(request.plan)
val newPlan = targetPlanRepository.createPlan(request.newName, Date.thisMonday(), true)
workouts.forEach { targetWorkoutRepository.saveWorkoutToLibrary(it, newPlan) }
val workouts = sourceWorkoutRepository.getWorkoutsFromLibrary(request.libraryContainer)
val newPlan = targetPlanRepository.createLibraryContainer(request.newName, Date.thisMonday(), true)
workouts.forEach { targetWorkoutRepository.saveWorkoutToLibrary(newPlan, it) }
return CopyPlanResponse(newPlan.name, workouts.size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.time.LocalDate
import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.TrainingType

class CopyPlannedWorkoutsRequest(
class CopyFromCalendarToCalendarRequest(
val startDate: LocalDate,
val endDate: LocalDate,
val types: List<TrainingType>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.time.LocalDate
import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.TrainingType

data class CopyPlannedToLibraryWorkoutsRequest(
data class CopyFromCalendarToLibraryRequest(
val name: String,
val isPlan: Boolean,
val startDate: LocalDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.freekode.tp2intervals.app.workout

import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainer
import org.freekode.tp2intervals.domain.workout.WorkoutDetails
import org.freekode.tp2intervals.rest.workout.WorkoutDetailsDTO

class CopyFromLibraryToLibraryRequest(
val workoutDetails: WorkoutDetailsDTO,
val targetLibraryContainer: LibraryContainer,
val sourcePlatform: Platform,
val targetPlatform: Platform,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.freekode.tp2intervals.app.workout

import java.time.LocalDate

data class CopyPlannedToLibraryResponse(
data class CopyWorkoutsResponse(
val copied: Int,
val filteredOut: Int,
val startDate: LocalDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,71 @@
package org.freekode.tp2intervals.app.workout

import java.time.LocalDate
import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.plan.PlanRepository
import org.freekode.tp2intervals.domain.workout.Workout
import org.freekode.tp2intervals.domain.TrainingType
import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainerRepository
import org.freekode.tp2intervals.domain.workout.WorkoutDetails
import org.freekode.tp2intervals.domain.workout.WorkoutRepository
import org.springframework.stereotype.Service

@Service
class WorkoutService(
workoutRepositories: List<WorkoutRepository>,
planRepositories: List<PlanRepository>,
planRepositories: List<LibraryContainerRepository>,
) {
private val workoutRepositoryMap = workoutRepositories.associateBy { it.platform() }
private val planRepositoryMap = planRepositories.associateBy { it.platform() }

fun copyPlannedWorkouts(request: CopyPlannedWorkoutsRequest): CopyPlannedWorkoutsResponse {
fun copyWorkoutsFromCalendarToCalendar(request: CopyFromCalendarToCalendarRequest): CopyWorkoutsResponse {
val sourceWorkoutRepository = workoutRepositoryMap[request.sourcePlatform]!!
val targetWorkoutRepository = workoutRepositoryMap[request.targetPlatform]!!

val allWorkoutsToPlan = sourceWorkoutRepository.getPlannedWorkouts(request.startDate, request.endDate)
val allWorkoutsToPlan = sourceWorkoutRepository.getWorkoutsFromCalendar(request.startDate, request.endDate)
var filteredWorkoutsToPlan = allWorkoutsToPlan
.filter { request.types.contains(it.type) }
.filter { request.types.contains(it.details.type) }
if (request.skipSynced) {
val plannedWorkouts = targetWorkoutRepository.getPlannedWorkouts(request.startDate, request.endDate)
.filter { request.types.contains(it.type) }
val plannedWorkouts = targetWorkoutRepository.getWorkoutsFromCalendar(request.startDate, request.endDate)
.filter { request.types.contains(it.details.type) }

filteredWorkoutsToPlan = filteredWorkoutsToPlan
.filter { !plannedWorkouts.contains(it) }
}

val response = CopyPlannedWorkoutsResponse(
val response = CopyWorkoutsResponse(
filteredWorkoutsToPlan.size,
allWorkoutsToPlan.size - filteredWorkoutsToPlan.size,
request.startDate,
request.endDate
)
filteredWorkoutsToPlan.forEach { targetWorkoutRepository.planWorkout(it) }
filteredWorkoutsToPlan.forEach { targetWorkoutRepository.saveWorkoutToCalendar(it) }
return response
}

fun copyPlannedWorkoutsToLibrary(request: CopyPlannedToLibraryWorkoutsRequest): CopyPlannedToLibraryResponse {
fun copyWorkoutsFromCalendarToLibrary(request: CopyFromCalendarToLibraryRequest): CopyWorkoutsResponse {
val sourceWorkoutRepository = workoutRepositoryMap[request.sourcePlatform]!!
val targetWorkoutRepository = workoutRepositoryMap[request.targetPlatform]!!
val targetPlanRepository = planRepositoryMap[request.targetPlatform]!!

val allWorkouts = sourceWorkoutRepository.getPlannedWorkouts(request.startDate, request.endDate)
val filteredWorkouts = allWorkouts.filter { request.types.contains(it.type) }
val allWorkouts = sourceWorkoutRepository.getWorkoutsFromCalendar(request.startDate, request.endDate)
val filteredWorkouts = allWorkouts.filter { request.types.contains(it.details.type) }

val plan = targetPlanRepository.createPlan(request.name, request.startDate, request.isPlan)
filteredWorkouts.forEach { targetWorkoutRepository.saveWorkoutToLibrary(it, plan) }
return CopyPlannedToLibraryResponse(
val plan = targetPlanRepository.createLibraryContainer(request.name, request.startDate, request.isPlan)
filteredWorkouts.forEach { targetWorkoutRepository.saveWorkoutToLibrary(plan, it) }
return CopyWorkoutsResponse(
filteredWorkouts.size, allWorkouts.size - filteredWorkouts.size, request.startDate, request.endDate
)
}

fun findWorkoutsByName(platform: Platform, name: String): List<Workout> {
TODO("not implemented")
fun copyWorkoutFromLibraryToLibrary(request: CopyFromLibraryToLibraryRequest): CopyWorkoutsResponse {
val sourceWorkoutRepository = workoutRepositoryMap[request.sourcePlatform]!!
val targetWorkoutRepository = workoutRepositoryMap[request.targetPlatform]!!

val workout = sourceWorkoutRepository.getWorkoutFromLibrary(request.workoutDetails.externalData)
targetWorkoutRepository.saveWorkoutToLibrary(request.targetLibraryContainer, workout)
return CopyWorkoutsResponse(1, 0, LocalDate.now(), LocalDate.now())
}

fun findWorkoutsByName(platform: Platform, name: String): List<WorkoutDetails> {
return workoutRepositoryMap[platform]!!.findWorkoutsFromLibraryByName(name)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.freekode.tp2intervals.domain.plan
package org.freekode.tp2intervals.domain.librarycontainer

import java.io.Serializable
import java.time.LocalDate
import org.freekode.tp2intervals.domain.ExternalData
import org.freekode.tp2intervals.infrastructure.utils.Date

data class Plan(
data class LibraryContainer(
val name: String,
val startDate: LocalDate,
val isPlan: Boolean,
val workoutsAmount: Int,
val externalData: ExternalData,
) : Serializable {
companion object {
fun planFromMonday(name: String, externalData: ExternalData): Plan {
return Plan(name, Date.thisMonday(), true, externalData)
fun planFromMonday(name: String, workoutsAmount: Int, externalData: ExternalData): LibraryContainer {
return LibraryContainer(name, Date.thisMonday(), true, workoutsAmount, externalData)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.freekode.tp2intervals.domain.librarycontainer

import org.freekode.tp2intervals.domain.Platform
import java.time.LocalDate

interface LibraryContainerRepository {
fun platform(): Platform

fun createLibraryContainer(name: String, startDate: LocalDate, isPlan: Boolean): LibraryContainer

fun getLibraryContainer(): List<LibraryContainer>
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import org.freekode.tp2intervals.domain.TrainingType
import org.freekode.tp2intervals.domain.workout.structure.WorkoutStructure

data class Workout(
val date: LocalDate,
val type: TrainingType,
val name: String,
val description: String?,
val duration: Duration?,
val load: Int?,
val details: WorkoutDetails,
val date: LocalDate?,
val structure: WorkoutStructure?,
val externalData: ExternalData,
) : Serializable {
constructor(
date: LocalDate, type: TrainingType, name: String, description: String?,
duration: Duration?, load: Int?, structure: WorkoutStructure?, externalData: ExternalData
) : this(WorkoutDetails(type, name, description, duration, load, externalData), date, structure)

companion object {
fun note(date: LocalDate, title: String, description: String?, externalData: ExternalData): Workout {
return Workout(date, TrainingType.NOTE, title, description, null, null, null, externalData)
}
}

fun withDate(date: LocalDate): Workout {
return Workout(date, type, name, description, duration, load, structure, externalData)
return Workout(date, details.type, details.name, details.description, details.duration, details.load, structure, details.externalData)
}

override fun equals(other: Any?): Boolean {
Expand All @@ -33,20 +33,11 @@ data class Workout(

other as Workout

if (type != other.type) return false
if (name != other.name) return false
if (load != other.load) return false
if (externalData != other.externalData) return false

return true
return details == other.details
}

override fun hashCode(): Int {
var result = type.hashCode()
result = 31 * result + name.hashCode()
result = 31 * result + (load ?: 0)
result = 31 * result + externalData.hashCode()
return result
return details.hashCode()
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.freekode.tp2intervals.domain.workout

import java.io.Serializable
import java.time.Duration
import java.time.LocalDate
import org.freekode.tp2intervals.domain.ExternalData
import org.freekode.tp2intervals.domain.TrainingType

data class WorkoutDetails(
val type: TrainingType,
val name: String,
val description: String?,
val duration: Duration?,
val load: Int?,
val externalData: ExternalData,
) : Serializable {

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as WorkoutDetails

if (type != other.type) return false
if (name != other.name) return false
if (load != other.load) return false
if (externalData != other.externalData) return false

return true
}

override fun hashCode(): Int {
var result = type.hashCode()
result = 31 * result + name.hashCode()
result = 31 * result + (load ?: 0)
result = 31 * result + externalData.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package org.freekode.tp2intervals.domain.workout

import java.time.LocalDate
import org.freekode.tp2intervals.domain.ExternalData
import org.freekode.tp2intervals.domain.Platform
import org.freekode.tp2intervals.domain.plan.Plan
import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainer

interface WorkoutRepository {
fun platform(): Platform

fun getPlannedWorkouts(startDate: LocalDate, endDate: LocalDate): List<Workout>
fun getWorkoutsFromCalendar(startDate: LocalDate, endDate: LocalDate): List<Workout>

fun planWorkout(workout: Workout)
fun getWorkoutsFromLibrary(libraryContainer: LibraryContainer): List<Workout>

fun getWorkoutsFromLibrary(plan: Plan): List<Workout>
fun getWorkoutFromLibrary(externalData: ExternalData): Workout

fun saveWorkoutToLibrary(workout: Workout, plan: Plan)
fun findWorkoutsFromLibraryByName(name: String): List<WorkoutDetails>

fun saveWorkoutToCalendar(workout: Workout)

fun saveWorkoutToLibrary(libraryContainer: LibraryContainer, workout: Workout)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.freekode.tp2intervals.infrastructure
class Signature {
companion object {
val description = """
Created by tp2intervals (https://github.com/freekode/tp2intervals)
Imported with tp2intervals (https://github.com/freekode/tp2intervals)
""".trimIndent()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class FolderDTO(
val name: String,
@JsonProperty("start_date_local")
val startDateLocal: LocalDate?,
val num_workouts: Int,
)
Loading

0 comments on commit 85ea873

Please sign in to comment.