-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeEntryController.java
More file actions
81 lines (71 loc) · 3.17 KB
/
Copy pathTimeEntryController.java
File metadata and controls
81 lines (71 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ch.fhnw.timerecordingbackend.controller;
import ch.fhnw.timerecordingbackend.dto.time.TimeEntryRequest;
import ch.fhnw.timerecordingbackend.dto.time.TimeEntryResponse;
import ch.fhnw.timerecordingbackend.service.TimeEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;
/**
* REST Controller für Zeiteinträge
* @author PD
* Code von anderen Teammitgliedern oder Quellen wird durch einzelne Kommentare deklariert
* @version 1.0
*/
@RestController
@RequestMapping("/api/time-entries")
public class TimeEntryController {
@Autowired
private TimeEntryService timeEntryService;
@PostMapping
public ResponseEntity<TimeEntryResponse> createTimeEntry(@Valid @RequestBody TimeEntryRequest timeEntryRequest) {
return ResponseEntity.ok(timeEntryService.createTimeEntry(timeEntryRequest));
}
@PutMapping("/{id}")
public ResponseEntity<?> updateTimeEntry(
@PathVariable Long id,
@Valid @RequestBody TimeEntryRequest timeEntryRequest) {
timeEntryService.updateTimeEntry(id, timeEntryRequest);
return ResponseEntity.ok().body(Map.of("message", "Eintrag aktualisiert"));
}
@DeleteMapping("/{id}")
@PreAuthorize("@timeEntryServiceImpl.isOwnerOfTimeEntry(#id) or hasAuthority('ADMIN')")
public ResponseEntity<?> deleteTimeEntry(@PathVariable Long id) {
timeEntryService.deleteTimeEntry(id);
return ResponseEntity.ok().body(Map.of("message", "Eintrag gelöscht"));
}
@GetMapping
public ResponseEntity<Map<String, List<TimeEntryResponse>>> getCurrentUserTimeEntries() {
List<TimeEntryResponse> entries = timeEntryService.getCurrentUserTimeEntries();
return ResponseEntity.ok(Map.of("entries", entries));
}
/**
* Zeiteinträge eines beliebigen Users abrufen
*/
@GetMapping("/user/{userId}")
@PreAuthorize("hasAuthority('ADMIN')")
public ResponseEntity<List<TimeEntryResponse>> getUserTimeEntries(@PathVariable Long userId) {
List<TimeEntryResponse> entries = timeEntryService.getUserTimeEntries(userId);
return ResponseEntity.ok(entries);
}
@PostMapping("/start")
public ResponseEntity<?> startTimeTracking(@RequestBody(required = false) Map<String, Long> body) {
Long projectId = body != null ? body.get("projectId") : null;
return ResponseEntity.ok(timeEntryService.startTimeTracking(projectId));
}
@PostMapping("/{entryId}/stop")
public ResponseEntity<?> stopTimeTracking(@PathVariable Long entryId) {
return ResponseEntity.ok(timeEntryService.stopTimeTracking(entryId));
}
@PostMapping("/{id}/assign-project")
public ResponseEntity<?> assignProject(
@PathVariable Long id,
@RequestBody Map<String, Long> requestBody) {
timeEntryService.assignProject(id, requestBody.get("projectId"));
return ResponseEntity.ok().body(Map.of(
"message", "Zeiteintrag zu Projekt zugewiesen"));
}
}