Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 유저 가입 카운트 기능 추가 #22

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package com.vacgom.backend.auth.application.dto.response
import com.vacgom.backend.member.domain.Member

class MeResponse(
val id: String,
val nickname: String?,
val level: String,
val healthConditions: List<HealthConditionResponse>,
val id: String,
val nickname: String?,
val healthConditions: List<HealthConditionResponse>,
) {
companion object {
fun of(member: Member): MeResponse {
return MeResponse(
id = member.id.toString(),
nickname = member.nickname?.nickname,
level = "레벨",
healthConditions = member.healthProfiles.map { HealthConditionResponse.of(it.healthCondition) },
id = member.id.toString(),
nickname = member.nickname?.nickname,
healthConditions = member.healthProfiles.map { HealthConditionResponse.of(it.healthCondition) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import org.springframework.stereotype.Service

@Service
class DiseaseService(
private val diseaseRepository: DiseaseRepository,
private val diseaseRepository: DiseaseRepository,
) {
fun findById(id: Long): Disease {
return diseaseRepository.findById(id).orElseThrow { IllegalArgumentException("Disease not found") }
}

fun findAll(): List<Disease> {
return diseaseRepository.findAll()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.vacgom.backend.disease.application.dto.response

import com.vacgom.backend.disease.domain.Disease
import com.vacgom.backend.search.application.dto.QnaResponse

class DiseaseResponse(
val id: Long?,
val name: String,
val iconImage: String?,
val description: String?,
val id: Long?,
val name: String,
val mainImage: String?,
val iconImage: String?,
val description: String?,
val qnaList: List<QnaResponse> = listOf(),
) {
companion object {
fun of(disease: Disease): DiseaseResponse {
return DiseaseResponse(
id = disease.id,
name = disease.name,
iconImage = disease.iconImage,
description = disease.description,
id = disease.id,
name = disease.name,
mainImage = disease.mainImage,
iconImage = disease.iconImage,
description = disease.description,
qnaList = disease.qnaList.map { QnaResponse.of(it.question, it.answer) },
)
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/main/kotlin/com/vacgom/backend/disease/domain/Disease.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import jakarta.persistence.*
@Entity()
@Table(name = "t_disease")
class Disease(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "disease_id")
val id: Long? = null,
val name: String,
val iconImage: String?,
val description: String?,
val ageFilter: Int,
val conditionalAgeFilter: Int,
val healthConditionFilter: Int,
val forbiddenHealthConditionFilter: Int,
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "disease_id")
val id: Long? = null,
val name: String,
val iconImage: String?,
val mainImage: String?,
val description: String?,
val ageFilter: Int,
val conditionalAgeFilter: Int,
val healthConditionFilter: Int,
val forbiddenHealthConditionFilter: Int,
@OneToMany(mappedBy = "disease", fetch = FetchType.EAGER, cascade = [CascadeType.ALL])
val qnaList: List<Qna>,
) : BaseEntity()
17 changes: 17 additions & 0 deletions src/main/kotlin/com/vacgom/backend/disease/domain/Qna.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.vacgom.backend.disease.domain

import jakarta.persistence.*

@Entity()
@Table(name = "t_qna")
class Qna(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "qna_id")
val id: Long? = null,
val question: String,
val answer: String,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "disease_id")
val disease: Disease,
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package com.vacgom.backend.disease.presentation

import com.vacgom.backend.disease.application.DiseaseService
import com.vacgom.backend.disease.application.dto.response.DiseaseResponse
import com.vacgom.backend.disease.domain.constants.HealthCondition
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1/diseases")
class DiseaseController(
val diseaseService: DiseaseService,
val diseaseService: DiseaseService,
) {
@GetMapping("/healthConditions")
fun healthConditions(): ResponseEntity<List<Pair<String, String>>> {
return ResponseEntity.ok(HealthCondition.entries.map { Pair(it.name, it.description) })
}

@GetMapping("/{id}")
fun getDisease(
@PathVariable id: Long,
): ResponseEntity<DiseaseResponse> {
val disease = diseaseService.findById(id)

return ResponseEntity.ok(DiseaseResponse.of(disease))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vacgom.backend.presentation.inoculation
package com.vacgom.backend.inoculation.presentation

import com.vacgom.backend.global.security.annotation.AuthId
import com.vacgom.backend.inoculation.application.InoculationService
Expand All @@ -12,22 +12,22 @@ import java.util.*
@RestController
@RequestMapping("/api/v1/inoculation")
class InoculationController(
private val inoculationService: InoculationService
private val inoculationService: InoculationService,
) {
@GetMapping("/simple")
fun getInoculationSimpleResponse(
@AuthId id: UUID,
@RequestParam type: String
@AuthId id: UUID,
@RequestParam type: String,
): ResponseEntity<List<InoculationSimpleResponse>> {
val responses = inoculationService.getInoculationSimpleResponse(id, type)
return ResponseEntity.ok(responses)
}

@GetMapping("/detail")
fun getInoculationDetailResponse(
@AuthId id: UUID,
@RequestBody request: DiseaseNameRequest,
@RequestParam type: String
@AuthId id: UUID,
@RequestBody request: DiseaseNameRequest,
@RequestParam type: String,
): ResponseEntity<List<InoculationDetailResponse>> {
val responses = inoculationService.getInoculationDetailResponse(id, request, type)
return ResponseEntity.ok(responses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ import java.util.*
@Component
@Transactional
class MemberService(
private val memberRepository: MemberRepository,
private val memberRepository: MemberRepository,
) {
fun getJoinCount(id: UUID): Number {
return memberRepository.getJoinCount(id)
}

fun findMember(id: UUID): Member? {
return memberRepository.findById(id).orElseThrow()
}

fun updateHealthCondition(
id: UUID,
healthProfiles: List<HealthCondition>,
id: UUID,
healthProfiles: List<HealthCondition>,
): Member {
val member =
memberRepository.findById(id).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}
memberRepository.findById(id).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}

member.healthProfiles.clear()
member.healthProfiles.addAll(healthProfiles.map { HealthProfile(member, it) })
Expand All @@ -35,9 +39,9 @@ class MemberService(

fun withdrawMember(id: UUID) {
val member =
memberRepository.findById(id).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}
memberRepository.findById(id).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}
memberRepository.delete(member)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.vacgom.backend.member.application.dto.response

class JoinCountResponse(
val userId: String,
val joinCount: Number,
)
6 changes: 3 additions & 3 deletions src/main/kotlin/com/vacgom/backend/member/domain/Member.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import java.util.*
@Entity
@Table(name = "t_member")
class Member(
var providerId: Long,
@Enumerated(EnumType.STRING) var providerType: ProviderType,
@Enumerated(EnumType.STRING) var role: Role,
var providerId: Long,
@Enumerated(EnumType.STRING) var providerType: ProviderType,
@Enumerated(EnumType.STRING) var role: Role,
) : BaseEntity() {
@Id
@GenericGenerator(name = "uuid2", strategy = "uuid2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import com.vacgom.backend.auth.domain.constants.ProviderType
import com.vacgom.backend.member.domain.Member
import com.vacgom.backend.member.domain.Nickname
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.util.*

interface MemberRepository : JpaRepository<Member, UUID> {
fun findByProviderIdAndProviderType(
providerId: Long,
providerType: ProviderType
providerId: Long,
providerType: ProviderType,
): Member?

fun existsMemberByNickname(nickname: Nickname): Boolean

@Query("SELECT COUNT(m)+1 FROM Member m WHERE m.createdDate <= (SELECT m.createdDate FROM Member m WHERE m.id = :uuid)")
fun getJoinCount(uuid: UUID): Number
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.vacgom.backend.global.exception.error.BusinessException
import com.vacgom.backend.global.security.annotation.AuthId
import com.vacgom.backend.member.application.MemberService
import com.vacgom.backend.member.application.dto.request.UpdateHealthConditionRequest
import com.vacgom.backend.member.application.dto.response.JoinCountResponse
import com.vacgom.backend.member.exception.HealthConditionError
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
Expand All @@ -14,11 +15,25 @@ import java.util.*
@RestController
@RequestMapping("/api/v1/me")
class MeController(
private val memberService: MemberService,
private val memberService: MemberService,
) {
@GetMapping("/joinCount")
fun getJoinCount(
@AuthId id: UUID,
): ResponseEntity<JoinCountResponse> {
val joinCount = this.memberService.getJoinCount(id)

return ResponseEntity.ok(
JoinCountResponse(
userId = id.toString(),
joinCount = joinCount,
),
)
}

@DeleteMapping
fun withdraw(
@AuthId id: UUID,
@AuthId id: UUID,
): ResponseEntity<Boolean> {
this.memberService.withdrawMember(id)

Expand All @@ -27,35 +42,35 @@ class MeController(

@GetMapping
fun findMe(
@AuthId id: UUID,
@AuthId id: UUID,
): ResponseEntity<MeResponse> {
val member = this.memberService.findMember(id) ?: throw Error()

return ResponseEntity.ok(
MeResponse.of(member),
MeResponse.of(member),
)
}

@PostMapping("/healthCondition")
fun updateHealthCondition(
@AuthId id: UUID,
@RequestBody request: UpdateHealthConditionRequest,
@AuthId id: UUID,
@RequestBody request: UpdateHealthConditionRequest,
): ResponseEntity<MeResponse> {
val conditions =
runCatching {
request.healthProfiles.map { HealthCondition.valueOf(it) }
}.getOrElse {
throw BusinessException(HealthConditionError.INVALID_HEALTH_CONDITION)
}
runCatching {
request.healthProfiles.map { HealthCondition.valueOf(it) }
}.getOrElse {
throw BusinessException(HealthConditionError.INVALID_HEALTH_CONDITION)
}

val member =
this.memberService.updateHealthCondition(
id,
conditions,
)
this.memberService.updateHealthCondition(
id,
conditions,
)

return ResponseEntity.ok(
MeResponse.of(member),
MeResponse.of(member),
)
}
}
Loading
Loading