Skip to content

Commit

Permalink
Merge pull request #167 from sopt-makers/sohyeon_#166
Browse files Browse the repository at this point in the history
[REFACT] 출석 기능 리팩토링
  • Loading branch information
thguss authored Oct 17, 2023
2 parents 79958e1 + 1a80545 commit 4c69328
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.sopt.makers.operation.common.ResponseMessage.*;

import org.sopt.makers.operation.common.ApiResponse;
import org.sopt.makers.operation.dto.attendance.AttendUpdateRequestDTO;
import org.sopt.makers.operation.dto.attendance.SubAttendanceUpdateRequestDTO;
import org.sopt.makers.operation.entity.Part;
import org.sopt.makers.operation.service.AttendanceService;
import org.springframework.data.domain.Pageable;
Expand All @@ -28,19 +28,19 @@ public class AttendanceController {

@ApiOperation(value = "출석 상태 변경")
@PatchMapping
public ResponseEntity<ApiResponse> updateAttendanceStatus(@RequestBody AttendUpdateRequestDTO requestDTO) {
val response = attendanceService.updateAttendanceStatus(requestDTO);
public ResponseEntity<ApiResponse> updateSubAttendance(@RequestBody SubAttendanceUpdateRequestDTO requestDTO) {
val response = attendanceService.updateSubAttendance(requestDTO);
return ResponseEntity.ok(ApiResponse.success(SUCCESS_UPDATE_ATTENDANCE_STATUS.getMessage(), response));
}

@ApiOperation(value = "유저별 출석 정보 조회")
@ApiOperation(value = "회원별 출석 정보 조회")
@GetMapping("/{memberId}")
public ResponseEntity<ApiResponse> findAttendancesByMember(@PathVariable Long memberId) {
val response = attendanceService.findAttendancesByMember(memberId);
return ResponseEntity.ok(ApiResponse.success(SUCCESS_GET_MEMBER_ATTENDANCE.getMessage(), response));
}

@ApiOperation(value = "출석 점수 갱신 성공")
@ApiOperation(value = "출석 점수 갱신")
@PatchMapping("/member/{memberId}")
public ResponseEntity<ApiResponse> updateMemberScore(@PathVariable Long memberId) {
val response = attendanceService.updateMemberScore(memberId);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.sopt.makers.operation.dto.attendance;

import static org.sopt.makers.operation.util.Generation32.*;

import java.util.List;

import org.sopt.makers.operation.entity.Attendance;
Expand Down Expand Up @@ -36,7 +34,7 @@ record LectureVO(
public static LectureVO of(Attendance attendance) {
return new LectureVO(
attendance.getLecture().getName(),
getUpdateScore(attendance.getLecture().getAttribute(), attendance.getStatus()),
attendance.getScore(),
attendance.getStatus().getName(),
attendance.getSubAttendances().stream().map(AttendanceVO::of).toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.sopt.makers.operation.dto.attendance;

import static org.sopt.makers.operation.util.Generation32.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.sopt.makers.operation.entity.Attendance;
Expand All @@ -15,14 +11,15 @@ public record MemberResponseDTO (
Long attendanceId,
MemberVO member,
List<SubAttendanceVO> attendances,
float updatedScore) {
float updatedScore
) {

public static MemberResponseDTO of(Attendance attendance) {
return new MemberResponseDTO(
attendance.getId(),
MemberVO.of(attendance.getMember()),
attendance.getSubAttendances().stream().map(SubAttendanceVO::of).toList(),
getUpdateScore(attendance.getLecture().getAttribute(), attendance.getStatus()));
attendance.getScore());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.makers.operation.dto.attendance;

import org.sopt.makers.operation.entity.AttendanceStatus;

public record SubAttendanceUpdateRequestDTO(
Long subAttendanceId,
AttendanceStatus status
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import org.sopt.makers.operation.entity.AttendanceStatus;
import org.sopt.makers.operation.entity.SubAttendance;

public record AttendUpdateResponseDTO(
public record SubAttendanceUpdateResponseDTO(
Long subAttendanceId,
AttendanceStatus status
) {
public static AttendUpdateResponseDTO of(SubAttendance subAttendance) {
return new AttendUpdateResponseDTO(subAttendance.getId(), subAttendance.getStatus());
public static SubAttendanceUpdateResponseDTO of(SubAttendance subAttendance) {
return new SubAttendanceUpdateResponseDTO(subAttendance.getId(), subAttendance.getStatus());
}
}
51 changes: 47 additions & 4 deletions src/main/java/org/sopt/makers/operation/entity/Attendance.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.sopt.makers.operation.entity;

import static javax.persistence.GenerationType.*;
import static org.sopt.makers.operation.common.ExceptionMessage.*;
import static org.sopt.makers.operation.entity.AttendanceStatus.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityNotFoundException;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
Expand All @@ -19,8 +22,7 @@

import org.sopt.makers.operation.entity.lecture.Lecture;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@NoArgsConstructor
Expand Down Expand Up @@ -51,8 +53,49 @@ public Attendance(Member member, Lecture lecture) {
this.status = AttendanceStatus.ABSENT;
}

public void updateStatus(AttendanceStatus status) {
this.status = status;
public void updateStatus() {
this.status = getStatus();
}

public AttendanceStatus getStatus() {
val first = getSubAttendanceByRound(1);
val second = getSubAttendanceByRound(2);
return switch (this.lecture.getAttribute()) {
case SEMINAR -> second.getStatus().equals(ATTENDANCE)
? first.getStatus().equals(ATTENDANCE) ? ATTENDANCE : TARDY
: ABSENT;
case EVENT -> second.getStatus().equals(ATTENDANCE) ? ATTENDANCE : ABSENT;
case ETC -> second.getStatus().equals(ATTENDANCE) ? PARTICIPATE : NOT_PARTICIPATE;
};
}

public float getScore() {
return switch (this.lecture.getAttribute()) {
case SEMINAR -> {
if (this.status.equals(ABSENT)) {
yield -1f;
} else if (this.status.equals(TARDY)) {
yield -0.5f;
} else {
yield 0f;
}
}
case EVENT -> this.status.equals(ATTENDANCE) ? 0.5f : 0f;
default -> 0f;
};
}

public void updateMemberScore() {
this.member.updateScore(this.getScore());
}

public void revertMemberScore() {
this.member.updateScore((-1) * this.getScore());
}

private SubAttendance getSubAttendanceByRound(int round) {
return this.subAttendances.stream().filter(o -> o.getSubLecture().getRound() == round).findFirst()
.orElseThrow(() -> new EntityNotFoundException(INVALID_SUB_ATTENDANCE.getName()));
}

private void setMember(Member member) {
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/sopt/makers/operation/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sopt.makers.operation.entity;

import static org.sopt.makers.operation.entity.lecture.LectureStatus.*;
import static org.sopt.makers.operation.util.Generation32.*;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -68,12 +67,8 @@ public void updateScore(float score) {

public void updateTotalScore() {
this.score = (float) (2 + this.attendances.stream()
.mapToDouble(attendance -> {
Lecture lecture = attendance.getLecture();
return lecture.getLectureStatus().equals(END)
? getUpdateScore(lecture.getAttribute(), attendance.getStatus())
: 0;
})
.filter(attendance -> attendance.getLecture().getLectureStatus().equals(END))
.mapToDouble(Attendance::getScore)
.sum());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ private void setSubLecture(SubLecture subLecture) {

public void updateStatus(AttendanceStatus status) {
this.status = status;
this.attendance.updateStatus();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sopt.makers.operation.entity.lecture;

import static javax.persistence.GenerationType.*;
import static org.sopt.makers.operation.util.Generation32.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -16,7 +15,6 @@
import javax.persistence.OneToMany;

import org.sopt.makers.operation.entity.Attendance;
import org.sopt.makers.operation.entity.AttendanceStatus;
import org.sopt.makers.operation.entity.BaseEntity;
import org.sopt.makers.operation.entity.Part;
import org.sopt.makers.operation.entity.SubLecture;
Expand Down Expand Up @@ -78,12 +76,6 @@ public void updateStatus(LectureStatus status) {

public void finish() {
this.lectureStatus = LectureStatus.END;
attendances.forEach(this::updateScore);
}

private void updateScore(Attendance attendance) {
Attribute attribute = this.attribute;
AttendanceStatus status = attendance.getStatus();
attendance.getMember().updateScore(getUpdateScore(attribute, status));
attendances.forEach(Attendance::updateMemberScore);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.sopt.makers.operation.repository.attendance;

import java.util.List;
import java.util.Optional;

import org.sopt.makers.operation.entity.Attendance;
import org.sopt.makers.operation.entity.Member;
import org.sopt.makers.operation.entity.Part;
import org.sopt.makers.operation.entity.SubAttendance;
import org.sopt.makers.operation.entity.lecture.Lecture;
import org.springframework.data.domain.Pageable;

public interface AttendanceCustomRepository {
List<Attendance> findAttendanceByMemberId(Long memberId);
List<Attendance> findAttendancesByLecture(Long lectureId, Part part, Pageable pageable);
List<Attendance> findAttendancesByMember(Long memberId);
Optional<Attendance> findAttendanceBySubAttendance(SubAttendance subAttendance);
List<Attendance> findByLecture(Lecture lecture);
List<Attendance> findByLecture(Long lectureId, Part part, Pageable pageable);
List<Attendance> findByMember(Member member);
List<Attendance> findCurrentAttendanceByMember(Long playGroundId);
List<SubAttendance> findSubAttendanceByAttendanceId(Long attendanceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;

import lombok.val;

import org.sopt.makers.operation.config.GenerationConfig;
import org.sopt.makers.operation.entity.Attendance;
import org.sopt.makers.operation.entity.Member;
import org.sopt.makers.operation.entity.Part;
import org.sopt.makers.operation.entity.QSubAttendance;
import org.sopt.makers.operation.entity.SubAttendance;
import org.sopt.makers.operation.entity.lecture.Lecture;
import org.sopt.makers.operation.entity.lecture.LectureStatus;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -52,14 +50,13 @@ public List<Attendance> findAttendanceByMemberId(Long memberId) {
}

@Override
public List<Attendance> findAttendancesByLecture(Long lectureId, Part part, Pageable pageable) {
public List<Attendance> findByLecture(Long lectureId, Part part, Pageable pageable) {
return queryFactory
.select(attendance)
.from(attendance)
.join(attendance.subAttendances, subAttendance).fetchJoin()
.join(subAttendance.subLecture, subLecture).fetchJoin()
.join(attendance.lecture, lecture).fetchJoin()
.join(attendance.member, member).fetchJoin().distinct()
.selectFrom(attendance)
.leftJoin(attendance.subAttendances, subAttendance).fetchJoin().distinct()
.leftJoin(subAttendance.subLecture, subLecture).fetchJoin()
.leftJoin(attendance.lecture, lecture).fetchJoin()
.leftJoin(attendance.member, member).fetchJoin().distinct()
.where(
attendance.lecture.id.eq(lectureId),
partEq(part)
Expand All @@ -71,39 +68,17 @@ public List<Attendance> findAttendancesByLecture(Long lectureId, Part part, Page
}

@Override
public List<Attendance> findAttendancesByMember(Long memberId) {
public List<Attendance> findByMember(Member member) {
return queryFactory
.select(attendance)
.from(attendance)
.selectFrom(attendance)
.leftJoin(attendance.subAttendances, subAttendance).fetchJoin().distinct()
.leftJoin(attendance.lecture, lecture).fetchJoin()
.leftJoin(subAttendance.subLecture, subLecture).fetchJoin()
.where(attendance.member.id.eq(memberId))
.where(attendance.member.eq(member))
.orderBy(lecture.startDate.desc())
.fetch();
}

@Override
public Optional<Attendance> findAttendanceBySubAttendance(SubAttendance subAttendance) {
return queryFactory
.select(attendance)
.from(attendance)
.join(attendance.subAttendances, QSubAttendance.subAttendance).fetchJoin()
.join(QSubAttendance.subAttendance.subLecture, subLecture).fetchJoin()
.where(attendance.subAttendances.contains(subAttendance))
.stream().findFirst();
}

@Override
public List<Attendance> findByLecture(Lecture lecture) {
return queryFactory
.select(attendance)
.from(attendance)
.leftJoin(attendance.member, member).fetchJoin().distinct()
.where(attendance.lecture.eq(lecture))
.fetch();
}

@Override
public List<Attendance> findCurrentAttendanceByMember(Long playGroundId) {
val now = LocalDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.sopt.makers.operation.entity.Part;
import org.sopt.makers.operation.entity.lecture.Lecture;
import java.util.List;
import java.util.Optional;

public interface LectureCustomRepository {
List<Lecture> findLectures(int generation, Part part);
List<Lecture> findLecturesToBeEnd();
Optional<Lecture> find(Long lectureId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static java.util.Objects.*;
import static org.sopt.makers.operation.entity.QAttendance.*;
Expand Down Expand Up @@ -46,6 +47,16 @@ public List<Lecture> findLecturesToBeEnd() {
.fetch();
}

@Override
public Optional<Lecture> find(Long lectureId) {
return queryFactory
.selectFrom(lecture)
.leftJoin(lecture.attendances, attendance).fetchJoin().distinct()
.leftJoin(attendance.member, member).fetchJoin().distinct()
.where(lecture.id.eq(lectureId))
.stream().findFirst();
}

private BooleanExpression partEq(Part part) {
return nonNull(part) ? lecture.part.eq(part) : null;
}
Expand Down
Loading

0 comments on commit 4c69328

Please sign in to comment.