From d1c72b538a713e517c7a8bc55d50eb47049d6bb1 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, 27 Mar 2023 21:39:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=203?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/MangaStore/model/Default/Manga.java | 15 ------- .../app/MangaStore/model/Default/Reader.java | 2 +- .../app/MangaStore/model/Dto/MangaDto.java | 9 ----- .../app/MangaStore/service/MangaService.java | 28 ++++++++++++- .../app/MangaStore/service/ReaderService.java | 8 ++-- .../java/com/LabWork/app/ReMangaTest.java | 39 +++++++++++++++---- 6 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/LabWork/app/MangaStore/model/Default/Manga.java b/src/main/java/com/LabWork/app/MangaStore/model/Default/Manga.java index 4cdeb2b..6d06411 100644 --- a/src/main/java/com/LabWork/app/MangaStore/model/Default/Manga.java +++ b/src/main/java/com/LabWork/app/MangaStore/model/Default/Manga.java @@ -22,12 +22,6 @@ public class Manga { @JoinColumn(name="creator_fk") private Creator creator; - @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name = "mangas_readers", - joinColumns = @JoinColumn(name = "manga_fk"), - inverseJoinColumns = @JoinColumn(name = "reader_fk")) - private List readers; - public Manga() { } @@ -35,7 +29,6 @@ public class Manga { this.creator = creator; this.mangaName = mangaName; this.chapterCount = chapterCount; - this.readers = new ArrayList<>(); } public Long getId() { @@ -66,14 +59,6 @@ public class Manga { return creator; } - public List getReaders() { - return readers; - } - - public void setReaders(List readers) { - this.readers = readers; - } - @Override public String toString() { return "Manga{" + 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 bbba8c6..47d50db 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(mappedBy = "readers", fetch = FetchType.EAGER, cascade = CascadeType.MERGE) + @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE) private List mangas; public Reader() { diff --git a/src/main/java/com/LabWork/app/MangaStore/model/Dto/MangaDto.java b/src/main/java/com/LabWork/app/MangaStore/model/Dto/MangaDto.java index fffdbe4..b022d26 100644 --- a/src/main/java/com/LabWork/app/MangaStore/model/Dto/MangaDto.java +++ b/src/main/java/com/LabWork/app/MangaStore/model/Dto/MangaDto.java @@ -14,16 +14,11 @@ public class MangaDto { private final String mangaName; private final Integer chapterCount; - private final List readers; - public MangaDto(Manga manga) { this.id = manga.getId(); this.creatorId = manga.getCreator().getId(); this.mangaName = manga.getMangaName(); this.chapterCount = manga.getChapterCount(); - this.readers = manga.getReaders().stream() - .map(y -> new String(y.getReaderName())) - .toList(); } public Long getId() { @@ -42,8 +37,4 @@ public class MangaDto { return creatorId; } - public List getReaders() { - return readers; - } - } 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 cbec7fe..d1a9556 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java @@ -2,14 +2,17 @@ package com.LabWork.app.MangaStore.service; import com.LabWork.app.MangaStore.model.Default.Creator; import com.LabWork.app.MangaStore.model.Default.Manga; +import com.LabWork.app.MangaStore.model.Default.Reader; import com.LabWork.app.MangaStore.service.Repository.CreatorRepository; import com.LabWork.app.MangaStore.service.Repository.MangaRepository; import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException; import com.LabWork.app.MangaStore.service.Exception.MangaNotFoundException; +import com.LabWork.app.MangaStore.service.Repository.ReaderRepository; import com.LabWork.app.MangaStore.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -17,11 +20,14 @@ import java.util.Optional; public class MangaService { public final MangaRepository mangaRepository; public final CreatorRepository creatorRepository; + + public final ReaderRepository readerRepository; private final ValidatorUtil validatorUtil; - public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, + public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ReaderRepository readerRepository, ValidatorUtil validatorUtil) { this.mangaRepository = mangaRepository; + this.readerRepository = readerRepository; this.creatorRepository = creatorRepository; this.validatorUtil = validatorUtil; } @@ -38,6 +44,21 @@ public class MangaService { return creator.orElseThrow(() -> new CreatorNotFoundException(id)); } + @Transactional + public List getReader(Long id) { + //em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate(); + //SELECT b FROM Book b WHERE ?1 MEMBER OF b.genres + //final List listReader = em.createQuery("select r from Reader r where " + id + " MEMBER OF r.mangas", Reader.class).getResultList(); + List listReader = new ArrayList<>(); + for (Reader reader : readerRepository.findAll()){ + for (Manga manga : reader.getMangas()){ + if (manga.getId() == id){ + listReader.add(reader); + } + } + } + return listReader; + } @Transactional(readOnly = true) public List findAllMangas() { @@ -65,6 +86,11 @@ public class MangaService { @Transactional public Manga deleteManga(Long id) { final Manga currentManga = findManga(id); + final List listReader = readerRepository.findAll(); + for (Reader reader : listReader){ + reader.getMangas().remove(currentManga); + readerRepository.save(reader); + } mangaRepository.delete(currentManga); return currentManga; } 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 6d53653..9e54222 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java @@ -58,7 +58,7 @@ public class ReaderService { final Reader reader = findReader(readerId); validatorUtil.validate(reader); reader.getMangas().add(manga); - manga.getReaders().add(reader); +/* manga.getReaders().add(reader);*/ return readerRepository.save(reader); } @@ -68,7 +68,7 @@ public class ReaderService { final Reader currentReader = findReader(readerId); final Manga currentManga = findManga(mangaId); currentReader.getMangas().remove(currentManga); - currentManga.getReaders().remove(currentReader); +/* currentManga.getReaders().remove(currentReader);*/ mangaRepository.save(currentManga); return readerRepository.save(currentReader); } @@ -85,9 +85,9 @@ public class ReaderService { @Transactional public Reader deleteReader(Long id) { final Reader currentReader = findReader(id); - for (Manga manga : currentReader.getMangas()){ +/* for (Manga manga : currentReader.getMangas()){ manga.getReaders().remove(currentReader); - } + }*/ readerRepository.delete(currentReader); return currentReader; } diff --git a/src/test/java/com/LabWork/app/ReMangaTest.java b/src/test/java/com/LabWork/app/ReMangaTest.java index 7af7b7c..b0fb8b2 100644 --- a/src/test/java/com/LabWork/app/ReMangaTest.java +++ b/src/test/java/com/LabWork/app/ReMangaTest.java @@ -26,6 +26,29 @@ public class ReMangaTest { private static final Logger log = LoggerFactory.getLogger(ReMangaTest.class); + @Test + void testAddToMangaReader2() { + readerService.deleteAllReaders(); + mangaService.deleteAllMangas(); + creatorService.deleteAllCreators(); + Creator c1 = creatorService.addCreator("first_C", "1"); + Manga m1 = mangaService.addManga(c1.getId(), 0, "vagabond"); + Reader r1 = readerService.addReader("first_R", "1"); + Reader r2 = readerService.addReader("2", "2"); + Reader r3 = readerService.addReader("3", "3"); + + readerService.addManga(m1.getId(), r1.getId()); + readerService.addManga(m1.getId(), r2.getId()); + + log.info(r1.getMangas().toString()); + + Reader r4 = readerService.findReader(r1.getId()); + log.info(r4.getMangas().toString()); + Assertions.assertEquals(2, mangaService.getReader(m1.getId()).size()); + readerService.deleteAllReaders(); + mangaService.deleteAllMangas(); + creatorService.deleteAllCreators(); + } @Test void testCreatorAddAndFind() { readerService.deleteAllReaders(); @@ -219,8 +242,8 @@ public class ReMangaTest { Assertions.assertEquals(1, mangaService.findAllMangas().size()); Assertions.assertEquals(1, r1.getMangas().size()); - mangaService.deleteAllMangas(); readerService.deleteAllReaders(); + mangaService.deleteAllMangas(); creatorService.deleteAllCreators(); } @@ -258,9 +281,9 @@ public class ReMangaTest { Manga m3 = mangaService.findManga(m1.getId()); log.info(r2.getMangas().toString()); Assertions.assertEquals(2, r2.getMangas().size()); - Assertions.assertEquals(1, m3.getReaders().size()); - mangaService.deleteAllMangas(); + //Assertions.assertEquals(1, m3.getReaders().size()); readerService.deleteAllReaders(); + mangaService.deleteAllMangas(); creatorService.deleteAllCreators(); } @@ -304,8 +327,8 @@ public class ReMangaTest { Assertions.assertEquals(1, r12.getMangas().size()); log.info(mangaService.findAllMangas().toString()); log.info(r12.getMangas().toString()); - mangaService.deleteAllMangas(); readerService.deleteAllReaders(); + mangaService.deleteAllMangas(); creatorService.deleteAllCreators(); } @@ -328,8 +351,8 @@ public class ReMangaTest { Manga m11 = mangaService.findManga(m1.getId()); log.info(readerService.findAllReaders().toString()); - log.info(m11.getReaders().toString()); - Assertions.assertEquals(0, m11.getReaders().size()); + //log.info(m11.getReaders().toString()); + //Assertions.assertEquals(0, m11.getReaders().size()); mangaService.deleteAllMangas(); readerService.deleteAllReaders(); creatorService.deleteAllCreators(); @@ -352,9 +375,9 @@ public class ReMangaTest { r11 = readerService.findReader(r11.getId()); m1 = mangaService.findManga(m1.getId()); Assertions.assertNotEquals(r11.getReaderName(), r1.getReaderName()); - Assertions.assertEquals(r11.getReaderName(), m1.getReaders().get(0).getReaderName()); - mangaService.deleteAllMangas(); + //Assertions.assertEquals(r11.getReaderName(), m1.getReaders().get(0).getReaderName()); readerService.deleteAllReaders(); + mangaService.deleteAllMangas(); creatorService.deleteAllCreators(); } }