From 1117bed0cd3d3db1138f07dee387e45075259c7f 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 19:58:58 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B1=D0=B5=D0=B7=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=BE=D0=B2=20=D0=BD=D0=BE=20=D1=81=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=D0=BC=20for?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MangaStore/service/CreatorService.java | 3 +- .../app/MangaStore/service/MangaService.java | 31 +++------------- .../app/MangaStore/service/ReaderService.java | 35 ++++++++++++++++++- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java b/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java index 7cbd632..1d67e4a 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java @@ -55,13 +55,14 @@ public class CreatorService { return creatorRepository.save(currentCreator); } + @Transactional public Creator deleteCreator(Long id) { final Creator currentCreator = findCreator(id); List listManga = currentCreator.getMangas(); for (Manga manga : listManga){ - for (final Reader reader :getReader(manga.getId())){ + for (final Reader reader :mangaService.getReader(manga.getId())){ reader.getMangas().remove(manga); } } 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 d55b06b..86a00bd 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 CreatorRepository creatorRepository ; + public final CreatorService creatorService; public final ReaderService readerService; public final ReaderRepository readerRepository; private final ValidatorUtil validatorUtil; - public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository , ReaderService readerService, ReaderRepository readerRepository, + public MangaService(MangaRepository mangaRepository, CreatorService creatorService , ReaderService readerService, ReaderRepository readerRepository, ValidatorUtil validatorUtil) { this.mangaRepository = mangaRepository; this.readerService = readerService; - this.creatorRepository = creatorRepository ; + this.creatorService = creatorService; this.readerRepository = readerRepository; this.validatorUtil = validatorUtil; } @@ -99,19 +99,7 @@ public class MangaService { return currentManga; } - @Transactional - public Manga addMangaToReader(Long mangaId, Long readerId) { - final Manga manga = findManga(mangaId); - final Reader reader = readerService.findReader(readerId); - validatorUtil.validate(reader); - if (reader.getMangas().contains(manga)) - { - return null; - } - reader.getMangas().add(manga); - readerRepository.save(reader); - return manga; - } + /* @Transactional public Manga addManga(Long mangaId, Long readerId) { @@ -119,16 +107,7 @@ public class MangaService { readerService.addManga(readerId, List.of(manga)); return manga; }*/ - @Transactional - public Manga removeMangaToReader(Long mangaId, Long readerId) { - //em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate(); - final Reader currentReader = readerService.findReader(readerId); - final Manga currentManga = findManga(mangaId); - currentReader.getMangas().remove(currentManga); - mangaRepository.save(currentManga); - readerRepository.save(currentReader); - return currentManga; - } + @Transactional public void deleteAllMangas() { diff --git a/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java b/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java index 7d531e5..aeee239 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java @@ -16,10 +16,12 @@ import java.util.Optional; @Service public class ReaderService { private final ReaderRepository readerRepository; + private final MangaRepository mangaRepository; private final ValidatorUtil validatorUtil; - public ReaderService(ReaderRepository readerRepository, ValidatorUtil validatorUtil) { + public ReaderService(ReaderRepository readerRepository, ValidatorUtil validatorUtil, MangaRepository mangaRepository) { this.readerRepository = readerRepository; + this.mangaRepository = mangaRepository; this.validatorUtil = validatorUtil; } @@ -66,4 +68,35 @@ public class ReaderService { public void deleteAllReaders() { readerRepository.deleteAll(); } + + @Transactional(readOnly = true) + public Manga findManga(Long id) { + final Optional manga = mangaRepository.findById(id); + return manga.orElseThrow(() -> new MangaNotFoundException(id)); + } + + @Transactional + public Manga addManga(Long mangaId, Long readerId) { + final Manga manga = findManga(mangaId); + final Reader reader = findReader(readerId); + validatorUtil.validate(reader); + if (reader.getMangas().contains(manga)) + { + return null; + } + reader.getMangas().add(manga); + readerRepository.save(reader); + return manga; + } + + @Transactional + public Manga removeManga(Long mangaId, Long readerId) { + //em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate(); + final Reader currentReader = findReader(readerId); + final Manga currentManga = findManga(mangaId); + currentReader.getMangas().remove(currentManga); + mangaRepository.save(currentManga); + readerRepository.save(currentReader); + return currentManga; + } }