Skip to content

Commit

Permalink
Merge pull request #42 from TeamPu/feat/#41
Browse files Browse the repository at this point in the history
불필요한 필드 삭제 및 start_time, end_time 기능 수정
  • Loading branch information
Muokok authored Nov 29, 2024
2 parents a23c740 + 9590acf commit 60cb292
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public ApiResponse<Void> createApplication(@RequestBody ApplicationRequest appli
public ApiResponse<Void> deleteApplication(@PathVariable Long id,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails) {

applicationService.deleteApplication(id, customMemberDetails);
applicationService.deleteApplication(id);

return ApiResponse.ok();
}

@GetMapping("/{id}")
public ApiResponse<ApplicationResponse> getDetailApplication(@PathVariable Long id,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails){
ApplicationResponse applicationResponse = applicationService.getDetailApplication(id, customMemberDetails);
ApplicationResponse applicationResponse = applicationService.getDetailApplication(id);

return ApiResponse.ok(applicationResponse);
}
Expand All @@ -64,7 +64,7 @@ public ApiResponse<ApplicationResponse> getDetailApplication(@PathVariable Long
public ApiResponse<Void> updateApplication(@PathVariable Long id,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails,
@RequestBody ApplicationRequest applicationRequest){
applicationService.updateApplication(id, customMemberDetails, applicationRequest);
applicationService.updateApplication(id, applicationRequest);
return ApiResponse.ok();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class Application {
@Column(name = "application_id")
private Long id;

@Column(name = "start_time", nullable = false)
@Column(name = "start_time", nullable = false, columnDefinition = "TIMESTAMP(0)") // 밀리초 자릿수를 0으로 지정
private LocalDateTime startTime;

@Column(name = "end_time", nullable = false)
@Column(name = "end_time", nullable = false, columnDefinition = "TIMESTAMP(0)") // 밀리초 자릿수를 0으로 지정
private LocalDateTime endTime;

@Column(name = "applied_date", nullable = false)
Expand All @@ -39,8 +39,11 @@ public class Application {
@Column(name = "status", nullable = false)
private ApplicationStatus status = ApplicationStatus.PENDING; // 기본값 설정

@Column(name = "participant_count")
private Integer participantCount;
@Column(name = "count_cp_with_applicant") // 신청자 포함 사용 인원 수
private Integer countCpWithApplicant;

@Column(name = "count_cp_only") // 신청자 제외 사용 인원 수
private Integer countCpOnly;

@ElementCollection // JPA에서 값 타입 컬렉션을 매핑할 때 사용하는 어노테이션
@CollectionTable(
Expand All @@ -60,9 +63,4 @@ public void updateStatus(ApplicationStatus status){
this.status = status;
}

private String applicantName; // Member의 name
private String applicantLoginId; // Member의 loginId
private String applicantPhone; // Member의 phoneNumber
private String applicantEmail; // Member의 email

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Getter;

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

/**
Expand All @@ -20,6 +21,8 @@
@AllArgsConstructor
public class ApplicationRequest {
// 로그인 정보를 통해 이름, 학번, 전화번호, 이메일 받아옴
private LocalDateTime startTime; // 시작 시간
private LocalDateTime endTime; // 종료 시간
private LocalDate appliedDate; // 날짜
private List<CoParticipantRequest> coParticipants; // 공동 참여자 목록 (이름, 전화번호)
private Boolean privacyAgreement; // 개인정보 동의 여부
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Getter
@Builder
public class ApplicationResponse {
private LocalDateTime startTime; // 시작 시간
private LocalDateTime endTime; // 종료 시간
private LocalDate appliedDate; // 날짜
private String name; // 이름
private Integer participantCount; // 참여 인원(신청자 제외)
private Integer countCpOnly; // 동반 참가자 수만 저장하는 필드


public static ApplicationResponse fromEntity(Application application){
return ApplicationResponse.builder()
.startTime(application.getStartTime()) // 시작 시간
.endTime(application.getEndTime()) // 종료 시간
.appliedDate(application.getAppliedDate()) // 날짜
.name(application.getMember().getName()) // 이름
.participantCount(application.getCoParticipants().size()) // 참여 인원(신청자 제외)
.countCpOnly(application.getCoParticipants().size()) // 참여 인원(신청자 제외)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.kyonggi.teampu.domain.application.repository;

import com.kyonggi.teampu.domain.application.domain.Application;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface ApplicationRepository extends JpaRepository<Application, Long> {
List<Application> findByAppliedDateBetween(LocalDate startDate, LocalDate endDate);

@EntityGraph(attributePaths = {"member"}) // fetch join 사용
Optional<Application> findById(Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.kyonggi.teampu.domain.application.repository.ApplicationRepository;
import com.kyonggi.teampu.domain.auth.domain.CustomMemberDetails;
import com.kyonggi.teampu.domain.member.domain.CoParticipant;
import com.kyonggi.teampu.domain.member.domain.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -38,22 +37,20 @@ public void createApplication(ApplicationRequest applicationRequest, CustomMembe
* 3. 사용 인원 계산
*/

Member member = customMemberDetails.getMember();
List<CoParticipant> coParticipants = applicationRequest.getCoParticipants().stream()
.map(CoParticipant::from)
.collect(Collectors.toList());

// Application.builder()를 사용하여 로그인한 사용자 정보와 입력받은 정보를 결합
Application application = Application.builder()
.member(member) // memberId
.applicantName(customMemberDetails.getMember().getName()) // 이름
.applicantLoginId(customMemberDetails.getMember().getLoginId()) // 학번
.applicantPhone(customMemberDetails.getMember().getPhoneNumber()) // 전화번호
.applicantEmail(customMemberDetails.getMember().getEmail()) // 이메일
.member(customMemberDetails.getMember()) // Member 객체만 전달

.appliedDate(applicationRequest.getAppliedDate()) // 날짜
.startTime(applicationRequest.getStartTime().withSecond(0).withNano(0)) // 시작 시간
.endTime(applicationRequest.getEndTime().withSecond(0).withNano(0)) // 종료 시간
.coParticipants(coParticipants) // 명단(이름, 전화번호)
.participantCount(applicationRequest.getCoParticipants().size() + 1) // 사용 인원 자동 계산
.countCpWithApplicant(applicationRequest.getCoParticipants().size()+1) // 신청자 포함 사용 인원 수
.countCpOnly(applicationRequest.getCoParticipants().size()) // 신청자 제외 사용 인원 수
.privacyAgreement(applicationRequest.getPrivacyAgreement()) // 개인정보 동의
.status(applicationRequest.getStatus())
.build();
Expand All @@ -62,24 +59,23 @@ public void createApplication(ApplicationRequest applicationRequest, CustomMembe
}

@Transactional
public void deleteApplication(Long id, CustomMemberDetails customMemberDetails){
public void deleteApplication(Long id){
Application application = applicationRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("신청서를 찾을 수 없습니다."));

applicationRepository.delete(application);
}

@Transactional
public ApplicationResponse getDetailApplication(Long id, CustomMemberDetails customMemberDetails){
public ApplicationResponse getDetailApplication(Long id){
Application application = applicationRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("신청서를 찾을 수 없습니다."));

return fromEntity(application);

}

@Transactional
public void updateApplication(Long id, CustomMemberDetails customMemberDetails, ApplicationRequest applicationRequest){
public void updateApplication(Long id, ApplicationRequest applicationRequest){
Application application = applicationRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("신청서를 찾을 수 없습니다."));

Expand All @@ -93,19 +89,17 @@ public void updateApplication(Long id, CustomMemberDetails customMemberDetails,
.collect(Collectors.toList()) :
application.getCoParticipants();

Member member = customMemberDetails.getMember();

// Builder를 사용한 업데이트
Application updatedApplication = Application.builder()
.member(member)
.applicantName(application.getApplicantName()) // 신청자 이름
.id(application.getId())
.applicantPhone(application.getApplicantPhone())
.applicantEmail(application.getApplicantEmail())
.applicantLoginId(application.getApplicantLoginId())
.member(application.getMember()) // 기존 member 정보 유지

.appliedDate(appliedDate) // 날짜
.startTime(applicationRequest.getStartTime().withSecond(0).withNano(0)) // 시작 시간
.endTime(applicationRequest.getEndTime().withSecond(0).withNano(0)) // 종료 시간
.coParticipants(coParticipants) // 공동 참여자 명단(이름, 전화번호)
.participantCount(application.getParticipantCount()) // 신청자 제외 사용 인원
.countCpWithApplicant(applicationRequest.getCoParticipants().size()+1) // 신청자 포함 사용 인원 수
.countCpOnly(applicationRequest.getCoParticipants().size()) // 신청자 제외 사용 인원 수
.privacyAgreement(application.getPrivacyAgreement())
.status(application.getStatus()) // 상태
.build();
Expand Down Expand Up @@ -157,4 +151,5 @@ public MainPageResponse.CalendarResponseDTO getCalendarData(Integer year, Intege

return new MainPageResponse.CalendarResponseDTO(targetYear, targetMonth, days);
}

}

0 comments on commit 60cb292

Please sign in to comment.