Skip to content

Commit 6f3ded1

Browse files
committed
stage 2 - add ewm-service
1 parent 873ef18 commit 6f3ded1

66 files changed

Lines changed: 2151 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ services:
5353
environment:
5454
SPRING_DATASOURCE_URL: jdbc:postgresql://ewm-db:5432/ewmdb
5555
SPRING_DATASOURCE_USERNAME: postgres
56-
SPRING_DATASOURCE_PASSWORD: password
56+
SPRING_DATASOURCE_PASSWORD: password
57+
STATS_SERVER_URL: http://stats-server:9090

ewm-service/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@
2727
<artifactId>lombok</artifactId>
2828
<optional>true</optional>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.postgresql</groupId>
36+
<artifactId>postgresql</artifactId>
37+
<scope>runtime</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-validation</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>ru.practicum</groupId>
45+
<artifactId>statistics-client</artifactId>
46+
<version>${project.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.h2database</groupId>
55+
<artifactId>h2</artifactId>
56+
<scope>test</scope>
57+
</dependency>
3058
</dependencies>
3159

3260
<build>

ewm-service/src/main/java/ru/practicum/ewm/EwmServiceApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

6-
@SpringBootApplication
6+
@SpringBootApplication(scanBasePackages = {"ru.practicum.ewm", "ru.practicum.statistics.client"})
77
public class EwmServiceApp {
88
public static void main(String[] args) {
99
SpringApplication.run(EwmServiceApp.class, args);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.practicum.ewm.category.controller;
2+
3+
import jakarta.validation.Valid;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.validation.annotation.Validated;
7+
import org.springframework.web.bind.annotation.*;
8+
import ru.practicum.ewm.category.dto.CategoryDto;
9+
import ru.practicum.ewm.category.dto.NewCategoryDto;
10+
import ru.practicum.ewm.category.service.CategoryService;
11+
12+
@RestController
13+
@RequestMapping("/admin/categories")
14+
@RequiredArgsConstructor
15+
@Validated
16+
public class AdminCategoryController {
17+
private final CategoryService categoryService;
18+
19+
@PostMapping
20+
@ResponseStatus(HttpStatus.CREATED)
21+
public CategoryDto createCategory(@Valid @RequestBody NewCategoryDto dto) {
22+
return categoryService.createCategory(dto);
23+
}
24+
25+
@PatchMapping("/{catId}")
26+
public CategoryDto updateCategory(@PathVariable Long catId,
27+
@Valid @RequestBody CategoryDto dto) {
28+
return categoryService.updateCategory(catId, dto);
29+
}
30+
31+
@DeleteMapping("/{catId}")
32+
@ResponseStatus(HttpStatus.NO_CONTENT)
33+
public void deleteCategory(@PathVariable Long catId) {
34+
categoryService.deleteCategory(catId);
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.practicum.ewm.category.controller;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.validation.annotation.Validated;
5+
import org.springframework.web.bind.annotation.*;
6+
import ru.practicum.ewm.category.dto.CategoryDto;
7+
import ru.practicum.ewm.category.service.CategoryService;
8+
9+
import java.util.List;
10+
11+
@RestController
12+
@RequestMapping("/categories")
13+
@RequiredArgsConstructor
14+
@Validated
15+
public class PublicCategoryController {
16+
private final CategoryService categoryService;
17+
18+
@GetMapping
19+
public List<CategoryDto> getCategories(@RequestParam(defaultValue = "0") int from,
20+
@RequestParam(defaultValue = "10") int size) {
21+
return categoryService.getCategories(from, size);
22+
}
23+
24+
@GetMapping("/{catId}")
25+
public CategoryDto getCategory(@PathVariable Long catId) {
26+
return categoryService.getCategory(catId);
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.practicum.ewm.category.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class CategoryDto {
11+
private Long id;
12+
private String name;
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.practicum.ewm.category.dto;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import lombok.Data;
5+
6+
@Data
7+
public class NewCategoryDto {
8+
@NotBlank(message = "Название категории не может быть пустым")
9+
private String name;
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.practicum.ewm.category.mapper;
2+
3+
import ru.practicum.ewm.category.dto.CategoryDto;
4+
import ru.practicum.ewm.category.dto.NewCategoryDto;
5+
import ru.practicum.ewm.category.model.Category;
6+
7+
public class CategoryMapper {
8+
9+
public static Category toCategory(NewCategoryDto dto) {
10+
Category category = new Category();
11+
category.setName(dto.getName());
12+
return category;
13+
}
14+
15+
public static CategoryDto toCategoryDto(Category category) {
16+
return new CategoryDto(category.getId(), category.getName());
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.practicum.ewm.category.model;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Entity
9+
@Table(name = "categories")
10+
@Data
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class Category {
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
@Column(nullable = false, length = 50, unique = true)
19+
private String name;
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.practicum.ewm.category.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import ru.practicum.ewm.category.model.Category;
5+
6+
public interface CategoryRepository extends JpaRepository<Category, Long> {
7+
boolean existsByName(String name);
8+
}

0 commit comments

Comments
 (0)