Skip to content

Commit

Permalink
Merge pull request #241 from sopt-makers/yongtaek_#238
Browse files Browse the repository at this point in the history
[REFACTOR] 2차 기능 리팩토링
  • Loading branch information
dragontaek-lee authored Feb 15, 2024
2 parents 6cc1af0 + dca3eda commit 223b4ff
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.security.Principal;

import org.sopt.makers.operation.app.attendance.dto.request.AttendanceRequest;
import org.sopt.makers.operation.app.attendance.dto.request.LectureAttendRequest;
import org.sopt.makers.operation.dto.BaseResponse;
import org.springframework.http.ResponseEntity;

Expand Down Expand Up @@ -36,6 +36,6 @@ public interface AppAttendanceApi {
}
)
ResponseEntity<BaseResponse<?>> attend(
@RequestBody AttendanceRequest request,
@RequestBody LectureAttendRequest request,
@Parameter(hidden = true) @NonNull Principal principal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.security.Principal;

import org.sopt.makers.operation.app.attendance.dto.request.AttendanceRequest;
import org.sopt.makers.operation.app.attendance.dto.request.LectureAttendRequest;
import org.sopt.makers.operation.app.attendance.service.AppAttendanceService;
import org.sopt.makers.operation.common.util.CommonUtils;
import org.sopt.makers.operation.util.ApiResponseUtil;
Expand All @@ -15,7 +15,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;

Expand All @@ -29,7 +28,7 @@ public class AppAttendanceApiController implements AppAttendanceApi {

@Override
@PostMapping("/attend")
public ResponseEntity<BaseResponse<?>> attend(@RequestBody AttendanceRequest request, @NonNull Principal principal) {
public ResponseEntity<BaseResponse<?>> attend(@RequestBody LectureAttendRequest request, Principal principal) {
val memberId = utils.getMemberId(principal);
val response = attendanceService.attend(memberId, request);
return ApiResponseUtil.success(SUCCESS_ATTEND, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.NonNull;

public record AttendanceRequest(
public record LectureAttendRequest(
long subLectureId,
@NonNull String code
) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.sopt.makers.operation.app.attendance.dto.response;

import static lombok.AccessLevel.*;

import lombok.Builder;

import org.sopt.makers.operation.attendance.domain.SubAttendance;

@Builder(access = PRIVATE)
public record LectureAttendResponse(
long subLectureId
) {
public static LectureAttendResponse of(SubAttendance subAttendance) {
return LectureAttendResponse.builder()
.subLectureId(subAttendance.getSubLecture().getId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.sopt.makers.operation.app.attendance.service;

import org.sopt.makers.operation.app.attendance.dto.request.AttendanceRequest;
import org.sopt.makers.operation.app.attendance.dto.response.AttendanceResponse;
import org.sopt.makers.operation.app.attendance.dto.request.LectureAttendRequest;
import org.sopt.makers.operation.app.attendance.dto.response.LectureAttendResponse;

public interface AppAttendanceService {
AttendanceResponse attend(long memberId, AttendanceRequest request);
LectureAttendResponse attend(long memberId, LectureAttendRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import static org.sopt.makers.operation.code.failure.lecture.LectureFailureCode.*;
import static org.sopt.makers.operation.code.failure.subLecture.subLectureFailureCode.*;

import org.sopt.makers.operation.app.attendance.dto.request.AttendanceRequest;
import org.sopt.makers.operation.app.attendance.dto.response.AttendanceResponse;
import org.sopt.makers.operation.app.attendance.dto.request.LectureAttendRequest;
import org.sopt.makers.operation.app.attendance.dto.response.LectureAttendResponse;
import org.sopt.makers.operation.attendance.domain.Attendance;
import org.sopt.makers.operation.attendance.domain.SubAttendance;
import org.sopt.makers.operation.attendance.repository.attendance.AttendanceRepository;
Expand Down Expand Up @@ -39,10 +39,10 @@ public class AppAttendanceServiceImpl implements AppAttendanceService {

@Override
@Transactional
public AttendanceResponse attend(long playgroundId, AttendanceRequest request) {
public LectureAttendResponse attend(long playgroundId, LectureAttendRequest request) {
val subAttendance = getSubAttendance(request.subLectureId(), request.code(), playgroundId);
subAttendance.updateStatus(ATTENDANCE);
return AttendanceResponse.of(subAttendance);
return LectureAttendResponse.of(subAttendance);
}

private SubAttendance getSubAttendance(long subLectureId, String code, long playgroundId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.sopt.makers.operation.app.lecture.dto.response;

import lombok.Builder;
import org.sopt.makers.operation.lecture.domain.SubLecture;

import static lombok.AccessLevel.PRIVATE;

@Builder(access = PRIVATE)
public record LectureCurrentRoundResponse(
Long id,
long id,
int round
) {
public static LectureCurrentRoundResponse of(SubLecture subLecture){
return new LectureCurrentRoundResponse(
subLecture.getId(),
subLecture.getRound()
);
return LectureCurrentRoundResponse.builder()
.id(subLecture.getId())
.round(subLecture.getRound())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static lombok.AccessLevel.*;

import org.sopt.makers.operation.attendance.domain.AttendanceStatus;
import org.sopt.makers.operation.attendance.domain.SubAttendance;
import org.sopt.makers.operation.lecture.domain.Lecture;

import lombok.Builder;

@Builder
@Builder(access = PRIVATE)
public record TodayLectureResponse(
LectureResponseType type,
Long id,
long id,
String location,
String name,
String startDate,
Expand All @@ -23,7 +26,6 @@ public record TodayLectureResponse(
List<LectureGetResponseVO> attendances
) {
public static TodayLectureResponse of(LectureResponseType type, Lecture lecture, String message, List<SubAttendance> attendances) {

return TodayLectureResponse.builder()
.type(type)
.id(lecture.getId())
Expand All @@ -38,6 +40,19 @@ public static TodayLectureResponse of(LectureResponseType type, Lecture lecture,
.build();
}

public static TodayLectureResponse getEmptyResponse() {
return TodayLectureResponse.builder()
.type(LectureResponseType.NO_SESSION)
.id(0L)
.location("")
.name("")
.startDate("")
.endDate("")
.message("")
.attendances(Collections.emptyList())
.build();
}

private static DateTimeFormatter convertFormat() {
return DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TodayLectureResponse getTodayLecture(long memberPlaygroundId) {
checkAttendancesSize(attendances);

if (attendances.isEmpty()) {
return getEmptyResponse();
return TodayLectureResponse.getEmptyResponse();
}

val attendance = getNowAttendance(attendances);
Expand All @@ -59,21 +59,12 @@ public TodayLectureResponse getTodayLecture(long memberPlaygroundId) {
}

val subAttendances = attendance.getSubAttendances();
val subAttendance = lecture.isFirst() ? subAttendances.get(0) : subAttendances.get(1);
return getTodayLectureResponse(subAttendance, responseType, lecture);
}

private TodayLectureResponse getEmptyResponse() {
return TodayLectureResponse.builder()
.type(LectureResponseType.NO_SESSION)
.id(0L)
.location("")
.name("")
.startDate("")
.endDate("")
.message("")
.attendances(Collections.emptyList())
.build();
if (lecture.isFirst()) {
return getTodayFirstLectureResponse(subAttendances.get(0), responseType, lecture);
}

return getTodaySecondLectureResponse(subAttendances, responseType, lecture);
}

private void checkAttendancesSize(List<Attendance> attendances) {
Expand All @@ -82,6 +73,11 @@ private void checkAttendancesSize(List<Attendance> attendances) {
}
}

private boolean checkOnAttendanceAbsence(SubLecture subLecture, SubAttendance subAttendance) {
val isOnAttendanceCheck = LocalDateTime.now().isBefore(subLecture.getStartAt().plusMinutes(10));
return isOnAttendanceCheck && subAttendance.getStatus().equals(ABSENT);
}

private Attendance getNowAttendance(List<Attendance> attendances) {
val index = getAttendanceIndex();
return attendances.get(index);
Expand All @@ -104,16 +100,29 @@ private String getMessage(Attribute attribute) {
};
}

private TodayLectureResponse getTodayLectureResponse(SubAttendance subAttendance, LectureResponseType responseType, Lecture lecture) {
private TodayLectureResponse getTodayFirstLectureResponse(SubAttendance subAttendance, LectureResponseType responseType, Lecture lecture) {
val subLecture = subAttendance.getSubLecture();
val isOnAttendanceCheck = LocalDateTime.now().isBefore(subLecture.getStartAt().plusMinutes(10));
val message = getMessage(lecture.getAttribute());
if (isOnAttendanceCheck && subAttendance.getStatus().equals(ABSENT)) {
if (checkOnAttendanceAbsence(subLecture, subAttendance)) {
return TodayLectureResponse.of(responseType, lecture, message, Collections.emptyList());
}
return TodayLectureResponse.of(responseType, lecture, message, Collections.singletonList(subAttendance));
}

private TodayLectureResponse getTodaySecondLectureResponse(
List<SubAttendance> subAttendances,
LectureResponseType responseType,
Lecture lecture
) {
val subAttendance = subAttendances.get(1);
val subLecture = subAttendance.getSubLecture();
val message = getMessage(lecture.getAttribute());
if (checkOnAttendanceAbsence(subLecture, subAttendance)) {
return TodayLectureResponse.of(responseType, lecture, message, Collections.singletonList(subAttendances.get(0)));
}
return TodayLectureResponse.of(responseType, lecture, message, subAttendances);
}

@Override
public LectureCurrentRoundResponse getCurrentLectureRound(long lectureId) {
val lecture = findLecture(lectureId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.util.List;

import org.sopt.makers.operation.app.attendance.dto.response.AttendanceTotalVO;
import lombok.Builder;
import org.sopt.makers.operation.common.domain.Part;
import org.sopt.makers.operation.member.domain.Member;

import static lombok.AccessLevel.PRIVATE;

@Builder(access = PRIVATE)
public record AttendanceTotalResponseDTO(
Part part,
int generation,
Expand All @@ -15,14 +18,14 @@ public record AttendanceTotalResponseDTO(
List<AttendanceTotalVO> attendances
) {
public static AttendanceTotalResponseDTO of(Member member, List<AttendanceTotalVO> attendances){
return new AttendanceTotalResponseDTO(
member.getPart(),
member.getGeneration(),
member.getName(),
member.getScore(),
getTotal(member),
attendances
);
return AttendanceTotalResponseDTO.builder()
.part(member.getPart())
.generation(member.getGeneration())
.name(member.getName())
.score(member.getScore())
.total(getTotal(member))
.attendances(attendances)
.build();
}

private static AttendanceStatusListVO getTotal(Member member) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.makers.operation.app.attendance.dto.response;
package org.sopt.makers.operation.app.member.dto.response;

import java.time.format.DateTimeFormatter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.sopt.makers.operation.app.member.dto.response;

import static lombok.AccessLevel.PRIVATE;

import lombok.Builder;

@Builder(access = PRIVATE)
public record MemberScoreGetResponse(
float score
) {
public static MemberScoreGetResponse of(float score){
return new MemberScoreGetResponse(
score
);
return MemberScoreGetResponse.builder()
.score(score)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

import org.sopt.makers.operation.app.member.dto.response.AttendanceTotalResponseDTO;
import org.sopt.makers.operation.app.attendance.dto.response.AttendanceTotalVO;
import org.sopt.makers.operation.app.member.dto.response.AttendanceTotalVO;
import org.sopt.makers.operation.app.member.dto.response.MemberScoreGetResponse;
import org.sopt.makers.operation.attendance.repository.attendance.AttendanceRepository;
import org.sopt.makers.operation.config.ValueConfig;
Expand Down

0 comments on commit 223b4ff

Please sign in to comment.