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

[BE] 아티클에 대표 이미지를 추가한다. #606

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public ResponseEntity<ArticleResponse> readArticle(@PathVariable("id") Long id)
}

@GetMapping("/articles")
public ResponseEntity<ArticlesDetailPreviewResponse> readArticles() {
return ResponseEntity.ok(articleService.readArticles());
public ResponseEntity<ArticlesPreviewResponse> readArticles() {
return ResponseEntity.ok(articleService.readArticlePreviews());
}

@GetMapping("/articles/latest")
public ResponseEntity<ArticlesPreviewResponse> readLatestArticles() {
return ResponseEntity.ok(articleService.readLatestArticles());
@GetMapping("/articles/detail")
public ResponseEntity<ArticlesDetailPreviewResponse> readArticlesDetail() {
return ResponseEntity.ok(articleService.readArticleDetailPreviews());
}

@DeleteMapping("/articles/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public class Article extends BaseEntity {

private String summary;

public Article(String title, String content, String keyword, String summary) {
private String thumbnail;

public Article(String title, String content, String keyword, String summary, String thumbnail) {
this.title = title;
this.content = content;
this.keyword = keyword;
this.summary = summary;
this.thumbnail = thumbnail;
}

protected Article() {
Expand All @@ -52,6 +55,10 @@ public String getSummary() {
return summary;
}

public String getThumbnail() {
return thumbnail;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -77,6 +84,7 @@ public String toString() {
", content='" + content + '\'' +
", keyword='" + keyword + '\'' +
", summary='" + summary + '\'' +
", thumbnail='" + thumbnail + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import jakarta.validation.constraints.NotBlank;

public record ArticleCreateRequest(@NotBlank(message = "제목을 입력해야 합니다.") String title, String content, String keyword,
String summary) {
String summary, String thumbnail) {

public Article toEntity() {
return new Article(title, content, keyword, summary);
return new Article(title, content, keyword, summary, thumbnail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.bang_ggood.article.domain.Article;
import java.time.LocalDateTime;

public record ArticleDetailPreviewResponse(Long articleId, String title, String keyword, String summary,
public record ArticleDetailPreviewResponse(Long articleId, String title, String keyword, String summary, String thumbnail,
LocalDateTime createdAt) {

public static ArticleDetailPreviewResponse from(Article article) {
Expand All @@ -12,6 +12,7 @@ public static ArticleDetailPreviewResponse from(Article article) {
article.getTitle(),
article.getKeyword(),
article.getSummary(),
article.getThumbnail(),
article.getCreatedAt()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.bang_ggood.article.domain.Article;

public record ArticlePreviewResponse(Long articleId, String title, String keyword) {
public record ArticlePreviewResponse(Long articleId, String title, String keyword, String thumbnail) {

public static ArticlePreviewResponse from(Article article) {
return new ArticlePreviewResponse(
article.getId(),
article.getTitle(),
article.getKeyword()
article.getKeyword(),
article.getThumbnail()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ default Article getById(Long id) {
Optional<Article> findById(@Param("id") Long id);

@Query("SELECT a FROM Article a " +
"WHERE a.deleted = false")
List<Article> findAll();
"WHERE a.deleted = false " +
"ORDER BY a.createdAt DESC ")
List<Article> findLatest();

@Query("SELECT a FROM Article a " +
"WHERE a.deleted = false " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public ArticleResponse readArticle(Long id) {
}

@Transactional(readOnly = true)
public ArticlesDetailPreviewResponse readArticles() {
List<ArticleDetailPreviewResponse> articles = articleRepository.findAll().stream()
public ArticlesDetailPreviewResponse readArticleDetailPreviews() {
List<ArticleDetailPreviewResponse> articles = articleRepository.findLatest().stream()
.map(ArticleDetailPreviewResponse::from)
.toList();
return new ArticlesDetailPreviewResponse(articles);
}

@Transactional(readOnly = true)
public ArticlesPreviewResponse readLatestArticles() {
public ArticlesPreviewResponse readArticlePreviews() {
List<ArticlePreviewResponse> articles = articleRepository.findLatest(MAX_ARTICLE_CARDS).stream()
.map(ArticlePreviewResponse::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.bang_ggood.checklist.dto.response.UserChecklistsPreviewResponse;
import com.bang_ggood.checklist.service.ChecklistManageService;
import com.bang_ggood.checklist.service.ChecklistService;
import com.bang_ggood.question.dto.request.CustomChecklistUpdateRequest;
import com.bang_ggood.question.dto.response.CategoryCustomChecklistQuestionsResponse;
import com.bang_ggood.question.dto.response.ChecklistQuestionsResponse;
import com.bang_ggood.user.domain.User;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -42,17 +45,28 @@ public ResponseEntity<Void> createChecklistLike(@AuthPrincipal User user, @PathV
return ResponseEntity.noContent().build();
}

@GetMapping("/checklists/questions")
public ResponseEntity<ChecklistQuestionsResponse> readChecklistQuestions(@AuthPrincipal User user) {
return ResponseEntity.ok(checklistService.readChecklistQuestions(user));
}

@GetMapping("/checklists/{id}")
public ResponseEntity<SelectedChecklistResponse> readChecklistById(@AuthPrincipal User user,
@PathVariable("id") Long checklistId) {
return ResponseEntity.ok(checklistManageService.readChecklist(user, checklistId));
@PathVariable("id") long id) {
return ResponseEntity.ok(checklistService.readChecklistById(user, id));
Comment on lines +55 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long 타입 아닌가요??

}

@GetMapping("/checklists")
public ResponseEntity<UserChecklistsPreviewResponse> readChecklistsPreview(@AuthPrincipal User user) {
return ResponseEntity.ok(checklistService.readChecklistsPreview(user));
}

@GetMapping("/custom-checklist/all")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이전 작업에서 QuestionController로 분리되지 않았었나요??

public ResponseEntity<CategoryCustomChecklistQuestionsResponse> readAllCustomChecklistQuestions(
@AuthPrincipal User user) {
return ResponseEntity.ok(checklistService.readAllCustomChecklistQuestions(user));
}

@GetMapping("/checklists/like")
public ResponseEntity<UserChecklistsPreviewResponse> readLikedChecklistsPreview(@AuthPrincipal User user) {
return ResponseEntity.ok(checklistService.readLikedChecklistsPreview(user));
Expand All @@ -67,6 +81,13 @@ public ResponseEntity<Void> updateChecklistById(
return ResponseEntity.noContent().build();
}

@PutMapping("/custom-checklist")
public ResponseEntity<Void> updateCustomChecklist(@AuthPrincipal User user,
@RequestBody CustomChecklistUpdateRequest request) {
checklistService.updateCustomChecklist(user, request);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/checklists/{id}")
public ResponseEntity<Void> deleteChecklistById(@AuthPrincipal User user, @PathVariable("id") long id) {
checklistService.deleteChecklistById(user, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import java.util.List;

public record SelectedChecklistResponse(SelectedRoomResponse room,
boolean isLiked,
List<SelectedOptionResponse> options,
List<SelectedCategoryQuestionsResponse> categories,
boolean isLiked) {
public static SelectedChecklistResponse of(SelectedRoomResponse room, List<SelectedOptionResponse> options,
List<SelectedCategoryQuestionsResponse> categories, boolean isLiked) {
return new SelectedChecklistResponse(room, options, categories, isLiked);
}
List<SelectedCategoryQuestionsResponse> categories) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public interface ChecklistRepository extends JpaRepository<Checklist, Long> {
+ "AND c.deleted = false")
Optional<Checklist> findById(@Param("id") long id);

default Checklist getById(@Param("id") Long id) {

default Checklist getById(@Param("id") long id) {
return findById(id).orElseThrow(() -> new BangggoodException(ExceptionCode.CHECKLIST_NOT_FOUND));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.maintenance.domain.ChecklistMaintenance;
import com.bang_ggood.maintenance.domain.MaintenanceItem;
import com.bang_ggood.maintenance.service.ChecklistMaintenanceService;
import com.bang_ggood.option.domain.ChecklistOption;
import com.bang_ggood.option.dto.response.SelectedOptionResponse;
import com.bang_ggood.option.service.ChecklistOptionService;
import com.bang_ggood.question.domain.Answer;
import com.bang_ggood.question.domain.Category;
import com.bang_ggood.question.domain.ChecklistQuestion;
import com.bang_ggood.question.domain.Question;
import com.bang_ggood.question.dto.response.SelectedCategoryQuestionsResponse;
import com.bang_ggood.question.dto.response.SelectedQuestionResponse;
import com.bang_ggood.question.service.ChecklistQuestionService;
import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.dto.response.SelectedRoomResponse;
import com.bang_ggood.room.service.RoomService;
import com.bang_ggood.user.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -47,7 +40,7 @@ public ChecklistManageService(RoomService roomService, ChecklistService checklis
}

@Transactional
public Long createChecklist(User user, ChecklistRequest checklistRequest) {
public long createChecklist(User user, ChecklistRequest checklistRequest) {
Room room = roomService.createRoom(checklistRequest.toRoomEntity());
Checklist checklist = checklistService.createChecklist(checklistRequest.toChecklistEntity(room, user));
createChecklistOptions(checklistRequest, checklist);
Expand Down Expand Up @@ -81,48 +74,4 @@ private void createChecklistMaintenances(ChecklistRequest checklistRequest, Chec
.collect(Collectors.toList());
checklistMaintenanceService.createMaintenances(checklistMaintenances);
}

@Transactional(readOnly = true)
public SelectedChecklistResponse readChecklist(User user, Long checklistId) {
Checklist checklist = checklistService.readChecklist(user, checklistId);

List<Integer> maintenances = readChecklistMaintenances(checklist);
List<SelectedOptionResponse> options = readChecklistOptions(checklist);
List<SelectedCategoryQuestionsResponse> questions = readChecklistQuestions(checklist);
SelectedRoomResponse room = SelectedRoomResponse.of(checklist, maintenances);
boolean isLiked = false; // TODO 좋아요 이후 리팩토링 필요

return SelectedChecklistResponse.of(room, options, questions, isLiked);
}

private List<Integer> readChecklistMaintenances(Checklist checklist) {
return checklistMaintenanceService.readChecklistMaintenances(checklist)
.stream()
.map(ChecklistMaintenance::getMaintenanceItemId)
.toList();
}

private List<SelectedOptionResponse> readChecklistOptions(Checklist checklist) {
return checklistOptionService.readChecklistOptions(checklist)
.stream()
.map(SelectedOptionResponse::from)
.toList();
}

private List<SelectedCategoryQuestionsResponse> readChecklistQuestions(Checklist checklist) {
List<ChecklistQuestion> checklistQuestions = checklistQuestionService.readChecklistQuestions(checklist);

return Arrays.stream(Category.values())
.map(category -> categorizeChecklistQuestions(category, checklistQuestions))
.toList();
}

private SelectedCategoryQuestionsResponse categorizeChecklistQuestions(Category category, List<ChecklistQuestion> checklistQuestions) {
List<SelectedQuestionResponse> selectedQuestionResponse = Question.filter(category, checklistQuestions)
.stream()
.map(SelectedQuestionResponse::new)
.toList();

return SelectedCategoryQuestionsResponse.of(category, selectedQuestionResponse);
}
}
Loading
Loading