-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add manner package/Add findByNickname in User
- Loading branch information
wonseok
committed
Jan 7, 2025
1 parent
9387c24
commit 2e77d1b
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/toyProject7/karrot/manner/MannerException.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,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", | ||
) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/toyProject7/karrot/manner/controller/MannerController.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,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() | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/toyProject7/karrot/manner/controller/MannerType.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,12 @@ | ||
package com.toyProject7.karrot.manner.controller | ||
|
||
enum class MannerType { | ||
KINDNESS, | ||
PUNCTUALITY, | ||
QUICK_RESPONSE, | ||
DELIVERY, | ||
ACCURATE_DESCRIPTION, | ||
AFFORDABLE_PRICE, | ||
DETAILED_DESCRIPTION, | ||
SHARING, | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/toyProject7/karrot/manner/persistence/MannerEntity.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,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, | ||
) |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/toyProject7/karrot/manner/persistence/MannerRepository.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,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? | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/com/toyProject7/karrot/manner/service/MannerService.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,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) | ||
} | ||
} | ||
} |
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