ExercisePlan, Detail Update, Delete
This commit is contained in:
@@ -4,6 +4,7 @@ import com.aitrainer.api.model.ExercisePlan
|
||||
import com.aitrainer.api.repository.ExercisePlanRepository
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.annotation.Secured
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import javax.validation.Valid
|
||||
|
||||
@@ -29,4 +30,23 @@ class ExercisePlanController( private val exercisePlanRepository: ExercisePlanRe
|
||||
ResponseEntity.ok().body(exercisePlan) else
|
||||
ResponseEntity.notFound().build()
|
||||
}
|
||||
|
||||
@Secured
|
||||
@PostMapping("/exercise_plan/{exercisePlanId}")
|
||||
fun updateExercisePlanById(@PathVariable(value = "exercisePlanId") exercisePlanId: Long,
|
||||
@Valid @RequestBody exercisePlan: ExercisePlan,
|
||||
@RequestHeader requestHeader: RequestHeader?
|
||||
): ResponseEntity<ExercisePlan> {
|
||||
val returnPlan = exercisePlanRepository.findById(exercisePlanId).orElse(null)
|
||||
?: return ResponseEntity.notFound().build()
|
||||
val updatedPlan = returnPlan.copy(
|
||||
customerId = exercisePlan.customerId,
|
||||
name = exercisePlan.name,
|
||||
dateAdd = exercisePlan.dateAdd,
|
||||
description = exercisePlan.description
|
||||
)
|
||||
updatedPlan.exercisePlanId = exercisePlan.exercisePlanId
|
||||
logger.info("-- updateExercisePlanById id: $updatedPlan ")
|
||||
return ResponseEntity.ok().body(exercisePlanRepository.save(updatedPlan))
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.ExercisePlan
|
||||
import com.aitrainer.api.model.ExercisePlanDetail
|
||||
import com.aitrainer.api.model.ExerciseType
|
||||
import com.aitrainer.api.repository.ExercisePlanDetailRepository
|
||||
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.*
|
||||
import javax.validation.Valid
|
||||
|
||||
@@ -25,4 +28,43 @@ class ExercisePlanDetailController( private val exercisePlanDetailRepository: Ex
|
||||
logger.info("-- getExercisePlanByCustomerId id: $customerId -- $list")
|
||||
return ResponseEntity.ok().body(list)
|
||||
}
|
||||
|
||||
@Secured
|
||||
@PostMapping("/exercise_plan_detail/{exercisePlanDetailId}")
|
||||
fun updateExercisePlanDetailById(@PathVariable(value = "exercisePlanDetailId") exercisePlanDetailId: Long,
|
||||
@Valid @RequestBody exercisePlanDetail: ExercisePlanDetail,
|
||||
@RequestHeader requestHeader: RequestHeader?
|
||||
): ResponseEntity<ExercisePlanDetail> {
|
||||
val returnPlanDetail = exercisePlanDetailRepository.findById(exercisePlanDetailId).orElse(null)
|
||||
?: return ResponseEntity.notFound().build()
|
||||
|
||||
val updatedPlanDetail = returnPlanDetail.copy(
|
||||
exercisePlan = exercisePlanDetail.exercisePlan,
|
||||
exerciseTypeId = exercisePlanDetail.exerciseTypeId,
|
||||
serie = exercisePlanDetail.serie,
|
||||
repeats = exercisePlanDetail.repeats,
|
||||
weightEquation = exercisePlanDetail.weightEquation
|
||||
)
|
||||
updatedPlanDetail.exercisePlanDetailId = exercisePlanDetail.exercisePlanDetailId
|
||||
logger.info("-- updateExercisePlanDetailById id: $updatedPlanDetail ")
|
||||
return ResponseEntity.ok().body(exercisePlanDetailRepository.save(updatedPlanDetail))
|
||||
}
|
||||
|
||||
@Secured
|
||||
@PostMapping("/exercise_plan_detail/delete/{exercisePlanDetailId}")
|
||||
fun deleteExercisePlanDetailById(@PathVariable(value = "exercisePlanDetailId") exercisePlanDetailId: Long,
|
||||
@Valid @RequestBody exercisePlanDetail: ExercisePlanDetail)
|
||||
:ResponseEntity<HttpStatus> {
|
||||
val returnPlanDetail = exercisePlanDetailRepository.findById(exercisePlanDetailId).orElse(null)
|
||||
if (returnPlanDetail == null) {
|
||||
return ResponseEntity.ok().body(HttpStatus.NOT_FOUND)
|
||||
}
|
||||
try {
|
||||
exercisePlanDetailRepository.delete(returnPlanDetail)
|
||||
} catch (e: IllegalArgumentException ) {
|
||||
return ResponseEntity.ok().body(HttpStatus.BAD_REQUEST)
|
||||
}
|
||||
|
||||
return ResponseEntity.ok().body(HttpStatus.OK)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user