62 lines
2.1 KiB
Java
62 lines
2.1 KiB
Java
package com.example.prapro2spring.controller;
|
|
|
|
import com.example.prapro2spring.model.Occupation;
|
|
import com.example.prapro2spring.service.OccupationService;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/occupations")
|
|
public class OccupationController {
|
|
|
|
private final OccupationService occupationService;
|
|
|
|
public OccupationController(OccupationService occupationService) {
|
|
this.occupationService = occupationService;
|
|
}
|
|
|
|
@PostMapping
|
|
public Occupation createOccupation(@RequestBody Occupation occupation) {
|
|
return occupationService.save(occupation);
|
|
}
|
|
|
|
@GetMapping
|
|
public List<Occupation> getAllActivities() {
|
|
return occupationService.findAll();
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public ResponseEntity<Occupation> getOccupationById(@PathVariable Integer id) {
|
|
Occupation occupation = occupationService.findById(id).orElse(null);
|
|
if (occupation != null) {
|
|
return ResponseEntity.ok(occupation);
|
|
} else {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
public ResponseEntity<Occupation> updateOccupation(@PathVariable Integer id, @RequestBody Occupation occupationDetails) {
|
|
Occupation occupation = occupationService.findById(id).orElse(null);
|
|
if (occupation != null) {
|
|
occupation.setName(occupationDetails.getName());
|
|
occupation.setValue(occupationDetails.getValue());
|
|
return ResponseEntity.ok(occupationService.save(occupation));
|
|
} else {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public ResponseEntity<Void> deleteOccupation(@PathVariable Integer id) {
|
|
Occupation occupation = occupationService.findById(id).orElse(null);
|
|
if (occupation != null) {
|
|
occupationService.delete(occupation);
|
|
return ResponseEntity.noContent().build();
|
|
} else {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
}
|
|
} |