-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryController.java
52 lines (42 loc) · 1.65 KB
/
CategoryController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ru.tbank.springapp.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.tbank.springapp.aspect.Timed;
import ru.tbank.springapp.dto.CategoryDTO;
import ru.tbank.springapp.model.Category;
import ru.tbank.springapp.service.CategoryService;
import java.util.List;
@RestController
@RequestMapping("/api/v1/places/categories")
@RequiredArgsConstructor
@Timed
public class CategoryController {
private final CategoryService categoryService;
@GetMapping
List<CategoryDTO> getCategories() {
return categoryService.findAll().stream().map(Category::toDTO).toList();
}
@GetMapping("/{id}")
CategoryDTO getCategory(@PathVariable String id) {
return categoryService.findById(id).toDTO();
}
@PostMapping
void createCategory(@RequestBody CategoryDTO categoryDTO) {
categoryService.create(categoryDTO.slug(), categoryDTO.name());
}
@PutMapping("/{id}")
void updateCategory(@PathVariable String id, @RequestBody CategoryDTO categoryDTO) {
categoryService.update(id, categoryDTO.name());
}
@DeleteMapping("/{id}")
void deleteCategory(@PathVariable String id) {
categoryService.delete(id);
}
}