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

독후감 리스트 반환 api #98

Merged
merged 9 commits into from
Apr 20, 2024
Merged
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 @@ -3,16 +3,16 @@
import com.mindway.server.v2.domain.book.presentation.dto.request.BookUpdateRequest;
import com.mindway.server.v2.domain.book.presentation.dto.request.BookWriteRequest;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookInfoResponse;
import com.mindway.server.v2.domain.book.service.BookDeleteService;
import com.mindway.server.v2.domain.book.service.BookUpdateService;
import com.mindway.server.v2.domain.book.service.BookWriteService;
import com.mindway.server.v2.domain.book.service.GetBookService;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookListResponse;
import com.mindway.server.v2.domain.book.service.*;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/book")
Expand All @@ -22,6 +22,7 @@ public class BookController {
private final BookDeleteService bookDeleteService;
private final BookUpdateService bookUpdateService;
private final GetBookService getBookService;
private final GetBookListService getBookListService;

@PostMapping
public ResponseEntity<Void> writeBookReport (@RequestBody @Valid BookWriteRequest bookWriteRequest) {
Expand All @@ -47,4 +48,10 @@ public ResponseEntity<BookInfoResponse> getDetailBookReport (@PathVariable(value
BookInfoResponse bookInfoResponse = getBookService.execute(id);
return ResponseEntity.ok(bookInfoResponse);
}

@GetMapping
public ResponseEntity<List<BookListResponse>> getBookReportList () {
List<BookListResponse> bookList = getBookListService.execute();
return ResponseEntity.ok(bookList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mindway.server.v2.domain.book.presentation.dto.response;

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;
import java.util.List;

@Builder
@Getter
public class BookListResponse {
private Long id;
private String title;
private String plot;
private LocalDate date;
}

Copy link
Member

Choose a reason for hiding this comment

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

date를 String말구 LocalDate로 하는 건 어떤가요??
그러면 BookConverter에서 굳이 형식을 지정하지 않아도 되고 간결해질 거 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

5ad0aa6

수정했습니다


Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.mindway.server.v2.domain.book.repository;

import com.mindway.server.v2.domain.book.entity.Book;
import com.mindway.server.v2.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookRepository extends JpaRepository<Book, Long> {

List<Book> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mindway.server.v2.domain.book.service;

import com.mindway.server.v2.domain.book.presentation.dto.response.BookListResponse;

import java.util.List;

public interface GetBookListService {
List<BookListResponse> execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mindway.server.v2.domain.book.service.impl;

import com.mindway.server.v2.domain.book.entity.Book;
import com.mindway.server.v2.domain.book.exception.NotSameAuthorException;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookListResponse;
import com.mindway.server.v2.domain.book.repository.BookRepository;
import com.mindway.server.v2.domain.book.service.GetBookListService;
import com.mindway.server.v2.domain.book.util.BookConverter;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.util.UserUtil;
import com.mindway.server.v2.global.annotation.ServiceWithReadOnlyTransaction;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@ServiceWithReadOnlyTransaction
@RequiredArgsConstructor
public class GetBookListServiceImpl implements GetBookListService {
private final UserUtil userUtil;
private final BookRepository bookRepository;
private final BookConverter bookConverter;

public List<BookListResponse> execute() {
User user = userUtil.getCurrentUser();
List<Book> books = bookRepository.findAllByUser(user);

if (user != books.get(0).getUser()) {
throw new NotSameAuthorException();
}

return books.stream()
.map(bookConverter::toListDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import com.mindway.server.v2.domain.book.entity.Book;
import com.mindway.server.v2.domain.book.presentation.dto.request.BookWriteRequest;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookInfoResponse;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookListResponse;
import com.mindway.server.v2.domain.user.entity.User;

import java.util.List;

public interface BookConverter {
Book toEntity (BookWriteRequest bookWriteRequest, User user);

BookInfoResponse toDto (Book book);

BookListResponse toListDto (Book book);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.mindway.server.v2.domain.book.entity.Book;
import com.mindway.server.v2.domain.book.presentation.dto.request.BookWriteRequest;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookInfoResponse;
import com.mindway.server.v2.domain.book.presentation.dto.response.BookListResponse;
import com.mindway.server.v2.domain.book.util.BookConverter;
import com.mindway.server.v2.domain.user.entity.User;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
public class BookConverterImpl implements BookConverter {

Expand All @@ -24,4 +28,13 @@ public BookInfoResponse toDto(Book book) {
.plot(book.getPlot())
.build();
}

public BookListResponse toListDto(Book book) {
return BookListResponse.builder()
Copy link
Member

Choose a reason for hiding this comment

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

@Override 없애도 될거같아용

Copy link
Contributor Author

Choose a reason for hiding this comment

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

c3ed979

수정했습니다

.id(book.getId())
.title(book.getTitle())
.plot(book.getPlot())
.date(LocalDate.from(book.getCreateAt()))
.build();
}
Copy link
Member

Choose a reason for hiding this comment

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

public BookListResponse toListDto(Book book) {
        return BookListResponse.builder()
                .id(book.getId())
                .title(book.getTitle())
                .plot(book.getPlot())
                .date(LocalDate.from(book.getCreateAt()))
                .build();
    }

이렇게 수정하면 될거같아요오

Copy link
Contributor Author

Choose a reason for hiding this comment

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

c0378e3

수정했습니다

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.PrePersist;
import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -11,6 +12,7 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(HttpMethod.DELETE, "/api/v2/book/{book_id}").authenticated()
.requestMatchers(HttpMethod.PATCH, "/api/v2/book/{book_id}").authenticated()
.requestMatchers(HttpMethod.GET, "/api/v2/book/{book_id}").authenticated()
.requestMatchers(HttpMethod.GET, "/api/v2/book").authenticated()

// notice
.requestMatchers(HttpMethod.POST, "/api/v2/notice").hasAnyAuthority(Authority.ROLE_TEACHER.name(), Authority.ROLE_HELPER.name())
Expand Down
Loading