Guest User

Untitled

a guest
Jan 29th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. @RestController
  2. @RequestMapping("/favorites")
  3. class FavoriteController(private val applicationUserRepository: ApplicationUserRepository,
  4. val lineService: LineService,
  5. val stationService: StationService,
  6. private var currentUser: ApplicationUser?) {
  7.  
  8. private fun provideCurrentUser() : ApplicationUser? {
  9. if(this.currentUser == null) {
  10. val currentUsername: String = SecurityContextHolder.getContext().authentication.principal.toString()
  11. this.currentUser = this.applicationUserRepository.findByUsername(currentUsername)
  12. }
  13. return this.currentUser
  14. }
  15.  
  16.  
  17. @PostMapping
  18. fun addFavorite(@RequestParam(value = "type", required = true)type: FavoriteType?,
  19. @RequestParam(value = "id", required = true)id: Long?) : ResponseEntity<String> {
  20. return if(provideCurrentUser() == null || type == null || id == null) {
  21. ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("You have to be logged and provide the type " +
  22. "and the id of the desired entity in order to add it to your favorites list")
  23. } else {
  24. when(type) {
  25. FavoriteType.STATION -> {provideCurrentUser()?.addToFavorites(this.stationService.findById(id) as Station)
  26. this.applicationUserRepository.save(provideCurrentUser() ?: throw Exception("User Not found"))}
  27. FavoriteType.LINE -> {provideCurrentUser()?.addToFavorites(this.lineService.findById(id) as Line)
  28. this.applicationUserRepository.save(provideCurrentUser() ?: throw Exception("User Not found")) }
  29. FavoriteType.UNKNOWN -> return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("Unknown entity type")
  30. }
  31. ResponseEntity.status(HttpStatus.CREATED).body("The $type with id $id has been successfully added to your favorites")
  32. }
  33. }
  34.  
  35. @Entity
  36. class ApplicationUser(
  37. @Id
  38. @GeneratedValue(strategy = GenerationType.IDENTITY)
  39. @Column(unique = true, nullable = false)
  40. val id: Long? = null,
  41. val username: String = "",
  42. var password: String = "",
  43. @ManyToMany(cascade= [CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE], fetch = FetchType.EAGER)
  44. @JoinTable(name="user_favstation", joinColumns= [JoinColumn(name="user_id")], inverseJoinColumns=[JoinColumn(name="station_id")])
  45. var favoriteStations: MutableSet<Station> = mutableSetOf(),
  46. @ManyToMany(cascade= [CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE] , fetch = FetchType.EAGER)
  47. @JoinTable(name="user_favline", joinColumns= [JoinColumn(name="user_id")], inverseJoinColumns=[JoinColumn(name="line_id")])
  48. var favoriteLines: MutableSet<Line> = mutableSetOf()) {
  49.  
  50. fun addToFavorites(station: Station): Boolean {
  51. return this.favoriteStations.add(station)
  52. }
  53.  
  54. fun addToFavorites(line: Line): Boolean {
  55. return this.favoriteLines.add(line)
  56. }
  57.  
  58. fun deleteFavorite(station: Station): Boolean {
  59. return this.favoriteStations.remove(station)
  60. }
  61.  
  62. fun deleteFavorite(line: Line): Boolean {
  63. return this.favoriteLines.remove(line)
  64. }
  65. }
Add Comment
Please, Sign In to add comment