сделал back но он точно не работает, к тому же нужно переделать тесты

This commit is contained in:
Николай 2023-03-26 15:29:19 +04:00
parent bd51c06084
commit a09eae2e6f
26 changed files with 662 additions and 260 deletions

View File

@ -16,6 +16,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -0,0 +1,33 @@
package com.LabWork.app.MangaStore.Dto;
import com.LabWork.app.MangaStore.model.Creator;
import com.LabWork.app.MangaStore.model.Manga;
import java.util.List;
public class CreatorDto {
private final long id;
private final String creatorName;
private final String hashedPassword;
private final List<Manga> mangas;
public CreatorDto(Creator creator) {
this.id = creator.getId();
this.creatorName = creator.getCreatorName();
this.hashedPassword = creator.getHashedPassword();
this.mangas = creator.getMangas();
}
public long getId() {
return id;
}
public String getCreatorName() {
return creatorName;
}
public String getHashedPassword() {
return hashedPassword;
}
public List<Manga> getMangas() { return mangas; }
}

View File

@ -0,0 +1,46 @@
package com.LabWork.app.MangaStore.Dto;
import com.LabWork.app.MangaStore.model.Creator;
import com.LabWork.app.MangaStore.model.Manga;
import com.LabWork.app.MangaStore.model.Reader;
import java.util.List;
public class MangaDto {
private final Long id;
private final Creator creator;
private final String mangaName;
private final Integer chapterCount;
private final List<Reader> readers;
public MangaDto(Manga manga) {
this.id = manga.getId();
this.creator = manga.getCreator();
this.mangaName = manga.getMangaName();
this.chapterCount = manga.getChapterCount();
this.readers = manga.getReaders();
}
public Long getId() {
return id;
}
public String getMangaName() {
return mangaName;
}
public Integer getChapterCount() {
return chapterCount;
}
public Creator getCreator() {
return creator;
}
public List<Reader> getReaders() {
return readers;
}
}

View File

@ -0,0 +1,33 @@
package com.LabWork.app.MangaStore.Dto;
import com.LabWork.app.MangaStore.model.Manga;
import com.LabWork.app.MangaStore.model.Reader;
import java.util.List;
public class ReaderDto {
private Long id;
private String readerName;
private String hashedPassword;
private List<Manga> mangas;
public ReaderDto(Reader reader) {
this.id = reader.getId();
this.readerName = reader.getReaderName();
this.hashedPassword = reader.getHashedPassword();
this.mangas = reader.getMangas();
}
public Long getId() {
return id;
}
public String getReaderName() { return readerName; }
public String getHashedPassword() { return hashedPassword; }
public List<Manga> getMangas() { return mangas; }
}

View File

@ -0,0 +1,47 @@
package com.LabWork.app.MangaStore.controller;
import com.LabWork.app.MangaStore.Dto.CreatorDto;
import com.LabWork.app.MangaStore.service.CreatorService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/creator")
public class CreatorController {
private final CreatorService creatorService;
public CreatorController(CreatorService creatorService) {
this.creatorService = creatorService;
}
@GetMapping("/{id}")
public CreatorDto getCreator(@PathVariable Long id) {
return new CreatorDto(creatorService.findCreator(id));
}
@GetMapping
public List<CreatorDto> getCreators() {
return creatorService.findAllCreators().stream()
.map(CreatorDto::new)
.toList();
}
@PostMapping
public CreatorDto createCreator(@RequestParam("creatorName") String creatorName,
@RequestParam("password") String password) {
return new CreatorDto(creatorService.addCreator(creatorName, password));
}
@PutMapping("/{id}")
public CreatorDto updateCreator(@PathVariable Long id,
@RequestParam("creatorName") String creatorName,
@RequestParam("password") String password) {
return new CreatorDto(creatorService.updateCreator(id, creatorName, password));
}
@DeleteMapping("/{id}")
public CreatorDto deleteCreator(@PathVariable Long id) {
return new CreatorDto(creatorService.deleteCreator(id));
}
}

View File

@ -0,0 +1,48 @@
package com.LabWork.app.MangaStore.controller;
import com.LabWork.app.MangaStore.Dto.MangaDto;
import com.LabWork.app.MangaStore.model.Creator;
import com.LabWork.app.MangaStore.service.MangaService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/manga")
public class MangaController {
private final MangaService mangaService;
public MangaController(MangaService mangaService) {
this.mangaService = mangaService;
}
@GetMapping("/{id}")
public MangaDto getManga(@PathVariable Long id) {
return new MangaDto(mangaService.findManga(id));
}
@GetMapping
public List<MangaDto> getMangas() {
return mangaService.findAllMangas().stream()
.map(MangaDto::new)
.toList();
}
@PostMapping
public MangaDto createManga(@RequestParam("creatorId") Long creatorId,
@RequestParam("chapterCount") Integer chapterCount,
@RequestParam("mangaName") String mangaName) {
return new MangaDto(mangaService.addManga(creatorId, chapterCount, mangaName));
}
@PutMapping("/{id}")
public MangaDto updateManga(@PathVariable Long id,
@RequestParam("chapterCount") Integer chapterCount) {
return new MangaDto(mangaService.updateManga(id, chapterCount));
}
@DeleteMapping("/{id}")
public MangaDto deleteManga(@PathVariable Long id) {
return new MangaDto(mangaService.deleteManga(id));
}
}

View File

@ -0,0 +1,59 @@
package com.LabWork.app.MangaStore.controller;
import com.LabWork.app.MangaStore.Dto.ReaderDto;
import com.LabWork.app.MangaStore.service.ReaderService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/reader")
public class ReaderController {
private final ReaderService readerService;
public ReaderController(ReaderService readerService) {
this.readerService = readerService;
}
@GetMapping("/{id}")
public ReaderDto getReader(@PathVariable Long id) {
return new ReaderDto(readerService.findReader(id));
}
@GetMapping
public List<ReaderDto> getReaders() {
return readerService.findAllReaders().stream()
.map(ReaderDto::new)
.toList();
}
@PostMapping
public ReaderDto createReader(@RequestParam("readerName") String readerName,
@RequestParam("password") String password) {
return new ReaderDto(readerService.addReader(readerName, password));
}
@PutMapping("/{id}")
public ReaderDto updateReader(@PathVariable Long id,
@RequestParam("readerName") String readerName,
@RequestParam("password") String password) {
return new ReaderDto(readerService.updateReader(id, readerName, password));
}
@PutMapping("/{id}")
public ReaderDto addManga(@PathVariable Long id,
@RequestParam("mangaId") Long mangaId) {
return new ReaderDto(readerService.addManga(mangaId, id));
}
@PutMapping("/{id}")
public ReaderDto removeManga(@PathVariable Long id,
@RequestParam("mangaId") Long mangaId) {
return new ReaderDto(readerService.removeManga(mangaId, id));
}
@DeleteMapping("/{id}")
public ReaderDto deleteReader(@PathVariable Long id) {
return new ReaderDto(readerService.deleteReader(id));
}
}

View File

@ -1,6 +1,8 @@
package com.LabWork.app.student.model;
package com.LabWork.app.MangaStore.model;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -11,9 +13,11 @@ public class Creator {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotBlank(message = "creatorName can't be null or empty")
@Column
private String creatorName;
@NotBlank(message = "hashedPassword can't be null or empty")
@Column
private String hashedPassword;

View File

@ -1,4 +1,4 @@
package com.LabWork.app.student.model;
package com.LabWork.app.MangaStore.model;
import jakarta.persistence.*;

View File

@ -1,4 +1,4 @@
package com.LabWork.app.student.model;
package com.LabWork.app.MangaStore.model;
import jakarta.persistence.*;

View File

@ -0,0 +1,7 @@
package com.LabWork.app.MangaStore.repository;
import com.LabWork.app.MangaStore.model.Creator;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CreatorRepository extends JpaRepository<Creator, Long> {
}

View File

@ -0,0 +1,8 @@
package com.LabWork.app.MangaStore.repository;
import com.LabWork.app.MangaStore.model.Manga;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MangaRepository extends JpaRepository<Manga, Long> {
}

View File

@ -0,0 +1,7 @@
package com.LabWork.app.MangaStore.repository;
import com.LabWork.app.MangaStore.model.Reader;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ReaderRepository extends JpaRepository<Reader, Long> {
}

View File

@ -0,0 +1,82 @@
package com.LabWork.app.MangaStore.service;
import com.LabWork.app.MangaStore.model.Creator;
import com.LabWork.app.MangaStore.repository.CreatorRepository;
import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException;
import com.LabWork.app.MangaStore.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class CreatorService {
private final CreatorRepository creatorRepository;
private final ValidatorUtil validatorUtil;
public CreatorService(CreatorRepository creatorRepository, ValidatorUtil validatorUtil) {
this.creatorRepository = creatorRepository;
this.validatorUtil = validatorUtil;
}
@Transactional(readOnly = true)
public Creator findCreator(Long id) {
final Optional<Creator> creator = creatorRepository.findById(id);
return creator.orElseThrow(() -> new CreatorNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Creator> findAllCreators() { return creatorRepository.findAll(); }
@Transactional
public Creator addCreator(String creatorName, String password) {
final Creator creator = new Creator(creatorName, password);
validatorUtil.validate(creator);
return creatorRepository.save(creator);
}
@Transactional
public Creator updateCreator(Long id, String creatorName, String password) {
final Creator currentCreator = findCreator(id);
currentCreator.setCreatorName(creatorName);
currentCreator.setHashedPassword(password);
validatorUtil.validate(currentCreator);
return creatorRepository.save(currentCreator);
}
@Transactional
public Creator deleteCreator(Long id) {
final Creator currentCreator = findCreator(id);
creatorRepository.delete(currentCreator);
return currentCreator;
}
@Transactional
public void deleteAllCreators() {
creatorRepository.deleteAll();
}
/*
//бесполезная штука
@Transactional
public Creator addManga(Long creatorId, Manga manga) {
final Creator creator = findCreator(creatorId);
creator.getMangas().add(manga);
em.merge(creator);
return creator;
}*/
/* //бесполезная штука
@Transactional
public Manga deleteManga(Long creatorId, Manga manga) {
Creator creator = findCreator(creatorId);
if (creator.getMangas().contains(manga)){
final Manga currentManga = em.createQuery("select m from Manga m where m.id = " + manga.getId(), Manga.class).getSingleResult();
em.remove(currentManga);
return currentManga;
}
return null;
}*/
}

View File

@ -0,0 +1,7 @@
package com.LabWork.app.MangaStore.service.Exception;
public class CreatorNotFoundException extends RuntimeException {
public CreatorNotFoundException(Long id) {
super(String.format("Creator with id [%s] is not found", id));
}
}

View File

@ -0,0 +1,7 @@
package com.LabWork.app.MangaStore.service.Exception;
public class MangaNotFoundException extends RuntimeException {
public MangaNotFoundException(Long id) {
super(String.format("Manga with id [%s] is not found", id));
}
}

View File

@ -0,0 +1,7 @@
package com.LabWork.app.MangaStore.service.Exception;
public class ReaderNotFoundException extends RuntimeException {
public ReaderNotFoundException(Long id) {
super(String.format("Reader with id [%s] is not found", id));
}
}

View File

@ -0,0 +1,75 @@
package com.LabWork.app.MangaStore.service;
import com.LabWork.app.MangaStore.model.Creator;
import com.LabWork.app.MangaStore.model.Manga;
import com.LabWork.app.MangaStore.repository.CreatorRepository;
import com.LabWork.app.MangaStore.repository.MangaRepository;
import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException;
import com.LabWork.app.MangaStore.service.Exception.MangaNotFoundException;
import com.LabWork.app.MangaStore.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class MangaService {
public final MangaRepository mangaRepository;
public final CreatorRepository creatorRepository;
private final ValidatorUtil validatorUtil;
public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ValidatorUtil validatorUtil) {
this.mangaRepository = mangaRepository;
this.creatorRepository = creatorRepository;
this.validatorUtil = validatorUtil;
}
@Transactional(readOnly = true)
public Manga findManga(Long id) {
final Optional<Manga> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
@Transactional(readOnly = true)
public Creator findCreator(Long id) {
final Optional<Creator> creator = creatorRepository.findById(id);
return creator.orElseThrow(() -> new CreatorNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Manga> findAllMangas() {
return mangaRepository.findAll();
}
@Transactional
public Manga addManga(Long creatorId, Integer chapterCount, String mangaName) {
final Creator currentCreator = findCreator(creatorId);
final Manga manga = new Manga(currentCreator, mangaName, chapterCount);
//manga.getCreator().getMangas().add(manga);
//в случае чего можно взять создателя из бд и изменить его здесь же
validatorUtil.validate(manga);
return mangaRepository.save(manga);
}
@Transactional
public Manga updateManga(Long id, Integer chapterCount) {
final Manga currentManga = findManga(id);
currentManga.setChapterCount(chapterCount);
validatorUtil.validate(currentManga);
return mangaRepository.save(currentManga);
}
@Transactional
public Manga deleteManga(Long id) {
final Manga currentManga = findManga(id);
mangaRepository.delete(currentManga);
return currentManga;
}
@Transactional
public void deleteAllMangas() {
mangaRepository.deleteAll();
}
}

View File

@ -0,0 +1,101 @@
package com.LabWork.app.MangaStore.service;
import com.LabWork.app.MangaStore.model.Manga;
import com.LabWork.app.MangaStore.model.Reader;
import com.LabWork.app.MangaStore.repository.MangaRepository;
import com.LabWork.app.MangaStore.repository.ReaderRepository;
import com.LabWork.app.MangaStore.service.Exception.MangaNotFoundException;
import com.LabWork.app.MangaStore.service.Exception.ReaderNotFoundException;
import com.LabWork.app.MangaStore.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Optional;
@Service
public class ReaderService {
private final ReaderRepository readerRepository;
private final MangaRepository mangaRepository;
private final ValidatorUtil validatorUtil;
public ReaderService(ReaderRepository readerRepository, MangaRepository mangaRepository, ValidatorUtil validatorUtil) {
this.readerRepository = readerRepository;
this.mangaRepository = mangaRepository;
this.validatorUtil = validatorUtil;
}
@Transactional
public Reader findReader(Long id) {
final Optional<Reader> reader = readerRepository.findById(id);
return reader.orElseThrow(() -> new ReaderNotFoundException(id));
}
@Transactional(readOnly = true)
public Manga findManga(Long id) {
final Optional<Manga> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Reader> findAllReaders() {
return readerRepository.findAll();
}
@Transactional
public Reader addReader(String readerName, String password) {
final Reader reader = new Reader(readerName, password);
validatorUtil.validate(reader);
return readerRepository.save(reader);
}
//СКОРЕЕ ВСЕГО НЕ БУДЕТ РАБОТАТЬ
@Transactional
public Reader addManga(Long mangaId, Long readerId) {
final Manga manga = findManga(mangaId);
final Reader reader = findReader(readerId);
validatorUtil.validate(reader);
reader.getMangas().add(manga);
manga.getReaders().add(reader);
return readerRepository.save(reader);
}
@Transactional
public Reader 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);
currentManga.getReaders().remove(currentReader);
mangaRepository.save(currentManga);
return readerRepository.save(currentReader);
}
@Transactional
public Reader updateReader(Long id, String readername, String password) {
final Reader currentReader = findReader(id);
currentReader.setReaderName(readername);
currentReader.setHashedPassword(password);
validatorUtil.validate(currentReader);
return readerRepository.save(currentReader);
}
@Transactional
public Reader deleteReader(Long id) {
final Reader currentReader = findReader(id);
/* for (Manga manga : currentReader.getMangas()){
manga.getReaders().remove(currentReader);
}
em.merge(currentReader);
em.remove(currentReader);*/
readerRepository.delete(currentReader);
return currentReader;
}
@Transactional
public void deleteAllReaders() {
readerRepository.deleteAll();
}
}

View File

@ -0,0 +1,42 @@
package com.LabWork.app.MangaStore.util.error;
import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException;
import com.LabWork.app.MangaStore.service.Exception.MangaNotFoundException;
import com.LabWork.app.MangaStore.service.Exception.ReaderNotFoundException;
import com.LabWork.app.MangaStore.util.validation.ValidationException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.util.stream.Collectors;
@ControllerAdvice
public class AdviceController {
@ExceptionHandler({
CreatorNotFoundException.class,
MangaNotFoundException.class,
ReaderNotFoundException.class,
ValidationException.class
})
public ResponseEntity<Object> handleException(Throwable e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
final ValidationException validationException = new ValidationException(
e.getBindingResult().getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toSet()));
return handleException(validationException);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleUnknownException(Throwable e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@ -0,0 +1,9 @@
package com.LabWork.app.MangaStore.util.validation;
import java.util.Set;
public class ValidationException extends RuntimeException {
public ValidationException(Set<String> errors) {
super(String.join("\n", errors));
}
}

View File

@ -0,0 +1,30 @@
package com.LabWork.app.MangaStore.util.validation;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
import java.util.stream.Collectors;
@Component
public class ValidatorUtil {
private final Validator validator;
public ValidatorUtil() {
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
this.validator = factory.getValidator();
}
}
public <T> void validate(T object) {
final Set<ConstraintViolation<T>> errors = validator.validate(object);
if (!errors.isEmpty()) {
throw new ValidationException(errors.stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.toSet()));
}
}
}

View File

@ -1,87 +0,0 @@
package com.LabWork.app.student.service;
import com.LabWork.app.student.model.Creator;
import com.LabWork.app.student.model.Manga;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Service
public class CreatorService {
@PersistenceContext
private EntityManager em;
@Transactional
public Creator findCreator(Long id) {
final Creator creator = em.find(Creator.class, id);
if (creator == null) {
throw new EntityNotFoundException(String.format("Creator with id [%s] is not found", id));
}
return creator;
}
@Transactional
public List<Creator> findAllCreators() {
return em.createQuery("select c from Creator c", Creator.class).getResultList();
}
@Transactional
public Creator addCreator(String creatorName, String password) {
if (!StringUtils.hasText(creatorName) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Creator's creatorName or password is empty");
}
final Creator creator = new Creator(creatorName, password);
em.persist(creator);
return creator;
}
@Transactional
public Creator updateCreator(Long id, String creatorName, String password) {
if (!StringUtils.hasText(creatorName) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Creator's creatorName or password is empty");
}
final Creator customer = findCreator(id);
customer.setCreatorName(creatorName);
customer.setHashedPassword(password);
return em.merge(customer);
}
@Transactional
public Creator deleteCreator(Long id) {
final Creator currentCustomer = findCreator(id);
em.remove(currentCustomer);
return currentCustomer;
}
@Transactional
public void deleteAllCreators() { em.createQuery("delete from Creator").executeUpdate(); }
/*
//бесполезная штука
@Transactional
public Creator addManga(Long creatorId, Manga manga) {
final Creator creator = findCreator(creatorId);
creator.getMangas().add(manga);
em.merge(creator);
return creator;
}*/
/* //бесполезная штука
@Transactional
public Manga deleteManga(Long creatorId, Manga manga) {
Creator creator = findCreator(creatorId);
if (creator.getMangas().contains(manga)){
final Manga currentManga = em.createQuery("select m from Manga m where m.id = " + manga.getId(), Manga.class).getSingleResult();
em.remove(currentManga);
return currentManga;
}
return null;
}*/
}

View File

@ -1,73 +0,0 @@
package com.LabWork.app.student.service;
import com.LabWork.app.student.model.Creator;
import com.LabWork.app.student.model.Manga;
import com.LabWork.app.student.model.Reader;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class MangaService {
@PersistenceContext
private EntityManager em;
@Transactional
public Manga findManga(Long id) {
final Manga manga = em.find(Manga.class, id);
if (manga == null) {
throw new EntityNotFoundException(String.format("Manga with id [%s] is not found", id));
}
return manga;
}
@Transactional
public List<Manga> findAllMangas() {
return em.createQuery("select c from Manga c", Manga.class).getResultList();
}
@Transactional
public Manga addManga(Creator creator, Integer chapterCount, String mangaName) {
if (creator == null) {
throw new IllegalArgumentException("Invalid creator");
}
if (chapterCount < 0 || chapterCount == null) {
throw new IllegalArgumentException("Invalid chapterCount");
}
if (!StringUtils.hasText(mangaName)) {
throw new IllegalArgumentException("Invalid mangaName");
}
final Manga manga = new Manga(creator, mangaName, chapterCount);
manga.getCreator().getMangas().add(manga);
em.persist(manga);
return manga;
}
@Transactional
public Manga updateManga(Long id, Integer chapterCount) {
if (chapterCount < 0 || chapterCount == null) {
throw new IllegalArgumentException("Invalid chapterCount");
}
final Manga manga = findManga(id);
manga.setChapterCount(chapterCount);
em.merge(manga);
return manga;
}
@Transactional
public Manga deleteManga(Long id) {
final Manga currentManga = findManga(id);
em.remove(currentManga);
return currentManga;
}
@Transactional
public void deleteAllMangas() {
em.createQuery("delete from Manga").executeUpdate();
}
}

View File

@ -1,91 +0,0 @@
package com.LabWork.app.student.service;
import com.LabWork.app.student.model.Manga;
import com.LabWork.app.student.model.Reader;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class ReaderService {
@PersistenceContext
private EntityManager em;
@Autowired
private MangaService mangaService;
@Transactional
public Reader findReader(Long id) {
final Reader reader = em.find(Reader.class, id);
if (reader == null) {
throw new EntityNotFoundException(String.format("Reader with id [%s] is not found", id));
}
return reader;
}
@Transactional
public List<Reader> findAllReaders() {
return em.createQuery("select c from Reader c", Reader.class).getResultList();
}
@Transactional
public Reader addReader(String readername, String password) {
if (!StringUtils.hasText(readername) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer's readername or password is empty");
}
final Reader reader = new Reader(readername, password);
em.persist(reader);
return reader;
}
@Transactional
public void addManga(Manga manga, Long readerId) {
final Reader reader = findReader(readerId);
reader.getMangas().add(manga);
manga.getReaders().add(reader);
em.merge(reader);
}
@Transactional
public void removeManga(Manga manga, 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 = em.find(Manga.class, manga.getId());
currentReader.getMangas().remove(currentManga);
currentManga.getReaders().remove(currentReader);
em.merge(currentReader);
em.merge(currentManga);
}
@Transactional
public Reader updateReader(Long id, String readername, String password) {
if (!StringUtils.hasText(readername) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer's readername or password is empty");
}
final Reader reader = findReader(id);
reader.setReaderName(readername);
reader.setHashedPassword(password);
return em.merge(reader);
}
@Transactional
public Reader deleteReader(Long id) {
final Reader currentReader = findReader(id);
for (Manga manga : currentReader.getMangas()){
manga.getReaders().remove(currentReader);
}
em.merge(currentReader);
em.remove(currentReader);
return currentReader;
}
@Transactional
public void deleteAllReaders() {
em.createQuery("delete from Reader").executeUpdate();
}
}

View File

@ -1,11 +1,11 @@
package com.LabWork.app;
import com.LabWork.app.student.model.Creator;
import com.LabWork.app.student.model.Manga;
import com.LabWork.app.student.model.Reader;
import com.LabWork.app.student.service.CreatorService;
import com.LabWork.app.student.service.MangaService;
import com.LabWork.app.student.service.ReaderService;
import com.LabWork.app.MangaStore.model.Creator;
import com.LabWork.app.MangaStore.model.Manga;
import com.LabWork.app.MangaStore.model.Reader;
import com.LabWork.app.MangaStore.service.CreatorService;
import com.LabWork.app.MangaStore.service.MangaService;
import com.LabWork.app.MangaStore.service.ReaderService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;