Skip to content

Commit

Permalink
Merge pull request #127 from DSM-PICK/add-Attendance
Browse files Browse the repository at this point in the history
🔀 :: (PICK-52)Add attendance
  • Loading branch information
rudeh2926 authored Apr 7, 2024
2 parents 240fc00 + c2166e8 commit 420227d
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class SaveAfterSchoolStudentService(
val user = findByStudentNumPort.findByStudentNum(grade, classNum, num)
AfterSchoolStudent(
id = null,
userId = user!!.id!!,
userId = user!!.id,
grade = grade,
classNum = classNum,
num = num,
name = user!!.name,
name = user.name,
status1 = Status.ATTENDANCE,
status2 = Status.ATTENDANCE,
status3 = Status.ATTENDANCE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dsm.pick2024.domain.attendance.domain

import dsm.pick2024.domain.afterschool.enums.Status
import java.util.*

data class Attendance(
val id: UUID? = null,
val userId: UUID,
val grade: Int,
val classNum: Int,
val num: Int,
val name: String,
val club: String,
val period6: Status,
val period7: Status,
val period8: Status,
val period9: Status,
val period10: Status
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dsm.pick2024.domain.attendance.entity

import dsm.pick2024.domain.afterschool.enums.Status
import dsm.pick2024.global.base.BaseUUIDEntity
import java.util.UUID
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated

@Entity(name = "tbl_attendance")
class AttendanceJpaEntity(
id: UUID?,
@Column(columnDefinition = "BINARY(16)")
val userId: UUID,
@Column(nullable = false)
val name: String,
@Column(nullable = false)
val grade: Int,
@Column(name = "class_num", nullable = false)
val classNum: Int,
@Column(nullable = false)
val num: Int,
@Column(name = "club")
val club: String? = null,
@Enumerated(value = EnumType.STRING)
val period6: Status,
@Enumerated(value = EnumType.STRING)
val period7: Status,
@Enumerated(value = EnumType.STRING)
val period8: Status,
@Enumerated(value = EnumType.STRING)
val period9: Status,
@Enumerated(value = EnumType.STRING)
val period10: Status
) : BaseUUIDEntity(id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dsm.pick2024.domain.attendance.mapper

import dsm.pick2024.domain.attendance.domain.Attendance
import dsm.pick2024.domain.attendance.entity.AttendanceJpaEntity
import dsm.pick2024.global.base.GenericMapper
import org.springframework.stereotype.Component

@Component
class AttendanceMapper : GenericMapper<AttendanceJpaEntity, Attendance> {
override fun toEntity(domain: Attendance) =
domain.run {
AttendanceJpaEntity(
id = id,
userId = userId,
grade = grade,
classNum = classNum,
num = num,
name = name,
club = club,
period6 = period6,
period7 = period7,
period8 = period8,
period9 = period9,
period10 = period10
)
}

override fun toDomain(entity: AttendanceJpaEntity) =
entity.run {
Attendance(
id = id,
userId = userId,
grade = grade,
classNum = classNum,
num = num,
name = name,
club = club!!,
period6 = period6,
period7 = period7,
period8 = period8,
period9 = period9,
period10 = period10
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dsm.pick2024.domain.attendance.persistence

import com.querydsl.jpa.impl.JPAQueryFactory
import dsm.pick2024.domain.attendance.domain.Attendance
import dsm.pick2024.domain.attendance.mapper.AttendanceMapper
import dsm.pick2024.domain.attendance.persistence.repository.AttendanceRepository
import dsm.pick2024.domain.attendance.port.out.AttendancePort
import org.springframework.stereotype.Component
import java.util.*

@Component
class AttendancePersistenceAdapter(
private val attendanceJpaRepository: AttendanceRepository,
private val attendanceMapper: AttendanceMapper,
private val jpaQueryFactory: JPAQueryFactory
) : AttendancePort {
override fun saveAll(attendance: MutableList<Attendance>) {
val entities = attendance.map { attendanceMapper.toEntity(it) }
attendanceJpaRepository.saveAll(entities)
}

override fun findByUserId(userId: UUID) =
attendanceJpaRepository.findByUserId(userId).let { attendanceMapper.toDomain(it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dsm.pick2024.domain.attendance.persistence.repository

import dsm.pick2024.domain.attendance.entity.AttendanceJpaEntity
import org.springframework.data.repository.Repository
import java.util.UUID

interface AttendanceRepository : Repository<AttendanceJpaEntity, UUID> {
fun saveAll(entity: Iterable<AttendanceJpaEntity>)

fun findByUserId(userId: UUID): AttendanceJpaEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dsm.pick2024.domain.attendance.port.`in`

import dsm.pick2024.domain.attendance.presentation.dto.request.ChangeAttendanceRequest

interface ChangeAttendanceUseCase {
fun changeAttendance(request: List<ChangeAttendanceRequest>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dsm.pick2024.domain.attendance.port.`in`

interface SaveAllAttendanceUseCase {
fun saveAll(key: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dsm.pick2024.domain.attendance.port.out

interface AttendancePort :
SaveAll,
FindByUserIdPort
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dsm.pick2024.domain.attendance.port.out

import dsm.pick2024.domain.attendance.domain.Attendance
import java.util.UUID

interface FindByUserIdPort {
fun findByUserId(userId: UUID): Attendance?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dsm.pick2024.domain.attendance.port.out

import dsm.pick2024.domain.attendance.domain.Attendance

interface SaveAll {
fun saveAll(attendance: MutableList<Attendance>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dsm.pick2024.domain.attendance.presentation

import dsm.pick2024.domain.attendance.port.`in`.ChangeAttendanceUseCase
import dsm.pick2024.domain.attendance.port.`in`.SaveAllAttendanceUseCase
import dsm.pick2024.domain.attendance.presentation.dto.request.ChangeAttendanceRequest
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@Tag(name = "attendance API")
@RestController
@RequestMapping("/attendance")
class AttendanceController(
private val saveAllAttendanceUseCase: SaveAllAttendanceUseCase,
private val changeAttendanceUseCase: ChangeAttendanceUseCase
) {

@Operation(summary = "데이터 저장 api")
@PostMapping("/all")
fun all(@RequestParam(name = "key") key: String) =
saveAllAttendanceUseCase.saveAll(key)

@Operation(summary = "자습 or 방과후 상태관리")
@PatchMapping("/modify")
fun changeAttendance(changeAttendanceRequest: List<ChangeAttendanceRequest>) =
changeAttendanceUseCase.changeAttendance(changeAttendanceRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dsm.pick2024.domain.attendance.presentation.dto.request

import dsm.pick2024.domain.afterschool.enums.Status
import java.util.UUID

data class ChangeAttendanceRequest(
val userId: UUID,
val statusList: List<Status>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dsm.pick2024.domain.attendance.service

import dsm.pick2024.domain.attendance.domain.Attendance
import dsm.pick2024.domain.attendance.port.`in`.ChangeAttendanceUseCase
import dsm.pick2024.domain.attendance.port.out.FindByUserIdPort
import dsm.pick2024.domain.attendance.port.out.SaveAll
import dsm.pick2024.domain.attendance.presentation.dto.request.ChangeAttendanceRequest
import dsm.pick2024.domain.user.exception.UserNotFoundException
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class ChangeAttendanceService(
private val saveAll: SaveAll,
private val findByUserIdPort: FindByUserIdPort
) : ChangeAttendanceUseCase {

@Transactional
override fun changeAttendance(request: List<ChangeAttendanceRequest>) {
val update = mutableListOf<Attendance>()

request.map {
it ->
val attendance = findByUserIdPort.findByUserId(it.userId)
?: throw UserNotFoundException
val list = it.statusList
val add = attendance.copy(
period6 = list.getOrElse(0) { attendance.period6 },
period7 = list.getOrElse(1) { attendance.period7 },
period8 = list.getOrElse(2) { attendance.period8 },
period9 = list.getOrElse(1) { attendance.period9 },
period10 = list.getOrElse(2) { attendance.period10 }
)
update.add(add)
}
saveAll.saveAll(update)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dsm.pick2024.domain.attendance.service

import dsm.pick2024.domain.afterschool.enums.Status
import dsm.pick2024.domain.attendance.domain.Attendance
import dsm.pick2024.domain.attendance.port.`in`.SaveAllAttendanceUseCase
import dsm.pick2024.domain.attendance.port.out.SaveAll
import dsm.pick2024.infrastructure.feign.client.XquareFeignClient
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class SaveAllAttendanceSaveAllUserService(
private val saveAll: SaveAll,
private val xquareFeignClient: XquareFeignClient
) : SaveAllAttendanceUseCase {

@Transactional
override fun saveAll(key: String) {
val xquare = xquareFeignClient.userAll(key)
val entity = xquare.map {
it ->
Attendance(
userId = it.id,
name = it.name,
grade = it.grade,
classNum = it.classNum,
num = it.num,
club = it.club,
period6 = Status.ATTENDANCE,
period7 = Status.ATTENDANCE,
period8 = Status.ATTENDANCE,
period9 = Status.ATTENDANCE,
period10 = Status.ATTENDANCE
)
}.toMutableList()
saveAll.saveAll(entity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ data class XquareResponse(
val classNum: Int,
val num: Int,
val userRole: String,
val clubName: String,
val birthDay: LocalDate
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class XquareUserAllResponse(
val name: String,
val grade: Int,
val classNum: Int,
val num: Int
val num: Int,
val club: String
)

0 comments on commit 420227d

Please sign in to comment.