src/main/java/hexlet/code/controller/LabelsController.java
package hexlet.code.controller;
import hexlet.code.dto.LabelDTO;
import hexlet.code.repository.LabelRepository;
import hexlet.code.service.LabelService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/labels")
public final class LabelsController {
@Autowired
private LabelRepository labelRepository;
@Autowired
private LabelService labelService;
@GetMapping(path = "")
public ResponseEntity<List<LabelDTO>> getAll() {
var labels = labelService.getAll();
return ResponseEntity
.status(HttpStatus.OK)
.header("X-Total-Count", String.valueOf(labels.size()))
.body(labels);
}
@GetMapping(path = "/{id}")
@ResponseStatus(HttpStatus.OK)
public LabelDTO getById(@PathVariable Long id) {
return labelService.findById(id);
}
@PostMapping(path = "")
@ResponseStatus(HttpStatus.CREATED)
public LabelDTO create(@Valid @RequestBody LabelDTO data) {
return labelService.create(data);
}
@PutMapping(path = "/{id}")
@ResponseStatus(HttpStatus.OK)
public LabelDTO update(@Valid @RequestBody LabelDTO data, @PathVariable Long id) {
return labelService.update(data, id);
}
@DeleteMapping(path = "/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
labelService.delete(id);
}
}