source: backend/src/main/java/com/finki/icare/controller/DiaryController.java

main
Last change on this file was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 2.7 KB
Line 
1package com.finki.icare.controller;
2
3import com.finki.icare.dto.CreateDiaryEntryRequest;
4import com.finki.icare.dto.DiaryEntryDTO;
5import com.finki.icare.dto.UpdateDiaryEntryRequest;
6import com.finki.icare.service.DiaryService;
7import com.finki.icare.utils.AuthUtils;
8import lombok.RequiredArgsConstructor;
9import org.springframework.http.HttpStatus;
10import org.springframework.http.ResponseEntity;
11import org.springframework.security.core.Authentication;
12import org.springframework.web.bind.annotation.*;
13
14import java.util.List;
15
16@RestController
17@RequestMapping("/api/diary")
18@CrossOrigin(origins = "http://localhost:3000")
19@RequiredArgsConstructor
20public class DiaryController {
21
22 private final DiaryService diaryService;
23
24 @GetMapping("/{patientId}")
25 public ResponseEntity<List<DiaryEntryDTO>> getPatientDiaryEntries(
26 @PathVariable Integer patientId,
27 @RequestParam int year,
28 @RequestParam int month,
29 Authentication authentication
30 ) {
31 Integer currentUserId = AuthUtils.getUserId(authentication);
32 String userType = AuthUtils.getUserType(authentication);
33
34 List<DiaryEntryDTO> entries = diaryService.getPatientDiaryEntriesForMonth(
35 patientId, year, month, currentUserId, userType
36 );
37 return ResponseEntity.ok(entries);
38 }
39
40 @PostMapping
41 public ResponseEntity<DiaryEntryDTO> createDiaryEntry(
42 @RequestBody CreateDiaryEntryRequest request,
43 Authentication authentication
44 ) {
45 Integer currentUserId = AuthUtils.getUserId(authentication);
46 String userType = AuthUtils.getUserType(authentication);
47
48 DiaryEntryDTO entry = diaryService.createDiaryEntry(request, currentUserId, userType);
49 return ResponseEntity.status(HttpStatus.CREATED).body(entry);
50 }
51
52 @PutMapping("/{diaryId}")
53 public ResponseEntity<DiaryEntryDTO> updateDiaryEntry(
54 @PathVariable Integer diaryId,
55 @RequestBody UpdateDiaryEntryRequest request,
56 Authentication authentication
57 ) {
58 Integer currentUserId = AuthUtils.getUserId(authentication);
59 String userType = AuthUtils.getUserType(authentication);
60
61 DiaryEntryDTO entry = diaryService.updateDiaryEntry(diaryId, request, currentUserId, userType);
62 return ResponseEntity.ok(entry);
63 }
64
65 @DeleteMapping("/{diaryId}")
66 public ResponseEntity<Void> deleteDiaryEntry(
67 @PathVariable Integer diaryId,
68 Authentication authentication
69 ) {
70 Integer currentUserId = AuthUtils.getUserId(authentication);
71 String userType = AuthUtils.getUserType(authentication);
72
73 diaryService.deleteDiaryEntry(diaryId, currentUserId, userType);
74 return ResponseEntity.noContent().build();
75 }
76}
Note: See TracBrowser for help on using the repository browser.