62 lines
2.0 KiB
Java
62 lines
2.0 KiB
Java
package com.example.prapro2spring.controller;
|
|
|
|
import com.example.prapro2spring.model.Activity;
|
|
import com.example.prapro2spring.service.ActivityService;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/activities")
|
|
public class ActivityController {
|
|
|
|
private final ActivityService activityService;
|
|
|
|
public ActivityController(ActivityService activityService) {
|
|
this.activityService = activityService;
|
|
}
|
|
|
|
@PostMapping
|
|
public Activity createActivity(@RequestBody Activity activity) {
|
|
return activityService.save(activity);
|
|
}
|
|
|
|
@GetMapping
|
|
public List<Activity> getAllActivities() {
|
|
return activityService.findAll();
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public ResponseEntity<Activity> getActivityById(@PathVariable Integer id) {
|
|
Activity activity = activityService.findById(id).orElse(null);
|
|
if (activity != null) {
|
|
return ResponseEntity.ok(activity);
|
|
} else {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
public ResponseEntity<Activity> updateActivity(@PathVariable Integer id, @RequestBody Activity activityDetails) {
|
|
Activity activity = activityService.findById(id).orElse(null);
|
|
if (activity != null) {
|
|
activity.setName(activityDetails.getName());
|
|
activity.setValue(activityDetails.getValue());
|
|
return ResponseEntity.ok(activityService.save(activity));
|
|
} else {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public ResponseEntity<Void> deleteActivity(@PathVariable Integer id) {
|
|
Activity activity = activityService.findById(id).orElse(null);
|
|
if (activity != null) {
|
|
activityService.delete(activity);
|
|
return ResponseEntity.noContent().build();
|
|
} else {
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
}
|
|
} |