Skip to content

Commit

Permalink
Merge pull request #76 from Kusitms-29th-ASAP/feature/#74-get-announc…
Browse files Browse the repository at this point in the history
…ement-detail

알림장 상세보기
  • Loading branch information
sominyun authored May 18, 2024
2 parents b9becbc + c1b2d06 commit 1f966e0
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 136 deletions.
9 changes: 6 additions & 3 deletions src/docs/asciidoc/Classroom.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

[[Add-Announcement]]
=== 알림장 추가
operation::ClassroomControllerTest/addAnnouncement[snippets='http-request,request-fields,http-response']
operation::ClassroomControllerTest/addClassroomAnnouncement[snippets='http-request,request-fields,http-response']

=== 오늘의 알림장 불러오기
operation::ClassroomControllerTest/getTodayAnnouncement[]
operation::ClassroomControllerTest/getTodayClassroomAnnouncement[]

=== 알림장 전체 불러오기
operation::ClassroomControllerTest/getAnnouncements[]
operation::ClassroomControllerTest/getClassroomAnnouncements[]

== 알림장 상세 보기
operation::ClassroomControllerTest/getClassroomAnnouncementDetail[]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.asap.asapbackend.batch.announcement
import com.asap.asapbackend.domain.announcement.domain.model.EducationOfficeAnnouncement
import com.asap.asapbackend.domain.announcement.domain.model.SchoolAnnouncement
import com.asap.asapbackend.domain.announcement.domain.service.AnnouncementAppender
import com.asap.asapbackend.domain.announcement.domain.service.AnnouncementReader
import com.asap.asapbackend.domain.announcement.domain.service.SchoolAnnouncementReader
import com.asap.asapbackend.global.util.ImageToTextConverter
import com.asap.asapbackend.global.util.TextSummaryHelper
import com.asap.asapbackend.global.util.TransactionUtils
Expand All @@ -20,7 +20,7 @@ class AnnouncementScheduler(
private val imageToTextConverter: ImageToTextConverter,
private val textSummaryHelper: TextSummaryHelper,
private val announcementAppender: AnnouncementAppender,
private val announcementReader: AnnouncementReader
private val schoolAnnouncementReader: SchoolAnnouncementReader
) {
// 매 평일 9시부터 18시까지 1시간마다 실행
@Scheduled(cron = "0 0 9-18 * * MON-FRI")
Expand Down Expand Up @@ -59,7 +59,7 @@ class AnnouncementScheduler(
@Scheduled(cron = "0 0 9-18 * * MON-FRI")
fun addEducationOfficeAnnouncement() {
val batchSize = 100
var startIdx = announcementReader.getLastOfficeEducationAnnouncementId()
var startIdx = schoolAnnouncementReader.getLastOfficeEducationAnnouncementId()
do {
val announcementDataContainer =
educationOfficeAnnouncementInfoProvider.retrieveAnnouncementInfo(batchSize, startIdx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.asap.asapbackend.domain.announcement.domain.repository.SchoolAnnounce
import org.springframework.stereotype.Service

@Service
class AnnouncementReader(
class SchoolAnnouncementReader(
private val schoolAnnouncementRepository: SchoolAnnouncementRepository,
private val educationOfficeAnnouncementRepository: EducationOfficeAnnouncementRepository
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.asap.asapbackend.domain.classroom.application

import com.asap.asapbackend.domain.child.domain.service.ChildReader
import com.asap.asapbackend.domain.classroom.application.dto.CreateAnnouncement
import com.asap.asapbackend.domain.classroom.application.dto.GetAnnouncements
import com.asap.asapbackend.domain.classroom.application.dto.GetTodayAnnouncement
import com.asap.asapbackend.domain.classroom.domain.service.ClassAnnouncementReader
import com.asap.asapbackend.domain.classroom.application.dto.CreateClassroomAnnouncement
import com.asap.asapbackend.domain.classroom.application.dto.GetClassroomAnnouncementDetail
import com.asap.asapbackend.domain.classroom.application.dto.GetClassroomAnnouncements
import com.asap.asapbackend.domain.classroom.application.dto.GetTodayClassroomAnnouncement
import com.asap.asapbackend.domain.classroom.domain.service.ClassModifier
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomAnnouncementReader
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomReader
import com.asap.asapbackend.domain.teacher.domain.service.TeacherReader
import com.asap.asapbackend.domain.todo.domain.model.Todo
import com.asap.asapbackend.domain.todo.domain.service.TodoAppender
import com.asap.asapbackend.global.security.getCurrentUserId
import com.asap.asapbackend.global.security.getTeacherId
Expand All @@ -23,11 +23,11 @@ class ClassroomService(
private val teacherReader: TeacherReader,
private val childReader: ChildReader,
private val todoAppender: TodoAppender,
private val classAnnouncementReader: ClassAnnouncementReader
private val classroomAnnouncementReader: ClassroomAnnouncementReader
) {

@Transactional
fun createAnnouncement(request: CreateAnnouncement.Request) {
fun createClassroomAnnouncement(request: CreateClassroomAnnouncement.Request) {
val teacherId = getTeacherId()
val classroom = classroomReader.findByTeacher(teacherId)
val teacher = teacherReader.findById(teacherId)
Expand All @@ -37,38 +37,35 @@ class ClassroomService(

val studentIds = classroom.getStudentIds()
val students = childReader.findAllByIds(studentIds)
val todos = request.announcementDetails.filter {
it.isLinkedWithTodo
}.flatMap { // 30*4 = 120
students.map { student ->
Todo(
child = student,
description = it.description,
type = it.todoType,
deadline = it.deadline,
isAssigned = true
)
}
}.toSet()
val todos = request.toTodo(students)
todoAppender.appendAllBatch(todos)
}

fun getTodayAnnouncement(): GetTodayAnnouncement.Response {
fun getTodayClassroomAnnouncement(): GetTodayClassroomAnnouncement.Response {
val userId = getCurrentUserId()
val childId = childReader.findPrimaryChild(userId).id
val classroomId = classroomReader.findByStudent(childId).id
val descriptions =
classAnnouncementReader.getRecentAnnouncementByClassroomIdOrNull(classroomId)?.descriptions ?: emptyList()
return GetTodayAnnouncement.Response(descriptions)
return GetTodayClassroomAnnouncement.convertClassAnnouncementToResponse{
classroomAnnouncementReader.findRecentClassroomAnnouncementByClassroomIdOrNull(classroomId)
}
}

fun getAnnouncements(): GetAnnouncements.Response {
fun getClassroomAnnouncements(): GetClassroomAnnouncements.Response {
val userId = getCurrentUserId()
val childId = childReader.findPrimaryChild(userId).id
val classroomId = classroomReader.findByStudent(childId).id
val teacher = teacherReader.findByClassroomId(classroomId).name
val announcementDataList = classAnnouncementReader.getAllByClassroomId(classroomId)
val announcements = GetAnnouncements().toAnnouncementInfo(announcementDataList)
return GetAnnouncements.Response(teacher, announcements)
val announcementDataList = classroomAnnouncementReader.findAllByClassroomId(classroomId)
val announcements = GetClassroomAnnouncements().toAnnouncementInfo(announcementDataList)
return GetClassroomAnnouncements.Response(teacher, announcements)
}

fun getClassroomAnnouncementDetail(classroomAnnouncementId: Long): GetClassroomAnnouncementDetail.Response {
val userId = getCurrentUserId()
val childId = childReader.findPrimaryChild(userId).id
val classroomId = classroomReader.findByStudent(childId).id
val teacherName = teacherReader.findByClassroomId(classroomId).name
val announcement = classroomAnnouncementReader.findById(classroomAnnouncementId)
return GetClassroomAnnouncementDetail.Response(teacherName, announcement.getWriteDate(), announcement.descriptions)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.asap.asapbackend.domain.classroom.application.dto

import com.asap.asapbackend.domain.child.domain.model.Child
import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import com.asap.asapbackend.domain.todo.domain.model.Todo
import com.asap.asapbackend.domain.todo.domain.vo.TodoType
import com.asap.asapbackend.global.exception.DefaultException
import java.time.LocalDate

class CreateAnnouncement {
class CreateClassroomAnnouncement {

data class Request(
val announcementDetails: List<AnnouncementDetails>
){
) {
init {
if(announcementDetails.isEmpty()){
if (announcementDetails.isEmpty()) {
throw DefaultException.IllegalPropertyException()
}
}
Expand All @@ -21,6 +23,22 @@ class CreateAnnouncement {
it.description
)
}

fun toTodo(students: Set<Child>): Set<Todo> {
return this.announcementDetails.filter {
it.isLinkedWithTodo
}.flatMap { // 30*4 = 120
students.map { student ->
Todo(
child = student,
description = it.description,
type = it.todoType,
deadline = it.deadline,
isAssigned = true
)
}
}.toSet()
}
}

data class AnnouncementDetails(
Expand All @@ -30,10 +48,10 @@ class CreateAnnouncement {
val deadline: LocalDate
) {
init {
if(todoType == TodoType.NONE && isLinkedWithTodo) {
if (todoType == TodoType.NONE && isLinkedWithTodo) {
throw DefaultException.IllegalPropertyException()
}
if(description.isBlank()){
if (description.isBlank()) {
throw DefaultException.IllegalPropertyException()
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.asap.asapbackend.domain.classroom.application.dto

import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import java.time.LocalDate

class GetTodayAnnouncement {
class GetClassroomAnnouncementDetail {
data class Response(
val teacherName: String,
val writeDate : LocalDate,
val descriptions: List<AnnouncementDescription>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.asap.asapbackend.domain.classroom.application.dto


import com.asap.asapbackend.domain.classroom.domain.model.ClassroomAnnouncement
import com.asap.asapbackend.domain.classroom.domain.vo.AnnouncementDescription
import java.time.LocalDate

class GetClassroomAnnouncements {
data class Response(
val teacherName: String,
val announcements : List<AnnouncementInfo>
)

data class AnnouncementInfo(
val descriptions: List<AnnouncementDescription>,
val writeDate : LocalDate
)

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

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

class GetTodayClassroomAnnouncement {
data class Response(
val announcementId : Long?,
val descriptions: List<AnnouncementDescription>
)

companion object {
fun convertClassAnnouncementToResponse(classroomAnnouncementQuery: () -> ClassroomAnnouncement?): Response{
val announcement = classroomAnnouncementQuery()
val descriptions = announcement?.getSubListFromDescription(0,3) ?: emptyList()
return Response(announcement?.id, descriptions)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.asap.asapbackend.domain.classroom.domain.exception

import com.asap.asapbackend.global.exception.BusinessException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class ClassroomAnnouncementException (
message: String,
errorCode: Int,
httpStatusCode: HttpStatusCode,
): BusinessException(DEFAULT_CODE_PREFIX, errorCode, httpStatusCode, message) {

class ClassroomAnnouncementNotFound(message: String = "알림장을을 찾을 수 없습니다.") : ClassroomAnnouncementException(message, 1, HttpStatus.NOT_FOUND)

companion object{
const val DEFAULT_CODE_PREFIX = "ANNOUNCEMENT"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ open class ClassroomException(

class ClassroomNotFoundException(message: String = "강의실을 찾을 수 없습니다.") : ClassroomException(message, 1, HttpStatus.NOT_FOUND)




companion object{
const val DEFAULT_CODE_PREFIX = "CLASSROOM"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.asap.asapbackend.domain.teacher.domain.model.Teacher
import com.asap.asapbackend.global.domain.BaseDateEntity
import jakarta.persistence.*
import org.hibernate.annotations.BatchSize
import java.time.LocalDate
import java.util.*

@Entity
Expand Down Expand Up @@ -43,7 +42,7 @@ class Classroom(
val teacherClassroomSet: MutableSet<TeacherClassroom> = mutableSetOf()

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

fun addChild(child: Child) {
childClassroomSet.add(ChildClassroom(child, this))
Expand All @@ -60,7 +59,7 @@ class Classroom(


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

@BatchSize(size = 100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import jakarta.persistence.FetchType
import jakarta.persistence.ManyToOne
import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes
import kotlin.math.min

@Entity
class ClassAnnouncement(
class ClassroomAnnouncement(
descriptions: List<AnnouncementDescription>,
classroom: Classroom,
teacher: Teacher
Expand All @@ -32,4 +33,8 @@ class ClassAnnouncement(
val teacher: Teacher = teacher

fun getWriteDate() = this.createdAt.toLocalDate()

fun getSubListFromDescription(fromIndex: Int, toIndex: Int) : List<AnnouncementDescription>{
return this.descriptions.subList(fromIndex, min(toIndex, this.descriptions.size))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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> {
fun findTopByClassroomIdOrderByCreatedAtDesc(classroomId : Long) : ClassroomAnnouncement?

fun findAllByClassroomId(classroomId : Long) : List<ClassroomAnnouncement>
}
Loading

0 comments on commit 1f966e0

Please sign in to comment.