Skip to content

Commit

Permalink
Merge pull request #24 from Gongjakso/feat/apply
Browse files Browse the repository at this point in the history
Feat/apply
  • Loading branch information
sycuuui authored Jan 31, 2024
2 parents 69c2a81 + 0e5cd71 commit ce54208
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
package com.gongjakso.server.domain.apply.controller;

import com.gongjakso.server.domain.apply.dto.AddApplyReq;
import com.gongjakso.server.domain.apply.entity.Apply;
import com.gongjakso.server.domain.apply.dto.ApplyReq;
import com.gongjakso.server.domain.apply.dto.ApplicationRes;
import com.gongjakso.server.domain.apply.dto.ApplyRes;
import com.gongjakso.server.domain.apply.service.ApplyService;
import com.gongjakso.server.global.common.ApplicationResponse;
import com.gongjakso.server.global.security.PrincipalDetails;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/apply")
@Tag(name = "Apply", description = "팀 빌딩 관련 API")
public class ApplyController {
private final ApplyService applyService;
@PostMapping("/1")
public ResponseEntity<Apply> addApply(@RequestBody AddApplyReq req){
Apply savedApply = applyService.save(req);
return ResponseEntity.status(HttpStatus.CREATED).body(savedApply);
//지원 요청 api
@PostMapping("/{post_id}")
public ApplicationResponse<Void> addApply(@AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("post_id") Long postId, @RequestBody ApplyReq req){
applyService.save(principalDetails.getMember(),postId,req);
return ApplicationResponse.created();
}
//프로젝트 지원서 요청 api
@GetMapping("/{post_id}")
public ApplicationResponse<ApplyRes> getApply(@PathVariable("post_id") Long postId){
return applyService.findApply(postId);
}
//지원서 열람 요청 api
@PatchMapping("/{apply_id}/open")
public ApplicationResponse<Void> updateIsOpenStatus(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("apply_id") Long applyId){
return applyService.updateOpen(applyId);
}
//지원서 지원 요청 api
@PatchMapping("/{apply_id}/recruit")
public ApplicationResponse<Void> updateIsRecruitStatus(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("apply_id") Long applyId){
return applyService.updateRecruit(applyId,true);
}
//지원서 미선발 요청 api
@PatchMapping("/{apply_id}/not-recruit")
public ApplicationResponse<Void> updateNotRecruitStatus(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("apply_id") Long applyId){
return applyService.updateRecruit(applyId,false);
}
// 특정 지원자 지원서 가져오는 api
@GetMapping("/{apply_id}/application")
public ApplicationResponse<ApplicationRes> findApplication(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("apply_id") Long applyId){
return applyService.findApplication(applyId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.gongjakso.server.domain.apply.dto;

import com.gongjakso.server.domain.apply.entity.Apply;
import lombok.Builder;

@Builder
public record ApplicationRes(
String application,
String recruit_part
// String[] category
) {
public static ApplicationRes of(Apply apply){
return new ApplicationRes(apply.getApplication(), apply.getRecruit_part());
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/gongjakso/server/domain/apply/dto/ApplyList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gongjakso.server.domain.apply.dto;

import com.gongjakso.server.domain.apply.entity.Apply;

public record ApplyList(
Long apply_id,
String name,
String state
) {
public static ApplyList of(Apply apply,String state){
return new ApplyList(apply.getApplyId(),apply.getMember().getName(),state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import com.gongjakso.server.domain.apply.entity.Apply;
import com.gongjakso.server.domain.apply.enumerate.PostType;
import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.post.entity.Post;

public record AddApplyReq(
Member memberId,
public record ApplyReq(
String application,
String recruit_part,
PostType type,
String type,
Boolean is_pass,
Boolean is_open
) {
public Apply toEntity(){
public Apply toEntity(Member member, Post post_id){
return Apply.builder()
.member(memberId)
.member(member)
.post(post_id)
.application(application)
.recruit_part(recruit_part)
.type(type)
.type(PostType.valueOf(type))
.is_pass(false)
.is_open(false)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package com.gongjakso.server.domain.apply.dto;

public record ApplyRes() {
import com.gongjakso.server.domain.post.entity.Post;

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

public record ApplyRes(
LocalDateTime startDate,
LocalDateTime endDate,
Long max_person,
int current_person,
List<ApplyList> apply_list
) {
public static ApplyRes of(Post post, int current_person,List<ApplyList> apply_list){
return new ApplyRes(post.getStartDate(),post.getEndDate(),post.getMaxPerson(),current_person,apply_list);
}
}
20 changes: 12 additions & 8 deletions src/main/java/com/gongjakso/server/domain/apply/entity/Apply.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@

import com.gongjakso.server.domain.apply.enumerate.PostType;
import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.post.entity.Post;
import com.gongjakso.server.global.common.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Getter
@Setter
@Entity
@Table(name = "apply")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Apply extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "list_id",nullable = false,columnDefinition = "bigint")
private Long listID;
@Column(name = "apply_id",nullable = false,columnDefinition = "bigint")
private Long applyId;

@ManyToOne(targetEntity = Member.class, fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(targetEntity = Post.class, fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

@Column(name = "application",nullable = false,columnDefinition = "varchar(500)")
private String application;

Expand All @@ -39,9 +42,10 @@ public class Apply extends BaseTimeEntity {
private Boolean is_open;

@Builder
public Apply(Long listID, Member member, String application,String recruit_part,PostType type, Boolean is_pass,Boolean is_open){
this.listID=listID;
public Apply(Long applyId, Member member,Post post, String application,String recruit_part,PostType type, Boolean is_pass,Boolean is_open){
this.applyId=applyId;
this.member=member;
this.post=post;
this.application=application;
this.recruit_part=recruit_part;
this.type=type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.gongjakso.server.domain.apply.repository;

import com.gongjakso.server.domain.apply.entity.Apply;
import com.gongjakso.server.domain.post.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ApplyRepository extends JpaRepository<Apply,Long> {
}
long countApplyByPost(Post post);
List<Apply> findAllByPost(Post post);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,74 @@
package com.gongjakso.server.domain.apply.service;

import com.gongjakso.server.domain.apply.dto.AddApplyReq;
import com.gongjakso.server.domain.apply.dto.ApplyList;
import com.gongjakso.server.domain.apply.dto.ApplyReq;
import com.gongjakso.server.domain.apply.dto.ApplicationRes;
import com.gongjakso.server.domain.apply.dto.ApplyRes;
import com.gongjakso.server.domain.apply.entity.Apply;
import com.gongjakso.server.domain.apply.repository.ApplyRepository;
import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.post.entity.Post;
import com.gongjakso.server.domain.post.repository.PostRepository;
import com.gongjakso.server.global.common.ApplicationResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.webjars.NotFoundException;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class ApplyService {
private final ApplyRepository applyRepository;
public Apply save(AddApplyReq req){
return applyRepository.save(req.toEntity());
private final PostRepository postRepository;
public Apply save(Member member, Long post_id, ApplyReq req){
Post post = postRepository.findByPostId(post_id);
if (post == null) {
// Handle the case where the Post entity with the given post_id is not found
throw new NotFoundException("Post not found with id: " + post_id);
}
Apply apply = req.toEntity(member, post);
return applyRepository.save(apply);
}

public ApplicationResponse<ApplyRes> findApply(Long post_id){
Post post = postRepository.findById(post_id).orElseThrow(()->new NotFoundException("Post not found with id: " + post_id));
int current_person = (int) applyRepository.countApplyByPost(post);
List<Apply> applies = applyRepository.findAllByPost(post);
List<ApplyList> applyLists = applies.stream()
.map(apply -> ApplyList.of(apply, decisionState(apply)))
.collect(Collectors.toList());
ApplyRes applyRes = ApplyRes.of(post,current_person,applyLists);
return ApplicationResponse.ok(applyRes);
}
private String decisionState(Apply apply){
if(apply.getIs_pass()) {
return "합류 완료";
}else {
if(apply.getIs_open()){
return "열람 완료";
}else {
return "미선발";
}
}
}
public ApplicationResponse<Void> updateOpen(Long apply_id){
Apply apply = applyRepository.findById(apply_id).orElseThrow(()->new NotFoundException("Apply not found with id: " + apply_id));
apply.setIs_open(true);
return ApplicationResponse.ok();
}
public ApplicationResponse<Void> updateRecruit(Long apply_id, Boolean isRecruit){
Apply apply = applyRepository.findById(apply_id).orElseThrow(()->new NotFoundException("Apply not found with id: " + apply_id));
apply.setIs_pass(isRecruit);
return ApplicationResponse.ok();
}
public ApplicationResponse<ApplicationRes> findApplication(Long apply_id){
Apply apply = applyRepository.findById(apply_id).orElseThrow(()->new NotFoundException("Apply not found with id: " + apply_id));
// ApplicationRes applicationRes = ApplicationRes.builder().application(apply.getApplication()).recruit_part(apply.getRecruit_part()).build();
ApplicationRes applicationRes = ApplicationRes.of(apply);
return ApplicationResponse.ok(applicationRes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {

Post findByPostId(Long post_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public static <T> ApplicationResponse<T> created(T data) {
.data(data)
.build();
}

public static <T> ApplicationResponse<T> created() {
return ApplicationResponse.<T>builder()
.timestamp(LocalDateTime.now())
.code(ErrorCode.SUCCESS.getCode())
.message(ErrorCode.SUCCESS.getMessage())
.build();
}
}

0 comments on commit ce54208

Please sign in to comment.