diff --git a/src/main/java/com/example/prapro2spring/controller/OccupationController.java b/src/main/java/com/example/prapro2spring/controller/OccupationController.java new file mode 100644 index 0000000..efd440c --- /dev/null +++ b/src/main/java/com/example/prapro2spring/controller/OccupationController.java @@ -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 getAllActivities() { + return occupationService.findAll(); + } + + @GetMapping("/{id}") + public ResponseEntity 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 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 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(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/prapro2spring/service/OccupationService.java b/src/main/java/com/example/prapro2spring/service/OccupationService.java new file mode 100644 index 0000000..8d46a40 --- /dev/null +++ b/src/main/java/com/example/prapro2spring/service/OccupationService.java @@ -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 { +} \ No newline at end of file