Skip to content

Commit

Permalink
✨ Add manner package/Add findByNickname in User
Browse files Browse the repository at this point in the history
  • Loading branch information
wonseok committed Jan 7, 2025
1 parent 9387c24 commit 2e77d1b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/kotlin/com/toyProject7/karrot/manner/MannerException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.toyProject7.karrot.manner

import com.toyProject7.karrot.DomainException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class MannerException(
errorCode: Int,
httpStatusCode: HttpStatusCode,
msg: String,
cause: Throwable? = null,
) : DomainException(errorCode, httpStatusCode, msg, cause)

class UserNotFoundException : MannerException(
errorCode = 0,
httpStatusCode = HttpStatus.NOT_FOUND,
msg = "User not found",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.toyProject7.karrot.manner.controller

import com.toyProject7.karrot.manner.service.MannerService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class MannerController(
private val mannerService: MannerService,
) {
@PatchMapping("api/profile/{nickname}/praise/{mannerType}")
fun increaseMannerCount(
@PathVariable nickname: String,
@PathVariable mannerType: MannerType,
): ResponseEntity<String> {
mannerService.increaseMannerCount(nickname, mannerType)
return ResponseEntity.noContent().build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.toyProject7.karrot.manner.controller

enum class MannerType {
KINDNESS,
PUNCTUALITY,
QUICK_RESPONSE,
DELIVERY,
ACCURATE_DESCRIPTION,
AFFORDABLE_PRICE,
DETAILED_DESCRIPTION,
SHARING,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.toyProject7.karrot.manner.persistence

import com.toyProject7.karrot.manner.controller.MannerType
import com.toyProject7.karrot.user.persistence.UserEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne

@Entity
class MannerEntity(
@Id
@GeneratedValue(strategy = GenerationType.UUID)
val id: Long? = null,
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
val user: UserEntity,
@Enumerated(EnumType.STRING)
@Column(name = "manner_type", nullable = false)
val mannerType: MannerType,
@Column(name = "count", nullable = false)
var count: Int = 0,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.toyProject7.karrot.manner.persistence

import com.toyProject7.karrot.manner.controller.MannerType
import org.springframework.data.jpa.repository.JpaRepository

interface MannerRepository : JpaRepository<MannerEntity, String> {
fun findByUserIdAndMannerType(
id: String,
mannerType: MannerType,
): MannerEntity?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.toyProject7.karrot.manner.service

import com.toyProject7.karrot.manner.UserNotFoundException
import com.toyProject7.karrot.manner.controller.MannerType
import com.toyProject7.karrot.manner.persistence.MannerEntity
import com.toyProject7.karrot.manner.persistence.MannerRepository
import com.toyProject7.karrot.user.controller.User
import com.toyProject7.karrot.user.persistence.UserRepository
import org.springframework.stereotype.Service

@Service
class MannerService(
private val mannerRepository: MannerRepository,
private val userRepository: UserRepository,
) {
fun increaseMannerCount(
nickname: String,
mannerType: MannerType,
) {
val userEntity = userRepository.findByNickname(nickname) ?: throw UserNotFoundException()
val user = User.fromEntity(userEntity)

val mannerEntity = mannerRepository.findByUserIdAndMannerType(user.id, mannerType)
if (mannerEntity != null) {
mannerEntity.count++
mannerRepository.save(mannerEntity)
} else {
val newMannerEntity =
MannerEntity(
user = userEntity,
mannerType = mannerType,
count = 1,
)
mannerRepository.save(newMannerEntity)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.springframework.data.jpa.repository.JpaRepository
interface UserRepository : JpaRepository<UserEntity, String> {
fun findByUserId(userId: String): UserEntity?

fun findByNickname(nickname: String): UserEntity?

fun existsByUserId(userId: String): Boolean

fun existsByNickname(nickname: String): Boolean
Expand Down

0 comments on commit 2e77d1b

Please sign in to comment.