Skip to content

Commit

Permalink
#33 - Feat: GET /api/v1/myBooks (내 도서 리스트 조회) 구현, response.MyBookDto,…
Browse files Browse the repository at this point in the history
… ProductDto 추가
  • Loading branch information
ahah525 committed Nov 8, 2022
1 parent 24e0b94 commit c23e7a8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.mutbooks.app.api.myBooks;

import com.example.mutbooks.app.base.dto.RsData;
import com.example.mutbooks.app.member.entity.Member;
import com.example.mutbooks.app.mybook.dto.response.MyBookDto;
import com.example.mutbooks.app.mybook.service.MyBookService;
import com.example.mutbooks.app.security.dto.MemberContext;
import com.example.mutbooks.util.Ut;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/myBooks")
@RequiredArgsConstructor
public class MyBooksApiController {
private final MyBookService myBookService;

// 내 도서 리스트
@GetMapping("")
public ResponseEntity<RsData> list(@AuthenticationPrincipal MemberContext memberContext) {
Member member = memberContext.getMember();
List<MyBookDto> myBookDtos = myBookService.findAllByOwner(member);

return Ut.spring.responseEntityOf(
RsData.successOf(Ut.mapOf("myBooks", myBookDtos))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.mutbooks.app.mybook.dto.response;

import com.example.mutbooks.app.mybook.entity.MyBook;
import com.example.mutbooks.app.product.dto.response.ProductDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@AllArgsConstructor
public class MyBookDto {
private Long id;
private LocalDateTime createDate;
private LocalDateTime modifyDate;
private Long ownerId;
private ProductDto product;

public static MyBookDto toDto(MyBook myBook) {
ProductDto productDto = ProductDto.toDto(myBook.getProduct());

return MyBookDto.builder()
.id(myBook.getId())
.createDate(myBook.getCreateDate())
.modifyDate(myBook.getUpdateDate())
.ownerId(myBook.getOwner().getId())
.product(productDto)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.example.mutbooks.app.mybook.repository;

import com.example.mutbooks.app.member.entity.Member;
import com.example.mutbooks.app.mybook.entity.MyBook;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface MyBookRepository extends JpaRepository<MyBook, Long> {
Optional<MyBook> findByProductIdAndOwnerId(Long productId, Long ownerId);

void deleteByProductIdAndOwnerId(Long productId, Long ownerId);

List<MyBook> findByOwner(Member owner);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.mutbooks.app.mybook.service;

import com.example.mutbooks.app.member.entity.Member;
import com.example.mutbooks.app.mybook.dto.response.MyBookDto;
import com.example.mutbooks.app.mybook.entity.MyBook;
import com.example.mutbooks.app.mybook.repository.MyBookRepository;
import com.example.mutbooks.app.order.entity.Order;
Expand All @@ -10,6 +11,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand Down Expand Up @@ -51,4 +55,14 @@ public void remove(Order order) {
public MyBook findByProductIdAndOwnerId(long productId, long ownerId) {
return myBookRepository.findByProductIdAndOwnerId(productId, ownerId).orElse(null);
}

public List<MyBookDto> findAllByOwner(Member owner) {
List<MyBook> myBooks = myBookRepository.findByOwner(owner);

List<MyBookDto> myBookDtos = myBooks.stream()
.map(myBook -> MyBookDto.toDto(myBook))
.collect(Collectors.toList());

return myBookDtos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.mutbooks.app.product.dto.response;

import com.example.mutbooks.app.product.entity.Product;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@AllArgsConstructor
public class ProductDto {
private Long id;
private LocalDateTime createDate;
private LocalDateTime modifyDate;
private Long authorId;
private String authorName;
private String subject;

public static ProductDto toDto(Product product) {
return ProductDto.builder()
.id(product.getId())
.createDate(product.getCreateDate())
.modifyDate(product.getUpdateDate())
.authorId(product.getAuthor().getId())
.authorName(product.getAuthor().getNickname())
.subject(product.getSubject())
.build();
}
}

0 comments on commit c23e7a8

Please sign in to comment.