generated from yandex-praktikum/java-explore-with-me-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Vladyak1/main_svc_category
main_svc_category
- Loading branch information
Showing
18 changed files
with
402 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
42 changes: 42 additions & 0 deletions
42
ewm-main-service/src/main/java/ru/practicum/category/controller/AdminCategoryController.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ru.practicum.category.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.practicum.category.dto.CategoryDto; | ||
import ru.practicum.category.dto.CategoryDtoRequest; | ||
import ru.practicum.category.service.CategoryService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Validated | ||
@RequestMapping("/admin/categories") | ||
@Slf4j | ||
public class AdminCategoryController { | ||
private final CategoryService categoryService; | ||
|
||
@PostMapping | ||
public ResponseEntity<CategoryDto> createCategory(@Valid @RequestBody CategoryDtoRequest categoryDtoRequest) { | ||
log.info("Calling the POST request to /admin/categories endpoint"); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(categoryService.createCategory(categoryDtoRequest)); | ||
} | ||
|
||
@DeleteMapping(value = "/{catId}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void deleteCategory(@NonNull @PathVariable Long catId) { | ||
log.info("Calling the DELETE request to /admin/categories/{catId} endpoint"); | ||
categoryService.deleteCategory(catId); | ||
} | ||
|
||
@PatchMapping(value = "/{catId}") | ||
public ResponseEntity<CategoryDto> updateCategory(@PathVariable long catId, | ||
@Valid @RequestBody CategoryDtoRequest categoryDtoRequest) { | ||
log.info("Calling the PATCH request to /admin/categories/{catId} endpoint"); | ||
return ResponseEntity.ok().body(categoryService.updateCategory(catId, categoryDtoRequest)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...main-service/src/main/java/ru/practicum/category/controller/PublicCategoryController.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.practicum.category.dto.CategoryDto; | ||
import ru.practicum.category.service.CategoryService; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Validated | ||
@RequestMapping("/categories") | ||
@Slf4j | ||
public class PublicCategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<CategoryDto>> getAllCategories( | ||
@PositiveOrZero @RequestParam(required = false, defaultValue = "0") Integer from, | ||
@Positive @RequestParam(required = false, defaultValue = "10") Integer size) { | ||
log.info("Calling the GET request to /categories endpoint"); | ||
return ResponseEntity.status(HttpStatus.OK).body(categoryService.getAllCategories(from, size)); | ||
} | ||
|
||
@GetMapping("/{catId}") | ||
public ResponseEntity<CategoryDto> getCategory(@PathVariable Long catId) { | ||
log.info("Calling the GET request to /categories/{catId} endpoint"); | ||
return ResponseEntity.status(HttpStatus.OK).body(categoryService.getCategoryById(catId)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ewm-main-service/src/main/java/ru/practicum/category/dto/CategoryDto.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ru.practicum.category.dto; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CategoryDto { | ||
private long id; | ||
private String name; | ||
} |
18 changes: 18 additions & 0 deletions
18
ewm-main-service/src/main/java/ru/practicum/category/dto/CategoryDtoRequest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.practicum.category.dto; | ||
|
||
import lombok.*; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CategoryDtoRequest { | ||
|
||
@NotBlank | ||
@Size(max = 50, message = "The name must be no more than 50 characters long") | ||
private String name; | ||
} |
12 changes: 12 additions & 0 deletions
12
ewm-main-service/src/main/java/ru/practicum/category/dto/CategoryMapper.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ru.practicum.category.dto; | ||
|
||
import org.mapstruct.Mapper; | ||
import ru.practicum.category.model.Category; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface CategoryMapper { | ||
|
||
Category toCategory(CategoryDtoRequest categoryDtoRequest); | ||
|
||
CategoryDto toCategoryDto(Category category); | ||
} |
21 changes: 21 additions & 0 deletions
21
ewm-main-service/src/main/java/ru/practicum/category/model/Category.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.practicum.category.model; | ||
|
||
import jakarta.persistence.Table; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "categories") | ||
public class Category { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
@Column(unique = true) | ||
private String name; | ||
} |
9 changes: 9 additions & 0 deletions
9
ewm-main-service/src/main/java/ru/practicum/category/repository/CategoryRepository.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.practicum.category.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import ru.practicum.category.model.Category; | ||
|
||
@Repository | ||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
} |
21 changes: 21 additions & 0 deletions
21
ewm-main-service/src/main/java/ru/practicum/category/service/CategoryService.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.practicum.category.service; | ||
|
||
import ru.practicum.category.dto.CategoryDto; | ||
import ru.practicum.category.dto.CategoryDtoRequest; | ||
import ru.practicum.category.model.Category; | ||
|
||
import java.util.List; | ||
|
||
public interface CategoryService { | ||
CategoryDto createCategory(CategoryDtoRequest categoryDtoRequest); | ||
|
||
void deleteCategory(Long catId); | ||
|
||
CategoryDto updateCategory(Long catId, CategoryDtoRequest categoryDtoRequest); | ||
|
||
List<CategoryDto> getAllCategories(Integer from, Integer size); | ||
|
||
CategoryDto getCategoryById(Long catsId); | ||
|
||
Category getCategoryByIdNotMapping(Long catId); | ||
} |
88 changes: 88 additions & 0 deletions
88
ewm-main-service/src/main/java/ru/practicum/category/service/CategoryServiceImpl.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package ru.practicum.category.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.stereotype.Service; | ||
import ru.practicum.category.dto.CategoryDto; | ||
import ru.practicum.category.dto.CategoryDtoRequest; | ||
import ru.practicum.category.dto.CategoryMapper; | ||
import ru.practicum.category.model.Category; | ||
import ru.practicum.category.repository.CategoryRepository; | ||
//import ru.practicum.errors.DataConflictRequest; | ||
import ru.practicum.errors.NotFoundException; | ||
//import ru.practicum.event.service.EventService; | ||
|
||
import jakarta.transaction.Transactional; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class CategoryServiceImpl implements CategoryService { | ||
|
||
private final CategoryRepository categoryRepository; | ||
private final CategoryMapper categoryMapper; | ||
// private final EventService eventService; | ||
|
||
// Часть admin | ||
|
||
public CategoryDto createCategory(CategoryDtoRequest categoryDtoRequest) { | ||
log.info("Категория {} успешно добавлен", categoryDtoRequest); | ||
return categoryMapper.toCategoryDto(categoryRepository.save(categoryMapper.toCategory(categoryDtoRequest))); | ||
} | ||
|
||
@Transactional | ||
public void deleteCategory(Long catId) { | ||
Optional<Category> category = categoryRepository.findById(catId); | ||
if (category.isEmpty()) { | ||
throw new NotFoundException("Category with id = " + catId + " was not found"); | ||
} else { | ||
categoryRepository.deleteById(catId); | ||
log.info("Категория с id = {}, успешно удалена", catId); | ||
} | ||
// else if (eventService.findByCategory(category.get()).isPresent()) { | ||
// throw new DataConflictRequest("Events are associated with the id = " + catId + " category"); | ||
// } | ||
|
||
} | ||
|
||
@Transactional | ||
public CategoryDto updateCategory(Long catId, CategoryDtoRequest categoryDtoRequest) { | ||
Optional<Category> category = categoryRepository.findById(catId); | ||
if (category.isEmpty()) { | ||
throw new NotFoundException("Category with id = " + catId + " was not found"); | ||
} else { | ||
Category categorySaved = category.get(); | ||
Category categoryNew = categoryMapper.toCategory(categoryDtoRequest); | ||
categorySaved.setName(categoryNew.getName()); | ||
categorySaved = categoryRepository.save(categorySaved); | ||
log.info("Категория с id = {} успешно обновлена", catId); | ||
return categoryMapper.toCategoryDto(categorySaved); | ||
} | ||
} | ||
|
||
// Часть public | ||
|
||
public List<CategoryDto> getAllCategories(Integer from, Integer size) { | ||
List<Category> categories; | ||
categories = categoryRepository.findAll(PageRequest.of(from / size, size)).getContent(); | ||
log.info("Список категорий успешно выдан"); | ||
return categories.stream().map(categoryMapper::toCategoryDto).collect(Collectors.toList()); | ||
} | ||
|
||
public CategoryDto getCategoryById(Long catId) { | ||
log.info("Поиск категории с id = {}", catId); | ||
return categoryMapper.toCategoryDto(getCategoryByIdNotMapping(catId)); | ||
} | ||
|
||
public Category getCategoryByIdNotMapping(Long catId) { | ||
Optional<Category> categoryOptional = categoryRepository.findById(catId); | ||
if (categoryOptional.isPresent()) { | ||
return categoryOptional.get(); | ||
} | ||
throw new NotFoundException("Category with id = " + catId + " was not found"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
ewm-main-service/src/main/java/ru/practicum/errors/ApiError.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ru.practicum.errors; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@AllArgsConstructor | ||
public class ApiError { | ||
|
||
private HttpStatus status; | ||
private String reason; | ||
private String message; | ||
private List<String> errors; | ||
private String timestamp; | ||
} |
7 changes: 7 additions & 0 deletions
7
ewm-main-service/src/main/java/ru/practicum/errors/DataConflictRequest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.practicum.errors; | ||
|
||
public class DataConflictRequest extends RuntimeException { | ||
public DataConflictRequest(String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.