Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
stats-server:
build: stat-service/server
build: stat-service/stats-server
image: ewm-stat-service
container_name: ewm-stat-service
ports:
Expand All @@ -23,6 +23,11 @@ services:
- POSTGRES_PASSWORD=explore
- POSTGRES_USER=explore
- POSTGRES_DB=stats_db
healthcheck:
test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER
timeout: 5s
interval: 5s
retries: 10


ewm-service:
Expand All @@ -34,18 +39,23 @@ services:
depends_on:
- ewm-db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://main-db:5433/main_db
- SPRING_DATASOURCE_URL=jdbc:postgresql://ewm-db:5432/main_db
- SPRING_DATASOURCE_USERNAME=explore
- SPRING_DATASOURCE_PASSWORD=explore
- SPRING_DATASOURCE_DRIVER-CLASS-NAME=org.postgresql.Driver

ewm-db:
image: postgres:16.1
container_name: main-db
container_name: ewm-db
ports:
- "6542:5433"
- "6542:5432"
environment:
- POSTGRES_PASSWORD=explore
- POSTGRES_USER=explore
- POSTGRES_DB=main_db
healthcheck:
test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER
timeout: 5s
interval: 5s
retries: 10

85 changes: 85 additions & 0 deletions main-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,71 @@
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<classifier>jakarta</classifier>
<version>5.1.0</version>
</dependency>

<!--HTTP CLIENT-->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>

</dependencies>

<build>
Expand All @@ -39,6 +100,30 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<classifier>jakarta</classifier>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(value = {"ru.practicum.ewm", "ru.practicum"})
public class MainServiceApp {

public static void main(String[] args) {
Expand Down
21 changes: 21 additions & 0 deletions main-service/src/main/java/ru/practicum/ewm/category/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.practicum.ewm.category;

import jakarta.persistence.*;
import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "category")
@Builder
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private int id;

@Column(name = "category_name", nullable = false, unique = true, length = 50)
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.practicum.ewm.category;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoryRepository extends JpaRepository<Category, Integer> {
Category findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.practicum.ewm.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.ewm.category.dto.CategoryDto;
import ru.practicum.ewm.category.dto.NewCategoryDto;
import ru.practicum.ewm.category.service.CategoryService;

@Slf4j
@RestController
@RequestMapping(path = "/admin/categories")
@RequiredArgsConstructor
public class CategoryAdminController {
private final CategoryService categoryService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CategoryDto createCategory(@Valid @RequestBody NewCategoryDto newCategoryDto) {
log.info("POST /admin/categories запрос with newCategoryDto {}", newCategoryDto);
return categoryService.createCategory(newCategoryDto);
}

@DeleteMapping("/{catId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCategory(@PathVariable("catId") int catId) {
log.info("DELETE /admin/categories/catId запрос with id {}", catId);
categoryService.deleteCategory(catId);
}

@PatchMapping("/{catId}")
public CategoryDto updateCategory(@PathVariable("catId") int catId,
@Valid @RequestBody CategoryDto categoryDto) {
log.info("PATCH /admin/categories/catId запрос with id {}, categoryDto {}", catId, categoryDto);
return categoryService.updateCategory(catId, categoryDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.practicum.ewm.category.controller;

import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ru.practicum.ewm.category.dto.CategoryDto;
import ru.practicum.ewm.category.service.CategoryService;

import java.util.Collection;

@Slf4j
@RestController
@RequestMapping(path = "/categories")
@RequiredArgsConstructor
public class CategoryPublicController {
private final CategoryService categoryService;

@GetMapping
public Collection<CategoryDto> getAllCategories(@RequestParam(defaultValue = "0") @PositiveOrZero int from,
@RequestParam(defaultValue = "10") @Positive int size) {
log.info("PUBLIC GET /categories запрос with from {}, size {}", from, size);
return categoryService.getAllCategories(from, size);
}

@GetMapping("/{catId}")
public CategoryDto getCategoryById(@PathVariable("catId") int catId) {
log.info("PUBLIC GET /categories/catId запрос with id {}", catId);
return categoryService.getCategoryById(catId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.practicum.ewm.category.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CategoryDto {
private int id;

@NotNull(message = "Поле name не может быть null")
@NotBlank(message = "Поле name не может быть пустым")
@Size(min = 1, max = 50)
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.practicum.ewm.category.dto;

import org.springframework.stereotype.Component;
import ru.practicum.ewm.category.Category;

@Component
public class CategoryMapper {

public static Category toCategory(NewCategoryDto newCategoryDto) {
return Category.builder()
.name(newCategoryDto.getName())
.build();
}

public static Category toCategory(CategoryDto categoryDto) {
return Category.builder()
.id(categoryDto.getId())
.name(categoryDto.getName())
.build();
}

public static CategoryDto toCategoryDto(Category category) {
return CategoryDto.builder()
.id(category.getId())
.name(category.getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.practicum.ewm.category.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class NewCategoryDto {
@NotNull(message = "Поле name не может быть null")
@NotBlank(message = "Поле name не может быть пустым")
@Size(min = 1, max = 50)
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.practicum.ewm.category.service;

import ru.practicum.ewm.category.dto.CategoryDto;
import ru.practicum.ewm.category.dto.NewCategoryDto;

import java.util.Collection;

public interface CategoryService {

CategoryDto createCategory(NewCategoryDto newCategoryDto);

void deleteCategory(int catId);

CategoryDto updateCategory(int catId, CategoryDto categoryDto);

Collection<CategoryDto> getAllCategories(int from, int size);

CategoryDto getCategoryById(int catId);
}
Loading