Skip to content

Commit

Permalink
Merge pull request #180 from PawWithU/feat/177-application-api-modify
Browse files Browse the repository at this point in the history
[Feature] 이동봉사 신청 관련 API 수정
  • Loading branch information
kyeong-hyeok authored May 5, 2024
2 parents 9bad48a + e574475 commit e31154f
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class ApplicationController {
responses = {@ApiResponse(responseCode = "204", description = "이동봉사자 소셜 로그인 추가 회원가입 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 이름은 필수 입력 값입니다. \t\n V1, 휴대전화 번호는 필수 입력 값입니다. \t\n V1, 유효하지 않은 휴대전화 번호입니다. \t\n " +
"V1, 교통수단은 필수 입력 값입니다. \t\n V1, 200자 이내로 입력해 주세요. \t\n M1, 해당 이동봉사자를 찾을 수 없습니다. \t\n " +
"P2, 해당 공고를 찾을 수 없습니다. \t\n AP1, 이미 신청된 공고입니다."
"V1, 100자 이내로 입력해 주세요. \t\n M1, 해당 이동봉사자를 찾을 수 없습니다. \t\n P2, 해당 공고를 찾을 수 없습니다. \t\n AP1, 이미 신청된 공고입니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/volunteers/posts/{postId}/applications")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ public record VolunteerApplyRequest(@NotBlank(message = "이름은 필수 입력
@NotBlank(message = "휴대전화 번호는 필수 입력 값입니다.")
@Pattern(regexp = "^010[0-9]{8}$", message = "유효하지 않은 휴대전화 번호입니다.")
String phone,
@NotBlank(message = "교통수단은 필수 입력 값입니다.")
String transportation,
@Size(max=200, message = "200자 이내로 입력해 주세요.")
@Size(max=100, message = "100자 이내로 입력해 주세요.")
String content) {

public Application toEntity(Post post, Intermediary intermediary, Volunteer volunteer) {
return Application.builder()
.status(ApplicationStatus.WAITING)
.volunteerName(name)
.phone(phone)
.transportation(transportation)
.content(content)
.post(post)
.intermediary(intermediary)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.pawwithu.connectdog.domain.application.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.pawwithu.connectdog.domain.application.entity.Application;

public record ApplicationIntermediaryGetOneResponse(Long id, String volunteerName, String phone, String transportation, String content) {
import java.time.LocalDate;

public record ApplicationIntermediaryGetOneResponse(Long id,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd", timezone = "Asia/Seoul")
LocalDate createdDate,
String volunteerName, String phone, String content) {

public static ApplicationIntermediaryGetOneResponse from(Application application) {
return new ApplicationIntermediaryGetOneResponse(application.getId(), application.getVolunteerName(),
application.getPhone(), application.getTransportation(), application.getContent());
return new ApplicationIntermediaryGetOneResponse(application.getId(), application.getCreatedDate().toLocalDate(),
application.getVolunteerName(), application.getPhone(), application.getContent());
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.pawwithu.connectdog.domain.application.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.pawwithu.connectdog.domain.application.entity.Application;

public record ApplicationVolunteerGetOneResponse(Long id, String volunteerName, String phone, String transportation, String content) {
import java.time.LocalDate;

public record ApplicationVolunteerGetOneResponse(Long id,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd", timezone = "Asia/Seoul")
LocalDate createdDate,
String volunteerName, String phone, String content) {

public static ApplicationVolunteerGetOneResponse from(Application application) {
return new ApplicationVolunteerGetOneResponse(application.getId(), application.getVolunteerName(), application.getPhone(), application.getTransportation(), application.getContent());
return new ApplicationVolunteerGetOneResponse(application.getId(), application.getCreatedDate().toLocalDate(),
application.getVolunteerName(), application.getPhone(), application.getContent());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"post_id"}))
//@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"post_id"}))
public class Application extends BaseTimeEntity {

@Id
Expand All @@ -23,10 +23,8 @@ public class Application extends BaseTimeEntity {
private String volunteerName; // 이동봉사자 이름
@Column(length = 15, nullable = false)
private String phone; // 전화번호
@Column(length = 10, nullable = false)
private String transportation; // 교통수단
@Column(length = 200, nullable = false)
private String content; // 신청 내용
private String content; // 전달 및 문의사항
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Post post; // 공고 id
Expand All @@ -38,11 +36,10 @@ public class Application extends BaseTimeEntity {
private Volunteer volunteer; // 이동봉사자 id

@Builder
public Application(ApplicationStatus status, String volunteerName, String phone, String transportation, String content, Post post, Intermediary intermediary, Volunteer volunteer) {
public Application(ApplicationStatus status, String volunteerName, String phone, String content, Post post, Intermediary intermediary, Volunteer volunteer) {
this.status = status;
this.volunteerName = volunteerName;
this.phone = phone;
this.transportation = transportation;
this.content = content;
this.post = post;
this.intermediary = intermediary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@RequiredArgsConstructor
public enum ApplicationStatus {

WAITING("승인 대기중"), PROGRESSING("진행중"), COMPLETED("봉사 완료");
WAITING("승인 대기중"), PROGRESSING("진행중"), COMPLETED("봉사 완료"), REJECTED("반려");

private final String key;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public interface CustomApplicationRepository {

// 진행한 이동봉사 건수
Long getCountOfCompletedApplications(Long id);

boolean existsByPostIdAndPostStatus(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ public Long getCountOfCompletedApplications(Long id) {
.and(application.status.eq(ApplicationStatus.COMPLETED)))
.fetchOne();
}

@Override
public boolean existsByPostIdAndPostStatus(Long postId) {
return queryFactory
.select(application)
.from(application)
.where(application.post.id.eq(postId)
.and(application.status.ne(ApplicationStatus.REJECTED)))
.fetchOne() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ public class ApplicationService {
private final IntermediaryRepository intermediaryRepository;

public void volunteerApply(String email, Long postId, VolunteerApplyRequest request) {
try {
// 이동봉사자
Volunteer volunteer = volunteerRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));
// 공고
Post post = postRepository.findById(postId).orElseThrow(() -> new BadRequestException(POST_NOT_FOUND));
// 이동봉사 중개
Intermediary intermediary = post.getIntermediary();

// 공고 신청 저장
Application application = request.toEntity(post, intermediary, volunteer);
applicationRepository.save(application);
// 공고 상태 승인 대기 중으로 변경
post.updateStatus(PostStatus.WAITING);
} catch (DataIntegrityViolationException e) {
// 이동봉사자
Volunteer volunteer = volunteerRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));
// 공고
Post post = postRepository.findById(postId).orElseThrow(() -> new BadRequestException(POST_NOT_FOUND));
// 이동봉사 중개
Intermediary intermediary = post.getIntermediary();

// 해당 공고에 대한 신청이 이미 존재할 경우 - 신청 상태가 반려가 아닐 경우
if (customApplicationRepository.existsByPostIdAndPostStatus(postId)) {
throw new BadRequestException(ALREADY_EXIST_APPLICATION);
}

// 공고 신청 저장
Application application = request.toEntity(post, intermediary, volunteer);
applicationRepository.save(application);

// 공고 상태 승인 대기 중으로 변경
post.updateStatus(PostStatus.WAITING);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -120,10 +122,10 @@ public ApplicationSuccessResponse cancelApplication(String email, Long applicati
Intermediary intermediary = intermediaryRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));
// 신청 내역 + post
Application application = customApplicationRepository.findByIdAndIntermediaryIdAndStatusWithPost(applicationId, intermediary.getId(), ApplicationStatus.WAITING).orElseThrow(() -> new BadRequestException(APPLICATION_NOT_FOUND));
applicationRepository.delete(application);
// 상태 업데이트 (승인 대기중 -> 모집중)
Post post = application.getPost();
// 상태 업데이트 (승인 대기중 -> 모집중)
post.updateStatus(PostStatus.RECRUITING);
application.updateStatus(ApplicationStatus.REJECTED);
ApplicationSuccessResponse isSuccess = ApplicationSuccessResponse.of(true);
return isSuccess;
}
Expand Down
Loading

0 comments on commit e31154f

Please sign in to comment.