Skip to content

Commit

Permalink
Make increaseInterest transactional
Browse files Browse the repository at this point in the history
Moves the logic inside service layer.
  • Loading branch information
radl97 committed Jun 8, 2022
1 parent 58594a4 commit 6f1fc3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/main/kotlin/hu/kirdev/schpincer/service/CircleService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ open class CircleService {
repo.save(circleEntity)
}

private fun internalIncreaseInterestAndSave(circleEntity: CircleEntity): Long {
val newInterest = circleEntity.interestCounter;
repo.save(circleEntity)
return newInterest
}

@Transactional(readOnly = false)
open fun increaseInterest(id: Long): Long? {
val circleEntity = getOne(id)
if circleEntity == null {
return null // TODO throw error?
}
return internalIncreaseInterestAndSave(circleEntity)
}

@Transactional(readOnly = false)
open fun increaseInterestByAlias(alias: String): Long? {
val circleEntity = findByAlias(id)
if circleEntity == null {
return null // TODO throw error?
}
return internalIncreaseInterestAndSave(circleEntity)
}

@Transactional(readOnly = true)
open fun getOne(id: Long): CircleEntity? {
return repo.getOne(id)
Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/hu/kirdev/schpincer/web/ApiController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,17 @@ open class ApiController(
@PostMapping("/provider/{circle}/increaseInterest")
@ResponseBody
fun circleIncreaseInterest(@PathVariable circle: String): ResponseEntity<String> {
val circleEntity: CircleEntity?
var result: Long?
if (circle.matches("^\\d+$".toRegex())) {
val id = circle.toLong()
circleEntity = circles.getOne(id)
result = circles.increaseInterest(id)
} else {
circleEntity = circles.findByAlias(circle)
result = circles.increaseInterestByAlias(circle)
}
if (circleEntity == null) {
if result != null {
return responseOf("Invalid Circle")
}
val newInterest = ++circleEntity.interestCounter
circles.save(circleEntity)
return responseOf(""+newInterest)
return responseOf(""+result)
}

// @PostMapping("/easteregg/trashpanda/{feedback}")
Expand Down

0 comments on commit 6f1fc3c

Please sign in to comment.