-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add request body * feat: modify dto nullability * feat: extract lab service && implement CRUD * feat: add lab language dto * feat: add lab events * fix: fix unique constraints for research language entity * feat: add methods to repositories * feat: add LAB CRUD APIs * refactor: seperate event services * refactor: professors to mutable var * fix: fix typos * comment: add comment to professor service method * test: remove unneed test file * feat: split search service and event service * feat: implement professor, research event service * fix: apply ktlint
- Loading branch information
Showing
22 changed files
with
775 additions
and
582 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/main/kotlin/com/wafflestudio/csereal/core/member/service/ProfessorEventService.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,120 @@ | ||
package com.wafflestudio.csereal.core.member.service | ||
|
||
import com.wafflestudio.csereal.core.member.database.MemberSearchEntity | ||
import com.wafflestudio.csereal.core.member.database.ProfessorEntity | ||
import com.wafflestudio.csereal.core.member.database.ProfessorRepository | ||
import com.wafflestudio.csereal.core.research.database.LabRepository | ||
import com.wafflestudio.csereal.core.research.event.LabCreatedEvent | ||
import com.wafflestudio.csereal.core.research.event.LabDeletedEvent | ||
import com.wafflestudio.csereal.core.research.event.LabModifiedEvent | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
interface ProfessorEventService { | ||
fun labDeletedEventListener(event: LabDeletedEvent) | ||
fun labModifiedEventListener(event: LabModifiedEvent) | ||
fun labCreatedEventListener(event: LabCreatedEvent) | ||
} | ||
|
||
@Service | ||
class ProfessorEventServiceImpl( | ||
private val professorRepository: ProfessorRepository, | ||
private val labRepository: LabRepository | ||
) : ProfessorEventService { | ||
@EventListener | ||
@Transactional | ||
override fun labCreatedEventListener(event: LabCreatedEvent) { | ||
if (event.professorIds.isEmpty()) { | ||
return | ||
} | ||
|
||
val lab = labRepository.findByIdOrNull(event.id)!! | ||
val professors = professorRepository.findAllById(event.professorIds) | ||
.takeIf { it.size == event.professorIds.size }!! | ||
|
||
// TODO: Consider professor's before lab value | ||
professors.forEach { | ||
it.lab = lab | ||
upsertProfessorSearchIndex(it) | ||
} | ||
} | ||
|
||
@EventListener | ||
@Transactional | ||
override fun labModifiedEventListener(event: LabModifiedEvent) { | ||
val lab = labRepository.findByIdOrNull(event.id)!! | ||
|
||
val oldProfessorIds = event.professorIdsModified.first | ||
val oldProfessors = oldProfessorIds.let { p -> | ||
professorRepository.findAllById(p) | ||
.takeIf { it.size == p.size }!! | ||
} | ||
|
||
val newProfessorIds = event.professorIdsModified.second | ||
val newProfessors = newProfessorIds.let { p -> | ||
professorRepository.findAllById(p) | ||
.takeIf { it.size == p.size }!! | ||
} | ||
|
||
when { | ||
oldProfessors.isEmpty() && newProfessors.isEmpty() -> {} | ||
|
||
oldProfessors.isEmpty() && newProfessors.isNotEmpty() -> { | ||
newProfessors.forEach { | ||
it.lab = lab | ||
upsertProfessorSearchIndex(it) | ||
} | ||
} | ||
|
||
oldProfessors.isNotEmpty() && newProfessors.isEmpty() -> { | ||
oldProfessors.forEach { | ||
it.lab = null | ||
upsertProfessorSearchIndex(it) | ||
} | ||
} | ||
|
||
oldProfessorIds == newProfessorIds -> { | ||
oldProfessors.forEach { upsertProfessorSearchIndex(it) } | ||
} | ||
|
||
else -> { | ||
val removeProfessorIds = oldProfessorIds - newProfessorIds | ||
oldProfessors.forEach { | ||
if (it.id in removeProfessorIds) { | ||
it.lab = null | ||
} | ||
upsertProfessorSearchIndex(it) | ||
} | ||
|
||
val addProfessorIds = newProfessorIds - oldProfessorIds | ||
newProfessors.forEach { | ||
if (it.id in addProfessorIds) { | ||
it.lab = lab | ||
} | ||
upsertProfessorSearchIndex(it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@EventListener | ||
@Transactional | ||
override fun labDeletedEventListener(event: LabDeletedEvent) { | ||
val lab = labRepository.findByIdOrNull(event.id)!! | ||
val professors = professorRepository.findAllById(event.professorIds) | ||
.takeIf { it.size == event.professorIds.size }!! | ||
|
||
professors.forEach { | ||
it.lab = null | ||
upsertProfessorSearchIndex(it) | ||
} | ||
} | ||
|
||
@Transactional | ||
fun upsertProfessorSearchIndex(professor: ProfessorEntity) { | ||
professor.memberSearch?.update(professor) | ||
?: let { professor.memberSearch = MemberSearchEntity.create(professor) } | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/wafflestudio/csereal/core/research/api/req/CreateLabLanguageReqBody.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.wafflestudio.csereal.core.research.api.req | ||
|
||
data class CreateLabLanguageReqBody( | ||
val ko: CreateLabReqBody, | ||
val en: CreateLabReqBody | ||
) | ||
|
||
data class CreateLabReqBody( | ||
val name: String, | ||
val description: String?, | ||
val groupId: Long?, | ||
val professorIds: Set<Long>, | ||
val location: String?, | ||
val tel: String?, | ||
val acronym: String?, | ||
val youtube: String?, | ||
val websiteURL: String? | ||
) |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/wafflestudio/csereal/core/research/api/req/ModifyLabLanguageReqBody.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,19 @@ | ||
package com.wafflestudio.csereal.core.research.api.req | ||
|
||
data class ModifyLabLanguageReqBody( | ||
val ko: ModifyLabReqBody, | ||
val en: ModifyLabReqBody | ||
) | ||
|
||
data class ModifyLabReqBody( | ||
val name: String, | ||
val description: String?, | ||
val location: String?, | ||
val tel: String?, | ||
val acronym: String?, | ||
val youtube: String?, | ||
val websiteURL: String?, | ||
val groupId: Long?, | ||
val professorIds: Set<Long>, | ||
val removePdf: Boolean | ||
) |
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
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
Oops, something went wrong.