переделанная лаба, нужно ещё доделать

This commit is contained in:
Николай 2023-03-15 23:27:03 +04:00
parent 2f10c85693
commit afea5a0b50
5 changed files with 120 additions and 15 deletions

View File

@ -69,4 +69,13 @@ public class Creator {
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Creator{" +
"id=" + id +
", creatorName='" + creatorName + '\'' +
", hashedPassword='" + hashedPassword + '\'' +
'}';
}
}

View File

@ -22,10 +22,10 @@ public class Manga {
@JoinColumn(name="creator_fk")
private Creator creator;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "mangs_readers",
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinTable(name = "mangas_readers",
joinColumns = @JoinColumn(name = "manga_fk"),
inverseJoinColumns = @JoinColumn(name = "user_fk"))
inverseJoinColumns = @JoinColumn(name = "reader_fk"))
private List<Reader> readers;
public Manga() {
@ -70,6 +70,14 @@ public class Manga {
this.readers = readers;
}
@Override
public String toString() {
return "Manga{" +
"id=" + id +
", firstName='" + mangaName + '\'' +
", lastName='" + chapterCount + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -18,6 +18,7 @@ public class Reader {
@Column
private String hashedPassword;
@ManyToMany(mappedBy = "readers", fetch = FetchType.EAGER)
private List<Manga> mangas;
@ -60,4 +61,13 @@ public class Reader {
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Reader{" +
"id=" + id +
", readerName='" + readerName + '\'' +
", hashedPassword='" + hashedPassword + '\'' +
'}';
}
}

View File

@ -41,15 +41,16 @@ public class ReaderService {
}
@Transactional
public void addManga(Manga manga, Reader reader) {
if (manga == null || reader == null) {
throw new IllegalArgumentException("manga or reader null");
}
public void addManga(Manga manga, Long readerId) {
final Reader reader = findReader(readerId);
reader.getMangas().add(manga);
em.merge(manga);
manga.getReaders().add(reader);
em.merge(reader);
em.merge(manga);
}
@Transactional
public void removeManga(Manga manga, Long readerId) {
em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate();
}
@Transactional
@ -60,14 +61,14 @@ public class ReaderService {
final Reader reader = findReader(id);
reader.setReaderName(readername);
reader.setHashedPassword(password);
return reader;
return em.merge(reader);
}
@Transactional
public Reader deleteReader(Long id) {
final Reader currentCustomer = findReader(id);
em.remove(currentCustomer);
return currentCustomer;
final Reader currentReader = findReader(id);
em.remove(currentReader);
return currentReader;
}
@Transactional

View File

@ -96,6 +96,81 @@ public class ReMangaTest {
}
@Test
void testAddToMangaReader() {
readerService.deleteAllReaders();
mangaService.deleteAllMangs();
creatorService.deleteAllCreators();
Creator c1 = creatorService.addCreator("first_C", "1");
Manga m1 = mangaService.addManga(c1, 0, "vagabond");
Manga m2 = mangaService.addManga(c1, 10, "Berserk");
Reader r1 = readerService.addReader("first_R", "1");
log.info(r1.getMangas().size() + "");
readerService.addManga(m1, r1.getId());
log.info(r1.getMangas().size() + "");
readerService.addManga(m2, r1.getId());
log.info(r1.getMangas().toString());
Reader r2 = readerService.findReader(r1.getId());
log.info(r2.getMangas().toString());
Assertions.assertEquals(2, r2.getMangas().size());
Assertions.assertEquals(1, m1.getReaders().size());
}
@Test
void testRemoveToMangaReader() {
readerService.deleteAllReaders();
mangaService.deleteAllMangs();
creatorService.deleteAllCreators();
Creator c1 = creatorService.addCreator("first_C", "1");
Manga m1 = mangaService.addManga(c1, 0, "Vagabond");
Manga m2 = mangaService.addManga(c1, 10, "Berserk");
Reader r1 = readerService.addReader("first_R", "1");
log.info(r1.getMangas().size() + "");
readerService.addManga(m1, r1.getId());
log.info(r1.getMangas().size() + "");
readerService.addManga(m2, r1.getId());
log.info(r1.getMangas().toString());
Reader r11 = readerService.findReader(r1.getId());
log.info(r11.getMangas().toString());
Assertions.assertEquals(2, r11.getMangas().size());
Assertions.assertEquals(1, m1.getReaders().size());
Reader r2 = readerService.addReader("second_R", "2");
readerService.addManga(m1, r2.getId());
readerService.addManga(m2, r2.getId());
Reader r22 = readerService.findReader(r2.getId());
Assertions.assertEquals(2, r22.getMangas().size());
Assertions.assertEquals(2, m1.getReaders().size());
log.info(r22.getMangas().toString());
readerService.removeManga(m1, r22.getId());
log.info(r22.getMangas().toString());
Reader r23 = readerService.findReader(r22.getId());
log.info(r23.getMangas().toString());
Manga m11 = mangaService.findManga(m1.getId());
log.info(m11.getReaders().toString());
log.info(m2.getReaders().toString());
Assertions.assertEquals(1, r23.getMangas().size());
log.info(mangaService.findAllMangas().toString());
readerService.deleteAllReaders();
mangaService.deleteAllMangs();
creatorService.deleteAllCreators();
}
@Test
void testReader() {
readerService.deleteAllReaders();
@ -113,10 +188,12 @@ public class ReMangaTest {
Reader r1 = readerService.addReader("first_R", "1");
Reader r2 = readerService.addReader("second_R", "2");
readerService.addManga(m1, r1);
readerService.addManga(m2, r2);
readerService.addManga(m1, r1.getId());
readerService.addManga(m2, r2.getId());
r1 = readerService.findReader(r1.getId());
log.info(r1.getMangas().get(0).getCreator().toString());
log.info(c1.toString());
r2 = readerService.findReader(r2.getId());
Assertions.assertEquals(c1, r1.getMangas().get(0).getCreator());
Assertions.assertEquals(c2, r2.getMangas().get(0).getCreator());