forked from s485936/PRAPRO2
feat: added occupation stack
This commit is contained in:
parent
72a5d8c593
commit
1ec5555423
@ -0,0 +1,62 @@
|
||||
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 Long 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 Long 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 Long id) {
|
||||
Occupation occupation = occupationService.findById(id).orElse(null);
|
||||
if (occupation != null) {
|
||||
occupationService.delete(occupation);
|
||||
return ResponseEntity.noContent().build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.prapro2spring.service;
|
||||
|
||||
import com.example.prapro2spring.model.Occupation;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public interface OccupationService extends JpaRepository<Occupation, Long> {
|
||||
}
|
Loading…
Reference in New Issue
Block a user