без запросов но с использованием for

This commit is contained in:
Николай 2023-04-10 19:58:58 +04:00
parent 0d7ee43b26
commit 1117bed0cd
3 changed files with 41 additions and 28 deletions

View File

@ -55,13 +55,14 @@ public class CreatorService {
return creatorRepository.save(currentCreator);
}
@Transactional
public Creator deleteCreator(Long id) {
final Creator currentCreator = findCreator(id);
List<Manga> 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);
}
}

View File

@ -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() {

View File

@ -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> 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;
}
}