Skip to content

Commit

Permalink
Merge pull request #187 from FOREGG-DEV/dev
Browse files Browse the repository at this point in the history
♻️ refactor: 코드 리팩터링
  • Loading branch information
DongJun1110 authored Jan 2, 2025
2 parents 462f033 + b36d929 commit 5c00f9f
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public enum ErrorStatus implements BaseErrorCode {
NOT_FOUND_MY_RECORD(BAD_REQUEST, "RECORD4004", "나의 기록이 존재하지 않습니다"),
NOT_RESERVED_HOSPITAL_RECORD(BAD_REQUEST, "RECORD4005", "예약된 병원 기록이 존재하지 않습니다"),
NOT_FOUND_REPEATTIME(BAD_REQUEST, "RECORD4006", "해당 시간에 기록이 없습니다"),
NOT_INJECTION_RECORD(BAD_REQUEST, "RECORD4007", "주사 일정이 아닙니다"),
MISMATCH_RECORD_AND_TYPE(BAD_REQUEST, "RECORD4007", "일정의 타입이 일치하지 않습니다"),
NOT_FOUND_LATEST_MEDICAL_RECORD(BAD_REQUEST, "RECORD4008", "최근의 진료 기록이 없습니다"),
NOT_FOUND_MY_INJECTION_RECORD(BAD_REQUEST, "RECORD4009","나의 주사 일정을 찾을 수 없습니다"),
MEN_ONLY_ETC(BAD_REQUEST, "RECORD4010","남편은 기타 일정 외에는 추가할 수 없습니다"),
INVALID_RECORD_DATE(BAD_REQUEST, "RECORD4011", "해당 날짜에는 일정이 존재하지 않습니다"),


//챌린지 관련 에러
Expand Down Expand Up @@ -85,6 +86,7 @@ public enum ErrorStatus implements BaseErrorCode {
NOT_FOUND_DAILY(BAD_REQUEST, "DAILY4002","하루 기록이 존재하지 않습니다"),
NOT_FRIDAY(BAD_REQUEST, "DAILY4003","금요일이 아닙니다"),
ALREADY_REPLY(BAD_REQUEST, "DAILY4004","이미 답장을 했습니다"),
ONLY_INJECTION_MEDICINE(BAD_REQUEST, "DAILY_4005", "주사와 약 정보만 제공됩니다"),

//부작용 관련 에러
NOT_FOUND_SIDEEFFECT(BAD_REQUEST, "SIDEEFFECT4001", "부작용이 존재하지 않습니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -188,9 +190,15 @@ public ApiResponse<String> getChallengeName() {
@GetMapping("/{challengeId}/{isSuccess}/participants")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "PAGE4001", description = "페이지 사이즈를 벗어났습니다")
})
public ApiResponse<List<ChallengeParticipantsDTO>> getCompleteList(@PathVariable(name = "challengeId") Long challengeId, @PathVariable(name = "isSuccess") boolean isSuccess) {
List<ChallengeParticipantsDTO> result = challengeQueryService.getParticipants(challengeId, isSuccess);
public ApiResponse<ChallengeParticipantsDTO> getCompleteList(
@PathVariable(name = "challengeId") Long challengeId,
@PathVariable(name = "isSuccess") boolean isSuccess,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
ChallengeParticipantsDTO result = challengeQueryService.getParticipants(challengeId, isSuccess, pageable);
return ApiResponse.onSuccess(result);
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/foregg/foreggserver/controller/DailyController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

package foregg.foreggserver.controller;

import foregg.foreggserver.apiPayload.ApiResponse;
import foregg.foreggserver.domain.enums.RecordType;
import foregg.foreggserver.dto.dailyDTO.*;
import foregg.foreggserver.dto.dailyDTO.DailyRequestDTO.DailyReplyRequestDTO;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO.DailyAllResponseDTO;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO.DailyByCountResponseDTO;
import foregg.foreggserver.dto.injectionDTO.InjectionResponseDTO;
import foregg.foreggserver.dto.injectionDTO.MedicalResponseDTO;
import foregg.foreggserver.service.dailyService.DailyQueryService;
import foregg.foreggserver.service.dailyService.DailyService;
import foregg.foreggserver.service.injectionService.InjectionQueryService;
Expand Down Expand Up @@ -184,12 +186,23 @@ public ApiResponse<String> sendNotificationInjection(@PathVariable(name = "id")
return ApiResponse.onSuccess();
}

@Operation(summary = "주사 정보 페이지 API")
@Operation(summary = "의약품 정보 페이지 API")
@PreAuthorize("isAuthenticated()")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "DAILY4001", description = "존재하지 않는 일정입니다"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "DAILY4005", description = "주사와 약 정보만 제공됩니다"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "DAILY4006", description = "해당 시간에 기록이 없습니다"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "DAILY4007", description = "일정의 타입이 일치하지 않습니다"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "DAILY4009", description = "나의 주사 일정을 찾을 수 없습니다"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "DAILY4011", description = "해당 날짜에는 일정이 존재하지 않습니다"),
})
@GetMapping("/injectionInfo/{id}")
public ApiResponse<InjectionResponseDTO> getInjectionInfo(@PathVariable(name = "id") Long id,
@RequestParam(name = "time") String time) {
InjectionResponseDTO dto = injectionQueryService.getInjectionInfo(id, time);
public ApiResponse<MedicalResponseDTO> getInjectionInfo(@PathVariable(name = "id") Long id,
@RequestParam(name = "type") RecordType type,
@RequestParam(name = "date") String date,
@RequestParam(name = "time") String time) {
MedicalResponseDTO dto = injectionQueryService.getMedicalInfo(id, type, date, time);
return ApiResponse.onSuccess(dto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import foregg.foreggserver.domain.ChallengeParticipation;
import foregg.foreggserver.domain.User;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeParticipantsDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeParticipantsDTO.ChallengeParticipantDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.MyChallengeTotalDTO.MyChallengeDTO;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -48,8 +48,8 @@ public static MyChallengeDTO toMyChallengeDTO(ChallengeParticipation cp, int par
.successDays(cp.getSuccessDays()).build();
}

public static ChallengeParticipantsDTO toChallengeParticipantDTO(ChallengeParticipation challengeParticipations, boolean isSupported) {
return ChallengeParticipantsDTO.builder()
public static ChallengeParticipantDTO toChallengeParticipantDTO(ChallengeParticipation challengeParticipations, boolean isSupported) {
return ChallengeParticipantDTO.builder()
.userId(challengeParticipations.getUser().getId())
.nickname(challengeParticipations.getUser().getChallengeName())
.thoughts(challengeParticipations.getThoughts())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Injection extends BaseEntity {
public class Medical extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ public static class MyChallengeDTO{
@NoArgsConstructor
public static class ChallengeParticipantsDTO {

private Long userId;
private String nickname;
private String thoughts;
private boolean isSupported;
private List<ChallengeParticipantDTO> dto;
private int currentPage;
private int totalPage;
private int totalItems;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class ChallengeParticipantDTO {
private Long userId;
private String nickname;
private String thoughts;
private boolean isSupported;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.checkerframework.checker.units.qual.N;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class InjectionResponseDTO {
public class MedicalResponseDTO {
private String name;
private String date;
private String description;
private String image;
private String time;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package foregg.foreggserver.repository;

import foregg.foreggserver.domain.Medical;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MedicalRepository extends JpaRepository<Medical, Long> {

Optional<Medical> findByName(String name);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package foregg.foreggserver.service.challengeService;

import foregg.foreggserver.apiPayload.exception.handler.ChallengeHandler;
import foregg.foreggserver.apiPayload.exception.handler.PageHandler;
import foregg.foreggserver.converter.ChallengeConverter;
import foregg.foreggserver.domain.Challenge;
import foregg.foreggserver.domain.ChallengeParticipation;
Expand All @@ -10,13 +11,15 @@
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeParticipantsDTO;
import foregg.foreggserver.dto.challengeDTO.ChallengeResponseDTO.ChallengeParticipantsDTO.ChallengeParticipantDTO;
import foregg.foreggserver.repository.NotificationRepository;
import foregg.foreggserver.repository.ChallengeParticipationRepository;
import foregg.foreggserver.repository.ChallengeRepository;
import foregg.foreggserver.service.userService.UserQueryService;
import foregg.foreggserver.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -76,42 +79,58 @@ public ChallengeResponseDTO.MyChallengeTotalDTO getMyChallenges() {
return ChallengeResponseDTO.MyChallengeTotalDTO.builder().dtos(result).firstDateOfWeek(DateUtil.getFirstDayOfWeek()).build();
}

public List<ChallengeParticipantsDTO> getParticipants(Long challengeId, boolean isSuccess) {

List<ChallengeParticipantsDTO> result = new ArrayList<>();
public ChallengeParticipantsDTO getParticipants(Long challengeId, boolean isSuccess, Pageable pageable) {
List<ChallengeParticipantDTO> result = new ArrayList<>();

// 기존 로직 수행
User currentUser = userQueryService.getUser();
Challenge challenge = isParticipating(challengeId);
List<ChallengeParticipation> challengeParticipations = challenge.getChallengeParticipations();

challengeParticipations.removeIf(cp -> cp.getUser().equals(currentUser));

if (isSuccess) {
challengeParticipations.removeIf(cp -> !cp.getSuccessDays().contains(DateUtil.getTodayDayOfWeek()));
}else{
} else {
challengeParticipations.removeIf(cp -> cp.getSuccessDays().contains(DateUtil.getTodayDayOfWeek()));
}

if (challengeParticipations.isEmpty()) {
return new ArrayList<>();
if (!challengeParticipations.isEmpty()) {
for (ChallengeParticipation cp : challengeParticipations) {
Notification notification;
if (isSuccess) {
notification = notificationRepository.findBySenderAndReceiverAndDateAndNotificationType(currentUser.getChallengeName(), cp.getUser(), LocalDate.now().toString(), NotificationType.CLAP);
} else {
notification = notificationRepository.findBySenderAndReceiverAndDateAndNotificationType(currentUser.getChallengeName(), cp.getUser(), LocalDate.now().toString(), NotificationType.SUPPORT);
}
boolean supported = notification != null;
result.add(ChallengeConverter.toChallengeParticipantDTO(cp, supported));
}
}

for (ChallengeParticipation cp : challengeParticipations) {
Notification notification;
if (isSuccess) {
notification = notificationRepository.findBySenderAndReceiverAndDateAndNotificationType(currentUser.getChallengeName(), cp.getUser(), LocalDate.now().toString(), NotificationType.CLAP);
} else {
notification = notificationRepository.findBySenderAndReceiverAndDateAndNotificationType(currentUser.getChallengeName(), cp.getUser(), LocalDate.now().toString(), NotificationType.SUPPORT);
}
if (notification == null) {
result.add(ChallengeConverter.toChallengeParticipantDTO(cp, false));
} else {
result.add(ChallengeConverter.toChallengeParticipantDTO(cp, true));
}
// 페이징 처리
int fromIndex = (int) pageable.getOffset();
int toIndex = Math.min(fromIndex + pageable.getPageSize(), result.size());

// 요청 범위 검증
if (fromIndex > result.size()) {
throw new PageHandler(PAGE_OUT_OF_RANGE);
}
return result;

List<ChallengeParticipantDTO> paginatedList = result.subList(fromIndex, toIndex);

// 페이징 정보만 포함한 PageResponse 반환
int totalPages = (int) Math.ceil((double) result.size() / pageable.getPageSize());
return ChallengeParticipantsDTO.builder()
.dto(paginatedList)
.currentPage(pageable.getPageNumber())
.totalPage(totalPages)
.totalItems(result.size())
.build();
}




//챌린지 검색 메서드
public ChallengeResponseDTO searchChallenge(String keyword) {
User user = userQueryService.getUser();
Expand Down
Loading

0 comments on commit 5c00f9f

Please sign in to comment.