Merge branch 'zalan' into 'master'
Zalan See merge request bossanyit/aitrainer_server!5
This commit is contained in:
commit
6bfbdd2b59
@ -6,3 +6,4 @@ provide a RESTful API to the mobile app
|
|||||||
finished API:
|
finished API:
|
||||||
|
|
||||||
customers
|
customers
|
||||||
|
exercise type impl.ed
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.aitrainer.api.controller
|
||||||
|
|
||||||
|
import com.aitrainer.api.model.ExerciseType
|
||||||
|
import com.aitrainer.api.repository.ExerciseTypeRepository
|
||||||
|
import org.springframework.http.HttpStatus
|
||||||
|
import org.springframework.http.ResponseEntity
|
||||||
|
import org.springframework.web.bind.annotation.*
|
||||||
|
import java.util.*
|
||||||
|
import javax.validation.Valid
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api")
|
||||||
|
class ExerciseTypeController ( private val exerciseTypeRepository: ExerciseTypeRepository ) {
|
||||||
|
|
||||||
|
@GetMapping("/exercise_type")
|
||||||
|
fun getAllExerciseTypes(): List<ExerciseType> =
|
||||||
|
exerciseTypeRepository.findAll()
|
||||||
|
|
||||||
|
@PostMapping("/exercise_type")
|
||||||
|
fun createNewExerciseType(@Valid @RequestBody exerciseType: ExerciseType): ExerciseType =
|
||||||
|
exerciseTypeRepository.save(exerciseType)
|
||||||
|
|
||||||
|
@GetMapping("/exercise_type/{id}")
|
||||||
|
fun getExerciseTypeById(@PathVariable(value = "id") exerciseTypeId: Long): ResponseEntity<ExerciseType> {
|
||||||
|
return exerciseTypeRepository.findById(exerciseTypeId).map { exerciseType ->
|
||||||
|
ResponseEntity.ok(exerciseType)
|
||||||
|
}.orElse(ResponseEntity.notFound().build())
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
@GetMapping("/exercise_type/name/{name}")
|
||||||
|
fun getExerciseTypeByName(@PathVariable(value = "name") name: String): ResponseEntity<ExerciseType> {
|
||||||
|
return exerciseTypeRepository.findByName(name).map { exerciseType ->
|
||||||
|
ResponseEntity.ok(exerciseType)
|
||||||
|
}.orElse(ResponseEntity.notFound().build())
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@PutMapping("/exercise_type/{id}")
|
||||||
|
fun updateExerciseTypesById(@PathVariable(value = "id") exerciseTypeId: Long,
|
||||||
|
@Valid @RequestBody newExerciseType: ExerciseType): ResponseEntity<ExerciseType> {
|
||||||
|
|
||||||
|
return exerciseTypeRepository.findById(exerciseTypeId).map { existingExerciseType ->
|
||||||
|
val updatedExerciseType: ExerciseType = existingExerciseType
|
||||||
|
.copy(name = newExerciseType.name,
|
||||||
|
description = newExerciseType.description,
|
||||||
|
video = newExerciseType.video)
|
||||||
|
ResponseEntity.ok().body(exerciseTypeRepository.save(updatedExerciseType))
|
||||||
|
}.orElse(ResponseEntity.notFound().build())
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
20
src/main/kotlin/com/aitrainer/api/model/ExerciseType.kt
Normal file
20
src/main/kotlin/com/aitrainer/api/model/ExerciseType.kt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.aitrainer.api.model
|
||||||
|
|
||||||
|
import javax.persistence.Entity
|
||||||
|
import javax.persistence.GeneratedValue
|
||||||
|
import javax.persistence.GenerationType
|
||||||
|
import javax.persistence.Id
|
||||||
|
import javax.validation.constraints.NotBlank
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
data class ExerciseType (
|
||||||
|
@get: NotBlank
|
||||||
|
|
||||||
|
val name: String = "",
|
||||||
|
val description: String = "",
|
||||||
|
val video: Long = 0,
|
||||||
|
|
||||||
|
|
||||||
|
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
val type_id: Long = 0
|
||||||
|
)
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.aitrainer.api.repository
|
||||||
|
|
||||||
|
import com.aitrainer.api.model.ExerciseType
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.stereotype.Repository
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
interface ExerciseTypeRepository : JpaRepository<ExerciseType, Long>{
|
||||||
|
|
||||||
|
fun findByName(name: String): ExerciseType
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user