Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 사용자별 공고 상세 조회 기능 + 지원자의 지원서 열람 기능 개발 #108

Merged
merged 12 commits into from
Apr 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ public ApplicationResponse<Void> updatePostPeriod(@AuthenticationPrincipal Princ
public ApplicationResponse<List<MyPageRes>> getMyApplyList(@AuthenticationPrincipal PrincipalDetails principalDetails) {
return ApplicationResponse.ok(applyService.getMyApplyList(principalDetails.getMember()));
}

@Operation(summary = "지원서 열람 API", description = "해당 공고에 대한 사용자의 지원서 정보 반환")
@GetMapping("/my/{post_id}")
public ApplicationResponse<ApplicationRes> getMyApplication(@AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("post_id") Long postId){
return ApplicationResponse.ok(applyService.getMyApplication(principalDetails.getMember(), postId));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gongjakso.server.domain.apply.entity;

import com.gongjakso.server.domain.post.entity.StackName;
import com.gongjakso.server.global.common.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.*;

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ public interface ApplyRepository extends JpaRepository<Apply,Long> {
Page<Apply> findAllByPost(Post post, Pageable pageable);
Page<Apply> findApplyByApplyType(ApplyType applyType, Pageable pageable);
List<Apply> findAllByMemberAndDeletedAtIsNull(Member member);
List<Apply> findAllByPost(Post post);
Long findApplyIdByMemberAndPost(Member member,Post post);
Apply findApplyByMemberAndPost(Member member,Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Optional;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.gongjakso.server.domain.post.enumerate.PostStatus.EXTENSION;
import static com.gongjakso.server.domain.post.enumerate.PostStatus.RECRUITING;
import static com.gongjakso.server.global.exception.ErrorCode.INVALID_VALUE_EXCEPTION;
import static com.gongjakso.server.global.exception.ErrorCode.NOT_FOUND_POST_EXCEPTION;


@Service
@Transactional
Expand Down Expand Up @@ -295,32 +293,33 @@ public List<MyPageRes> getMyApplyList(Member member) {
.collect(Collectors.toList());
}

public Long getApplicantApplyId(Long memberId, Long postId) {
Post post = postRepository.findWithStackNameAndCategoryUsingFetchJoinByPostId(postId);

if (post == null) {
throw new ApplicationException(NOT_FOUND_POST_EXCEPTION);
}

List<Apply> applyList = applyRepository.findAllByPost(post);

// 지원자 목록에 해당 멤버가 포함되어 있는지 확인하고 해당하는 apply의 id를 반환
Optional<Apply> applicantApply = applyList.stream()
.filter(apply -> memberId.equals(apply.getMember().getMemberId()))
.findFirst();

return applicantApply.map(Apply::getApplyId)
.orElse(null);
}

public ApplicationRes getMyApplication(Member member, Long postId){
Post post = postRepository.findWithStackNameAndCategoryUsingFetchJoinByPostId(postId);

Post post = postRepository.findByPostId(postId);
if (post == null) {
throw new ApplicationException(ErrorCode.NOT_FOUND_POST_EXCEPTION);
}else{
Long applyId = applyRepository.findApplyIdByMemberAndPost(member, post);
return findApplication(member, applyId, postId);
Apply apply = applyRepository.findApplyByMemberAndPost(member, post);
if(apply == null){
throw new ApplicationException(ErrorCode.NOT_FOUND_APPLY_EXCEPTION);
}else {
//Change List Type
List<String> categoryList = changeCategoryType(post);
List<String> stackNameList;
List<String> applyStackList = null;
if(post.isPostType()){
stackNameList = changeStackNameType(post);
System.out.println("change stack name");
List<ApplyStack> applyStacks = applyStackRepository.findAllByApply(apply);
applyStackList = new ArrayList<>();
for(ApplyStack applyStack : applyStacks){
applyStackList.add(applyStack.getStackName().getStackNameType());
}
}else {
stackNameList= null;
}

return ApplicationRes.of(apply, categoryList, stackNameList,applyStackList);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gongjakso.server.domain.post.controller;

import com.gongjakso.server.domain.apply.service.ApplyService;
import com.gongjakso.server.domain.post.dto.*;
import com.gongjakso.server.domain.post.service.PostService;
import com.gongjakso.server.global.common.ApplicationResponse;
Expand All @@ -22,16 +23,18 @@
public class PostController {

private final PostService postService;
private final ApplyService applyService;

@Operation(summary = "공모전/프로젝트 공고 생성 API", description = "팀빌딩 페이지에서 정보 입력 후 공고 생성")
@PostMapping("")
public ApplicationResponse<PostRes> create(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestBody PostReq req) {
return ApplicationResponse.ok(postService.create(principalDetails.getMember(), req));
}

@Operation(summary = "공모전/프로젝트 공고 상세 조회 API", description = "공모전/프로젝트 공고 상세 조회")
@GetMapping("/{id}")
public ApplicationResponse<PostDetailRes> read(@PathVariable("id") Long id) {
return ApplicationResponse.ok(postService.read(id));
@Operation(summary = "사용자 별 상세 조회 API", description = "사용자별로 공고 상세 조회 다르게 반환")
@GetMapping("/read")
public ApplicationResponse<?> read(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestParam(value = "id", required = true) Long postId, @RequestParam(value = "role", required = false) String role ) {
return ApplicationResponse.ok(postService.read(principalDetails, postId, role));
}

@Operation(summary = "공모전/프로젝트 공고 수정 API", description = "팀빌딩 페이지에서 정보 입력 후 공고 수정")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.gongjakso.server.domain.post.dto;

import com.gongjakso.server.domain.post.entity.Post;
import lombok.Builder;

@Builder
public record GetPostRelation(
String status
String role
) {
public static GetPostRelation of(String status) {
public static GetPostRelation of(String role) {
return GetPostRelation.builder()
.status(status)
.role(role)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,23 @@ public record PostDetailRes(
)
Long scrapCount,

@Schema(
description = "사용자가 일반접근자|지원자|팀장인지 구분"
)
String role,

@Schema(
description = "접근한 사용자의 memberId"
)
Long currentId,
@Schema(
description = "조회수 반환 관리",
defaultValue = "0"
)
Long postView
) {

public static PostDetailRes of(Post post, int currentPerson) {
public static PostDetailRes of(Post post, int currentPerson, String role, Long currentId) {
return PostDetailRes.builder()
.postId(post.getPostId())
.memberId(post.getMember().getMemberId())
Expand All @@ -163,6 +172,8 @@ public static PostDetailRes of(Post post, int currentPerson) {
.postType(post.isPostType())
.createdAt(post.getCreatedAt())
.scrapCount(post.getScrapCount())
.role(role)
.currentId(currentId)
.postView(post.getPostView())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.gongjakso.server.domain.post.entity;

import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.post.dto.PostDetailRes;
import com.gongjakso.server.domain.post.dto.PostModifyReq;
import com.gongjakso.server.domain.post.dto.PostReq;
import com.gongjakso.server.domain.post.enumerate.MeetingMethod;
import com.gongjakso.server.domain.post.enumerate.PostStatus;
import com.gongjakso.server.global.common.BaseTimeEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.gongjakso.server.domain.post.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -11,14 +10,14 @@
@Entity
@Table(name = "stack_name")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class StackName {
public class StackName{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "stack_name_id", nullable = false, columnDefinition = "bigint")
private Long stackNameId;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "post_id", nullable = false, columnDefinition = "bigint")
@JsonIgnore
private Post post;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gongjakso.server.domain.post.enumerate;

public enum CategoryType {
PLAN,DESIGN,FE,BE,ETC,LATER;
PLAN,DESIGN,FE,BE,ETC;
@Override
public String toString() {
return name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.gongjakso.server.domain.post.entity.Post;
import com.gongjakso.server.domain.post.entity.StackName;
import com.gongjakso.server.domain.post.enumerate.StackNameType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.post.dto.CalendarRes;
import com.gongjakso.server.domain.post.dto.GetContestRes;
import com.gongjakso.server.domain.post.dto.ScrapPost;
import com.gongjakso.server.domain.post.entity.Post;
import com.gongjakso.server.domain.post.entity.PostScrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.gongjakso.server.domain.post.repository.PostRepository;
import com.gongjakso.server.domain.post.repository.PostScrapRepository;
import com.gongjakso.server.global.exception.ApplicationException;
import com.gongjakso.server.global.security.PrincipalDetails;
import lombok.RequiredArgsConstructor;
import org.hibernate.Hibernate;
import org.springframework.data.domain.Page;
Expand All @@ -23,6 +24,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.gongjakso.server.domain.post.enumerate.PostStatus.RECRUITING;
Expand Down Expand Up @@ -64,7 +66,7 @@ public PostRes create(Member member, PostReq req) {
}

@Transactional
public PostDetailRes read(Long id) {
public Optional<?> read(PrincipalDetails principalDetails, Long id, String role) {
Post post = postRepository.findWithStackNameAndCategoryUsingFetchJoinByPostId(id);
if (post == null) {
throw new ApplicationException(NOT_FOUND_POST_EXCEPTION);
Expand All @@ -75,9 +77,17 @@ public PostDetailRes read(Long id) {

Hibernate.initialize(post.getStackNames());
Hibernate.initialize(post.getCategories());
return PostDetailRes.of(post, current_person);

if(principalDetails == null) {
return Optional.of(PostDetailRes.of(post, current_person, role, null));
}else if(("GENERAL".equals(role) || "LEADER".equals(role) || "APPLICANT".equals(role)) && principalDetails != null){
return Optional.of(PostDetailRes.of(post, current_person, role, principalDetails.getMember().getMemberId()));
} else {
throw new ApplicationException(NOT_FOUND_POST_EXCEPTION);
}
}


@Transactional
public PostRes modify(Member member, Long id, PostModifyReq req) {
Post entity = postRepository.findByPostIdAndDeletedAtIsNull(id)
Expand Down Expand Up @@ -320,17 +330,17 @@ public GetPostRelation checkPostRelation(Member member, Long postId) {
Post post = postRepository.findByPostIdAndDeletedAtIsNull(postId).orElseThrow(() -> new ApplicationException(ALREADY_DELETE_EXCEPTION));

// Business Logic
String status = "GENERAL";
String role = "GENERAL";
if(post.getMember().getMemberId().equals(member.getMemberId())){
status = "LEADER";
role = "LEADER";
}
else {
if(applyRepository.existsApplyByMemberAndPost(member, post)) {
status = "APPLICANT";
role = "APPLICANT";
}
}

// Return
return GetPostRelation.of(status);
return GetPostRelation.of(role);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.gongjakso.server.domain.post.service;

import static org.junit.jupiter.api.Assertions.*;

import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.post.entity.Post;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Loading