Skip to content

Commit

Permalink
Merge pull request #83 from KATJ-HH2/feature/tony/user-register
Browse files Browse the repository at this point in the history
Feature/tony/user register
  • Loading branch information
TonyKim9401 authored Oct 9, 2023
2 parents 3354ec6 + 7b5fd2b commit b633f50
Show file tree
Hide file tree
Showing 24 changed files with 956 additions and 297 deletions.
1 change: 0 additions & 1 deletion .github/workflows/dev-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ jobs:
with:
files: '**/build/test-results/test/TEST-*.xml'


slack_noti:
name: Slack_notification
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ repositories {
mavenCentral()
}


dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springframework.boot:spring-boot-starter-aop")

runtimeOnly("com.mysql:mysql-connector-j")

testImplementation("org.springframework.boot:spring-boot-starter-test")

}

tasks.withType<KotlinCompile> {
Expand Down
109 changes: 109 additions & 0 deletions src/main/kotlin/com/hh2/katj/favorite/component/FavoriteManager.kt
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)



}
20 changes: 3 additions & 17 deletions src/main/kotlin/com/hh2/katj/favorite/component/FavoriteReader.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hh2.katj.favorite.component

import com.hh2.katj.favorite.model.entity.Favorite
import com.hh2.katj.favorite.model.dto.RequestAddFavorite
import com.hh2.katj.favorite.model.dto.request.RequestAddFavorite
import com.hh2.katj.favorite.repository.FavoriteRepository
import com.hh2.katj.user.model.entity.User
import com.hh2.katj.user.repository.UserRepository
Expand All @@ -14,9 +14,8 @@ import java.lang.IllegalArgumentException

@Component
class FavoriteReader (
private val favoriteRepository: FavoriteRepository,
private val userRepository: UserRepository,

private val favoriteRepository: FavoriteRepository,
private val userRepository: UserRepository,
) {

@Transactional
Expand Down Expand Up @@ -62,24 +61,11 @@ class FavoriteReader (
return findOneFavorite
}

@Transactional
fun updateFavorite(userId: Long?, favoriteId: Long?, requestFavorite: Favorite): Favorite {
userValidation(userId)

val findFavorite = favoriteRepository.findByUserIdAndId(userId, favoriteId)
?: failWithMessage(ID_DOES_NOT_EXIST.name)

findFavorite.update(requestFavorite)

return findFavorite
}

/**
* 유저 유효성 체크
*/
private fun userValidation(userId: Long?): User {
return userRepository.findByIdOrThrowMessage(userId, USER_DOES_NOT_EXIST.name)
}


}
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)
}
}

This file was deleted.

This file was deleted.

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,
)
}
}
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,
)
}
}
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,
)
Loading

0 comments on commit b633f50

Please sign in to comment.