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] 홈 API 구현 #34

Merged
merged 6 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
76 changes: 76 additions & 0 deletions src/main/java/org/sopt/lequuServer/InitDb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.sopt.lequuServer;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.member.model.Member;
import org.sopt.lequuServer.domain.member.model.SocialPlatform;
import org.sopt.lequuServer.domain.note.model.Note;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
public class InitDb {
Copy link
Contributor

Choose a reason for hiding this comment

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

InitDb 만드는거 귀찮은 작업인데,,, 맛있게 쓰겠습니다,,,

private final InitService initService;

@PostConstruct
public void init() {
initService.dbInit();
}

@Component
@Transactional
@RequiredArgsConstructor
static class InitService {

private final EntityManager em;

@Transactional
public void dbInit() {
Member member = Member.builder()
.socialPlatform(SocialPlatform.KAKAO)
.socialId("dwq4d1sa68xx1qw61")
.build();
em.persist(member);

for (int i = 0; i < 7; i++) {
Book book = Book.builder()
.uuid("dwq65d19asx6qw1c")
.favoriteName(String.valueOf(i))
.favoriteImage("dqw84dsaac9q9")
.title(String.valueOf(i))
.description("test")
.backgroundColor(11)
.member(member)
.isPopular(true)
.build();
em.persist(book);
}

Book book1 = Book.builder()
.uuid("dwq65d19asx6qw1c")
.favoriteName("test")
.favoriteImage("dqw84dsaac9q9")
.title("test")
.description("test")
.backgroundColor(11)
.member(member)
.isPopular(true)
.build();
em.persist(book1);

for (int i = 0; i < 7; i++) {
Note note = Note.builder()
.content(String.valueOf(i))
.background("back")
.textColor(i)
.member(member)
.book(book1)
.build();
em.persist(note);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Book extends BaseTimeEntity {

private int backgroundColor;

private boolean isPopular;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
Expand All @@ -57,18 +59,19 @@ public void addPostedSticker(PostedSticker postedSticker) {
}

@Builder
public Book(String uuid, String favoriteName, String favoriteImage, String title, String description, int backgroundColor, Member member) {
public Book(String uuid, String favoriteName, String favoriteImage, String title, String description, int backgroundColor, Member member, boolean isPopular) {
this.uuid = uuid;
this.favoriteName = favoriteName;
this.favoriteImage = favoriteImage;
this.title = title;
this.description = description;
this.backgroundColor = backgroundColor;
this.member = member;
this.isPopular = isPopular;
}

public static Book of(String uuid, String favoriteName, String favoriteImage, String title, String description, int backgroundColor, Member member) {
return new Book(uuid, favoriteName, favoriteImage, title, description, backgroundColor, member);
public static Book of(String uuid, String favoriteName, String favoriteImage, String title, String description, int backgroundColor, Member member, boolean isPopular) {
return new Book(uuid, favoriteName, favoriteImage, title, description, backgroundColor, member, isPopular);
Comment on lines +62 to +74
Copy link
Contributor

Choose a reason for hiding this comment

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

필드 추가 확인했습니당~

}

// TODO S3 테스트용, 추후 삭제
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.sopt.lequuServer.domain.book.repository;

import java.util.List;
import org.sopt.lequuServer.domain.book.model.Book;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface BookRepository extends JpaRepository<Book, Long> {
@Query(value = "select b from Book b where b.isPopular = :isPopular")
List<Book> getAllByPopular(@Param("isPopular") Boolean isPopular, PageRequest pageRequest);
Copy link
Contributor

Choose a reason for hiding this comment

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

와우 Paging 쓰는거 처음보는데, 신기하군뇨~~

Copy link
Contributor

Choose a reason for hiding this comment

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

[P3] 혹시 여기 Book 뒤에 띄어쓰기 한번 더 들어간거 같은데 수정해주실 수 있나요~?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sopt.lequuServer.domain.book.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.book.repository.BookRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BookService {

private final BookRepository bookRepository;

public List<Book> getPopularBook() {
return bookRepository.getAllByPopular(true, PageRequest.of(0, 6));
Copy link
Contributor

Choose a reason for hiding this comment

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

오호 PageRequest에 대해 새롭게 알고갑니다 !!!! 😁

}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.sopt.lequuServer.domain.common.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.common.dto.response.PopularBookResponseDto;
import org.sopt.lequuServer.domain.common.dto.response.SplashDto;
import org.sopt.lequuServer.domain.common.facade.CommonFacade;
import org.sopt.lequuServer.global.common.dto.ApiResponse;
import org.sopt.lequuServer.global.exception.enums.SuccessType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/api/common")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/common")
public class CommonController {

private final CommonFacade commonFacade;
Expand All @@ -19,4 +23,9 @@ public ApiResponse<SplashDto> getSplash() {
return ApiResponse.success(SuccessType.GET_SPLASH_SUCCESS, commonFacade.getSplash());
}

@GetMapping("/home")
public ApiResponse<List<PopularBookResponseDto>> getHome() {
return ApiResponse.success(SuccessType.GET_HOME_SUCCESS, commonFacade.getHome());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.sopt.lequuServer.domain.common.dto.response;

import org.sopt.lequuServer.domain.book.model.Book;

public record PopularBookResponseDto(
Long bookId,
String bookUuid,
String favoriteName,
String favoriteImage
) {
public static PopularBookResponseDto of(Book book) {
return new PopularBookResponseDto(book.getId(), book.getUuid(), book.getFavoriteName(),
book.getFavoriteImage());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.sopt.lequuServer.domain.common.facade;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.service.BookService;
import org.sopt.lequuServer.domain.common.dto.response.PopularBookResponseDto;
import org.sopt.lequuServer.domain.common.dto.response.SplashDto;
import org.sopt.lequuServer.domain.note.service.NoteService;
import org.springframework.stereotype.Service;
Expand All @@ -10,9 +13,14 @@
public class CommonFacade {

private final NoteService noteService;
private final BookService bookService;

public SplashDto getSplash() {
return SplashDto.of(noteService.getAllNoteCount());
}

public List<PopularBookResponseDto> getHome() {
return bookService.getPopularBook().stream().map(PopularBookResponseDto::of).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "note")
public class Note extends BaseTimeEntity {
public class
Note extends BaseTimeEntity {
Copy link
Contributor

Choose a reason for hiding this comment

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

[P2] 여기 불필요한 엔터가 들어간 것 같아요! 수정 부탁드림당~


@Id
@Column(name = "note_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class SecurityConfig {
private static final String[] AUTH_WHITELIST = {
"/api/kakao/**", "/loading", "/error", "/api/login", "/api/reissue",
"/api/test/**", "/health", "/actuator/health",
"/api/images/**", "/", "/swagger-ui/**", "/swagger-resources/**", "/api-docs/**"
"/api/images/**", "/", "/swagger-ui/**", "/swagger-resources/**", "/api-docs/**",
"/api/common/**"
};

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum SuccessType {

STICKER_PACK_LIST_SUCCESS(HttpStatus.OK, "스티커팩 목록 조회에 성공했습니다."),
GET_SPLASH_SUCCESS(HttpStatus.OK, "스플래시 조회에 성공했습니다."),
GET_HOME_SUCCESS(HttpStatus.OK, "홈 화면 조회에 성공했습니다."),
;

private final HttpStatus httpStatus;
Expand Down
Loading