-
Notifications
You must be signed in to change notification settings - Fork 0
Sprint Sections 17 Stage 2 Categories #8
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
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
36 changes: 36 additions & 0 deletions
36
main-service/src/main/java/ru/practicum/category/controller/AdminCategoryController.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,40 @@ | ||
| package ru.practicum.category.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.category.dto.CategoryDto; | ||
| import ru.practicum.category.dto.NewCategoryDto; | ||
| import ru.practicum.category.service.CategoryService; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/admin/categories") | ||
| @RequiredArgsConstructor | ||
| public class AdminCategoryController { | ||
| private final CategoryService categoryService; | ||
|
|
||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public CategoryDto createCategory(@RequestBody @Valid NewCategoryDto newCategoryDto) { | ||
| log.debug("Controller: createCategory with data {}", newCategoryDto); | ||
| return categoryService.createCategory(newCategoryDto); | ||
| } | ||
|
|
||
| @DeleteMapping("/{categoryId}") | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void deleteCategory(@PathVariable Long categoryId) { | ||
| log.debug("Controller: deleteCategory with id={}", categoryId); | ||
| categoryService.deleteCategory(categoryId); | ||
| } | ||
|
|
||
| @PatchMapping("/{categoryId}") | ||
| public CategoryDto updateCategory( | ||
| @PathVariable Long categoryId, | ||
| @RequestBody @Valid NewCategoryDto newCategoryDto) { | ||
| log.debug("Controller: updateCategory with id={} with data={}", categoryId, newCategoryDto); | ||
| return categoryService.updateCategory(categoryId, newCategoryDto); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
main-service/src/main/java/ru/practicum/category/controller/PublicCategoryController.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,37 @@ | ||
| package ru.practicum.category.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.category.dto.CategoryDto; | ||
| import ru.practicum.category.service.CategoryService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/categories") | ||
| @RequiredArgsConstructor | ||
| public class PublicCategoryController { | ||
| private final CategoryService categoryService; | ||
|
|
||
| @GetMapping | ||
| public List<CategoryDto> getCategories( | ||
| @RequestParam(defaultValue = "0") @PositiveOrZero Integer from, | ||
| @RequestParam(defaultValue = "10") @Positive Integer size) { | ||
| int page = from / size; | ||
| Pageable pageable = PageRequest.of(page, size); | ||
| log.debug("Controller: getCategories with from={}, size={}", from, size); | ||
| return categoryService.getCategories(pageable); | ||
| } | ||
|
|
||
| @GetMapping("/{categoryId}") | ||
| public CategoryDto getCategoryById(@PathVariable Long categoryId) { | ||
| log.debug("Controller: getCategoryById with id={}", categoryId); | ||
| return categoryService.getCategoryById(categoryId); | ||
| } | ||
| } |
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
3 changes: 1 addition & 2 deletions
3
main-service/src/main/java/ru/practicum/category/model/Category.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
21 changes: 20 additions & 1 deletion
21
main-service/src/main/java/ru/practicum/category/repository/CategoryRepository.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,23 @@ | ||
| package ru.practicum.category.repository; | ||
|
|
||
| public class CategoryRepository { | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import ru.practicum.category.model.Category; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
|
|
||
| @Query(""" | ||
| SELECT c FROM Category c | ||
| WHERE c.name = :name""" | ||
| ) | ||
| Optional<Category> findByName(@Param("name") String name); | ||
|
|
||
| @Query(""" | ||
| SELECT c FROM Category c | ||
| WHERE c.name = :name AND c.id != :id""" | ||
| ) | ||
| Optional<Category> findByNameAndIdNot(@Param("name") String name, @Param("id") Long id); | ||
| } |
15 changes: 15 additions & 0 deletions
15
main-service/src/main/java/ru/practicum/category/service/CategoryService.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.category.service; | ||
|
|
||
| import org.springframework.data.domain.Pageable; | ||
| import ru.practicum.category.dto.CategoryDto; | ||
| import ru.practicum.category.dto.NewCategoryDto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface CategoryService { | ||
| CategoryDto createCategory(NewCategoryDto newCategoryDto); | ||
|
|
||
| void deleteCategory(Long categoryId); | ||
|
|
||
| CategoryDto updateCategory(Long categoryId, NewCategoryDto newCategoryDto); | ||
|
|
||
| List<CategoryDto> getCategories(Pageable pageable); | ||
|
|
||
| CategoryDto getCategoryById(Long categoryId); | ||
| } |
105 changes: 105 additions & 0 deletions
105
main-service/src/main/java/ru/practicum/category/service/CategoryServiceImpl.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,109 @@ | ||
| package ru.practicum.category.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import ru.practicum.category.dto.CategoryDto; | ||
| import ru.practicum.category.dto.NewCategoryDto; | ||
| import ru.practicum.category.mapper.CategoryMapper; | ||
| import ru.practicum.category.model.Category; | ||
| import ru.practicum.category.repository.CategoryRepository; | ||
| import ru.practicum.event.repository.EventRepository; | ||
| import ru.practicum.exception.ConflictException; | ||
| import ru.practicum.exception.NotFoundException; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @Transactional(readOnly = true) | ||
| @RequiredArgsConstructor | ||
| public class CategoryServiceImpl implements CategoryService { | ||
| private final CategoryRepository categoryRepository; | ||
| private final CategoryMapper categoryMapper; | ||
| private final EventRepository eventRepository; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public CategoryDto createCategory(NewCategoryDto newCategoryDto) { | ||
| checkCategoryNameUnique(newCategoryDto.name(), null); | ||
|
|
||
| Category category = categoryMapper.toEntity(newCategoryDto); | ||
|
|
||
| Category savedCategory = categoryRepository.save(category); | ||
|
|
||
| log.info("Создана новая категория: {}", savedCategory); | ||
| return categoryMapper.toDto(savedCategory); | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void deleteCategory(Long categoryId) { | ||
| Category category = getCategoryByIdOrThrow(categoryId); | ||
|
|
||
| if (eventRepository.existsByCategoryId(categoryId)) { | ||
| throw new ConflictException("Невозможно удалить категорию с существующими событиями"); | ||
| } | ||
|
|
||
| categoryRepository.delete(category); | ||
| log.info("Удалена категория с ID: {}", categoryId); | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public CategoryDto updateCategory(Long categoryId, NewCategoryDto newCategoryDto) { | ||
| Category category = getCategoryByIdOrThrow(categoryId); | ||
|
|
||
| checkCategoryNameUnique(newCategoryDto.name(), categoryId); | ||
|
|
||
| categoryMapper.updateCategoryFromDto(newCategoryDto, category); | ||
| Category updatedCategory = categoryRepository.save(category); | ||
|
|
||
| log.info("Обновлена категория: {}", updatedCategory); | ||
| return categoryMapper.toDto(updatedCategory); | ||
| } | ||
|
|
||
| @Override | ||
| public List<CategoryDto> getCategories(Pageable pageable) { | ||
| Page<Category> categoriesPage = categoryRepository.findAll(pageable); | ||
|
|
||
| if (categoriesPage.isEmpty()) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| List<CategoryDto> result = categoriesPage.stream() | ||
| .map(categoryMapper::toDto) | ||
| .toList(); | ||
|
|
||
| log.info("Найдено {} категорий", result.size()); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public CategoryDto getCategoryById(Long categoryId) { | ||
| Category category = getCategoryByIdOrThrow(categoryId); | ||
| CategoryDto result = categoryMapper.toDto(category); | ||
|
|
||
| log.info("Найдена категория: {}", result); | ||
| return result; | ||
| } | ||
|
|
||
| private void checkCategoryNameUnique(String name, Long excludedId) { | ||
| Optional<Category> existingCategory = (excludedId == null) | ||
| ? categoryRepository.findByName(name) | ||
| : categoryRepository.findByNameAndIdNot(name, excludedId); | ||
|
|
||
| existingCategory.ifPresent(category -> { | ||
| throw new ConflictException("Категория с названием '" + name + "' уже существует"); | ||
| }); | ||
| } | ||
|
|
||
| private Category getCategoryByIdOrThrow(Long categoryId) { | ||
| return categoryRepository.findById(categoryId) | ||
| .orElseThrow(() -> new NotFoundException("Категория с id " + categoryId + " не найдена")); | ||
| } | ||
| } | ||
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
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.