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 />
<div className = "catalog_wrapper">
<h1>Каталог</h1>
<h2>
{/* <h2>
<select>
<option value="1">По рейтингу</option>
<option value="2">По лайкам</option>
@ -40,7 +40,7 @@ export default function Catalog() {
<option value="7">Рандом</option>
</select>
<button type="button" className="btn btn-dark">&#8593;&#8595;</button>
</h2>
</h2> */}
<div className="p-2 d-flex flex-wrap">
{mangs.map((manga, index) => (
<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 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 (
<main className="p-3">
<div className="container d-flex" >
<div className="d-flex flex-column">
<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 className="container table text-white fs-4 ms-4">
<div className="row text-white fw-bold fs-3">О манге</div>
@ -74,6 +104,9 @@ export default function MangaPage() {
<p>Как Ким Кон Чжа же будет использовать эти навыки, чтобы победить конкурентов и подняться на вершину?</p>
</div>
</div>
<ReaderList
readers={mangaModel.readers}
/>
</div>
</div>
</main>

View File

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

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.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<ReaderMangaDto> getReaders(@PathVariable Long id) {
return mangaService.getReader(id).stream()
.map(x -> new ReaderMangaDto(x))
.toList();
}
/*
@PostMapping
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.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<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
public Creator deleteCreator(Long id) {
final Creator currentCreator = findCreator(id);
List<Manga> listManga = currentCreator.getMangas();mangaService.findAllMangas();
List<Manga> 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);
}

View File

@ -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> creator = creatorRepository.findById(id);
return creator.orElseThrow(() -> new CreatorNotFoundException(id));
}
@Transactional
public List<Reader> 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<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);
}
}
}
final Manga currentManga = findManga(id);
final List<Reader> 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> 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<Reader> listReader = readerRepository.findAll();
final List<Reader> listReader = readerService.findAllReaders();
for (Reader reader : listReader){
reader.getMangas().remove(currentManga);
readerRepository.save(reader);

View File

@ -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> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Reader> 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> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
@Transactional
public Manga addManga(Long mangaId, Long readerId) {
final Manga manga = findManga(mangaId);

View File

@ -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<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 org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
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.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<ConfigurableServletWebServerFactory> 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");
}
}