Compare commits
4 Commits
LabWork06R
...
LabWork03
Author | SHA1 | Date | |
---|---|---|---|
ccb905e20a | |||
69a85da85a | |||
|
83673a42b8 | ||
3bc5b4440e |
@ -15,6 +15,7 @@ repositories {
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'com.h2database:h2:2.1.210'
|
||||
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -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() {
|
||||
@ -62,14 +55,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{" +
|
||||
|
@ -19,7 +19,7 @@ public class Reader {
|
||||
private String hashedPassword;
|
||||
|
||||
|
||||
@ManyToMany(mappedBy = "readers", fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
private List<Manga> mangas;
|
||||
|
||||
public Reader() {
|
||||
|
@ -59,9 +59,25 @@ public class MangaService {
|
||||
return manga;
|
||||
}
|
||||
|
||||
@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 Manga currentManga = em.find(Manga.class, id);
|
||||
final List<Reader> listReader = em.createQuery("select r from Reader r where :currentManga MEMBER OF r.mangas", Reader.class)
|
||||
.setParameter("currentManga", currentManga)
|
||||
.getResultList();
|
||||
return listReader;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Manga deleteManga(Long id) {
|
||||
final Manga currentManga = findManga(id);
|
||||
final List<Reader> listReader = em.createQuery("select c from Reader c", Reader.class).getResultList();
|
||||
for (Reader reader : listReader){
|
||||
reader.getMangas().remove(currentManga);
|
||||
em.merge(reader);
|
||||
}
|
||||
em.remove(currentManga);
|
||||
return currentManga;
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ public class ReaderService {
|
||||
public void addManga(Manga manga, Long readerId) {
|
||||
final Reader reader = findReader(readerId);
|
||||
reader.getMangas().add(manga);
|
||||
manga.getReaders().add(reader);
|
||||
//manga.getReaders().add(reader);
|
||||
em.merge(reader);
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ public class ReaderService {
|
||||
final Reader currentReader = findReader(readerId);
|
||||
final Manga currentManga = em.find(Manga.class, manga.getId());
|
||||
currentReader.getMangas().remove(currentManga);
|
||||
currentManga.getReaders().remove(currentReader);
|
||||
//currentManga.getReaders().remove(currentReader);
|
||||
em.merge(currentReader);
|
||||
em.merge(currentManga);
|
||||
}
|
||||
@ -76,9 +76,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);
|
||||
}
|
||||
}*/
|
||||
em.merge(currentReader);
|
||||
em.remove(currentReader);
|
||||
return currentReader;
|
||||
|
@ -13,6 +13,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class ReMangaTest {
|
||||
@Autowired
|
||||
@ -26,6 +28,33 @@ 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, 0, "vagabond");
|
||||
Reader r1 = readerService.addReader("first_R", "1");
|
||||
Reader r2 = readerService.addReader("2", "2");
|
||||
Reader r3 = readerService.addReader("3", "3");
|
||||
|
||||
readerService.addManga(m1, r1.getId());
|
||||
readerService.addManga(m1, r2.getId());
|
||||
|
||||
log.info(r1.getMangas().toString());
|
||||
|
||||
Reader r4 = readerService.findReader(r1.getId());
|
||||
log.info(r4.getMangas().toString());
|
||||
List<Reader> listReader = mangaService.getReader(m1.getId());
|
||||
log.info(listReader.toString());
|
||||
Assertions.assertEquals(true, mangaService.getReader(m1.getId()).contains(r4));
|
||||
Assertions.assertEquals(true, mangaService.getReader(m1.getId()).contains(r2));
|
||||
readerService.deleteAllReaders();
|
||||
mangaService.deleteAllMangas();
|
||||
creatorService.deleteAllCreators();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreatorAddAndFind() {
|
||||
readerService.deleteAllReaders();
|
||||
@ -201,7 +230,7 @@ public class ReMangaTest {
|
||||
Reader r2 = readerService.findReader(r1.getId());
|
||||
log.info(r2.getMangas().toString());
|
||||
Assertions.assertEquals(2, r2.getMangas().size());
|
||||
Assertions.assertEquals(1, m1.getReaders().size());
|
||||
Assertions.assertEquals(1, mangaService.getReader(m1.getId()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -261,8 +290,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(mangaService.getReader(m11.getId()).toString());
|
||||
Assertions.assertEquals(0, mangaService.getReader(m11.getId()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -282,6 +311,6 @@ 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());
|
||||
Assertions.assertEquals(r11.getReaderName(), mangaService.getReader(m1.getId()).get(0).getReaderName());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user