Skip to content

Commit

Permalink
Merge pull request #69 from Team-FAB/015-Apply
Browse files Browse the repository at this point in the history
015 apply
  • Loading branch information
wonho-seo authored Jun 19, 2023
2 parents d8895d7 + e5ef26d commit d259edd
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -25,6 +27,7 @@
@RequiredArgsConstructor
@RequestMapping("api/my")
@PermitAll
@Validated
public class MyContentController {

private final MyContentService myContentService;
Expand Down Expand Up @@ -70,4 +73,19 @@ public ResponseEntity<?> postMyInfoImage(@AuthenticationPrincipal User user,
return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity();
}

@GetMapping("/from-applicants")
public ResponseEntity<?> getMyFromApplicant(@AuthenticationPrincipal User user,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
var result = myContentService.getMyFromApplicantList(user, page, size);
return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity();
}

@GetMapping("/to-applicants")
public ResponseEntity<?> getMyToApplicantList(@AuthenticationPrincipal User user,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
var result = myContentService.getMyToApplicantList(user, page, size);
return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity();
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/fab/banggabgo/dto/apply/ApplyListResultDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fab.banggabgo.dto.apply;

import com.fab.banggabgo.entity.Apply;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ApplyListResultDto {

Integer applyId;
Integer articleId;
String articleTitle;
Integer otherUserId;
String otherUserName;
String matchStatus;

public static List<ApplyListResultDto> toFromApplicantDtoList(List<Apply> applyPage) {
return applyPage.stream().map(ApplyListResultDto::toFromApplicantDto)
.collect(Collectors.toList());
}

public static ApplyListResultDto toFromApplicantDto(Apply apply) {
return ApplyListResultDto.builder()
.applyId(apply.getId())
.articleId(apply.getArticle().getId())
.articleTitle(apply.getArticle().getTitle())
.otherUserId(apply.getApplicantUser().getId())
.otherUserName(apply.getApplicantUser().getNickname())
.matchStatus(apply.getApproveStatus().getValue())
.build();
}
public static List<ApplyListResultDto> toToApplicantDtoList(List<Apply> applyPage) {
return applyPage.stream().map(ApplyListResultDto::toToApplicantDto)
.collect(Collectors.toList());
}

public static ApplyListResultDto toToApplicantDto(Apply apply) {
return ApplyListResultDto.builder()
.applyId(apply.getId())
.articleId(apply.getArticle().getId())
.articleTitle(apply.getArticle().getTitle())
.otherUserId(apply.getArticle().getUser().getId())
.otherUserName(apply.getArticle().getUser().getNickname())
.matchStatus(apply.getApproveStatus().getValue())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.stereotype.Repository;

@Repository
public interface ApplyRepository extends JpaRepository<Apply, Integer> {
public interface ApplyRepository extends JpaRepository<Apply, Integer>, ApplyRepositoryCustom {

boolean existsByApplicantUserIdAndArticleId(Integer applicantUserId, Integer articleId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fab.banggabgo.repository;

import com.fab.banggabgo.entity.Apply;
import java.util.List;
import org.springframework.data.domain.Pageable;

public interface ApplyRepositoryCustom {

List<Apply> getMyApplicant(Pageable pageable, Integer userId);
List<Apply> getMyToApplicant(Pageable pageable, Integer userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fab.banggabgo.repository.impl;

import com.fab.banggabgo.entity.Apply;
import com.fab.banggabgo.entity.QApply;
import com.fab.banggabgo.repository.ApplyRepositoryCustom;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;

@RequiredArgsConstructor
public class ApplyRepositoryImpl implements ApplyRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

QApply qApply = QApply.apply;

@Override
public List<Apply> getMyApplicant(Pageable pageable, Integer userId) {
var pageQuery = jpaQueryFactory.selectFrom(qApply)
.leftJoin(qApply.applicantUser)
.fetchJoin()
.leftJoin(qApply.article)
.fetchJoin()
.where(qApply.article.user.id.eq(userId))
.orderBy(qApply.lastModifiedDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize());

return pageQuery.fetch();
}
@Override
public List<Apply> getMyToApplicant(Pageable pageable, Integer userId) {
var pageQuery = jpaQueryFactory.selectFrom(qApply)
.leftJoin(qApply.article)
.fetchJoin()
.leftJoin(qApply.article.user)
.where(qApply.applicantUser.id.eq(userId))
.orderBy(qApply.lastModifiedDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize());

return pageQuery.fetch();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/fab/banggabgo/service/MyContentService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.fab.banggabgo.service;

import com.fab.banggabgo.common.exception.CustomException;
import com.fab.banggabgo.dto.apply.ApplyListResultDto;
import com.fab.banggabgo.dto.mycontent.FavoriteArticleDto;
import com.fab.banggabgo.dto.mycontent.MyArticleDto;
import com.fab.banggabgo.dto.mycontent.MyInfoDto;
import com.fab.banggabgo.dto.mycontent.PatchMyInfoDto;
import com.fab.banggabgo.dto.mycontent.PatchMyInfoRequestDto;
import com.fab.banggabgo.dto.mycontent.PatchMyInfoResultDto;
import com.fab.banggabgo.dto.mycontent.PatchMyNicknameRequestDto;
Expand All @@ -29,4 +31,9 @@ PostMyInfoImageResultDto postMyInfoImage(User user, PostMyInfoImageRequestDto dt
throws IOException;
PatchMyInfoResultDto patchMyInfo(User user, PatchMyInfoRequestDto form);

PatchMyInfoResultDto patchMyInfo(User user, PatchMyInfoDto form);

List<ApplyListResultDto> getMyFromApplicantList(User user, Integer page, Integer size);

List<ApplyListResultDto> getMyToApplicantList(User user, Integer page, Integer size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fab.banggabgo.common.exception.CustomException;
import com.fab.banggabgo.common.exception.ErrorCode;
import com.fab.banggabgo.dto.apply.ApplyListResultDto;
import com.fab.banggabgo.dto.mycontent.FavoriteArticleDto;
import com.fab.banggabgo.dto.mycontent.MyArticleDto;
import com.fab.banggabgo.dto.mycontent.MyInfoDto;
Expand All @@ -12,6 +13,7 @@
import com.fab.banggabgo.dto.mycontent.PostMyInfoImageRequestDto;
import com.fab.banggabgo.dto.mycontent.PostMyInfoImageResultDto;
import com.fab.banggabgo.entity.User;
import com.fab.banggabgo.repository.ApplyRepository;
import com.fab.banggabgo.repository.ArticleRepository;
import com.fab.banggabgo.repository.UserRepository;
import com.fab.banggabgo.service.MyContentService;
Expand All @@ -25,6 +27,8 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -33,6 +37,7 @@ public class MyContentServiceImpl implements MyContentService {

private final ArticleRepository articleRepository;
private final UserRepository userRepository;
private final ApplyRepository applyRepository;
private final S3Service s3Service;

@Override
Expand Down Expand Up @@ -98,9 +103,25 @@ public User convertUserData(User user, PatchMyInfoRequestDto dto) {
changed_user.setActivityTime(ActivityTime.fromValue(dto.getActivityTime()));
changed_user.setTag(new HashSet<>(dto.getTags()));
changed_user.setDetail(dto.getDetail());
}catch (Exception e){
} catch (Exception e) {
throw new CustomException(ErrorCode.PATCH_MY_INFO_CONVERT_FAIL);
}
return changed_user;
}

public List<ApplyListResultDto> getMyFromApplicantList(User user, Integer page, Integer size) {
page = page > 0 ? page - 1 : 0;

Pageable pageable = PageRequest.of(page, size);
return ApplyListResultDto.toFromApplicantDtoList(
applyRepository.getMyApplicant(pageable, user.getId()));
}

public List<ApplyListResultDto> getMyToApplicantList(User user, Integer page, Integer size) {
page = page > 0 ? page - 1 : 0;

Pageable pageable = PageRequest.of(page, size);
return ApplyListResultDto.toToApplicantDtoList(
applyRepository.getMyToApplicant(pageable, user.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,78 @@ void patchMyInfo() throws Exception {
//then
}

@Nested
@DisplayName("내가받은 신청자리스트")
class applicantList {

@Test
@DisplayName("신청자리스트 - 성공")
@WithMockUser
void successGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/from-applicants?page=1&size=4")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk());
}
@Test
@DisplayName("신청자리스트 - 성공 페이지 누락")
@WithMockUser
void successLostPageGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/from-applicants?size=4")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk());
}
@Test
@DisplayName("신청자리스트 - 성공 사이즈 누락")
@WithMockUser
void successLostSizeGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/from-applicants?page=1")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk());
}
@Test
@DisplayName("신청자리스트 - 실패 계정 누락")
void FailNonAuthGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/from-applicants?page=1&size=4")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isUnauthorized());
}
}
@Nested
@DisplayName("내가신청한 신청자리스트")
class toApplicantList {

@Test
@DisplayName("신청한리스트 - 성공")
@WithMockUser
void successGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/to-applicants?page=1&size=4")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk());
}
@Test
@DisplayName("신청한리스트 - 성공 페이지 누락")
@WithMockUser
void successLostPageGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/to-applicants?size=4")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk());
}
@Test
@DisplayName("신청한리스트 - 성공 사이즈 누락")
@WithMockUser
void successLostSizeGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/to-applicants?page=1")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk());
}
@Test
@DisplayName("신청한리스트 - 실패 계정 누락")
void FailNonAuthGetApplicantList() throws Exception{
mockMvc.perform(get("/api/my/to-applicants?page=1&size=4")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isUnauthorized());
}

@Test
@DisplayName("이미지 변경 테스트")
@WithMockUser
Expand Down
Loading

0 comments on commit d259edd

Please sign in to comment.