From 5e9c107b87ff23f3433b8b0456f490060ba07751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Mon, 10 Apr 2023 20:04:29 +0400 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D1=82?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D0=BE=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/MangaStore/model/Default/Reader.java | 2 +- .../app/MangaStore/service/MangaService.java | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/LabWork/app/MangaStore/model/Default/Reader.java b/src/main/java/com/LabWork/app/MangaStore/model/Default/Reader.java index bd216ab..2e10a77 100644 --- a/src/main/java/com/LabWork/app/MangaStore/model/Default/Reader.java +++ b/src/main/java/com/LabWork/app/MangaStore/model/Default/Reader.java @@ -18,7 +18,7 @@ public class Reader { @Column private String hashedPassword; - @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE) + @ManyToMany(fetch = FetchType.EAGER) private List mangas; public Reader() { diff --git a/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java b/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java index 86a00bd..1df858b 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java @@ -20,16 +20,16 @@ import java.util.Optional; @Service public class MangaService { public final MangaRepository mangaRepository; - public final CreatorService creatorService; + public final CreatorRepository creatorRepository; public final ReaderService readerService; public final ReaderRepository readerRepository; private final ValidatorUtil validatorUtil; - public MangaService(MangaRepository mangaRepository, CreatorService creatorService , ReaderService readerService, ReaderRepository readerRepository, + public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ReaderService readerService, ReaderRepository readerRepository, ValidatorUtil validatorUtil) { this.mangaRepository = mangaRepository; this.readerService = readerService; - this.creatorService = creatorService; + this.creatorRepository = creatorRepository; this.readerRepository = readerRepository; this.validatorUtil = validatorUtil; } @@ -52,10 +52,15 @@ public class MangaService { return mangaRepository.findAll(); } + @Transactional(readOnly = true) + public Creator findCreator(Long id) { + final Optional creator = creatorRepository.findById(id); + return creator.orElseThrow(() -> new CreatorNotFoundException(id)); + } @Transactional public Manga addManga(Long creatorId, Integer chapterCount, String mangaName) { - final Creator currentCreator = creatorService.findCreator(creatorId); + final Creator currentCreator = findCreator(creatorId); final Manga manga = new Manga(currentCreator, mangaName, chapterCount); validatorUtil.validate(manga); return mangaRepository.save(manga); @@ -63,7 +68,7 @@ public class MangaService { @Transactional public Manga addManga(MangaDto mangaDto) { - final Creator currentCreator = creatorService.findCreator(mangaDto.getCreatorId()); + final Creator currentCreator = findCreator(mangaDto.getCreatorId()); final Manga manga = new Manga(currentCreator, mangaDto); validatorUtil.validate(manga); return mangaRepository.save(manga);