-
Notifications
You must be signed in to change notification settings - Fork 0
Sprint Sections 17 Stage 2 Compilations #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
432589b
The assembly module has been completed, statistics have been corrected
7771179
The assembly module has been completed, statistics have been corrected
1a1bfb6
Merge remote-tracking branch 'origin/main_svc_compilations' into main…
b5395fa
Edits have been made based on the review
b66cec6
Refactor controller methods for improved readability; update paramete…
Naz1anmak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| When performing a code review, respond in Russian. | ||
| Комментарии в Pull Request обязательно должны быть на русском языке! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...service/src/main/java/ru/practicum/compilation/controller/AdminCompilationController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,41 @@ | ||
| package ru.practicum.compilation.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.Positive; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.compilation.dto.CompilationDto; | ||
| import ru.practicum.compilation.dto.NewCompilationDto; | ||
| import ru.practicum.compilation.dto.UpdateCompilationRequest; | ||
| import ru.practicum.compilation.service.CompilationService; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/admin/compilations") | ||
| @RequiredArgsConstructor | ||
| public class AdminCompilationController { | ||
| private final CompilationService compilationService; | ||
|
|
||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public CompilationDto createCompilation(@RequestBody @Valid NewCompilationDto request) { | ||
| log.debug("Controller: createCompilation data {}", request); | ||
| return compilationService.createCompilation(request); | ||
| } | ||
|
|
||
| @DeleteMapping("/{compId}") | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void deleteCompilation(@PathVariable @Positive Long compId) { | ||
| log.debug("Controller: deleteCompilation compId={}", compId); | ||
| compilationService.deleteCompilation(compId); | ||
| } | ||
|
|
||
| @PatchMapping("/{compId}") | ||
| public CompilationDto updateCompilation(@PathVariable @Positive Long compId, | ||
| @RequestBody @Valid UpdateCompilationRequest request) { | ||
| log.debug("Controller: updateCompilation compId={} and data {}", compId, request); | ||
| return compilationService.updateCompilation(compId, request); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
...ervice/src/main/java/ru/practicum/compilation/controller/PublicCompilationController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,36 @@ | ||
| package ru.practicum.compilation.controller; | ||
|
|
||
| import jakarta.validation.constraints.Positive; | ||
| import jakarta.validation.constraints.PositiveOrZero; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.compilation.dto.CompilationDto; | ||
| import ru.practicum.compilation.service.CompilationService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/compilations") | ||
| @RequiredArgsConstructor | ||
| public class PublicCompilationController { | ||
| private final CompilationService compilationService; | ||
|
|
||
| @GetMapping | ||
| public List<CompilationDto> getCompilations(@RequestParam(required = false) Boolean pinned, | ||
| @RequestParam(defaultValue = "0") @PositiveOrZero Integer from, | ||
| @RequestParam(defaultValue = "10") @Positive Integer size) { | ||
| Pageable pageable = PageRequest.of(from / size, size); | ||
| log.debug("Controller: getCompilations pinned={}, from={}, size={}", pinned, from, size); | ||
| return compilationService.getCompilations(pinned, pageable); | ||
|
internet-prodashi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @GetMapping("/{compId}") | ||
| public CompilationDto getCompilation(@PathVariable @Positive Long compId) { | ||
| log.debug("Controller: getCompilation compId={}", compId); | ||
| return compilationService.getCompilationById(compId); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
main-service/src/main/java/ru/practicum/compilation/mapper/CompilationMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,19 @@ | ||
| package ru.practicum.compilation.mapper; | ||
|
|
||
| public class CompilationMapper { | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import ru.practicum.compilation.dto.CompilationDto; | ||
| import ru.practicum.compilation.dto.NewCompilationDto; | ||
| import ru.practicum.compilation.model.Compilation; | ||
| import ru.practicum.event.mapper.EventMapper; | ||
|
|
||
| @Mapper(componentModel = "spring", uses = EventMapper.class) | ||
| public interface CompilationMapper { | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| @Mapping(target = "events", ignore = true) | ||
| Compilation toEntity(NewCompilationDto request); | ||
|
|
||
| @Mapping(target = "events", qualifiedByName = "toEventShortWithoutStats") | ||
| CompilationDto toDto(Compilation compilation); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
main-service/src/main/java/ru/practicum/compilation/repository/CompilationRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,24 @@ | ||
| package ru.practicum.compilation.repository; | ||
|
|
||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import ru.practicum.compilation.model.Compilation; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface CompilationRepository extends JpaRepository<Compilation, Long> { | ||
| Page<Compilation> findByPinned(Boolean pinned, Pageable pageable); | ||
|
|
||
| @Query(""" | ||
| SELECT c FROM Compilation c | ||
| LEFT JOIN FETCH c.events e | ||
| LEFT JOIN FETCH e.category | ||
| LEFT JOIN FETCH e.initiator | ||
| WHERE c.id = :id""") | ||
| Optional<Compilation> findByIdWithEvents(@Param("id") Long id); | ||
|
|
||
| boolean existsByTitle(String title); | ||
| } |
16 changes: 16 additions & 0 deletions
16
main-service/src/main/java/ru/practicum/compilation/service/CompilationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,20 @@ | ||
| package ru.practicum.compilation.service; | ||
|
|
||
| import org.springframework.data.domain.Pageable; | ||
| import ru.practicum.compilation.dto.CompilationDto; | ||
| import ru.practicum.compilation.dto.NewCompilationDto; | ||
| import ru.practicum.compilation.dto.UpdateCompilationRequest; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface CompilationService { | ||
| CompilationDto createCompilation(NewCompilationDto request); | ||
|
|
||
| void deleteCompilation(Long compId); | ||
|
|
||
| CompilationDto updateCompilation(Long compId, UpdateCompilationRequest request); | ||
|
|
||
| List<CompilationDto> getCompilations(Boolean pinned, Pageable pageable); | ||
|
|
||
| CompilationDto getCompilationById(Long compId); | ||
| } |
127 changes: 127 additions & 0 deletions
127
main-service/src/main/java/ru/practicum/compilation/service/CompilationServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,131 @@ | ||
| package ru.practicum.compilation.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import ru.practicum.compilation.dto.CompilationDto; | ||
| import ru.practicum.compilation.dto.NewCompilationDto; | ||
| import ru.practicum.compilation.dto.UpdateCompilationRequest; | ||
| import ru.practicum.compilation.mapper.CompilationMapper; | ||
| import ru.practicum.compilation.model.Compilation; | ||
| import ru.practicum.compilation.repository.CompilationRepository; | ||
| import ru.practicum.event.model.Event; | ||
| import ru.practicum.event.repository.EventRepository; | ||
| import ru.practicum.exception.ConflictException; | ||
| import ru.practicum.exception.NotFoundException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
|
internet-prodashi marked this conversation as resolved.
|
||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class CompilationServiceImpl implements CompilationService { | ||
| private final CompilationRepository compilationRepository; | ||
| private final EventRepository eventRepository; | ||
| private final CompilationMapper compilationMapper; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public CompilationDto createCompilation(NewCompilationDto request) { | ||
| if (compilationRepository.existsByTitle(request.title())) | ||
| throw new ConflictException("Сборник с таким названием (" + request.title() + ") уже существует"); | ||
|
|
||
| Compilation compilation = compilationMapper.toEntity(request); | ||
|
|
||
| if (request.events() != null && !request.events().isEmpty()) { | ||
| List<Event> events = eventRepository.findAllByEventIds(new ArrayList<>(request.events())); | ||
|
|
||
| if (events.size() != request.events().size()) throw new NotFoundException("Не все события найдены"); | ||
|
internet-prodashi marked this conversation as resolved.
|
||
| compilation.setEvents(new HashSet<>(events)); | ||
|
|
||
| } else compilation.setEvents(new HashSet<>()); | ||
| Compilation savedCompilation = compilationRepository.save(compilation); | ||
|
|
||
| log.info("Создан сборник: {}", request); | ||
| return compilationMapper.toDto(savedCompilation); | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void deleteCompilation(Long compId) { | ||
| if (!compilationRepository.existsById(compId)) | ||
| throw new NotFoundException("Сборник с идентификатором " + compId + " не найден"); | ||
|
|
||
| log.info("Удален сборник compId={}", compId); | ||
| compilationRepository.deleteById(compId); | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public CompilationDto updateCompilation(Long compId, UpdateCompilationRequest request) { | ||
| Compilation compilation = getCompilationOrThrow(compId); | ||
|
|
||
| if (request.title() != null && !request.title().equals(compilation.getTitle()) | ||
| && compilationRepository.existsByTitle(request.title())) | ||
| throw new ConflictException("Сборник с таким названием (" + request.title() + ") уже существует"); | ||
|
|
||
| if (request.title() != null) compilation.setTitle(request.title()); | ||
| compilation.setPinned(request.pinned()); | ||
|
|
||
| if (request.events() != null) { | ||
|
internet-prodashi marked this conversation as resolved.
|
||
| if (request.events().isEmpty()) { | ||
| compilation.setEvents(new HashSet<>()); | ||
| } else { | ||
| List<Event> events = eventRepository.findAllByEventIds(new ArrayList<>(request.events())); | ||
| if (events.size() != request.events().size()) | ||
| throw new NotFoundException("Некоторые события не найдены"); | ||
| compilation.setEvents(new HashSet<>(events)); | ||
| } | ||
| } | ||
|
|
||
| log.info("Обновлен сборник request={}", request); | ||
| return compilationMapper.toDto(compilationRepository.save(compilation)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<CompilationDto> getCompilations(Boolean pinned, Pageable pageable) { | ||
| Pageable sortedPageable = PageRequest.of( | ||
| pageable.getPageNumber(), | ||
| pageable.getPageSize(), | ||
| Sort.by("id").ascending() | ||
| ); | ||
|
|
||
| Page<Compilation> compilationsPage; | ||
| if (pinned != null) | ||
| compilationsPage = compilationRepository.findByPinned(pinned, sortedPageable); | ||
| else | ||
| compilationsPage = compilationRepository.findAll(sortedPageable); | ||
|
|
||
| List<Compilation> compilations = compilationsPage.getContent(); | ||
|
|
||
| if (!compilations.isEmpty()) { | ||
| log.info("Получен список сборников pinned={}, pageable={}", pinned, pageable); | ||
| return compilations.stream() | ||
| .map(compilationMapper::toDto) | ||
|
internet-prodashi marked this conversation as resolved.
|
||
| .toList(); | ||
| } | ||
| log.info("Список сборников пуст"); | ||
| return List.of(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompilationDto getCompilationById(Long compId) { | ||
| Compilation compilation = getCompilationOrThrow(compId); | ||
|
|
||
| log.info("Получен сборник compId={}", compId); | ||
| return compilationMapper.toDto(compilation); | ||
| } | ||
|
|
||
| private Compilation getCompilationOrThrow(Long compId) { | ||
| return compilationRepository.findByIdWithEvents(compId) | ||
| .orElseThrow(() -> new NotFoundException("Сборник с идентификатором " + compId + " не найден")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.