-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from KATJ-HH2/feature/tony/user-register
Feature/tony/user register
- Loading branch information
Showing
24 changed files
with
956 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/kotlin/com/hh2/katj/favorite/component/FavoriteManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.hh2.katj.favorite.component | ||
|
||
import com.hh2.katj.favorite.model.entity.Favorite | ||
import com.hh2.katj.favorite.repository.FavoriteRepository | ||
import com.hh2.katj.util.exception.ExceptionMessage.* | ||
import com.hh2.katj.util.exception.failWithMessage | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Component | ||
class FavoriteManager ( | ||
private val favoriteRepository: FavoriteRepository, | ||
) { | ||
|
||
@Transactional(readOnly = true) | ||
fun findAllFavorite(userId: Long): MutableList<Favorite> { | ||
val findAllFavorite = favoriteRepository.findAllByUserId(userId) | ||
|
||
return findAllFavorite | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
fun findOneFavorite(userId: Long, favoriteId: Long): Favorite { | ||
val findOneFavorite = favoriteExistValidation(userId, favoriteId) | ||
|
||
return findOneFavorite | ||
} | ||
|
||
@Transactional | ||
fun addFavorite(request: Favorite): Favorite { | ||
// 즐겨찾기 타이틀 중복 체크 | ||
val favoriteTitleDuplicate = favoriteRepository.findByTitle(request.title) | ||
|
||
if (favoriteTitleDuplicate != null) { | ||
throw IllegalArgumentException(DUPLICATED_DATA_ALREADY_EXISTS.name) | ||
} | ||
|
||
val savedFavorite = favoriteRepository.save(request) | ||
|
||
return savedFavorite | ||
} | ||
|
||
@Transactional | ||
fun updateFavorite(favoriteId: Long, request: Favorite): Favorite { | ||
favoriteExistValidation(request.user.id, favoriteId) | ||
|
||
val findFavorite = favoriteRepository.findByUserIdAndId(request.user.id, favoriteId) | ||
?: failWithMessage(ID_DOES_NOT_EXIST.name) | ||
|
||
findFavorite.update(request) | ||
|
||
try { | ||
findFavorite.update(request) | ||
} catch (e: Exception) { | ||
throw Exception(INTERNAL_SERVER_ERROR_FROM_DATABASE.name) | ||
} | ||
|
||
return findFavorite | ||
} | ||
|
||
@Transactional | ||
fun deleteOneFavorite(userId: Long, favoriteId: Long): Boolean { | ||
|
||
val deleteFavorite = favoriteExistValidation(userId, favoriteId) | ||
|
||
try { | ||
favoriteRepository.delete(deleteFavorite) | ||
} catch (e: Exception) { | ||
throw Exception(INTERNAL_SERVER_ERROR_FROM_DATABASE.name) | ||
} | ||
|
||
return true | ||
} | ||
|
||
@Transactional | ||
fun deleteAllFavorite(userId: Long): Boolean { | ||
|
||
try { | ||
favoriteRepository.deleteAllByUserId(userId) | ||
} catch (e: Exception) { | ||
throw Exception(INTERNAL_SERVER_ERROR_FROM_DATABASE.name) | ||
} | ||
|
||
return true | ||
} | ||
|
||
@Transactional | ||
fun deleteMultiFavorite(userId: Long, deleteFavoriteIds: List<Long>): Boolean { | ||
|
||
val deleteRowCount = favoriteRepository.deleteFavoritesByUserIdAndIdIn(userId, deleteFavoriteIds) | ||
|
||
if (deleteRowCount != deleteFavoriteIds.size) { | ||
throw Exception(INTERNAL_SERVER_ERROR_FROM_DATABASE.name) | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* 즐겨찾기 유효성 체크 | ||
* 해당 유저에게 즐겨찾기가 존재하는지 확인한다 | ||
*/ | ||
private fun favoriteExistValidation(userId: Long, favoriteId: Long): Favorite = | ||
favoriteRepository.findByUserIdAndId(userId, favoriteId) | ||
?: failWithMessage(ID_DOES_NOT_EXIST.name) | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/kotlin/com/hh2/katj/favorite/controller/FavoriteController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.hh2.katj.favorite.controller | ||
|
||
import com.hh2.katj.favorite.model.dto.request.RequestAddFavorite | ||
import com.hh2.katj.favorite.model.dto.request.RequestUpdateFavorite | ||
import com.hh2.katj.favorite.model.dto.response.ResponseFavorite | ||
import com.hh2.katj.favorite.service.FavoriteService | ||
import com.hh2.katj.user.service.UserService | ||
import jakarta.validation.Valid | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RequestMapping("/favorite") | ||
@RestController | ||
class FavoriteController ( | ||
private val favoriteService: FavoriteService, | ||
private val userService: UserService, | ||
){ | ||
|
||
/** | ||
* 해당 사용자에 즐겨찾기 추가 | ||
*/ | ||
@PostMapping | ||
fun addFavorite(@RequestParam userId: Long, @Valid @RequestBody requestAddFavorite: RequestAddFavorite): ResponseEntity<ResponseFavorite> { | ||
val findUser = userService.findByUserId(userId) | ||
|
||
val responseFavorite = favoriteService.addFavorite(requestAddFavorite.toEntity(findUser)) | ||
|
||
return ResponseEntity.ok(responseFavorite) | ||
} | ||
|
||
|
||
/** | ||
* 해당 사용자의 모든 즐겨찾기 조회 | ||
*/ | ||
@GetMapping | ||
fun findAllFavorite(@RequestParam userId: Long): ResponseEntity<List<ResponseFavorite>>{ | ||
val responseFavoriteList = favoriteService.findAllFavorite(userId) | ||
return ResponseEntity.ok(responseFavoriteList) | ||
} | ||
/** | ||
* 해당 사용자의 해당 즐겨찾기 조회 | ||
*/ | ||
@GetMapping("/{favoriteId}") | ||
fun findOneFavorite(@RequestParam userId: Long, @PathVariable("favoriteId") favoriteId: Long): ResponseEntity<ResponseFavorite> { | ||
val findOneFavorite = favoriteService.findOneFavorite(userId, favoriteId) | ||
return ResponseEntity.ok(findOneFavorite) | ||
} | ||
|
||
/** | ||
* 해당 사용자의 해당 즐겨찾기 수정 | ||
*/ | ||
@PutMapping("/{favoriteId}") | ||
fun updateFavorite(@RequestParam userId: Long, @PathVariable("favoriteId") favoriteId: Long, @RequestBody requestUpdateFavorite: RequestUpdateFavorite): ResponseEntity<ResponseFavorite> { | ||
val findUser = userService.findByUserId(userId) | ||
|
||
val updateFavorite = favoriteService.updateFavorite(favoriteId, requestUpdateFavorite.toEntity(findUser)) | ||
return ResponseEntity.ok(updateFavorite) | ||
} | ||
|
||
/** | ||
* 해당 사용자의 해당 즐겨찾기 삭제 | ||
*/ | ||
@DeleteMapping("/{favoriteId}") | ||
fun deleteOneFavorite(@RequestParam userId: Long, @PathVariable("favoriteId") favoriteId: Long): ResponseEntity<Boolean> { | ||
val deleteOneFavorite = favoriteService.deleteOneFavorite(userId, favoriteId) | ||
return ResponseEntity.ok(deleteOneFavorite) | ||
} | ||
|
||
/** | ||
* 해당 사용자의 요청온 모든 즐겨찾기 삭제 | ||
*/ | ||
@DeleteMapping("/delete-multi") | ||
fun deleteMultiFavorite(@RequestParam userId: Long, @RequestBody deleteFavoriteIds: List<Long>): ResponseEntity<Boolean> { | ||
val deleteOneFavorite = favoriteService.deleteMultiFavorite(userId, deleteFavoriteIds) | ||
return ResponseEntity.ok(deleteOneFavorite) | ||
} | ||
|
||
/** | ||
* 해당 사용자의 모든 즐겨찾기 삭제 | ||
*/ | ||
@DeleteMapping("/delete-all") | ||
fun deleteAllFavorite(@RequestParam userId: Long): ResponseEntity<Boolean> { | ||
val deleteOneFavorite = favoriteService.deleteAllFavorite(userId) | ||
return ResponseEntity.ok(deleteOneFavorite) | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
src/main/kotlin/com/hh2/katj/favorite/model/dto/RequestAddFavorite.kt
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
src/main/kotlin/com/hh2/katj/favorite/model/dto/RequestUpdateFavorite.kt
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/hh2/katj/favorite/model/dto/request/RequestAddFavorite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.hh2.katj.favorite.model.dto.request | ||
|
||
import com.hh2.katj.favorite.model.entity.Favorite | ||
import com.hh2.katj.user.model.entity.User | ||
import com.hh2.katj.util.model.RoadAddress | ||
|
||
/** | ||
* @author : tony | ||
* @description : 즐겨찾기 추가시 Controller 사용 목적의 DTO | ||
* @since : 2023-10-05 | ||
*/ | ||
data class RequestAddFavorite( | ||
val id: Long?, | ||
var roadAddress: RoadAddress, | ||
var title: String, | ||
var description: String?, | ||
) { | ||
fun toEntity(user: User): Favorite { | ||
return Favorite( | ||
roadAddress = this.roadAddress, | ||
title = this.title, | ||
description = this.description, | ||
user = user, | ||
) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/hh2/katj/favorite/model/dto/request/RequestUpdateFavorite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.hh2.katj.favorite.model.dto.request | ||
|
||
import com.hh2.katj.favorite.model.entity.Favorite | ||
import com.hh2.katj.user.model.entity.User | ||
import com.hh2.katj.util.model.RoadAddress | ||
|
||
/** | ||
* @author : tony | ||
* @description : 즐겨찾기 수정시 Controller 사용 목적의 DTO | ||
* @since : 2023-10-05 | ||
*/ | ||
data class RequestUpdateFavorite( | ||
val id: Long?, | ||
var roadAddress: RoadAddress, | ||
var title: String, | ||
var description: String?, | ||
) { | ||
fun toEntity(user: User): Favorite { | ||
return Favorite( | ||
roadAddress = this.roadAddress, | ||
title = this.title, | ||
description = this.description, | ||
user = user, | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/hh2/katj/favorite/model/dto/response/ResponseFavorite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.hh2.katj.favorite.model.dto.response | ||
|
||
import com.hh2.katj.user.model.entity.User | ||
import com.hh2.katj.util.model.RoadAddress | ||
|
||
/** | ||
* @author : tony | ||
* @description : 즐겨찾기 조회 후 결과 반환시 Controller 사용 목적의 DTO | ||
* @since : 2023-10-06 | ||
*/ | ||
data class ResponseFavorite( | ||
val id: Long?, | ||
var roadAddress: RoadAddress, | ||
var title: String, | ||
var description: String?, | ||
var user: User, | ||
) |
Oops, something went wrong.