Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestController
- @RequestMapping("/favorites")
- class FavoriteController(private val applicationUserRepository: ApplicationUserRepository,
- val lineService: LineService,
- val stationService: StationService,
- private var currentUser: ApplicationUser?) {
- private fun provideCurrentUser() : ApplicationUser? {
- if(this.currentUser == null) {
- val currentUsername: String = SecurityContextHolder.getContext().authentication.principal.toString()
- this.currentUser = this.applicationUserRepository.findByUsername(currentUsername)
- }
- return this.currentUser
- }
- @PostMapping
- fun addFavorite(@RequestParam(value = "type", required = true)type: FavoriteType?,
- @RequestParam(value = "id", required = true)id: Long?) : ResponseEntity<String> {
- return if(provideCurrentUser() == null || type == null || id == null) {
- ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("You have to be logged and provide the type " +
- "and the id of the desired entity in order to add it to your favorites list")
- } else {
- when(type) {
- FavoriteType.STATION -> {provideCurrentUser()?.addToFavorites(this.stationService.findById(id) as Station)
- this.applicationUserRepository.save(provideCurrentUser() ?: throw Exception("User Not found"))}
- FavoriteType.LINE -> {provideCurrentUser()?.addToFavorites(this.lineService.findById(id) as Line)
- this.applicationUserRepository.save(provideCurrentUser() ?: throw Exception("User Not found")) }
- FavoriteType.UNKNOWN -> return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("Unknown entity type")
- }
- ResponseEntity.status(HttpStatus.CREATED).body("The $type with id $id has been successfully added to your favorites")
- }
- }
- @Entity
- class ApplicationUser(
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(unique = true, nullable = false)
- val id: Long? = null,
- val username: String = "",
- var password: String = "",
- @ManyToMany(cascade= [CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE], fetch = FetchType.EAGER)
- @JoinTable(name="user_favstation", joinColumns= [JoinColumn(name="user_id")], inverseJoinColumns=[JoinColumn(name="station_id")])
- var favoriteStations: MutableSet<Station> = mutableSetOf(),
- @ManyToMany(cascade= [CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE] , fetch = FetchType.EAGER)
- @JoinTable(name="user_favline", joinColumns= [JoinColumn(name="user_id")], inverseJoinColumns=[JoinColumn(name="line_id")])
- var favoriteLines: MutableSet<Line> = mutableSetOf()) {
- fun addToFavorites(station: Station): Boolean {
- return this.favoriteStations.add(station)
- }
- fun addToFavorites(line: Line): Boolean {
- return this.favoriteLines.add(line)
- }
- fun deleteFavorite(station: Station): Boolean {
- return this.favoriteStations.remove(station)
- }
- fun deleteFavorite(line: Line): Boolean {
- return this.favoriteLines.remove(line)
- }
- }
Add Comment
Please, Sign In to add comment