This commit is contained in:
Katerine881 2023-04-10 17:41:00 +04:00
parent c73632f42f
commit f132446fff
12 changed files with 129 additions and 49 deletions

Binary file not shown.

View File

@ -29,7 +29,7 @@ export default function Catalog() {
<Banner /> <Banner />
<div className = "catalog_wrapper"> <div className = "catalog_wrapper">
<h1>Каталог</h1> <h1>Каталог</h1>
<h2> {/* <h2>
<select> <select>
<option value="1">По рейтингу</option> <option value="1">По рейтингу</option>
<option value="2">По лайкам</option> <option value="2">По лайкам</option>
@ -40,7 +40,7 @@ export default function Catalog() {
<option value="7">Рандом</option> <option value="7">Рандом</option>
</select> </select>
<button type="button" className="btn btn-dark">&#8593;&#8595;</button> <button type="button" className="btn btn-dark">&#8593;&#8595;</button>
</h2> </h2> */}
<div className="p-2 d-flex flex-wrap"> <div className="p-2 d-flex flex-wrap">
{mangs.map((manga, index) => ( {mangs.map((manga, index) => (
<NavLink key={manga.id} to={`/mangapage?id=${manga.id}`}><img src={manga.image} alt={manga.mangaName} className="slideshow"/></NavLink> <NavLink key={manga.id} to={`/mangapage?id=${manga.id}`}><img src={manga.image} alt={manga.mangaName} className="slideshow"/></NavLink>

View File

@ -1,10 +1,13 @@
import React, {useState, useEffect} from 'react'; import React, {useState, useEffect} from 'react';
import MangaDto from '../Dto/Manga-Dto'; import MangaDto from '../Dto/Manga-Dto';
import ReaderList from "../components/List/ReaderList";
export default function MangaPage() { export default function MangaPage() {
const [mangaModel, setMangaModel] = useState(new MangaDto({})); const [mangaModel, setMangaModel] = useState(new MangaDto({}));
const [readerData, setReaderData] = useState([]);
const host = "http://localhost:8080"; const host = "http://localhost:8080";
useEffect(() => { useEffect(() => {
@ -13,16 +16,36 @@ export default function MangaPage() {
const id = urlParams.get('id'); const id = urlParams.get('id');
getCreator(id) getCreator(id)
.then(_data =>setMangaModel(_data)); .then(_data =>setMangaModel(_data));
getReaderData(id)
.then(_data =>setReaderData(_data));
console.log(readerData);
}, []); }, []);
const transformer = (mangaModel) => new MangaDto(mangaModel); const transformer = (mangaModel) => new MangaDto(mangaModel);
const url = "manga/"; 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(() => { useEffect(() => {
console.log(mangaModel); console.log(mangaModel);
console.log(readerData);
}, [mangaModel]); }, [mangaModel]);
useEffect(() => {
console.log(readerData);
});
const getCreator = async function (id) { const getCreator = async function (id) {
const requestParams = { 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 ( return (
<main className="p-3"> <main className="p-3">
<div className="container d-flex" > <div className="container d-flex" >
<div className="d-flex flex-column"> <div className="d-flex flex-column">
<img className="img_style01" style={{borderRadius: "3%"}}src={mangaModel.image} alt={mangaModel.mangaName}/> <img className="img_style01" style={{borderRadius: "3%"}}src={mangaModel.image} alt={mangaModel.mangaName}/>
<button type="button" className="btn btn-primary mt-3">Добавить в избранное</button> <button type="button" onClick={addMangaButton} className="btn btn-primary mt-3">Добавить в избранное</button>
</div> </div>
<div className="container table text-white fs-4 ms-4"> <div className="container table text-white fs-4 ms-4">
<div className="row text-white fw-bold fs-3">О манге</div> <div className="row text-white fw-bold fs-3">О манге</div>
@ -74,6 +104,9 @@ export default function MangaPage() {
<p>Как Ким Кон Чжа же будет использовать эти навыки, чтобы победить конкурентов и подняться на вершину?</p> <p>Как Ким Кон Чжа же будет использовать эти навыки, чтобы победить конкурентов и подняться на вершину?</p>
</div> </div>
</div> </div>
<ReaderList
readers={mangaModel.readers}
/>
</div> </div>
</div> </div>
</main> </main>

View File

@ -23,6 +23,10 @@ export default function ReaderAction() {
const [mangaName, setMangaName] = useState(""); const [mangaName, setMangaName] = useState("");
useEffect(() => { useEffect(() => {
const quryString = window.location.search;
const urlParams = new URLSearchParams(quryString);
const id = urlParams.get('id');
setReaderId(id);
getReaderData() getReaderData()
.then(_data =>setReaderData(_data)); .then(_data =>setReaderData(_data));
getMangaData() getMangaData()

View File

@ -0,0 +1,24 @@
import { useEffect, useState } from "react";
import { NavLink } from 'react-router-dom';
export default function ReaderList(props) {
return (
<div className="row table-responsive text-white">
{
props.readers?.map((reader) =>
<div key={reader.id} className="d-flex flex-row flex-wrap flex-grow-1 align-items-center mt-3">
<div>
<div className="pt-3 description d-flex flex-column justify-content-start mb-3 fs-6 fw-bold">
<NavLink className="text-white fs-5 fw-bold pt-3 mb-3"
to={`/readeraction?id=${reader.id}`}>Имя пользователя: {reader.readerName}
</NavLink>
</div>
</div>
</div>
)
}
</div>
);
}

View File

@ -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.MangaReaderDto;
import com.LabWork.app.MangaStore.model.Dto.ReaderMangaDto;
import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto; import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto;
import com.LabWork.app.MangaStore.service.MangaService; import com.LabWork.app.MangaStore.service.MangaService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -29,6 +30,13 @@ public class MangaController {
.map(x -> new MangaReaderDto(x, mangaService.getReader(x.getId()))) .map(x -> new MangaReaderDto(x, mangaService.getReader(x.getId())))
.toList(); .toList();
} }
@GetMapping("/{id}/readers")
public List<ReaderMangaDto> getReaders(@PathVariable Long id) {
return mangaService.getReader(id).stream()
.map(x -> new ReaderMangaDto(x))
.toList();
}
/* /*
@PostMapping @PostMapping
public MangaReaderDto createManga(@RequestParam("creatorId") Long creatorId, public MangaReaderDto createManga(@RequestParam("creatorId") Long creatorId,

View File

@ -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.Creator;
import com.LabWork.app.MangaStore.model.Default.Manga; import com.LabWork.app.MangaStore.model.Default.Manga;
import com.LabWork.app.MangaStore.model.Default.Reader; 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.Repository.CreatorRepository;
import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException; import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException;
import com.LabWork.app.MangaStore.service.Repository.MangaRepository; import com.LabWork.app.MangaStore.service.Repository.MangaRepository;
@ -16,18 +17,15 @@ import java.util.Optional;
@Service @Service
public class CreatorService { public class CreatorService {
private final CreatorRepository creatorRepository; private final CreatorRepository creatorRepository;
private final ReaderRepository readerRepository; private final ReaderRepository readerRepository;
private final MangaRepository mangaRepository;
private final MangaService mangaService;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public CreatorService(CreatorRepository creatorRepository, MangaService mangaService, ReaderRepository readerRepository, public CreatorService(CreatorRepository creatorRepository, MangaRepository mangaRepository, ReaderRepository readerRepository,
ValidatorUtil validatorUtil) { ValidatorUtil validatorUtil) {
this.creatorRepository = creatorRepository; this.creatorRepository = creatorRepository;
this.readerRepository = readerRepository; this.readerRepository = readerRepository;
this.mangaService = mangaService; this.mangaRepository = mangaRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
} }
@ -57,12 +55,30 @@ public class CreatorService {
return creatorRepository.save(currentCreator); return creatorRepository.save(currentCreator);
} }
@Transactional(readOnly = true)
public List<Manga> findAllMangas() {
return mangaRepository.findAll();
}
@Transactional(readOnly = true)
public Manga findManga(Long id) {
final Optional<Manga> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
@Transactional
public List<Reader> getReader(Long id) {
final Manga currentManga = findManga(id);
final List<Reader> listReader = mangaRepository.getReaders(currentManga);
return listReader;
}
@Transactional @Transactional
public Creator deleteCreator(Long id) { public Creator deleteCreator(Long id) {
final Creator currentCreator = findCreator(id); final Creator currentCreator = findCreator(id);
List<Manga> listManga = currentCreator.getMangas();mangaService.findAllMangas(); List<Manga> listManga = currentCreator.getMangas();
findAllMangas();
for (Manga manga : listManga){ for (Manga manga : listManga){
for (final Reader reader :mangaService.getReader(manga.getId())){ for (final Reader reader :getReader(manga.getId())){
reader.getMangas().remove(manga); reader.getMangas().remove(manga);
readerRepository.save(reader); readerRepository.save(reader);
} }

View File

@ -21,15 +21,16 @@ import java.util.Optional;
public class MangaService { public class MangaService {
public final MangaRepository mangaRepository; public final MangaRepository mangaRepository;
public final CreatorRepository creatorRepository; public final CreatorRepository creatorRepository;
public final ReaderService readerService;
public final ReaderRepository readerRepository; public final ReaderRepository readerRepository;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ReaderRepository readerRepository, public MangaService(MangaRepository mangaRepository, CreatorRepository creatorRepository, ReaderService readerService, ReaderRepository readerRepository,
ValidatorUtil validatorUtil) { ValidatorUtil validatorUtil) {
this.mangaRepository = mangaRepository; this.mangaRepository = mangaRepository;
this.readerRepository = readerRepository; this.readerService = readerService;
this.creatorRepository = creatorRepository; this.creatorRepository = creatorRepository;
this.readerRepository = readerRepository;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
} }
@ -39,25 +40,10 @@ public class MangaService {
return manga.orElseThrow(() -> new MangaNotFoundException(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 @Transactional
public List<Reader> getReader(Long id) { public List<Reader> getReader(Long id) {
//em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate(); final Manga currentManga = findManga(id);
//SELECT b FROM Book b WHERE ?1 MEMBER OF b.genres final List<Reader> listReader = mangaRepository.getReaders(currentManga);
//final List<Reader> listReader = em.createQuery("select r from Reader r where " + id + " MEMBER OF r.mangas", Reader.class).getResultList();
List<Reader> listReader = new ArrayList<>();
for (Reader reader : readerRepository.findAll()){
for (Manga manga : reader.getMangas()){
if (manga.getId() == id){
listReader.add(reader);
}
}
}
return listReader; return listReader;
} }
@ -66,6 +52,12 @@ public class MangaService {
return mangaRepository.findAll(); return mangaRepository.findAll();
} }
@Transactional(readOnly = true)
public Creator findCreator(Long id) {
final Optional<Creator> creator = creatorRepository.findById(id);
return creator.orElseThrow(() -> new CreatorNotFoundException(id));
}
@Transactional @Transactional
public Manga addManga(Long creatorId, Integer chapterCount, String mangaName) { public Manga addManga(Long creatorId, Integer chapterCount, String mangaName) {
final Creator currentCreator = findCreator(creatorId); final Creator currentCreator = findCreator(creatorId);
@ -106,7 +98,7 @@ public class MangaService {
@Transactional @Transactional
public Manga deleteManga(Long id) { public Manga deleteManga(Long id) {
final Manga currentManga = findManga(id); final Manga currentManga = findManga(id);
final List<Reader> listReader = readerRepository.findAll(); final List<Reader> listReader = readerService.findAllReaders();
for (Reader reader : listReader){ for (Reader reader : listReader){
reader.getMangas().remove(currentManga); reader.getMangas().remove(currentManga);
readerRepository.save(reader); readerRepository.save(reader);

View File

@ -16,7 +16,6 @@ import java.util.Optional;
@Service @Service
public class ReaderService { public class ReaderService {
private final ReaderRepository readerRepository; private final ReaderRepository readerRepository;
private final MangaRepository mangaRepository; private final MangaRepository mangaRepository;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
@ -33,12 +32,6 @@ public class ReaderService {
return reader.orElseThrow(() -> new ReaderNotFoundException(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) @Transactional(readOnly = true)
public List<Reader> findAllReaders() { public List<Reader> findAllReaders() {
return readerRepository.findAll(); return readerRepository.findAll();
@ -51,7 +44,11 @@ public class ReaderService {
return readerRepository.save(reader); return readerRepository.save(reader);
} }
//СКОРЕЕ ВСЕГО НЕ БУДЕТ РАБОТАТЬ @Transactional(readOnly = true)
public Manga findManga(Long id) {
final Optional<Manga> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
@Transactional @Transactional
public Manga addManga(Long mangaId, Long readerId) { public Manga addManga(Long mangaId, Long readerId) {
final Manga manga = findManga(mangaId); final Manga manga = findManga(mangaId);

View File

@ -1,8 +1,14 @@
package com.LabWork.app.MangaStore.service.Repository; package com.LabWork.app.MangaStore.service.Repository;
import com.LabWork.app.MangaStore.model.Default.Manga; 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.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface MangaRepository extends JpaRepository<Manga, Long> { public interface MangaRepository extends JpaRepository<Manga, Long> {
@Query("select r from Reader r where :manga MEMBER OF r.mangas")
List<Reader> getReaders(Manga manga);
} }

View File

@ -2,6 +2,9 @@ package com.LabWork.app.MangaStore.service.Repository;
import com.LabWork.app.MangaStore.model.Default.Reader; import com.LabWork.app.MangaStore.model.Default.Reader;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface ReaderRepository extends JpaRepository<Reader, Long> { public interface ReaderRepository extends JpaRepository<Reader, Long> {
} }

View File

@ -7,6 +7,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistry; 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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -18,16 +19,12 @@ public class WebConfiguration implements WebMvcConfigurer {
} }
@Override @Override
public void addViewControllers(ViewControllerRegistry registry) { 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);
// Alternative way (404 error hits the console):
@Bean // > registry.addViewController("/notFound").setViewName("forward:/index.html");
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
return container -> {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
"/notFound"));
};
} }
} }