Skip to content

Commit

Permalink
Merge pull request #78 from Team-FAB/024-users-article
Browse files Browse the repository at this point in the history
024 users article
  • Loading branch information
yjjjwww authored Jun 19, 2023
2 parents 67c9d5b + d9482ba commit d8895d7
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public ResponseEntity<?> getArticle(
return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity();
}

@GetMapping("/users/{userId}")
public ResponseEntity<?> getUserArticles(
@PathVariable int userId
) {
var result = articleService.getUserArticles(userId);
return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity();
}

@PutMapping("/{id}")
public ResponseEntity<?> putArticle(
@AuthenticationPrincipal User user,
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/fab/banggabgo/dto/article/ArticleInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fab.banggabgo.dto.article;

import com.fab.banggabgo.entity.Article;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

private Integer id;
private String title;

public static ArticleInfoDto toDto(Article article) {
return ArticleInfoDto.builder()
.id(article.getId())
.title(article.getTitle())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fab.banggabgo.repository;

import com.fab.banggabgo.dto.article.ArticleInfoDto;
import com.fab.banggabgo.dto.mycontent.FavoriteArticleDto;
import com.fab.banggabgo.dto.mycontent.MyArticleDto;
import com.fab.banggabgo.entity.Article;
Expand All @@ -20,4 +21,6 @@ Page<Article> getArticleByFilter(Pageable pageable, boolean isRecruiting, String
List<MyArticleDto> getMyArticle(User user);

List<FavoriteArticleDto> getFavoriteArticle(User user);

List<ArticleInfoDto> getUserArticle(User user);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fab.banggabgo.repository.impl;

import com.fab.banggabgo.dto.article.ArticleInfoDto;
import com.fab.banggabgo.dto.mycontent.FavoriteArticleDto;
import com.fab.banggabgo.dto.mycontent.MyArticleDto;
import com.fab.banggabgo.entity.Article;
Expand Down Expand Up @@ -105,7 +106,7 @@ public List<MyArticleDto> getMyArticle(User user) {
.stream().map(MyArticleDto::toDto)
.collect(Collectors.toList());
}

public List<FavoriteArticleDto> getFavoriteArticle(User user){

var getMyFavoriteArticleQuery=queryFactory
Expand All @@ -119,4 +120,19 @@ public List<FavoriteArticleDto> getFavoriteArticle(User user){
.stream().map(FavoriteArticleDto::toDto)
.collect(Collectors.toList());
}

public List<ArticleInfoDto> getUserArticle(User user) {

var getUserArticleQuery = queryFactory.selectFrom(qArticle)
.join(qArticle.user, qUser)
.fetchJoin()
.where(qUser.eq(user)
.and(qArticle.isDeleted.eq(false))
.and(qArticle.isRecruiting.eq(true)))
.orderBy(qArticle.createDate.desc());

return getUserArticleQuery.fetch()
.stream().map(ArticleInfoDto::toDto)
.collect(Collectors.toList());
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/fab/banggabgo/service/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fab.banggabgo.dto.apply.ApplyUserResultDto;
import com.fab.banggabgo.dto.article.ArticleEditDto;
import com.fab.banggabgo.dto.article.ArticleInfoDto;
import com.fab.banggabgo.dto.article.ArticlePageDto;
import com.fab.banggabgo.dto.article.ArticleRegisterDto;
import com.fab.banggabgo.entity.User;
Expand All @@ -19,6 +20,11 @@ public interface ArticleService {
*/
ArticlePageDto getArticle(Integer id);

/**
* ์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ฒŒ์‹œ๊ธ€ ๊ฐ€์ ธ์˜ค๊ธฐ
*/
List<ArticleInfoDto> getUserArticles(Integer userId);

/**
* ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fab.banggabgo.common.exception.ErrorCode;
import com.fab.banggabgo.dto.apply.ApplyUserResultDto;
import com.fab.banggabgo.dto.article.ArticleEditDto;
import com.fab.banggabgo.dto.article.ArticleInfoDto;
import com.fab.banggabgo.dto.article.ArticlePageDto;
import com.fab.banggabgo.dto.article.ArticleRegisterDto;
import com.fab.banggabgo.entity.Apply;
Expand All @@ -13,6 +14,7 @@
import com.fab.banggabgo.repository.ApplyRepository;
import com.fab.banggabgo.repository.ArticleRepository;
import com.fab.banggabgo.repository.LikeArticleRepository;
import com.fab.banggabgo.repository.UserRepository;
import com.fab.banggabgo.service.ArticleService;
import com.fab.banggabgo.type.ApproveStatus;
import com.fab.banggabgo.type.Gender;
Expand All @@ -35,6 +37,7 @@ public class ArticleServiceImpl implements ArticleService {
private final ArticleRepository articleRepository;
private final LikeArticleRepository likeArticleRepository;
private final ApplyRepository applyRepository;
private final UserRepository userRepository;

private static final String ADD_LIKE_ARTICLE_SUCCESS = "์ฐœ ๋“ฑ๋ก ์™„๋ฃŒ";
private static final String DELETE_LIKE_ARTICLE_SUCCESS = "์ฐœ ์‚ญ์ œ ์™„๋ฃŒ";
Expand Down Expand Up @@ -98,6 +101,14 @@ public ArticlePageDto getArticle(Integer id) {
return ArticlePageDto.toDto(article);
}

@Override
public List<ArticleInfoDto> getUserArticles(Integer userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_IS_NULL));

return articleRepository.getUserArticle(user);
}

@Override
public void putArticle(User user, Integer id, ArticleEditDto dto) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,41 @@ void getArticleFail_ARTICLE_NOT_EXISTS() throws Exception {
assertThat(errorCode).isEqualTo("ARTICLE_NOT_EXISTS");
}

@Test
@DisplayName("์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ์„ฑ๊ณต")
@WithMockUser
void getUserArticlesSuccess() throws Exception {
//given
//when
//then
mockMvc.perform(get("/api/articles/users/1")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isOk())
.andDo(print());
}

@Test
@DisplayName("์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ : ์œ ์ €๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Œ")
@WithMockUser
void getUserArticlesFail_USER_IS_NULL() throws Exception {
//given
doThrow(new CustomException(ErrorCode.USER_IS_NULL))
.when(articleService)
.getUserArticles(anyInt());

//when
MvcResult result = mockMvc.perform(get("/api/articles/users/1")
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isBadRequest())
.andReturn();

//then
String responseBody = result.getResponse().getContentAsString();
JsonNode responseJson = objectMapper.readTree(responseBody);
String errorCode = responseJson.get("code").asText();
assertThat(errorCode).isEqualTo("USER_IS_NULL");
}

@Test
@DisplayName("๊ธ€ ์ˆ˜์ • ์„ฑ๊ณต")
@WithMockUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fab.banggabgo.common.exception.CustomException;
import com.fab.banggabgo.common.exception.ErrorCode;
import com.fab.banggabgo.dto.article.ArticleEditDto;
import com.fab.banggabgo.dto.article.ArticleInfoDto;
import com.fab.banggabgo.dto.article.ArticlePageDto;
import com.fab.banggabgo.dto.article.ArticleRegisterDto;
import com.fab.banggabgo.entity.Article;
Expand All @@ -22,6 +23,7 @@
import com.fab.banggabgo.repository.ApplyRepository;
import com.fab.banggabgo.repository.ArticleRepository;
import com.fab.banggabgo.repository.LikeArticleRepository;
import com.fab.banggabgo.repository.UserRepository;
import com.fab.banggabgo.type.ActivityTime;
import com.fab.banggabgo.type.ApproveStatus;
import com.fab.banggabgo.type.Gender;
Expand Down Expand Up @@ -56,6 +58,10 @@ class ArticleServiceImplTest {

@Mock
private ApplyRepository applyRepository;

@Mock
private UserRepository userRepository;

@InjectMocks
private ArticleServiceImpl articleService;

Expand Down Expand Up @@ -199,6 +205,46 @@ void getArticleFail_ARTICLE_NOT_EXISTS() {
assertEquals(exception.getMessage(), "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒŒ์‹œ๊ธ€ ์ž…๋‹ˆ๋‹ค.");
}

@Test
@DisplayName("์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ์„ฑ๊ณต")
void getUserArticlesSuccess() {
//given
List<ArticleInfoDto> articleList = new ArrayList<>();

for (int i = 0; i < 5; i++) {
ArticleInfoDto articleInfoDto = ArticleInfoDto.builder()
.id(i + 1)
.title("๊ธ€ ์ œ๋ชฉ" + i)
.build();

articleList.add(articleInfoDto);
}

given(articleRepository.getUserArticle(any()))
.willReturn(articleList);

given(userRepository.findById(anyInt()))
.willReturn(Optional.ofNullable(User.builder().build()));

//when
List<ArticleInfoDto> result = articleService.getUserArticles(1);

//then
assertEquals(5, result.size());
}

@Test
@DisplayName("์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ : ์œ ์ €๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Œ")
void getUserArticlesFail_USER_IS_NULL() {
//given
//when
CustomException exception = assertThrows(CustomException.class,
() -> articleService.getUserArticles(1));

//then
assertEquals(exception.getMessage(), "์œ ์ € ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
}

@Test
@DisplayName("๊ธ€ ์ˆ˜์ • ์„ฑ๊ณต")
void putArticleSuccess() {
Expand Down

0 comments on commit d8895d7

Please sign in to comment.