ExercisePlan, Detail Update, Delete

This commit is contained in:
Bossanyi Tibor
2020-09-09 00:02:49 +02:00
parent f5a6f17ef5
commit d860d79667
11 changed files with 157 additions and 7 deletions
@@ -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)
}
}
@@ -11,7 +11,8 @@ data class ExercisePlan(
@get: NonNull var name: String? = null,
@get: NonNull var description: String? = null,
@get: NonNull var private: Boolean = false,
@get: NonNull var dateAdd: String? = null
@get: NonNull var dateAdd: String? = null,
@get: NonNull var dateUpd: String? = null
) {
@Id
@@ -19,5 +19,5 @@ data class ExercisePlanDetail (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val exercisePlanDetailId: Long = 0
var exercisePlanDetailId: Long = 0
}