Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 내 출석 조회에 결석 현황 응답값 추가 #33

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface SessionGateway {

fun findAllByGeneration(generation: Int): List<Session>

fun findByGenerationAndWeek(generation: Int, week: Int): Session
fun findByGenerationAndWeek(generation: Int, week: Int): Session?

fun findByStartTimeBetween(startTime: LocalDateTime, endTime: LocalDateTime): Session?
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ data class Attendance(
val generation: Int,
val week: Int,
val member: Member,
val sessionType: SessionType,
val attendanceStatus: AttendanceStatus,
val attendanceTime: LocalDateTime?
) {
Expand Down Expand Up @@ -80,12 +81,14 @@ data class Attendance(
generation: Int,
week: Int,
member: Member,
sessionType: SessionType = SessionType.ONLINE,
attendance: AttendanceStatus = AttendanceStatus.ATTENDANCE_ON_HOLD
) = Attendance(
attendanceId = generateULID(),
generation = generation,
week = week,
member = member,
sessionType = sessionType,
attendanceStatus = attendance,
attendanceTime = null
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.depromeet.makers.domain.model

enum class AttendanceStatus(
val description: String
val description: String,
val point: Double,
) {
ATTENDANCE_ON_HOLD("출석 대기"),
ATTENDANCE("출석"),
ABSENCE("결석"),
TARDY("지각");
ATTENDANCE_ON_HOLD("출석 대기", 0.0),
ATTENDANCE("출석", 0.0),
ABSENCE("결석", 1.0),
TARDY("지각", 0.5);

fun isAttendanceOnHold() = this == ATTENDANCE_ON_HOLD

Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/com/depromeet/makers/domain/model/Session.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.makers.domain.model

import com.depromeet.makers.util.generateULID
import java.time.LocalDateTime

data class Session(
Expand Down Expand Up @@ -35,4 +36,27 @@ data class Session(
place = place,
)
}

companion object {
fun newSession(
generation: Int,
week: Int,
title: String = "세션 제목입니다.",
description: String? = "",
startTime: LocalDateTime = LocalDateTime.now(),
sessionType: SessionType = SessionType.ONLINE,
place: Place = Place.emptyPlace(),
): Session {
return Session(
sessionId = generateULID(),
generation = generation,
week = week,
title = title,
description = description,
startTime = startTime,
sessionType = sessionType,
place = place,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,62 @@ package com.depromeet.makers.domain.usecase

import com.depromeet.makers.domain.gateway.AttendanceGateway
import com.depromeet.makers.domain.gateway.MemberGateway
import com.depromeet.makers.domain.gateway.SessionGateway
import com.depromeet.makers.domain.model.Attendance
import com.depromeet.makers.domain.model.Session

class GetMemberAttendances(
private val attendanceGateway: AttendanceGateway,
private val memberGateway: MemberGateway,
) : UseCase<GetMemberAttendances.GetMemberAttendancesInput, List<Attendance>> {
private val sessionGateway: SessionGateway,
) : UseCase<GetMemberAttendances.GetMemberAttendancesInput, GetMemberAttendances.GetMemberAttendancesOutput> {
data class GetMemberAttendancesInput(
val memberId: String,
val generation: Int,
)

override fun execute(input: GetMemberAttendancesInput): List<Attendance> {
data class GetMemberAttendancesOutput(
val generation: Int,
val offlineAbsenceScore: Double,
val totalAbsenceScore: Double,
val attendances: List<Attendance>,
)

override fun execute(input: GetMemberAttendancesInput): GetMemberAttendancesOutput {
val member = memberGateway.getById(input.memberId)

val attendances = attendanceGateway.findAllByMemberIdAndGeneration(member.memberId, input.generation)
.associate { it.week to it }
.toMutableMap()
// 기본값 없으면 세팅 (추후 걷어낼 로직)
val sessions = (1..16).map {
sessionGateway.findByGenerationAndWeek(input.generation, it) ?: sessionGateway.save(
Session.newSession(
generation = input.generation,
week = it,
)
)
}

(1..16).filter { week -> !attendances.contains(week) }
.forEach { week ->
attendances[week] = Attendance.newAttendance(
val attendances = (1..16).map {
attendanceGateway.save(
Attendance.newAttendance(
member = member,
generation = input.generation,
week = week,
week = it,
sessionType = sessions[it - 1].sessionType,
)
}
)
}

return attendances.values.sortedBy { it.week }
var offlineAbsenceScore = 0.0
var totalAbsenceScore = 0.0
(1..16).forEach { week ->
offlineAbsenceScore += if (attendances[week - 1].attendanceStatus.isAbsence() && sessions[week - 1].isOffline()) 1.0 else 0.0
totalAbsenceScore += attendances[week - 1].attendanceStatus.point
}
return GetMemberAttendancesOutput(
generation = input.generation,
offlineAbsenceScore = offlineAbsenceScore,
totalAbsenceScore = totalAbsenceScore,
attendances = attendances.sortedBy { it.week }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.depromeet.makers.infrastructure.db.entity

import com.depromeet.makers.domain.model.Attendance
import com.depromeet.makers.domain.model.AttendanceStatus
import com.depromeet.makers.domain.model.SessionType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
Expand All @@ -28,6 +29,10 @@ class AttendanceEntity private constructor(
@JoinColumn(name = "member_id", nullable = false)
val member: MemberEntity,

@Enumerated(EnumType.STRING)
@Column(name = "session_type", nullable = false)
val sessionType: SessionType,

@Enumerated(EnumType.STRING)
@Column(name = "attendance_status", nullable = false)
val attendanceStatus: AttendanceStatus,
Expand All @@ -41,6 +46,7 @@ class AttendanceEntity private constructor(
week = week,
member = member.toDomain(),
attendanceStatus = attendanceStatus,
sessionType = sessionType,
attendanceTime = attendanceTime,
)

Expand All @@ -51,6 +57,7 @@ class AttendanceEntity private constructor(
generation = generation,
week = week,
member = MemberEntity.fromDomain(member),
sessionType = sessionType,
attendanceStatus = attendanceStatus,
attendanceTime = attendanceTime
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface JpaSessionRepository : JpaRepository<SessionEntity, String> {

fun findAllByGeneration(generation: Int): List<SessionEntity>

fun findByGenerationAndWeek(generation: Int, week: Int): SessionEntity
fun findByGenerationAndWeek(generation: Int, week: Int): SessionEntity?

fun findByStartTimeBetween(startTime: LocalDateTime, endTime: LocalDateTime): List<SessionEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class SessionGatewayImpl(
.map { it.toDomain() }
}

override fun findByGenerationAndWeek(generation: Int, week: Int): Session {
override fun findByGenerationAndWeek(generation: Int, week: Int): Session? {
return jpaSessionRepository
.findByGenerationAndWeek(generation, week)
.toDomain()
?.toDomain()
}

override fun findByStartTimeBetween(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.depromeet.makers.presentation.restapi.controller
import com.depromeet.makers.domain.usecase.GetMemberAttendances
import com.depromeet.makers.domain.usecase.UpdateAttendance
import com.depromeet.makers.presentation.restapi.dto.request.UpdateAttendanceRequest
import com.depromeet.makers.presentation.restapi.dto.response.AttendanceResponse
import com.depromeet.makers.presentation.restapi.dto.response.MyAttendanceResponse
import com.depromeet.makers.presentation.restapi.dto.response.UpdateAttendanceResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
Expand All @@ -26,14 +26,15 @@ class AttendanceController(
fun getMyAttendance(
authentication: Authentication,
@RequestParam(defaultValue = "15") generation: Int,
): List<AttendanceResponse> {
val attendances = getMemberAttendances.execute(
GetMemberAttendances.GetMemberAttendancesInput(
memberId = authentication.name,
generation = generation,
): MyAttendanceResponse {
return MyAttendanceResponse.fromDomain(
getMemberAttendances.execute(
GetMemberAttendances.GetMemberAttendancesInput(
memberId = authentication.name,
generation = generation,
)
)
)
return attendances.map { AttendanceResponse.fromDomain(it) }
}

@PreAuthorize("hasRole('ORGANIZER')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.depromeet.makers.presentation.restapi.dto.response

import com.depromeet.makers.domain.model.Attendance
import com.depromeet.makers.domain.model.AttendanceStatus
import com.depromeet.makers.domain.model.SessionType
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDateTime

Expand All @@ -19,6 +20,9 @@ data class AttendanceResponse(
@Schema(description = "회원 ID", example = "01HWPNRE5TS9S7VC99WPETE5KE")
val memberId: String,

@Schema(description = "세션 타입", example = "ONLINE")
val sessionType: SessionType,

@Schema(description = "출석 상태", example = "ATTENDANCE")
val attendanceStatus: AttendanceStatus,

Expand All @@ -32,6 +36,7 @@ data class AttendanceResponse(
generation = generation,
week = week,
memberId = member.memberId,
sessionType = sessionType,
attendanceStatus = attendanceStatus,
attendanceTime = attendanceTime,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.depromeet.makers.presentation.restapi.dto.response

import com.depromeet.makers.domain.usecase.GetMemberAttendances
import io.swagger.v3.oas.annotations.media.Schema

data class MyAttendanceResponse(

@Schema(description = "기수", example = "15")
val generation: Int,

@Schema(description = "오프라인 결석 점수", example = "1.0")
val offlineAbsenceScore: Double,

@Schema(description = "총 결석 점수 (지각: 0.5)", example = "2.5")
val totalAbsenceScore: Double,

val attendances: List<AttendanceResponse>
) {
companion object {
fun fromDomain(getMemberAttendancesOutput: GetMemberAttendances.GetMemberAttendancesOutput) = MyAttendanceResponse(
generation = getMemberAttendancesOutput.generation,
offlineAbsenceScore = getMemberAttendancesOutput.offlineAbsenceScore,
totalAbsenceScore = getMemberAttendancesOutput.totalAbsenceScore,
attendances = getMemberAttendancesOutput.attendances.map { AttendanceResponse.fromDomain(it) }
)
}
}