Skip to content

Commit

Permalink
refactor: 학급 알림장 번역 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tlarbals824 committed Aug 27, 2024
1 parent f76d7c5 commit 79fca4c
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@ package com.asap.asapbackend.domain.classroom.application

import com.asap.asapbackend.domain.child.domain.service.ChildReader
import com.asap.asapbackend.domain.classroom.application.dto.*
import com.asap.asapbackend.domain.classroom.domain.service.ClassModifier
import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomAnnouncementAppender
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomAnnouncementReader
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomReader
import com.asap.asapbackend.domain.classroom.event.ClassroomAnnouncementCreateEvent
import com.asap.asapbackend.domain.teacher.domain.service.TeacherReader
import com.asap.asapbackend.domain.todo.domain.service.TodoAppender
import com.asap.asapbackend.global.security.getCurrentUserId
import com.asap.asapbackend.global.security.getTeacherId
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class ClassroomService(
private val classroomReader: ClassroomReader,
private val classModifier: ClassModifier,
private val teacherReader: TeacherReader,
private val childReader: ChildReader,
private val todoAppender: TodoAppender,
private val classroomAnnouncementReader: ClassroomAnnouncementReader
private val classroomAnnouncementReader: ClassroomAnnouncementReader,
private val classroomAnnouncementAppender: ClassroomAnnouncementAppender,
private val applicationEventPublisher: ApplicationEventPublisher
) {

@Transactional
Expand All @@ -29,8 +33,15 @@ class ClassroomService(
val classroom = classroomReader.findByTeacher(teacherId)
val teacher = teacherReader.findById(teacherId)

classroom.addAnnouncement(teacher, request.toAnnouncementDescription())
classModifier.update(classroom)
val createAnnouncement = classroomAnnouncementAppender.append(
ClassroomAnnouncement(
descriptions = request.toAnnouncementDescription(),
classroomId = classroom.id,
teacherId = teacher.id
)
)

applicationEventPublisher.publishEvent(ClassroomAnnouncementCreateEvent(createAnnouncement))

val studentIds = classroom.getStudentIds()
val students = childReader.findAllByIds(studentIds)
Expand Down Expand Up @@ -65,7 +76,7 @@ class ClassroomService(
val announcement = classroomAnnouncementReader.findById(classroomAnnouncementId)
return GetClassroomAnnouncementDetail.Response(
teacherName,
announcement.getWriteDate(),
announcement.writeDate,
announcement.descriptions
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GetClassroomAnnouncements {

fun toAnnouncementInfo(classroomAnnouncements: List<ClassroomAnnouncement>) : List<AnnouncementInfo> {
return classroomAnnouncements.map {
AnnouncementInfo(it.descriptions,it.getWriteDate())
AnnouncementInfo(it.descriptions,it.writeDate)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.asap.asapbackend.domain.classroom.domain.model

import com.asap.asapbackend.domain.child.domain.model.Child
import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import com.asap.asapbackend.domain.classroom.domain.vo.Grade
import com.asap.asapbackend.domain.classroom.domain.vo.Year
import com.asap.asapbackend.domain.school.domain.model.School
Expand Down Expand Up @@ -40,9 +39,6 @@ class Classroom(
@OneToMany(mappedBy = "classroom", cascade = [CascadeType.ALL])
val teacherClassroomSet: MutableSet<TeacherClassroom> = mutableSetOf()

@OneToMany(mappedBy = "classroom", cascade = [CascadeType.ALL])
val classroomAnnouncementList: MutableList<ClassroomAnnouncement> = mutableListOf()

fun addChild(child: Child) {
childClassroomSet.add(ChildClassroom(child, this))
}
Expand All @@ -56,11 +52,6 @@ class Classroom(
return this.grade == classroom.grade && this.className == classroom.className && this.school.id == classroom.school.id
}


fun addAnnouncement(teacher: Teacher, descriptions: List<AnnouncementDescription>) {
classroomAnnouncementList.add(ClassroomAnnouncement(descriptions, this, teacher))
}

@BatchSize(size = 100)
fun getStudentIds(): Set<Long>{
return childClassroomSet.map { it.student.id }.toSet()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package com.asap.asapbackend.domain.classroom.domain.model

import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import com.asap.asapbackend.domain.teacher.domain.model.Teacher
import com.asap.asapbackend.global.domain.BaseDateEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.ManyToOne
import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes
import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.math.min

@Entity
class ClassroomAnnouncement(
id: Long = 0,
descriptions: List<AnnouncementDescription>,
classroom: Classroom,
teacher: Teacher
) : BaseDateEntity() {
classroomId: Long,
teacherId: Long,
createdAt: LocalDateTime = LocalDateTime.now(),
updatedAt: LocalDateTime = LocalDateTime.now()
) {
val id: Long = id


@Column(
nullable = false,
columnDefinition = "json"
)
@JdbcTypeCode(SqlTypes.JSON)
val descriptions: List<AnnouncementDescription> = descriptions

@ManyToOne(fetch = FetchType.LAZY)
val classroom: Classroom = classroom
val classroomId: Long = classroomId

val teacherId : Long = teacherId

@ManyToOne(fetch = FetchType.LAZY)
val teacher: Teacher = teacher
val createdAt: LocalDateTime = createdAt
val updatedAt: LocalDateTime = updatedAt

fun getWriteDate() = this.createdAt.toLocalDate()
val writeDate: LocalDate = this.createdAt.toLocalDate()

fun getSubListFromDescription(fromIndex: Int, toIndex: Int) : List<AnnouncementDescription>{
return this.descriptions.subList(fromIndex, min(toIndex, this.descriptions.size))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.asap.asapbackend.domain.classroom.domain.repository

import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement
import org.springframework.data.jpa.repository.JpaRepository

interface ClassroomAnnouncementRepository: JpaRepository<ClassroomAnnouncement,Long> {
interface ClassroomAnnouncementRepository{
fun findTopByClassroomIdOrderByCreatedAtDesc(classroomId : Long) : ClassroomAnnouncement?

fun findAllByClassroomIdOrderByCreatedAtDesc(classroomId : Long) : List<ClassroomAnnouncement>

fun findByIdOrNull(classroomAnnouncementId : Long) : ClassroomAnnouncement?

fun save(classroomAnnouncement : ClassroomAnnouncement) : ClassroomAnnouncement
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.asap.asapbackend.domain.classroom.domain.service

import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement
import com.asap.asapbackend.domain.classroom.domain.repository.ClassroomAnnouncementRepository
import org.springframework.stereotype.Service

@Service
class ClassroomAnnouncementAppender(
private val classroomAnnouncementRepository: ClassroomAnnouncementRepository
) {

fun append(classroomAnnouncement: ClassroomAnnouncement): ClassroomAnnouncement {
return classroomAnnouncementRepository.save(classroomAnnouncement)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.asap.asapbackend.domain.classroom.event

import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement

data class ClassroomAnnouncementCreateEvent(
val classroomAnnouncement: ClassroomAnnouncement
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.asap.asapbackend.infrastructure.jpa.classroom

import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement
import com.asap.asapbackend.domain.classroom.domain.repository.ClassroomAnnouncementRepository
import com.asap.asapbackend.global.util.LanguageExtractor
import com.asap.asapbackend.infrastructure.jpa.MultiLanguageId
import com.asap.asapbackend.infrastructure.jpa.classroom.repository.ClassroomAnnouncementJpaRepository
import com.asap.asapbackend.infrastructure.jpa.classroom.repository.TranslatedClassroomAnnouncementJpaRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Repository

@Repository
class ClassroomAnnouncementRepositoryImpl(
private val classroomAnnouncementJpaRepository: ClassroomAnnouncementJpaRepository,
private val languageExtractor: LanguageExtractor,
private val translatedClassroomAnnouncementJpaRepository: TranslatedClassroomAnnouncementJpaRepository
) : ClassroomAnnouncementRepository {
override fun findTopByClassroomIdOrderByCreatedAtDesc(classroomId: Long): ClassroomAnnouncement? {
return classroomAnnouncementJpaRepository.findTopByClassroomIdOrderByCreatedAtDesc(classroomId)?.let {
translatedClassroomAnnouncementJpaRepository.findByIdOrNull(
MultiLanguageId(
it.id,
languageExtractor.getRequestLanguage()
)
)?.let { translated ->
it.updateDescriptions(translated.descriptions)
}
ClassroomMapper.toClassroomAnnouncementModel(it)
}
}

override fun findAllByClassroomIdOrderByCreatedAtDesc(classroomId: Long): List<ClassroomAnnouncement> {
return classroomAnnouncementJpaRepository.findAllByClassroomIdOrderByCreatedAtDesc(classroomId).map {
translatedClassroomAnnouncementJpaRepository.findByIdOrNull(
MultiLanguageId(
it.id,
languageExtractor.getRequestLanguage()
)
)?.let { translated ->
it.updateDescriptions(translated.descriptions)
}
ClassroomMapper.toClassroomAnnouncementModel(it)
}
}

override fun findByIdOrNull(classroomAnnouncementId: Long): ClassroomAnnouncement? {
return classroomAnnouncementJpaRepository.findByIdOrNull(classroomAnnouncementId)?.let {
translatedClassroomAnnouncementJpaRepository.findByIdOrNull(
MultiLanguageId(
it.id,
languageExtractor.getRequestLanguage()
)
)?.let { translated ->
it.updateDescriptions(translated.descriptions)
}
ClassroomMapper.toClassroomAnnouncementModel(it)
}
}

override fun save(classroomAnnouncement: ClassroomAnnouncement): ClassroomAnnouncement {
val entity = ClassroomMapper.toClassroomAnnouncementEntity(classroomAnnouncement)
val savedEntity = classroomAnnouncementJpaRepository.save(entity)
return ClassroomMapper.toClassroomAnnouncementModel(savedEntity)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.asap.asapbackend.infrastructure.jpa.classroom

import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement
import com.asap.asapbackend.infrastructure.jpa.classroom.entity.ClassroomAnnouncementEntity

object ClassroomMapper {

fun toClassroomAnnouncementEntity(classroomAnnouncement: ClassroomAnnouncement): ClassroomAnnouncementEntity {
return ClassroomAnnouncementEntity(
id = classroomAnnouncement.id,
descriptions = classroomAnnouncement.descriptions,
classroomId = classroomAnnouncement.classroomId,
teacherId = classroomAnnouncement.teacherId,
createdAt = classroomAnnouncement.createdAt,
updatedAt = classroomAnnouncement.updatedAt
)
}

fun toClassroomAnnouncementModel(classroomAnnouncementEntity: ClassroomAnnouncementEntity): ClassroomAnnouncement {
return ClassroomAnnouncement(
id = classroomAnnouncementEntity.id,
descriptions = classroomAnnouncementEntity.descriptions,
classroomId = classroomAnnouncementEntity.classroomId,
teacherId = classroomAnnouncementEntity.teacherId,
createdAt = classroomAnnouncementEntity.createdAt,
updatedAt = classroomAnnouncementEntity.updatedAt
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.asap.asapbackend.infrastructure.jpa.classroom.entity

import com.asap.asapbackend.domain.classroom.domain.model.Classroom
import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import jakarta.persistence.*
import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes
import java.time.LocalDateTime

@Entity
@Table(
name = "classroom_announcement",
indexes = [
Index(name = "idx_classroom_announcement_classroom_id", columnList = "classroom_id"),
Index(name = "idx_classroom_announcement_teacher_id", columnList = "teacher_id")
]
)
class ClassroomAnnouncementEntity(
id: Long,
descriptions: List<AnnouncementDescription>,
classroomId: Long,
teacherId: Long,
createdAt: LocalDateTime,
updatedAt: LocalDateTime
) {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = id

@Column(
nullable = false,
columnDefinition = "json"
)
@JdbcTypeCode(SqlTypes.JSON)
var descriptions: List<AnnouncementDescription> = descriptions

@Column(
nullable = false,
name = "classroom_id"
)
val classroomId: Long = classroomId

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "classroom_id", insertable = false, updatable = false)
lateinit var classroom: Classroom

@Column(
nullable = false,
name = "teacher_id"
)
val teacherId: Long = teacherId

val createdAt: LocalDateTime = createdAt
val updatedAt: LocalDateTime = updatedAt

fun updateDescriptions(descriptions: List<AnnouncementDescription>){
this.descriptions = descriptions
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.asap.asapbackend.infrastructure.jpa.classroom.entity

import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import com.asap.asapbackend.global.vo.Language
import com.asap.asapbackend.infrastructure.jpa.MultiLanguageId
import jakarta.persistence.Column
import jakarta.persistence.EmbeddedId
import jakarta.persistence.Entity
import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes

@Entity
class TranslatedClassroomAnnouncementEntity(
classroomAnnouncementId: Long,
language: Language,
descriptions: List<AnnouncementDescription>
) {

@EmbeddedId
val id: MultiLanguageId = MultiLanguageId(classroomAnnouncementId, language)

@Column(
nullable = false,
columnDefinition = "json"
)
@JdbcTypeCode(SqlTypes.JSON)
val descriptions: List<AnnouncementDescription> = descriptions
}
Loading

0 comments on commit 79fca4c

Please sign in to comment.