View Javadoc
1   package fr.tiogars.chat.domains.nomenclature.controllers;
2   
3   import java.net.URI;
4   import java.util.List;
5   import fr.tiogars.chat.domains.nomenclature.models.Nomenclature;
6   import fr.tiogars.chat.domains.nomenclature.models.CreateNomenclatureRequest;
7   import fr.tiogars.chat.domains.nomenclature.models.UpdateNomenclatureRequest;
8   import fr.tiogars.chat.domains.taxonomy.TaxonomyService;
9   import jakarta.validation.Valid;
10  import org.springframework.http.ResponseEntity;
11  import org.springframework.web.bind.annotation.RestController;
12  import org.springframework.web.bind.annotation.RequestMapping;
13  import org.springframework.web.bind.annotation.GetMapping;
14  import org.springframework.web.bind.annotation.PostMapping;
15  import org.springframework.web.bind.annotation.PutMapping;
16  import org.springframework.web.bind.annotation.DeleteMapping;
17  import org.springframework.web.bind.annotation.PathVariable;
18  import org.springframework.web.bind.annotation.RequestBody;
19  
20  @RestController
21  @RequestMapping("/api/v1/nomenclature")
22  public class NomenclatureController {
23      private final TaxonomyService service;
24  
25      public NomenclatureController(TaxonomyService service) {
26          this.service = service;
27      }
28  
29      @GetMapping
30      public List<Nomenclature> findAll() {
31          return service.nomenclatures();
32      }
33  
34      @GetMapping("/{id}")
35      public Nomenclature findById(@PathVariable Long id) {
36          return service.nomenclature(id);
37      }
38  
39      @PostMapping
40      public ResponseEntity<Nomenclature> create(@Valid @RequestBody CreateNomenclatureRequest request) {
41          Nomenclature created = service.createNomenclature(request);
42          return ResponseEntity.created(URI.create("/api/v1/nomenclature/" + created.id())).body(created);
43      }
44  
45      @PutMapping("/{id}")
46      public Nomenclature update(@PathVariable Long id, @Valid @RequestBody UpdateNomenclatureRequest request) {
47          return service.updateNomenclature(id, request);
48      }
49  
50      @DeleteMapping("/{id}")
51      public ResponseEntity<Void> delete(@PathVariable Long id) {
52          service.deleteNomenclature(id);
53          return ResponseEntity.noContent().build();
54      }
55  }