source: backend/src/main/java/com/finki/icare/service/DiaryService.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: 5.2 KB
RevLine 
[700e2f9]1package com.finki.icare.service;
2
3import com.finki.icare.dto.CreateDiaryEntryRequest;
4import com.finki.icare.dto.DiaryEntryDTO;
5import com.finki.icare.dto.UpdateDiaryEntryRequest;
6import com.finki.icare.enums.UserType;
7import com.finki.icare.exceptions.ICareException;
8import com.finki.icare.mapper.DiaryMapper;
9import com.finki.icare.model.Diary;
10import com.finki.icare.model.Patient;
11import com.finki.icare.repository.DiaryRepository;
12import com.finki.icare.repository.PatientRepository;
13import lombok.RequiredArgsConstructor;
14import org.springframework.stereotype.Service;
15
16import java.time.LocalDate;
17import java.util.List;
18
19@Service
20@RequiredArgsConstructor
21public class DiaryService {
22
23 private final DiaryRepository diaryRepository;
24 private final PatientRepository patientRepository;
25 private final DiaryMapper diaryMapper;
26
27 public List<DiaryEntryDTO> getPatientDiaryEntriesForMonth(Integer patientId, int year, int month, Integer currentUserId, String userType) {
28 if (!canAccessDiary(patientId, currentUserId, userType)) {
29 throw ICareException.forbidden("You do not have permission to access this patient's diary");
30 }
31
32 LocalDate startDate = LocalDate.of(year, month, 1);
33 LocalDate endDate = startDate.withDayOfMonth(startDate.lengthOfMonth());
34
35 List<Diary> diaries = diaryRepository.findByPatientIdAndDateRange(patientId, startDate, endDate);
36
37 return diaries.stream()
38 .map(diary -> {
39 DiaryEntryDTO dto = diaryMapper.toDTO(diary);
40 if (UserType.THERAPIST.equals(userType)) {
41 dto.setContent(null);
42 }
43 return dto;
44 })
45 .toList();
46 }
47
48 public DiaryEntryDTO createDiaryEntry(CreateDiaryEntryRequest request, Integer currentUserId, String userType) {
49 if (!UserType.PATIENT.equals(userType)) {
50 throw ICareException.forbidden("Only patients can create diary entries");
51 }
52
53 LocalDate today = LocalDate.now();
54 if (diaryRepository.findByPatientIdAndDate(currentUserId, today).isPresent()) {
55 throw ICareException.conflict("A diary entry already exists for this date");
56 }
57
58 if (request.getDailyRating() < 1 || request.getDailyRating() > 10) {
59 throw ICareException.badRequest("Daily rating must be between 1 and 10");
60 }
61
62 Patient patient = patientRepository
63 .findById(currentUserId)
64 .orElseThrow(() -> ICareException.notFound("Patient not found"));
65
66 Diary diary = new Diary();
67 diary.setPatient(patient);
68 diary.setDate(today);
69 diary.setDailyRating(request.getDailyRating());
70 diary.setContent(request.getContent());
71
72 Diary savedDiary = diaryRepository.save(diary);
73 return diaryMapper.toDTO(savedDiary);
74 }
75
76 public DiaryEntryDTO updateDiaryEntry(Integer diaryId, UpdateDiaryEntryRequest request, Integer currentUserId, String userType) {
77 Diary diary = diaryRepository.findById(diaryId)
78 .orElseThrow(() -> ICareException.notFound("Diary entry not found"));
79
80 if (!canAccessDiary(diary.getPatient().getIdUser(), currentUserId, userType)) {
81 throw ICareException.forbidden("You do not have permission to update this diary entry");
82 }
83
84 if (!UserType.PATIENT.equals(userType) || !diary.getPatient().getIdUser().equals(currentUserId)) {
85 throw ICareException.forbidden("Only the owner of this diary entry can update it");
86 }
87
88 diary.setContent(request.getContent());
89
90 if (request.getDailyRating() != null) {
91 if (!diary.getDate().equals(LocalDate.now())) {
92 throw ICareException.badRequest("Rating can only be changed for today's entry");
93 }
94 if (request.getDailyRating() < 1 || request.getDailyRating() > 10) {
95 throw ICareException.badRequest("Daily rating must be between 1 and 10");
96 }
97 diary.setDailyRating(request.getDailyRating());
98 }
99
100 Diary updatedDiary = diaryRepository.save(diary);
101 return diaryMapper.toDTO(updatedDiary);
102 }
103
104 public void deleteDiaryEntry(Integer diaryId, Integer currentUserId, String userType) {
105 Diary diary = diaryRepository
106 .findById(diaryId)
107 .orElseThrow(() -> ICareException.notFound("Diary entry not found"));
108
109 if (!diary.getDate().equals(LocalDate.now())) {
110 throw ICareException.badRequest("You can only delete today's diary entry");
111 }
112
113 if (!UserType.PATIENT.equals(userType) || !diary.getPatient().getIdUser().equals(currentUserId)) {
114 throw ICareException.forbidden("Only the patient who created the entry can delete it");
115 }
116
117 diaryRepository.delete(diary);
118 }
119
120 private boolean canAccessDiary(Integer patientId, Integer currentUserId, String userType) {
121 if (UserType.PATIENT.equals(userType) && patientId.equals(currentUserId)) {
122 return true;
123 }
124
125 if (UserType.THERAPIST.equals(userType)) {
126 return patientRepository.isPatientAssignedToTherapist(patientId, currentUserId);
127 }
128
129 return false;
130 }
131}
Note: See TracBrowser for help on using the repository browser.