feat: added occupation stack

This commit is contained in:
Marcin Alchimowicz 2024-01-17 20:07:27 +01:00
parent 72a5d8c593
commit 1ec5555423
2 changed files with 71 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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> {
}