diff --git a/data.mv.db b/data.mv.db index 8f2e488..2e60d4c 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/front/src/Main/Catalog.jsx b/front/src/Main/Catalog.jsx index 2f855b3..7d30052 100644 --- a/front/src/Main/Catalog.jsx +++ b/front/src/Main/Catalog.jsx @@ -29,7 +29,7 @@ export default function Catalog() {

Каталог

-

+ {/*

-

+ */}
{mangs.map((manga, index) => ( {manga.mangaName} diff --git a/front/src/Main/MangaPage.jsx b/front/src/Main/MangaPage.jsx index dbc99d3..8e29062 100644 --- a/front/src/Main/MangaPage.jsx +++ b/front/src/Main/MangaPage.jsx @@ -1,10 +1,13 @@ import React, {useState, useEffect} from 'react'; import MangaDto from '../Dto/Manga-Dto'; +import ReaderList from "../components/List/ReaderList"; export default function MangaPage() { const [mangaModel, setMangaModel] = useState(new MangaDto({})); + const [readerData, setReaderData] = useState([]); + const host = "http://localhost:8080"; useEffect(() => { @@ -13,16 +16,36 @@ export default function MangaPage() { const id = urlParams.get('id'); getCreator(id) .then(_data =>setMangaModel(_data)); + getReaderData(id) + .then(_data =>setReaderData(_data)); + console.log(readerData); }, []); const transformer = (mangaModel) => new MangaDto(mangaModel); const url = "manga/"; + const getReaderData = async function (id) { + const requestParams = { + method: "GET", + headers: { + "Content-Type": "application/json", + } + }; + const response = await fetch(host + `/manga/` + id + `/readers`, requestParams); + const _data = await response.json() + console.log(_data); + return _data; + } useEffect(() => { console.log(mangaModel); + console.log(readerData); }, [mangaModel]); + useEffect(() => { + console.log(readerData); + }); + const getCreator = async function (id) { const requestParams = { @@ -37,12 +60,19 @@ export default function MangaPage() { } + const addMangaButton = (e) =>{ + e.preventDefault() + getReaderData(253) + .then(_data =>setReaderData(_data)); + console.log(readerData); + } + return (
{mangaModel.mangaName}/ - +
О манге
@@ -74,6 +104,9 @@ export default function MangaPage() {

Как Ким Кон Чжа же будет использовать эти навыки, чтобы победить конкурентов и подняться на вершину?

+
diff --git a/front/src/Main/ReaderAction.jsx b/front/src/Main/ReaderAction.jsx index 9f4f7fe..09427fc 100644 --- a/front/src/Main/ReaderAction.jsx +++ b/front/src/Main/ReaderAction.jsx @@ -23,6 +23,10 @@ export default function ReaderAction() { const [mangaName, setMangaName] = useState(""); useEffect(() => { + const quryString = window.location.search; + const urlParams = new URLSearchParams(quryString); + const id = urlParams.get('id'); + setReaderId(id); getReaderData() .then(_data =>setReaderData(_data)); getMangaData() diff --git a/front/src/components/List/ReaderList.jsx b/front/src/components/List/ReaderList.jsx new file mode 100644 index 0000000..dd59012 --- /dev/null +++ b/front/src/components/List/ReaderList.jsx @@ -0,0 +1,24 @@ +import { useEffect, useState } from "react"; +import { NavLink } from 'react-router-dom'; + +export default function ReaderList(props) { + + + return ( +
+ { + props.readers?.map((reader) => +
+
+
+ Имя пользователя: {reader.readerName} + +
+
+
+ ) + } +
+ ); +} \ No newline at end of file diff --git a/src/main/java/com/LabWork/app/MangaStore/controller/MangaController.java b/src/main/java/com/LabWork/app/MangaStore/controller/MangaController.java index 0641d96..58f8724 100644 --- a/src/main/java/com/LabWork/app/MangaStore/controller/MangaController.java +++ b/src/main/java/com/LabWork/app/MangaStore/controller/MangaController.java @@ -2,6 +2,7 @@ package com.LabWork.app.MangaStore.controller; import com.LabWork.app.MangaStore.model.Dto.MangaReaderDto; +import com.LabWork.app.MangaStore.model.Dto.ReaderMangaDto; import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto; import com.LabWork.app.MangaStore.service.MangaService; import jakarta.validation.Valid; @@ -29,6 +30,13 @@ public class MangaController { .map(x -> new MangaReaderDto(x, mangaService.getReader(x.getId()))) .toList(); } + + @GetMapping("/{id}/readers") + public List getReaders(@PathVariable Long id) { + return mangaService.getReader(id).stream() + .map(x -> new ReaderMangaDto(x)) + .toList(); + } /* @PostMapping public MangaReaderDto createManga(@RequestParam("creatorId") Long creatorId, diff --git a/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java b/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java index 239d836..bcde466 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/CreatorService.java @@ -3,6 +3,7 @@ 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.Exception.MangaNotFoundException; import com.LabWork.app.MangaStore.service.Repository.CreatorRepository; import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException; import com.LabWork.app.MangaStore.service.Repository.MangaRepository; @@ -16,18 +17,15 @@ import java.util.Optional; @Service public class CreatorService { private final CreatorRepository creatorRepository; - private final ReaderRepository readerRepository; - - private final MangaService mangaService; - + private final MangaRepository mangaRepository; private final ValidatorUtil validatorUtil; - public CreatorService(CreatorRepository creatorRepository, MangaService mangaService, ReaderRepository readerRepository, + public CreatorService(CreatorRepository creatorRepository, MangaRepository mangaRepository, ReaderRepository readerRepository, ValidatorUtil validatorUtil) { this.creatorRepository = creatorRepository; this.readerRepository = readerRepository; - this.mangaService = mangaService; + this.mangaRepository = mangaRepository; this.validatorUtil = validatorUtil; } @@ -57,12 +55,30 @@ public class CreatorService { return creatorRepository.save(currentCreator); } + @Transactional(readOnly = true) + public List findAllMangas() { + return mangaRepository.findAll(); + } + + @Transactional(readOnly = true) + public Manga findManga(Long id) { + final Optional manga = mangaRepository.findById(id); + return manga.orElseThrow(() -> new MangaNotFoundException(id)); + } + @Transactional + public List getReader(Long id) { + final Manga currentManga = findManga(id); + final List listReader = mangaRepository.getReaders(currentManga); + return listReader; + } + @Transactional public Creator deleteCreator(Long id) { final Creator currentCreator = findCreator(id); - List listManga = currentCreator.getMangas();mangaService.findAllMangas(); + List listManga = currentCreator.getMangas(); + findAllMangas(); for (Manga manga : listManga){ - for (final Reader reader :mangaService.getReader(manga.getId())){ + for (final Reader reader :getReader(manga.getId())){ reader.getMangas().remove(manga); readerRepository.save(reader); } diff --git a/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java b/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java index 4b47271..abd898a 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/MangaService.java @@ -21,15 +21,16 @@ import java.util.Optional; public class MangaService { public final MangaRepository mangaRepository; public final CreatorRepository creatorRepository; - + public final ReaderService readerService; public final ReaderRepository readerRepository; private final ValidatorUtil validatorUtil; - public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ReaderRepository readerRepository, + public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ReaderService readerService, ReaderRepository readerRepository, ValidatorUtil validatorUtil) { this.mangaRepository = mangaRepository; - this.readerRepository = readerRepository; + this.readerService = readerService; this.creatorRepository = creatorRepository; + this.readerRepository = readerRepository; this.validatorUtil = validatorUtil; } @@ -39,25 +40,10 @@ public class MangaService { return manga.orElseThrow(() -> new MangaNotFoundException(id)); } - @Transactional(readOnly = true) - public Creator findCreator(Long id) { - final Optional creator = creatorRepository.findById(id); - return creator.orElseThrow(() -> new CreatorNotFoundException(id)); - } - @Transactional public List 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 listReader = em.createQuery("select r from Reader r where " + id + " MEMBER OF r.mangas", Reader.class).getResultList(); - List listReader = new ArrayList<>(); - for (Reader reader : readerRepository.findAll()){ - for (Manga manga : reader.getMangas()){ - if (manga.getId() == id){ - listReader.add(reader); - } - } - } + final Manga currentManga = findManga(id); + final List listReader = mangaRepository.getReaders(currentManga); return listReader; } @@ -66,6 +52,12 @@ public class MangaService { return mangaRepository.findAll(); } + @Transactional(readOnly = true) + public Creator findCreator(Long id) { + final Optional creator = creatorRepository.findById(id); + return creator.orElseThrow(() -> new CreatorNotFoundException(id)); + } + @Transactional public Manga addManga(Long creatorId, Integer chapterCount, String mangaName) { final Creator currentCreator = findCreator(creatorId); @@ -106,7 +98,7 @@ public class MangaService { @Transactional public Manga deleteManga(Long id) { final Manga currentManga = findManga(id); - final List listReader = readerRepository.findAll(); + final List listReader = readerService.findAllReaders(); for (Reader reader : listReader){ reader.getMangas().remove(currentManga); readerRepository.save(reader); diff --git a/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java b/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java index 33c4609..a24f5f6 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/ReaderService.java @@ -16,7 +16,6 @@ import java.util.Optional; @Service public class ReaderService { private final ReaderRepository readerRepository; - private final MangaRepository mangaRepository; private final ValidatorUtil validatorUtil; @@ -33,12 +32,6 @@ public class ReaderService { return reader.orElseThrow(() -> new ReaderNotFoundException(id)); } - @Transactional(readOnly = true) - public Manga findManga(Long id) { - final Optional manga = mangaRepository.findById(id); - return manga.orElseThrow(() -> new MangaNotFoundException(id)); - } - @Transactional(readOnly = true) public List findAllReaders() { return readerRepository.findAll(); @@ -51,7 +44,11 @@ public class ReaderService { return readerRepository.save(reader); } - //СКОРЕЕ ВСЕГО НЕ БУДЕТ РАБОТАТЬ + @Transactional(readOnly = true) + public Manga findManga(Long id) { + final Optional manga = mangaRepository.findById(id); + return manga.orElseThrow(() -> new MangaNotFoundException(id)); + } @Transactional public Manga addManga(Long mangaId, Long readerId) { final Manga manga = findManga(mangaId); diff --git a/src/main/java/com/LabWork/app/MangaStore/service/Repository/MangaRepository.java b/src/main/java/com/LabWork/app/MangaStore/service/Repository/MangaRepository.java index f115309..bab5f03 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/Repository/MangaRepository.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/Repository/MangaRepository.java @@ -1,8 +1,14 @@ package com.LabWork.app.MangaStore.service.Repository; import com.LabWork.app.MangaStore.model.Default.Manga; +import com.LabWork.app.MangaStore.model.Default.Reader; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; public interface MangaRepository extends JpaRepository { + @Query("select r from Reader r where :manga MEMBER OF r.mangas") + List getReaders(Manga manga); } diff --git a/src/main/java/com/LabWork/app/MangaStore/service/Repository/ReaderRepository.java b/src/main/java/com/LabWork/app/MangaStore/service/Repository/ReaderRepository.java index 1863ba6..3d4c54c 100644 --- a/src/main/java/com/LabWork/app/MangaStore/service/Repository/ReaderRepository.java +++ b/src/main/java/com/LabWork/app/MangaStore/service/Repository/ReaderRepository.java @@ -2,6 +2,9 @@ package com.LabWork.app.MangaStore.service.Repository; import com.LabWork.app.MangaStore.model.Default.Reader; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; public interface ReaderRepository extends JpaRepository { } diff --git a/src/main/java/com/LabWork/app/WebConfiguration.java b/src/main/java/com/LabWork/app/WebConfiguration.java index ee0671c..fefcfad 100644 --- a/src/main/java/com/LabWork/app/WebConfiguration.java +++ b/src/main/java/com/LabWork/app/WebConfiguration.java @@ -7,6 +7,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -18,16 +19,12 @@ public class WebConfiguration implements WebMvcConfigurer { } @Override public void addViewControllers(ViewControllerRegistry registry) { - registry.addViewController("/notFound").setViewName("forward:/index.html"); - } + ViewControllerRegistration registration = registry.addViewController("/notFound"); + registration.setViewName("forward:/index.html"); + registration.setStatusCode(HttpStatus.OK); - - @Bean - public WebServerFactoryCustomizer containerCustomizer() { - return container -> { - container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, - "/notFound")); - }; +// Alternative way (404 error hits the console): +// > registry.addViewController("/notFound").setViewName("forward:/index.html"); } } \ No newline at end of file