| 1 | package com.finki.icare.controller;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.CreateDiaryEntryRequest;
|
|---|
| 4 | import com.finki.icare.dto.DiaryEntryDTO;
|
|---|
| 5 | import com.finki.icare.dto.UpdateDiaryEntryRequest;
|
|---|
| 6 | import com.finki.icare.service.DiaryService;
|
|---|
| 7 | import com.finki.icare.utils.AuthUtils;
|
|---|
| 8 | import lombok.RequiredArgsConstructor;
|
|---|
| 9 | import org.springframework.http.HttpStatus;
|
|---|
| 10 | import org.springframework.http.ResponseEntity;
|
|---|
| 11 | import org.springframework.security.core.Authentication;
|
|---|
| 12 | import org.springframework.web.bind.annotation.*;
|
|---|
| 13 |
|
|---|
| 14 | import java.util.List;
|
|---|
| 15 |
|
|---|
| 16 | @RestController
|
|---|
| 17 | @RequestMapping("/api/diary")
|
|---|
| 18 | @CrossOrigin(origins = "http://localhost:3000")
|
|---|
| 19 | @RequiredArgsConstructor
|
|---|
| 20 | public 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 | }
|
|---|