Готовая 3 лабораторная
This commit is contained in:
parent
282f57099b
commit
d1c72b538a
@ -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<Reader> 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<Reader> getReaders() {
|
||||
return readers;
|
||||
}
|
||||
|
||||
public void setReaders(List<Reader> readers) {
|
||||
this.readers = readers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Manga{" +
|
||||
|
@ -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<Manga> mangas;
|
||||
|
||||
public Reader() {
|
||||
|
@ -14,16 +14,11 @@ public class MangaDto {
|
||||
private final String mangaName;
|
||||
private final Integer chapterCount;
|
||||
|
||||
private final List<String> 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<String> getReaders() {
|
||||
return readers;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<Reader> 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<Reader> listReader = em.createQuery("select r from Reader r where " + id + " MEMBER OF r.mangas", Reader.class).getResultList();
|
||||
List<Reader> 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<Manga> findAllMangas() {
|
||||
@ -65,6 +86,11 @@ public class MangaService {
|
||||
@Transactional
|
||||
public Manga deleteManga(Long id) {
|
||||
final Manga currentManga = findManga(id);
|
||||
final List<Reader> listReader = readerRepository.findAll();
|
||||
for (Reader reader : listReader){
|
||||
reader.getMangas().remove(currentManga);
|
||||
readerRepository.save(reader);
|
||||
}
|
||||
mangaRepository.delete(currentManga);
|
||||
return currentManga;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user