msa : monolithic - get webook chapter list

This commit is contained in:
haerong22
2021-07-29 16:59:16 +09:00
parent bcc07b6e7e
commit 4f899cca59
5 changed files with 60 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
package com.monolithicdemo.controller;
import com.monolithicdemo.model.dto.WebBookChapterDto;
import com.monolithicdemo.model.dto.WebBookDto;
import com.monolithicdemo.model.form.RegisterReaderForm;
import com.monolithicdemo.service.ReaderService;
@@ -27,4 +28,11 @@ public class ReaderController {
public ResponseEntity<List<WebBookDto>> getWebBookList() {
return ResponseEntity.ok().body(webBookService.getWebBookList());
}
@GetMapping("/{readerId}/webBook/{webBookId}/chapter")
public ResponseEntity<List<WebBookChapterDto>> getWebBookChapterList(
@PathVariable(value = "readerId") Long readerId,
@PathVariable(value = "webBookId") Long webBookId) {
return ResponseEntity.ok().body(webBookService.getWebBookChapterList(readerId, webBookId));
}
}

View File

@@ -1,5 +1,6 @@
package com.monolithicdemo.model.dto;
import com.monolithicdemo.model.entity.WebBookChapter;
import lombok.*;
import java.time.LocalDateTime;
@@ -11,11 +12,19 @@ import java.time.LocalDateTime;
@NoArgsConstructor
public class WebBookChapterDto {
private Long webBookChapterId;
private Long webBookId;
private String name;
private String detail;
private LocalDateTime createdAt;
private Integer price;
private Boolean isPaid;
public static WebBookChapterDto from(WebBookChapter webBookChapter) {
return WebBookChapterDto.builder()
.webBookChapterId(webBookChapter.getWebBookChapterId())
.name(webBookChapter.getName())
.detail(webBookChapter.getDetail())
.createdAt(webBookChapter.getCreatedAt())
.price(webBookChapter.getPrice())
.build();
}
}

View File

@@ -20,6 +20,8 @@ public class ReaderWebBookPayment {
private Long webBookChapterId;
private Long readerId;
private Integer paymentAmount;
private LocalDateTime createdAt;

View File

@@ -0,0 +1,13 @@
package com.monolithicdemo.repository;
import com.monolithicdemo.model.entity.ReaderWebBookPayment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ReaderWebBookPaymentRepository extends JpaRepository<ReaderWebBookPayment, Long> {
List<ReaderWebBookPayment> findAllByReaderId(Long readerId);
}

View File

@@ -1,6 +1,10 @@
package com.monolithicdemo.service;
import com.monolithicdemo.model.dto.WebBookChapterDto;
import com.monolithicdemo.model.dto.WebBookDto;
import com.monolithicdemo.model.entity.ReaderWebBookPayment;
import com.monolithicdemo.repository.ReaderWebBookPaymentRepository;
import com.monolithicdemo.repository.WebBookChapterRepository;
import com.monolithicdemo.repository.WebBookRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -13,10 +17,32 @@ import java.util.stream.Collectors;
public class WebBookService {
private final WebBookRepository webBookRepository;
private final WebBookChapterRepository webBookChapterRepository;
private final ReaderWebBookPaymentRepository readerWebBookPaymentRepository;
public List<WebBookDto> getWebBookList() {
return webBookRepository.findAll().stream()
.map(webBook -> WebBookDto.from(webBook))
.collect(Collectors.toList());
}
public List<WebBookChapterDto> getWebBookChapterList(Long readerId, Long webBookId) {
List<ReaderWebBookPayment> readerWebBookPayments = readerWebBookPaymentRepository.findAllByReaderId(readerId);
List<WebBookChapterDto> webBookChapterDtos = webBookChapterRepository.findAllById(List.of(webBookId)).stream()
.map(WebBookChapterDto::from)
.collect(Collectors.toList());
readerWebBookPayments.forEach(readerWebBookPayment -> {
WebBookChapterDto webBookChapterDto = webBookChapterDtos.stream().filter(dto -> dto.getWebBookChapterId()
.equals(readerWebBookPayment.getWebBookChapterId())).findFirst().get();
if (webBookChapterDto != null) {
webBookChapterDto.setIsPaid(true);
} else {
webBookChapterDto.setIsPaid(false);
}
});
return webBookChapterDtos;
}
}