CategoryController.java
package com.codecool.bytebattlers.controller;
import com.codecool.bytebattlers.controller.dto.CategoryDto;
import com.codecool.bytebattlers.controller.exception.ResourceNotFoundException;
import com.codecool.bytebattlers.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/api/categories")
public class CategoryController {
private final CategoryService categoryService;
@Autowired
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}
@GetMapping
public ResponseEntity<List<CategoryDto>> getAllCategory() {
if (categoryService.findAll().isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<>(categoryService.findAll(), HttpStatus.OK);
}
}
@GetMapping("/{id}")
public ResponseEntity<CategoryDto> getCategoryById(@PathVariable UUID id) {
if (categoryService.findById(id) == null) {
throw new ResourceNotFoundException("Not found Tutorial with id = " + id);
} else {
return new ResponseEntity<>(categoryService.findById(id), HttpStatus.OK);
}
}
@PostMapping
public ResponseEntity<CategoryDto> addNewCategory(@RequestBody CategoryDto board) {
return new ResponseEntity<>(categoryService.save(board), HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<CategoryDto> deleteCategoryById(@PathVariable UUID id) {
categoryService.deleteById(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}