generated from yandex-praktikum/java-explore-with-me
-
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 #2 from TY95MC/main_svc
Stage 2. 1 iteration
- Loading branch information
Showing
82 changed files
with
2,722 additions
and
53 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
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
FROM amazoncorretto:11-alpine-jdk | ||
ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8081 | ||
COPY target/*.jar ewm-service.jar | ||
ENTRYPOINT ["java","-jar","/ewm-service.jar"] |
62 changes: 62 additions & 0 deletions
62
event-service/src/main/java/ru/practicum/category/CategoryController.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,62 @@ | ||
package ru.practicum.category; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import ru.practicum.category.model.CategoryMapper; | ||
import ru.practicum.category.model.dto.CategoryDto; | ||
import ru.practicum.category.model.dto.NewCategoryDto; | ||
import ru.practicum.category.service.CategoryService; | ||
|
||
import javax.validation.Valid; | ||
import javax.validation.constraints.Positive; | ||
import javax.validation.constraints.PositiveOrZero; | ||
import java.util.List; | ||
|
||
import static ru.practicum.constants.Constants.FROM; | ||
import static ru.practicum.constants.Constants.SIZE; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Validated | ||
public class CategoryController { | ||
|
||
private final CategoryService service; | ||
private final CategoryMapper mapper; | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@RequestMapping(value = "/admin/categories", method = RequestMethod.POST) | ||
public CategoryDto addNewCategory(@RequestBody @Valid NewCategoryDto dto) { | ||
return service.addNew(dto); | ||
} | ||
|
||
@RequestMapping(value = "/admin/categories/{catId}", method = RequestMethod.PATCH) | ||
public CategoryDto updateCategory(@PathVariable @Positive Integer catId, | ||
@RequestBody @Valid NewCategoryDto dto) { | ||
return service.update(catId, dto); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@RequestMapping(value = "/admin/categories/{catId}", method = RequestMethod.DELETE) | ||
public void deleteCategory(@PathVariable @Positive Integer catId) { | ||
service.delete(catId); | ||
} | ||
|
||
@RequestMapping(value = "/categories", method = RequestMethod.GET) | ||
public List<CategoryDto> getCategories(@RequestParam(defaultValue = FROM) @PositiveOrZero Integer from, | ||
@RequestParam(defaultValue = SIZE) @Positive Integer size) { | ||
return service.getCategories(from, size); | ||
} | ||
|
||
@RequestMapping(value = "/categories/{catId}", method = RequestMethod.GET) | ||
public CategoryDto getCategory(@PathVariable @Positive Integer catId) { | ||
return mapper.mapCategoryToCategoryDto(service.getCategory(catId)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
event-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,28 @@ | ||
package ru.practicum.category.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "categories") | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
|
||
private String name; | ||
} |
12 changes: 12 additions & 0 deletions
12
event-service/src/main/java/ru/practicum/category/model/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.model; | ||
|
||
import org.mapstruct.Mapper; | ||
import ru.practicum.category.model.dto.CategoryDto; | ||
import ru.practicum.category.model.dto.NewCategoryDto; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface CategoryMapper { | ||
Category mapNewCategoryDtoToCategory(NewCategoryDto dto); | ||
|
||
CategoryDto mapCategoryToCategoryDto(Category category); | ||
} |
20 changes: 20 additions & 0 deletions
20
event-service/src/main/java/ru/practicum/category/model/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,20 @@ | ||
package ru.practicum.category.model.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class CategoryDto { | ||
private Integer id; | ||
|
||
@Length(min = 1, max = 50) | ||
private String name; | ||
} |
21 changes: 21 additions & 0 deletions
21
event-service/src/main/java/ru/practicum/category/model/dto/NewCategoryDto.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.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class NewCategoryDto { | ||
@NotBlank | ||
@Length(min = 1, max = 50) | ||
private String name; | ||
} |
9 changes: 9 additions & 0 deletions
9
event-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, Integer> { | ||
} |
19 changes: 19 additions & 0 deletions
19
event-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,19 @@ | ||
package ru.practicum.category.service; | ||
|
||
import ru.practicum.category.model.Category; | ||
import ru.practicum.category.model.dto.CategoryDto; | ||
import ru.practicum.category.model.dto.NewCategoryDto; | ||
|
||
import java.util.List; | ||
|
||
public interface CategoryService { | ||
CategoryDto addNew(NewCategoryDto dto); | ||
|
||
CategoryDto update(int catId, NewCategoryDto dto); | ||
|
||
void delete(int catId); | ||
|
||
List<CategoryDto> getCategories(int from, int size); | ||
|
||
Category getCategory(int catId); | ||
} |
73 changes: 73 additions & 0 deletions
73
event-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,73 @@ | ||
package ru.practicum.category.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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.category.model.Category; | ||
import ru.practicum.category.model.CategoryMapper; | ||
import ru.practicum.category.model.dto.CategoryDto; | ||
import ru.practicum.category.model.dto.NewCategoryDto; | ||
import ru.practicum.category.repository.CategoryRepository; | ||
import ru.practicum.event.repository.EventRepository; | ||
import ru.practicum.exception.ConflictException; | ||
import ru.practicum.exception.EntityNotFoundException; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class CategoryServiceImpl implements CategoryService { | ||
|
||
private final CategoryRepository repository; | ||
private final EventRepository eventRepository; | ||
private final CategoryMapper mapper; | ||
|
||
@Override | ||
public CategoryDto addNew(NewCategoryDto dto) { | ||
Category cat = mapper.mapNewCategoryDtoToCategory(dto); | ||
return mapper.mapCategoryToCategoryDto(repository.saveAndFlush(cat)); | ||
} | ||
|
||
@Override | ||
public CategoryDto update(int catId, NewCategoryDto dto) { | ||
Category cat = checkIfCategoryExist(catId); | ||
cat.setName(dto.getName()); | ||
return mapper.mapCategoryToCategoryDto(repository.saveAndFlush(cat)); | ||
} | ||
|
||
@Override | ||
public void delete(int catId) { | ||
checkIfCategoryExist(catId); | ||
if (eventRepository.findAllByCategoryId(catId).isEmpty()) { | ||
repository.deleteById(catId); | ||
} else { | ||
throw new ConflictException("Category belongs to event"); | ||
} | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
@Override | ||
public List<CategoryDto> getCategories(int from, int size) { | ||
Pageable page = PageRequest.of(from / size, size, Sort.by(Sort.Direction.DESC, "id")); | ||
return repository.findAll(page).stream() | ||
.map(mapper::mapCategoryToCategoryDto) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
@Override | ||
public Category getCategory(int catId) { | ||
return checkIfCategoryExist(catId); | ||
} | ||
|
||
private Category checkIfCategoryExist(int id) { | ||
return repository.findById(id).orElseThrow( | ||
() -> new EntityNotFoundException("Category with id=" + id + " was not found") | ||
); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
event-service/src/main/java/ru/practicum/compilation/CompilationController.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,62 @@ | ||
package ru.practicum.compilation; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import ru.practicum.compilation.model.dto.CompilationDto; | ||
import ru.practicum.compilation.model.dto.NewCompilationDto; | ||
import ru.practicum.compilation.model.dto.UpdateCompilationRequest; | ||
import ru.practicum.compilation.service.CompilationService; | ||
|
||
import javax.validation.Valid; | ||
import javax.validation.constraints.Positive; | ||
import javax.validation.constraints.PositiveOrZero; | ||
import java.util.List; | ||
|
||
import static ru.practicum.constants.Constants.FROM; | ||
import static ru.practicum.constants.Constants.SIZE; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Validated | ||
public class CompilationController { | ||
|
||
private final CompilationService service; | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@RequestMapping(value = "/admin/compilations", method = RequestMethod.POST) | ||
public CompilationDto addNewCompilationAdmin(@RequestBody @Valid NewCompilationDto dto) { | ||
return service.addNewCompilationAdmin(dto); | ||
} | ||
|
||
@RequestMapping(value = "/admin/compilations/{compId}", method = RequestMethod.PATCH) | ||
public CompilationDto updateCompilationAdmin(@PathVariable @Positive Long compId, | ||
@RequestBody @Valid UpdateCompilationRequest dto) { | ||
return service.updateCompilationAdmin(compId, dto); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@RequestMapping(value = "/admin/compilations/{compId}", method = RequestMethod.DELETE) | ||
public void deleteCompilationAdmin(@PathVariable Long compId) { | ||
service.deleteCompilationAdmin(compId); | ||
} | ||
|
||
@RequestMapping(value = "/compilations", method = RequestMethod.GET) | ||
public List<CompilationDto> getCompilationsPublic(@RequestParam(required = false) boolean pinned, | ||
@RequestParam(defaultValue = FROM) @PositiveOrZero Integer from, | ||
@RequestParam(defaultValue = SIZE) @Positive Integer size) { | ||
return service.getCompilationsPublic(pinned, from, size); | ||
} | ||
|
||
@RequestMapping(value = "/compilations/{compId}", method = RequestMethod.GET) | ||
public CompilationDto getCompilationPublic(@PathVariable @Positive Long compId) { | ||
return service.getCompilationPublic(compId); | ||
} | ||
} |
Oops, something went wrong.