엘리스트랙

최종 프로젝트 개발1

Zmann 2024. 4. 28. 13:36
728x90

 

 

 

도메인별로 역할을 나누기로 했는데 모임 부분을 맡기로 했다.

 

모임에 대한 엔티티를 작성하려고 했는데 모임에 카테고리 선택도 들어가야 해서 먼저 카테고리를 생성해 주었다.

 

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    @NotBlank(message = "카테고리를 입력하세요.")
    public String name;
}

 

@RestController
@RequestMapping("/categories")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping
    public ResponseEntity<?> createCategory(@RequestBody @Valid CreateDto createDto) {
        String name = createDto.getName();
        Category category = categoryService.createCategory(name);
        return new ResponseEntity<>(category, HttpStatus.CREATED);
    }

    @GetMapping
    public List<Category> getAllCategories() {
        return categoryService.getAllCategories();
    }

    @PatchMapping("/{categoryId}")
    public ResponseEntity<?> updateCategory(@PathVariable Long categoryId, @RequestBody @Valid CreateDto createDto) {
        String name = createDto.getName();
        Category category = categoryService.updateCategory(categoryId, name);
        return new ResponseEntity<>(category, HttpStatus.CREATED);
    }

    @DeleteMapping("/{categoryId}")
    public void deleteCategory(@PathVariable Long categoryId) {
        categoryService.deleteCategory(categoryId);
    }
}

 

 

카테고리에 대한 간단한 CRUD를 만들어주었다.

 

 

 

 

728x90