ExercisePlan, ExercisePlanDetail - no OneToMany connection, Delete operation
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.ExercisePlan
|
||||
import com.aitrainer.api.model.ExercisePlanDetail
|
||||
import com.aitrainer.api.repository.ExercisePlanRepository
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.annotation.Secured
|
||||
import org.springframework.web.bind.annotation.*
|
||||
@@ -33,7 +35,7 @@ class ExercisePlanController( private val exercisePlanRepository: ExercisePlanRe
|
||||
|
||||
@Secured
|
||||
@PostMapping("/exercise_plan/{exercisePlanId}")
|
||||
fun updateExercisePlanById(@PathVariable(value = "exercisePlanId") exercisePlanId: Long,
|
||||
fun updateExercisePlanById(@PathVariable(value = "exercisePlanId") exercisePlanId: Int,
|
||||
@Valid @RequestBody exercisePlan: ExercisePlan,
|
||||
@RequestHeader requestHeader: RequestHeader?
|
||||
): ResponseEntity<ExercisePlan> {
|
||||
@@ -49,4 +51,20 @@ class ExercisePlanController( private val exercisePlanRepository: ExercisePlanRe
|
||||
logger.info("-- updateExercisePlanById id: $updatedPlan ")
|
||||
return ResponseEntity.ok().body(exercisePlanRepository.save(updatedPlan))
|
||||
}
|
||||
|
||||
@Secured
|
||||
@PostMapping("/exercise_plan/delete/{exercisePlanId}")
|
||||
fun deleteExercisePlanById(@PathVariable(value = "exercisePlanId") exercisePlanId: Int,
|
||||
@Valid @RequestBody exercisePlan: ExercisePlan)
|
||||
:ResponseEntity<HttpStatus> {
|
||||
val returnPlan = exercisePlanRepository.findById(exercisePlanId).orElse(null)
|
||||
?: return ResponseEntity.ok().body(HttpStatus.NOT_FOUND)
|
||||
try {
|
||||
exercisePlanRepository.delete(returnPlan)
|
||||
} catch (e: IllegalArgumentException ) {
|
||||
return ResponseEntity.ok().body(HttpStatus.BAD_REQUEST)
|
||||
}
|
||||
|
||||
return ResponseEntity.ok().body(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
@@ -22,12 +22,21 @@ class ExercisePlanDetailController( private val exercisePlanDetailRepository: Ex
|
||||
return ResponseEntity.ok().body(exercisePlanDetailRepository.save(exercisePlanDetail))
|
||||
}
|
||||
|
||||
@GetMapping("/exercise_plan_detail/last/{customerId}")
|
||||
/* @GetMapping("/exercise_plan_detail/last/{customerId}")
|
||||
fun getExercisePlanByCustomerId(@PathVariable(value = "customerId") customerId: Long): ResponseEntity<List<ExercisePlanDetail>> {
|
||||
val list: List<ExercisePlanDetail> = exercisePlanDetailRepository.getLastExercisePlanDetail(customerId)
|
||||
logger.info("-- getExercisePlanByCustomerId id: $customerId -- $list")
|
||||
return ResponseEntity.ok().body(list)
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
@GetMapping("/exercise_plan_detail/{exercisePlanId}")
|
||||
fun getExercisePlanByExercisePlanId(@PathVariable(value = "exercisePlanId") exercisePlanId: Int): ResponseEntity<List<ExercisePlanDetail>> {
|
||||
val list: List<ExercisePlanDetail> = exercisePlanDetailRepository.findByExercisePlanId(exercisePlanId)
|
||||
logger.info("-- getExercisePlanByExercisePlanId id: $exercisePlanId -- $list")
|
||||
return ResponseEntity.ok().body(list)
|
||||
}
|
||||
|
||||
@Secured
|
||||
@PostMapping("/exercise_plan_detail/{exercisePlanDetailId}")
|
||||
@@ -39,7 +48,7 @@ class ExercisePlanDetailController( private val exercisePlanDetailRepository: Ex
|
||||
?: return ResponseEntity.notFound().build()
|
||||
|
||||
val updatedPlanDetail = returnPlanDetail.copy(
|
||||
exercisePlan = exercisePlanDetail.exercisePlan,
|
||||
exercisePlanId = exercisePlanDetail.exercisePlanId,
|
||||
exerciseTypeId = exercisePlanDetail.exerciseTypeId,
|
||||
serie = exercisePlanDetail.serie,
|
||||
repeats = exercisePlanDetail.repeats,
|
||||
@@ -56,9 +65,7 @@ class ExercisePlanDetailController( private val exercisePlanDetailRepository: Ex
|
||||
@Valid @RequestBody exercisePlanDetail: ExercisePlanDetail)
|
||||
:ResponseEntity<HttpStatus> {
|
||||
val returnPlanDetail = exercisePlanDetailRepository.findById(exercisePlanDetailId).orElse(null)
|
||||
if (returnPlanDetail == null) {
|
||||
return ResponseEntity.ok().body(HttpStatus.NOT_FOUND)
|
||||
}
|
||||
?: return ResponseEntity.ok().body(HttpStatus.NOT_FOUND)
|
||||
try {
|
||||
exercisePlanDetailRepository.delete(returnPlanDetail)
|
||||
} catch (e: IllegalArgumentException ) {
|
||||
|
||||
@@ -17,11 +17,13 @@ data class ExercisePlan(
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
var exercisePlanId: Long = 0
|
||||
var exercisePlanId: Int = 0
|
||||
|
||||
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "exercisePlan")
|
||||
/* @OneToMany(fetch = FetchType.LAZY, mappedBy = "exercisePlan")
|
||||
@Fetch(value = FetchMode.SUBSELECT)
|
||||
val planDetailList: List<ExercisePlanDetail> = mutableListOf<ExercisePlanDetail>()
|
||||
|
||||
*/
|
||||
}
|
||||
@@ -6,15 +6,18 @@ import javax.persistence.*
|
||||
|
||||
@Entity
|
||||
data class ExercisePlanDetail (
|
||||
@get: NonNull var exerciseTypeId: Long = 0,
|
||||
@get: NonNull var exercisePlanId: Int = 0,
|
||||
@get: NonNull var exerciseTypeId: Int = 0,
|
||||
@get: NonNull var serie: Int = 0,
|
||||
@get: NonNull var repeats: Int = 0,
|
||||
@get: NonNull var weightEquation: String? = "",
|
||||
@get: NonNull var weightEquation: String? = ""
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
||||
/* @ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "exercisePlanId", nullable = false)
|
||||
@JsonIgnore
|
||||
val exercisePlan: ExercisePlan? = null
|
||||
|
||||
*/
|
||||
) {
|
||||
|
||||
@Id
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
package com.aitrainer.api.repository
|
||||
|
||||
import com.aitrainer.api.model.ExercisePlan
|
||||
import com.aitrainer.api.model.ExercisePlanDetail
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface ExercisePlanDetailRepository: JpaRepository<ExercisePlanDetail, Long> {
|
||||
|
||||
@Query("FROM ExercisePlanDetail as d " +
|
||||
/* @Query("FROM ExercisePlanDetail as d " +
|
||||
"INNER JOIN ExercisePlan as p ON p.exercisePlanId = d.exercisePlan " +
|
||||
"WHERE p.private = 1 " +
|
||||
"AND p.customerId = :customerId " +
|
||||
"ORDER BY p.dateAdd DESC ")
|
||||
fun getLastExercisePlanDetail(customerId: Long): List<ExercisePlanDetail>
|
||||
|
||||
*/
|
||||
|
||||
fun findByExercisePlanId(exercisePlanId: Int): List<ExercisePlanDetail>
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface ExercisePlanRepository: JpaRepository<ExercisePlan, Long> {
|
||||
interface ExercisePlanRepository: JpaRepository<ExercisePlan, Int> {
|
||||
@Query(" FROM ExercisePlan" +
|
||||
" WHERE customerId = :customerId AND private = 1" +
|
||||
" AND dateAdd in (select Max(dateAdd) from ExercisePlan where customerId = :customerId and private = 1) "+
|
||||
|
||||
Reference in New Issue
Block a user