From 1b9e179bce591e529d7163cc1d2985921c5419d5 Mon Sep 17 00:00:00 2001 From: jombi Date: Thu, 9 May 2024 08:21:42 +0900 Subject: [PATCH] feat: lanugage --- .../molohala/grow/api/info/InfoController.kt | 2 +- .../grow/api/language/LanguageController.kt | 25 +++++++++ .../application/service/InfoServiceImpl.kt | 1 + .../application/dto/req/EditLanguageReq.kt | 7 +++ .../application/runner/LanguageLoader.kt | 43 +++++++++++++++ .../application/service/LanguageService.kt | 9 ++++ .../service/LanguageServiceImpl.kt | 52 +++++++++++++++++++ .../core/language/domain/entity/Language.kt | 12 +++++ .../domain/entity/MemberAndLanguage.kt | 22 ++++++++ .../exception/LanguageExceptionCode.kt | 13 +++++ .../repository/LanguageJpaRepository.kt | 6 +++ .../repository/MemberLanguageJpaRepository.kt | 8 +++ .../MemberLanguageQueryDslRepository.kt | 29 +++++++++++ .../MemberLanguageQueryRepository.kt | 7 +++ 14 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 grow-api/src/main/kotlin/com/molohala/grow/api/language/LanguageController.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/application/dto/req/EditLanguageReq.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/application/runner/LanguageLoader.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageService.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageServiceImpl.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/Language.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/MemberAndLanguage.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/exception/LanguageExceptionCode.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/LanguageJpaRepository.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageJpaRepository.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryDslRepository.kt create mode 100644 grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryRepository.kt diff --git a/grow-api/src/main/kotlin/com/molohala/grow/api/info/InfoController.kt b/grow-api/src/main/kotlin/com/molohala/grow/api/info/InfoController.kt index fa3a6c1..f483b47 100644 --- a/grow-api/src/main/kotlin/com/molohala/grow/api/info/InfoController.kt +++ b/grow-api/src/main/kotlin/com/molohala/grow/api/info/InfoController.kt @@ -25,7 +25,7 @@ class InfoController( fun getMyUserInfo() = ResponseData.ok("내 정보 조회 완료", infoService.getMyInfo()) @PatchMapping("/me") - fun patchMyInfo(@RequestBody data: EditUserInfoReq): Response { + fun editMyInfo(@RequestBody data: EditUserInfoReq): Response { infoService.editInfo(data.bio, data.job) return Response.ok("내 정보 수정 완료") } diff --git a/grow-api/src/main/kotlin/com/molohala/grow/api/language/LanguageController.kt b/grow-api/src/main/kotlin/com/molohala/grow/api/language/LanguageController.kt new file mode 100644 index 0000000..f1a3d8d --- /dev/null +++ b/grow-api/src/main/kotlin/com/molohala/grow/api/language/LanguageController.kt @@ -0,0 +1,25 @@ +package com.molohala.grow.api.language + +import com.molohala.grow.api.response.Response +import com.molohala.grow.api.response.ResponseData +import com.molohala.grow.core.language.application.dto.req.EditLanguageReq +import com.molohala.grow.core.language.application.service.LanguageService +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/language") +class LanguageController( + private val languageService: LanguageService +) { + @GetMapping + fun getAll() = ResponseData.ok("언어 리스트 조회 완료", languageService.getAvailableLanguage()) + + @GetMapping("/me") + fun languages() = ResponseData.ok("사용하는 언어 조회 완료", languageService.getUsingLanguages()) + + @PatchMapping("/me") + fun editLanguage(@RequestBody data: EditLanguageReq): Response { + languageService.updateUsingLanguages(data.langs) + return Response.ok("사용하는 언어 갱신 완료") + } +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/info/application/service/InfoServiceImpl.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/info/application/service/InfoServiceImpl.kt index 02b51ae..7adfff0 100644 --- a/grow-core/src/main/kotlin/com/molohala/grow/core/info/application/service/InfoServiceImpl.kt +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/info/application/service/InfoServiceImpl.kt @@ -132,6 +132,7 @@ class InfoServiceImpl( socialAccountJpaRepository.save(SocialAccount(name, SocialType.SOLVED_AC, member.id)) } + @Transactional(rollbackOn = [Exception::class]) override fun editInfo(bio: String?, job: String?) { memberJpaRepository.save( memberSessionHolder.current().apply { updateInfo(bio ?: this.bio, MemberJob.parse(job) ?: this.job) } diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/dto/req/EditLanguageReq.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/dto/req/EditLanguageReq.kt new file mode 100644 index 0000000..2ace552 --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/dto/req/EditLanguageReq.kt @@ -0,0 +1,7 @@ +package com.molohala.grow.core.language.application.dto.req + +import com.fasterxml.jackson.annotation.JsonCreator + +data class EditLanguageReq @JsonCreator constructor( + val langs: List +) \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/runner/LanguageLoader.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/runner/LanguageLoader.kt new file mode 100644 index 0000000..d413336 --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/runner/LanguageLoader.kt @@ -0,0 +1,43 @@ +package com.molohala.grow.core.language.application.runner + +import com.molohala.grow.core.language.domain.entity.Language +import com.molohala.grow.core.language.repository.LanguageJpaRepository +import org.springframework.boot.ApplicationArguments +import org.springframework.boot.ApplicationRunner +import org.springframework.stereotype.Component + +@Component +class LanguageLoader( + private val languageJpaRepository: LanguageJpaRepository +) : ApplicationRunner { + override fun run(args: ApplicationArguments?) { + val languages = languageJpaRepository.findAll() + val m = listOf( + "Python", + "HTML", + "CSS", + "JavaScript", + "TypeScript", + "C", + "C++", + "C#", + "Kotlin", + "Java", + "Swift", + "Dart", + "Go", + "Ruby", + "Rust", + "SQL", + "PHP", + "Scala", + "Lua", + ) + val addLang = ArrayList() + + for (s in m) if (languages.none { it.name.lowercase() == s.lowercase() }) addLang.add(Language(s)) + + languageJpaRepository.saveAll(addLang) + } + +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageService.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageService.kt new file mode 100644 index 0000000..0e8c659 --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageService.kt @@ -0,0 +1,9 @@ +package com.molohala.grow.core.language.application.service + +import com.molohala.grow.core.language.domain.entity.Language + +interface LanguageService { + fun updateUsingLanguages(langs: List) + fun getUsingLanguages(): List + fun getAvailableLanguage(): List +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageServiceImpl.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageServiceImpl.kt new file mode 100644 index 0000000..6d88504 --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/application/service/LanguageServiceImpl.kt @@ -0,0 +1,52 @@ +package com.molohala.grow.core.language.application.service + +import com.molohala.grow.common.exception.custom.CustomException +import com.molohala.grow.core.language.domain.entity.Language +import com.molohala.grow.core.language.domain.entity.MemberAndLanguage +import com.molohala.grow.core.language.exception.LanguageExceptionCode +import com.molohala.grow.core.language.repository.LanguageJpaRepository +import com.molohala.grow.core.language.repository.MemberLanguageJpaRepository +import com.molohala.grow.core.language.repository.MemberLanguageQueryRepository +import com.molohala.grow.core.member.application.MemberSessionHolder +import jakarta.transaction.Transactional +import org.springframework.stereotype.Service + +@Service +class LanguageServiceImpl( + private val memberSessionHolder: MemberSessionHolder, + private val languageJpaRepository: LanguageJpaRepository, + private val memberLanguageJpaRepository: MemberLanguageJpaRepository, + private val memberLanguageQueryRepository: MemberLanguageQueryRepository +) : LanguageService { + @Transactional(rollbackOn = [Exception::class]) + override fun updateUsingLanguages(langs: List) { + val member = memberSessionHolder.current() + val dbLang = languageJpaRepository.findAll() + + val owning = + memberLanguageJpaRepository + .findAllByMemberIdIs(member.id!!) + + memberLanguageJpaRepository.deleteAllInBatch(owning.filter { it.languageId !in langs }) + + val toAdd = ArrayList() + + for (lang in langs) { + val ent = dbLang.find { it.id == lang } + if (ent == null) + throw CustomException(LanguageExceptionCode.LANGUAGE_NOT_FOUND) + if (owning.none { it.languageId == lang }) toAdd.add(ent) // not owning + } + + memberLanguageJpaRepository.saveAllAndFlush( + toAdd.map { MemberAndLanguage(member.id, it.id!!) } + ) + } + + override fun getUsingLanguages(): List { + val member = memberSessionHolder.current() + return memberLanguageQueryRepository.getLanguagesByMemberId(member.id!!) + } + + override fun getAvailableLanguage(): List = languageJpaRepository.findAll() +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/Language.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/Language.kt new file mode 100644 index 0000000..b74b5d3 --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/Language.kt @@ -0,0 +1,12 @@ +package com.molohala.grow.core.language.domain.entity + +import jakarta.persistence.* + +@Entity(name = "tbl_langs") +data class Language( + val name: String, + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(updatable = false, nullable = false, name = "id") + val id: Long? = null +) \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/MemberAndLanguage.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/MemberAndLanguage.kt new file mode 100644 index 0000000..914358a --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/domain/entity/MemberAndLanguage.kt @@ -0,0 +1,22 @@ +package com.molohala.grow.core.language.domain.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id + +@Entity(name = "tbl_many_member_language") +class MemberAndLanguage( + val memberId: Long, + val languageId: Long, + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column( + name = "id", + nullable = false, + updatable = false + ) + val id: Long? = null, +) \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/exception/LanguageExceptionCode.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/exception/LanguageExceptionCode.kt new file mode 100644 index 0000000..98fe79f --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/exception/LanguageExceptionCode.kt @@ -0,0 +1,13 @@ +package com.molohala.grow.core.language.exception + +import com.molohala.grow.common.exception.ExceptionCode +import org.springframework.http.HttpStatus + +enum class LanguageExceptionCode(private val status: HttpStatus, private val message: String) : ExceptionCode { + LANGUAGE_NOT_FOUND(HttpStatus.BAD_REQUEST, "해당하는 언어를 찾을 수 없음"), + ; + + override fun getHttpStatus() = status + override fun getExceptionName() = name + override fun getMessage() = message +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/LanguageJpaRepository.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/LanguageJpaRepository.kt new file mode 100644 index 0000000..767529d --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/LanguageJpaRepository.kt @@ -0,0 +1,6 @@ +package com.molohala.grow.core.language.repository + +import com.molohala.grow.core.language.domain.entity.Language +import org.springframework.data.jpa.repository.JpaRepository + +interface LanguageJpaRepository : JpaRepository \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageJpaRepository.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageJpaRepository.kt new file mode 100644 index 0000000..f010528 --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageJpaRepository.kt @@ -0,0 +1,8 @@ +package com.molohala.grow.core.language.repository + +import com.molohala.grow.core.language.domain.entity.MemberAndLanguage +import org.springframework.data.jpa.repository.JpaRepository + +interface MemberLanguageJpaRepository : JpaRepository { + fun findAllByMemberIdIs(memberId: Long): List +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryDslRepository.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryDslRepository.kt new file mode 100644 index 0000000..f78c6ba --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryDslRepository.kt @@ -0,0 +1,29 @@ +package com.molohala.grow.core.language.repository + +import com.molohala.grow.core.language.domain.entity.Language +import com.molohala.grow.core.language.domain.entity.QLanguage.language +import com.molohala.grow.core.language.domain.entity.QMemberAndLanguage.memberAndLanguage +import com.querydsl.core.types.Projections +import com.querydsl.jpa.impl.JPAQueryFactory +import org.springframework.stereotype.Repository + +@Repository +class MemberLanguageQueryDslRepository( + private val queryFactory: JPAQueryFactory +) : MemberLanguageQueryRepository { + override fun getLanguagesByMemberId(memberId: Long): List { + return queryFactory + .select( + Projections.constructor( + Language::class.java, + language.name, + language.id + ) + ) + .from(memberAndLanguage) + .where(memberAndLanguage.memberId.eq(memberId)) + .innerJoin(language) + .on(memberAndLanguage.languageId.eq(language.id)) + .fetch() + } +} \ No newline at end of file diff --git a/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryRepository.kt b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryRepository.kt new file mode 100644 index 0000000..b514def --- /dev/null +++ b/grow-core/src/main/kotlin/com/molohala/grow/core/language/repository/MemberLanguageQueryRepository.kt @@ -0,0 +1,7 @@ +package com.molohala.grow.core.language.repository + +import com.molohala.grow.core.language.domain.entity.Language + +interface MemberLanguageQueryRepository { + fun getLanguagesByMemberId(memberId: Long): List +} \ No newline at end of file