Skip to content

Commit

Permalink
Merge pull request #2 from TY95MC/main_svc
Browse files Browse the repository at this point in the history
Stage 2. 1 iteration
  • Loading branch information
TY95MC authored Jan 20, 2024
2 parents 8446f34 + e57b9b8 commit db94ad5
Show file tree
Hide file tree
Showing 82 changed files with 2,722 additions and 53 deletions.
36 changes: 30 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
version: '3.1'
services:

statistics-db:
ewm-db:
image: postgres:14-alpine
container_name: events_postgres_container
ports:
- "6542:5432"
environment:
- POSTGRES_DB=event-db
- POSTGRES_USER=iameventroot
- POSTGRES_PASSWORD=iameventroot

ewm-service:
build: event-service
container_name: event_service_container
ports:
- "8080:8080"
- "8081:8081"
depends_on:
- ewm-db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://ewm-db:5432/event-db
- SPRING_DATASOURCE_USERNAME=iameventroot
- SPRING_DATASOURCE_PASSWORD=iameventroot
- STATS_SERVER_URL=http://stats-server:9090

stats-db:
image: postgres:14-alpine
container_name: statistics_postgres_container
ports:
Expand All @@ -11,15 +35,15 @@ services:
- POSTGRES_USER=iamstatsroot
- POSTGRES_PASSWORD=iamstatsroot

statistics-server:
stats-server:
build: statistics-service/statistics-server
image: statistics_server_image
container_name: statistics_server_container
container_name: statistics_service_container
ports:
- "9090:9090"
- "9091:9091"
depends_on:
- statistics-db
- stats-db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://statistics-db:5432/statistics-db
- SPRING_DATASOURCE_URL=jdbc:postgresql://stats-db:5432/statistics-db
- SPRING_DATASOURCE_USERNAME=iamstatsroot
- SPRING_DATASOURCE_PASSWORD=iamstatsroot
3 changes: 0 additions & 3 deletions event-service/DockerFile

This file was deleted.

4 changes: 4 additions & 0 deletions event-service/Dockerfile
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"]
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));
}
}
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;
}
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);
}
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;
}
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;
}
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> {
}
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);
}
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")
);
}
}
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);
}
}
Loading

0 comments on commit db94ad5

Please sign in to comment.