diff --git a/src/main/java/com/sswugdsc4a/withparents/config/SpringSecurityConfig.java b/src/main/java/com/sswugdsc4a/withparents/config/SpringSecurityConfig.java index 0048ca1..e5d621c 100644 --- a/src/main/java/com/sswugdsc4a/withparents/config/SpringSecurityConfig.java +++ b/src/main/java/com/sswugdsc4a/withparents/config/SpringSecurityConfig.java @@ -1,6 +1,7 @@ package com.sswugdsc4a.withparents.config; import com.sswugdsc4a.withparents.filter.JwtAuthorizationFilter; +import com.sswugdsc4a.withparents.repository.LastApiCallTimeRepository; import com.sswugdsc4a.withparents.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.security.servlet.PathRequest; @@ -19,6 +20,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { private final PropertyConfig propertyConfig; private final UserRepository userRepository; + private final LastApiCallTimeRepository lastApiCallTimeRepository; @Override protected void configure(HttpSecurity http) throws Exception { @@ -32,7 +34,7 @@ protected void configure(HttpSecurity http) throws Exception { // jwt 필터 추가 http.addFilterAfter( - new JwtAuthorizationFilter(userRepository, propertyConfig), + new JwtAuthorizationFilter(userRepository, lastApiCallTimeRepository, propertyConfig), BasicAuthenticationFilter.class ); diff --git a/src/main/java/com/sswugdsc4a/withparents/controller/MedicationController.java b/src/main/java/com/sswugdsc4a/withparents/controller/MedicationController.java index fbe33c5..4c9866d 100644 --- a/src/main/java/com/sswugdsc4a/withparents/controller/MedicationController.java +++ b/src/main/java/com/sswugdsc4a/withparents/controller/MedicationController.java @@ -23,7 +23,8 @@ public MedicationDTO createMedication( body.getUserId(), body.getDescription(), body.getDayOfTheWeekList(), - body.getDosingTime() + body.getDosingTimeList(), + body.getNotificationStatus() ); } @@ -35,7 +36,8 @@ public MedicationDTO modifyMedication( body.getMedicationId(), body.getDescription(), body.getDayOfTheWeekList(), - body.getDosingTime() + body.getDosingTimeList(), + body.getNotificationStatus() ); } diff --git a/src/main/java/com/sswugdsc4a/withparents/controller/PhotoController.java b/src/main/java/com/sswugdsc4a/withparents/controller/PhotoController.java new file mode 100644 index 0000000..d3f42a4 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/controller/PhotoController.java @@ -0,0 +1,52 @@ +package com.sswugdsc4a.withparents.controller; + +import com.sswugdsc4a.withparents.dto.Request.gallery.ModifyPhotoRequest; +import com.sswugdsc4a.withparents.dto.dto.photo.PhotoDTO; +import com.sswugdsc4a.withparents.dto.request.gallery.UploadPhotoRequest; +import com.sswugdsc4a.withparents.entity.Family; +import com.sswugdsc4a.withparents.service.PhotoService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class PhotoController { + + private final PhotoService photoService; + + @PostMapping("/api/gallery/uploadPhoto") + public PhotoDTO uploadPhoto( + @RequestBody UploadPhotoRequest body + ){ + return photoService.uploadPhoto( + body.getImageUrl(), + body.getDescription() + ); + } + @PatchMapping("/api/gallery/modifyPhoto") + public PhotoDTO modifyPhoto( + @RequestBody ModifyPhotoRequest body + ){ + return photoService.modifyPhoto( + body.getPhotoId(), + body.getImageUrl(), + body.getDescription() + ); + } + @DeleteMapping("/api/gallery/deletePhoto") + public void deletePhoto( + @RequestParam Long photoId + ){ + photoService.deletePhoto(photoId); + } + + @GetMapping("/api/gallery/getPhotoList") + public List getPhotoList( + @RequestParam Long familyId + ){ + return photoService.getPhotoList(familyId); + } + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/controller/ScheduleController.java b/src/main/java/com/sswugdsc4a/withparents/controller/ScheduleController.java new file mode 100644 index 0000000..f4546c0 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/controller/ScheduleController.java @@ -0,0 +1,63 @@ +package com.sswugdsc4a.withparents.controller; + +import com.sswugdsc4a.withparents.dto.Request.schedule.ModifyScheduleRequest; +import com.sswugdsc4a.withparents.dto.dto.ScheduleDTO; +import com.sswugdsc4a.withparents.dto.request.schedule.CreateScheduleRequest; +import com.sswugdsc4a.withparents.service.ScheduleService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class ScheduleController { + private final ScheduleService scheduleService; + + @PostMapping("/api/schedule/createSchedule") + public ScheduleDTO createSchedule( + @RequestBody CreateScheduleRequest body + ) { + return scheduleService.createSchedule( + body.getTitle(), + body.getDate(), + body.getTime(), + body.getNotificationStatus() + ); + } + @PatchMapping("/api/schedule/modifySchedule") + public ScheduleDTO modifySchedule( + @RequestBody ModifyScheduleRequest body + ){ + return scheduleService.modifySchedule( + body.getScheduleId(), + body.getTitle(), + body.getDate(), + body.getTime(), + body.getNotificationStatus() + ); + } + + + @GetMapping("/api/schedule/getScheduleList") + public List getScheduleList( + @RequestParam int year, + @RequestParam int month + ) { + return scheduleService.getScheduleList(year, month); + } + + @GetMapping("/api/schedule/getTodayScheduleList") + public List getTodayScheduleList( + ) { + return scheduleService.getTodayScheduleList(); + } + + @DeleteMapping("/api/schedule/deleteSchedule") + public void deleteSchedule( + @RequestParam Long scheduleId + ){ + scheduleService.deleteSchedule(scheduleId); + } +}; + diff --git a/src/main/java/com/sswugdsc4a/withparents/controller/UserController.java b/src/main/java/com/sswugdsc4a/withparents/controller/UserController.java index 6624f13..56839ad 100644 --- a/src/main/java/com/sswugdsc4a/withparents/controller/UserController.java +++ b/src/main/java/com/sswugdsc4a/withparents/controller/UserController.java @@ -1,15 +1,16 @@ package com.sswugdsc4a.withparents.controller; -import com.sswugdsc4a.withparents.dto.dto.user.FamilyDTO; -import com.sswugdsc4a.withparents.dto.dto.user.LocationInfoDTO; -import com.sswugdsc4a.withparents.dto.dto.user.SimpleUserInfoDTO; -import com.sswugdsc4a.withparents.dto.dto.user.UserDTO; +import com.sswugdsc4a.withparents.dto.dto.user.*; import com.sswugdsc4a.withparents.dto.request.user.CreateFamilyRequest; import com.sswugdsc4a.withparents.dto.request.user.ModifyUserInfoRequest; import com.sswugdsc4a.withparents.dto.request.user.SetLocationInfoRequest; +import com.sswugdsc4a.withparents.entity.LastApiCallTime; import com.sswugdsc4a.withparents.service.UserService; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -47,14 +48,14 @@ public UserDTO getUserInfo(){ public LocationInfoDTO setLocationInfo( @RequestBody SetLocationInfoRequest body ){ - return userService.setLocationInfo(body.getLocationInfo()); + return userService.setLocationInfo( + body.getLatitude(), + body.getLatitude()); } @GetMapping("/api/user/getLocationInfo") - public LocationInfoDTO getLocationInfo( - @RequestParam Long userId - ){ - return userService.getLocationInfo(userId); + public List getLocationInfo(){ + return userService.getLocationInfo(); } @GetMapping("/api/user/getFamilyMemberList") @@ -62,4 +63,15 @@ public List getFamilyMemberIdList(){ return userService.getFamilyMemberList(); } + + @GetMapping("/api/user/getHomeInfo") + public HomeInfoDTO getHomeInfo() { + return userService.getHomeInfo(); + } + + @GetMapping("/api/user/getParentsLastApiCallTime") + public List getParentsLastApiCallTime(){ + return userService.getParentsLastApiCallTime(); + } + } diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/gallery/ModifyPhotoRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/gallery/ModifyPhotoRequest.java new file mode 100644 index 0000000..b3c982a --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/gallery/ModifyPhotoRequest.java @@ -0,0 +1,12 @@ +package com.sswugdsc4a.withparents.dto.Request.gallery; + +import lombok.Getter; + +@Getter +public class ModifyPhotoRequest { + + private Long photoId; + private String imageUrl; + private String description; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/gallery/UploadPhotoRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/gallery/UploadPhotoRequest.java new file mode 100644 index 0000000..6ecd46a --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/gallery/UploadPhotoRequest.java @@ -0,0 +1,11 @@ +package com.sswugdsc4a.withparents.dto.request.gallery; + +import lombok.Getter; + +@Getter +public class UploadPhotoRequest { + private Long familyId; + private String imageUrl; + private String description; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/CreateMedicationRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/CreateMedicationRequest.java index af32a6a..8daac39 100644 --- a/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/CreateMedicationRequest.java +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/CreateMedicationRequest.java @@ -3,6 +3,7 @@ import lombok.Getter; import java.time.LocalTime; +import java.util.List; @Getter public class CreateMedicationRequest { @@ -10,6 +11,6 @@ public class CreateMedicationRequest { private Long userId; private String description; private String dayOfTheWeekList; - private LocalTime dosingTime; - + private List dosingTimeList; + private Boolean notificationStatus; } diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/ModifyMedicationRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/ModifyMedicationRequest.java index 7758df7..e270dc5 100644 --- a/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/ModifyMedicationRequest.java +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/medication/ModifyMedicationRequest.java @@ -11,6 +11,7 @@ public class ModifyMedicationRequest { private Long medicationId; private String description; private String dayOfTheWeekList; - private LocalTime dosingTime; + private List dosingTimeList; + private Boolean notificationStatus;; } diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/schedule/CreateScheduleRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/schedule/CreateScheduleRequest.java new file mode 100644 index 0000000..06e0b05 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/schedule/CreateScheduleRequest.java @@ -0,0 +1,14 @@ +package com.sswugdsc4a.withparents.dto.request.schedule; + +import lombok.Getter; + +import java.time.LocalDate; +import java.time.LocalTime; + +@Getter +public class CreateScheduleRequest { + private String title; + private LocalDate date; + private LocalTime time; + private Boolean notificationStatus; +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/schedule/ModifyScheduleRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/schedule/ModifyScheduleRequest.java new file mode 100644 index 0000000..1c11c14 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/schedule/ModifyScheduleRequest.java @@ -0,0 +1,18 @@ +package com.sswugdsc4a.withparents.dto.Request.schedule; + + +import lombok.Getter; + +import java.time.LocalDate; +import java.time.LocalTime; + +@Getter +public class ModifyScheduleRequest { + + private Long scheduleId; + private String title; + private LocalDate date; + private LocalTime time; + private Boolean notificationStatus; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/Request/user/SetLocationInfoRequest.java b/src/main/java/com/sswugdsc4a/withparents/dto/Request/user/SetLocationInfoRequest.java index 6710e5c..4ca62b7 100644 --- a/src/main/java/com/sswugdsc4a/withparents/dto/Request/user/SetLocationInfoRequest.java +++ b/src/main/java/com/sswugdsc4a/withparents/dto/Request/user/SetLocationInfoRequest.java @@ -4,5 +4,6 @@ @Getter public class SetLocationInfoRequest { - private String locationInfo; + private String latitude; + private String longitude; } diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/dto/ScheduleDTO.java b/src/main/java/com/sswugdsc4a/withparents/dto/dto/ScheduleDTO.java new file mode 100644 index 0000000..44bcd03 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/dto/ScheduleDTO.java @@ -0,0 +1,33 @@ +package com.sswugdsc4a.withparents.dto.dto; + +import com.sswugdsc4a.withparents.entity.Schedule; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDate; +import java.time.LocalTime; + +@Getter +@Setter +@AllArgsConstructor +public class ScheduleDTO { + + private Long id; + private Long creatorId; + private String title; + private LocalDate date; + private LocalTime time; + private Boolean notificationStatus; + + public static ScheduleDTO entityToDTO(Schedule e){ + return new ScheduleDTO( + e.getId(), + e.getCreator().getId(), + e.getTitle(), + e.getDate(), + e.getTime(), + e.getNotificationState() + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/dto/medication/MedicationDTO.java b/src/main/java/com/sswugdsc4a/withparents/dto/dto/medication/MedicationDTO.java index 4a6be1c..38e1d28 100644 --- a/src/main/java/com/sswugdsc4a/withparents/dto/dto/medication/MedicationDTO.java +++ b/src/main/java/com/sswugdsc4a/withparents/dto/dto/medication/MedicationDTO.java @@ -6,6 +6,8 @@ import lombok.Setter; import java.time.LocalTime; +import java.util.List; +import java.util.stream.Collectors; @Getter @Setter @@ -16,7 +18,8 @@ public class MedicationDTO { private Long userId; private String description; private String dayOfTheWeekList; - private LocalTime dosingTime; + private List dosingTimes; + private Boolean notificationStatus; public static MedicationDTO entityToDto(Medication e){ return new MedicationDTO( @@ -24,7 +27,11 @@ public static MedicationDTO entityToDto(Medication e){ e.getUser().getId(), e.getDescription(), e.getDayOfTheWeekList(), - e.getDosingTime() + e.getDosingTimeList() + .stream() + .map(t -> {return t.getTime();}) + .collect(Collectors.toList()), + e.getNotificationStatus() ); } diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/dto/photo/PhotoDTO.java b/src/main/java/com/sswugdsc4a/withparents/dto/dto/photo/PhotoDTO.java new file mode 100644 index 0000000..bebedd7 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/dto/photo/PhotoDTO.java @@ -0,0 +1,33 @@ +package com.sswugdsc4a.withparents.dto.dto.photo; + +import com.sswugdsc4a.withparents.entity.Photo; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +@Getter +@Setter +@AllArgsConstructor +public class PhotoDTO { + + private Long id; + private Long creatorId; + private String creatorName; + private String imageUrl; + private String description; + private LocalDateTime regDate; + + public static PhotoDTO entityToDto(Photo e){ + return new PhotoDTO( + e.getId(), + e.getCreator().getId(), + e.getCreator().getNickname(), + e.getImageUrl(), + e.getDescription(), + e.getRegDate() + ); + } + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/HomeInfoDTO.java b/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/HomeInfoDTO.java new file mode 100644 index 0000000..298bc20 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/HomeInfoDTO.java @@ -0,0 +1,22 @@ +package com.sswugdsc4a.withparents.dto.dto.user; + +import com.sswugdsc4a.withparents.dto.dto.ScheduleDTO; +import com.sswugdsc4a.withparents.dto.dto.medication.MedicationDTO; +import com.sswugdsc4a.withparents.dto.dto.photo.PhotoDTO; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +public class HomeInfoDTO { + + private List userList; + private List todayMedicationList; + private List todayScheduleList; + private List recentPhotoList; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationAndNicknameDTO.java b/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationAndNicknameDTO.java new file mode 100644 index 0000000..f668146 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationAndNicknameDTO.java @@ -0,0 +1,15 @@ +package com.sswugdsc4a.withparents.dto.dto.user; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class LocationAndNicknameDTO { + + private Long userId; + private String nickname; + private String latitude; + private String longitude; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationInfoDTO.java b/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationInfoDTO.java index c4cff4c..0f75747 100644 --- a/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationInfoDTO.java +++ b/src/main/java/com/sswugdsc4a/withparents/dto/dto/user/LocationInfoDTO.java @@ -14,13 +14,15 @@ public class LocationInfoDTO { private Long userId; private LocalDateTime lastModifiedDate; - private String locationInfo; + private String latitude; + private String longitude; public static LocationInfoDTO entityToDTO(LocationInfo e){ return new LocationInfoDTO( e.getUserId(), e.getLastModifiedDate(), - e.getLocationInfo() + e.getLatitude(), + e.getLongitude() ); } diff --git a/src/main/java/com/sswugdsc4a/withparents/entity/LastApiCallTime.java b/src/main/java/com/sswugdsc4a/withparents/entity/LastApiCallTime.java new file mode 100644 index 0000000..ab9fb9c --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/entity/LastApiCallTime.java @@ -0,0 +1,24 @@ +package com.sswugdsc4a.withparents.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import javax.persistence.Entity; +import javax.persistence.Id; +import java.time.LocalDateTime; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class LastApiCallTime { + + @Id + private Long userId; + + private LocalDateTime lastApiCallTime; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/entity/LocationInfo.java b/src/main/java/com/sswugdsc4a/withparents/entity/LocationInfo.java index ced0766..d306d8d 100644 --- a/src/main/java/com/sswugdsc4a/withparents/entity/LocationInfo.java +++ b/src/main/java/com/sswugdsc4a/withparents/entity/LocationInfo.java @@ -25,6 +25,8 @@ public class LocationInfo { @LastModifiedDate private LocalDateTime lastModifiedDate; - private String locationInfo; + private String latitude; + + private String longitude; } diff --git a/src/main/java/com/sswugdsc4a/withparents/entity/Medication.java b/src/main/java/com/sswugdsc4a/withparents/entity/Medication.java index 8bb91c4..68ab8ff 100644 --- a/src/main/java/com/sswugdsc4a/withparents/entity/Medication.java +++ b/src/main/java/com/sswugdsc4a/withparents/entity/Medication.java @@ -6,7 +6,8 @@ import lombok.Setter; import javax.persistence.*; -import java.time.LocalTime; +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -30,6 +31,10 @@ public class Medication { @Column(length = 7) private String dayOfTheWeekList; - private LocalTime dosingTime; + @ElementCollection + @CollectionTable(name = "medication_dosing_time", joinColumns = @JoinColumn(name = "medicationId")) + private List dosingTimeList = new ArrayList<>(); + + private Boolean notificationStatus; } diff --git a/src/main/java/com/sswugdsc4a/withparents/entity/MedicationDosingTime.java b/src/main/java/com/sswugdsc4a/withparents/entity/MedicationDosingTime.java new file mode 100644 index 0000000..2db69d5 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/entity/MedicationDosingTime.java @@ -0,0 +1,33 @@ +package com.sswugdsc4a.withparents.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import javax.persistence.*; +import java.time.LocalTime; +import java.util.Objects; + +@Embeddable +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +public class MedicationDosingTime { + + private LocalTime time; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MedicationDosingTime that = (MedicationDosingTime) o; + return Objects.equals(time, that.time); + } + + @Override + public int hashCode() { + return Objects.hash(time); + } +} diff --git a/src/main/java/com/sswugdsc4a/withparents/entity/Photo.java b/src/main/java/com/sswugdsc4a/withparents/entity/Photo.java new file mode 100644 index 0000000..737806c --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/entity/Photo.java @@ -0,0 +1,41 @@ +package com.sswugdsc4a.withparents.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.*; +import java.time.LocalDateTime; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@EntityListeners(AuditingEntityListener.class) +public class Photo { + + @Id + @Column(name = "photo_id") + @GeneratedValue(strategy=GenerationType.IDENTITY) + private Long id; + + @OneToOne + @JoinColumn(name = "creator_id") + private User creator; + + @ManyToOne + @JoinColumn(name = "family_id") + private Family family; + + private String imageUrl; + + private String description; + + @CreatedDate + private LocalDateTime regDate; + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/entity/Schedule.java b/src/main/java/com/sswugdsc4a/withparents/entity/Schedule.java new file mode 100644 index 0000000..4de8261 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/entity/Schedule.java @@ -0,0 +1,40 @@ +package com.sswugdsc4a.withparents.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.springframework.data.annotation.CreatedDate; + +import javax.persistence.*; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class Schedule{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "schedule_id") + private Long id; + + @ManyToOne + @JoinColumn(name = "family_id") + private Family family; + + @ManyToOne + @JoinColumn(name = "creator_id") + private User creator; + + @CreatedDate + private LocalDateTime createDate; + + private String title; + private LocalDate date; + private LocalTime time; + private Boolean notificationState; +} diff --git a/src/main/java/com/sswugdsc4a/withparents/filter/JwtAuthorizationFilter.java b/src/main/java/com/sswugdsc4a/withparents/filter/JwtAuthorizationFilter.java index a2261a7..4d4cc5f 100644 --- a/src/main/java/com/sswugdsc4a/withparents/filter/JwtAuthorizationFilter.java +++ b/src/main/java/com/sswugdsc4a/withparents/filter/JwtAuthorizationFilter.java @@ -1,8 +1,10 @@ package com.sswugdsc4a.withparents.filter; import com.sswugdsc4a.withparents.config.PropertyConfig; +import com.sswugdsc4a.withparents.entity.LastApiCallTime; import com.sswugdsc4a.withparents.entity.User; import com.sswugdsc4a.withparents.exception.CustomException; +import com.sswugdsc4a.withparents.repository.LastApiCallTimeRepository; import com.sswugdsc4a.withparents.repository.UserRepository; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.security.Keys; @@ -18,12 +20,14 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; import java.util.Optional; @RequiredArgsConstructor public class JwtAuthorizationFilter extends OncePerRequestFilter { private final UserRepository userRepository; + private final LastApiCallTimeRepository lastApiCallTimeRepository; private final PropertyConfig propertyConfig; @Override @@ -67,6 +71,8 @@ protected void doFilterInternal( SecurityContextHolder.getContext().setAuthentication(authentication); + lastApiCallTimeRepository.save(new LastApiCallTime(user.get().getId(), LocalDateTime.now())); + }catch (Exception e){ } diff --git a/src/main/java/com/sswugdsc4a/withparents/repository/LastApiCallTimeRepository.java b/src/main/java/com/sswugdsc4a/withparents/repository/LastApiCallTimeRepository.java new file mode 100644 index 0000000..a8fc99a --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/repository/LastApiCallTimeRepository.java @@ -0,0 +1,7 @@ +package com.sswugdsc4a.withparents.repository; + +import com.sswugdsc4a.withparents.entity.LastApiCallTime; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface LastApiCallTimeRepository extends JpaRepository { +} diff --git a/src/main/java/com/sswugdsc4a/withparents/repository/PhotoRepository.java b/src/main/java/com/sswugdsc4a/withparents/repository/PhotoRepository.java new file mode 100644 index 0000000..375ab5f --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/repository/PhotoRepository.java @@ -0,0 +1,16 @@ +package com.sswugdsc4a.withparents.repository; + +import com.sswugdsc4a.withparents.entity.Photo; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +public interface PhotoRepository extends JpaRepository { + + @Query(value = "select * from photo where family_id = ?1 " + + "limit 5 ;" + , nativeQuery = true) + List getRecentPhotos(Long familyId); + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/repository/ScheduleRepository.java b/src/main/java/com/sswugdsc4a/withparents/repository/ScheduleRepository.java new file mode 100644 index 0000000..60016a6 --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/repository/ScheduleRepository.java @@ -0,0 +1,22 @@ +package com.sswugdsc4a.withparents.repository; + +import com.sswugdsc4a.withparents.entity.Schedule; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.time.LocalDate; +import java.util.List; + +public interface ScheduleRepository extends JpaRepository { + + @Query(value = "SELECT * FROM schedule WHERE family_id = ?1 AND " + + "YEAR(date) = ?2 AND MONTH(date) = ?3 ; " + , nativeQuery = true) + List getScheduleByYearAndMonth(Long familyId, int year, int month); + + @Query(value = "SELECT * FROM schedule WHERE family_id = ?1 AND " + + "date = ?2 ; " + , nativeQuery = true) + List getTodaySchedule(Long familyId, LocalDate date); + +} diff --git a/src/main/java/com/sswugdsc4a/withparents/repository/UserRepository.java b/src/main/java/com/sswugdsc4a/withparents/repository/UserRepository.java index c2b5afc..91e9524 100644 --- a/src/main/java/com/sswugdsc4a/withparents/repository/UserRepository.java +++ b/src/main/java/com/sswugdsc4a/withparents/repository/UserRepository.java @@ -2,6 +2,7 @@ import com.sswugdsc4a.withparents.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.List; import java.util.Optional; @@ -11,4 +12,9 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); List findAllByFamilyId(Long familyId); + + @Query(value = "SELECT * from users " + + "WHERE family_id = ?1 AND " + + "is_parent = true", nativeQuery = true) + List getParents(Long familyId); } diff --git a/src/main/java/com/sswugdsc4a/withparents/service/MedicationService.java b/src/main/java/com/sswugdsc4a/withparents/service/MedicationService.java index fbb5eb0..dad52a7 100644 --- a/src/main/java/com/sswugdsc4a/withparents/service/MedicationService.java +++ b/src/main/java/com/sswugdsc4a/withparents/service/MedicationService.java @@ -2,6 +2,7 @@ import com.sswugdsc4a.withparents.dto.dto.medication.MedicationDTO; import com.sswugdsc4a.withparents.entity.Medication; +import com.sswugdsc4a.withparents.entity.MedicationDosingTime; import com.sswugdsc4a.withparents.entity.User; import com.sswugdsc4a.withparents.exception.CustomException; import com.sswugdsc4a.withparents.repository.MedicationRepository; @@ -26,27 +27,26 @@ public MedicationDTO createMedication( Long userId, String description, String dayOfTheWeekList, - LocalTime dosingTime + List dosingTimeList, + Boolean notificationStatus ) { if (!userService.areTheyAFamily(userId)) { throw new CustomException("Family id is different"); } - return MedicationDTO.entityToDto( - medicationRepository.save( - new Medication( - null, - userService.getUserById(userId), - description, - dayOfTheWeekList, - dosingTime - - ) - ) + Medication medication = medicationRepository.save( + new Medication( + null, + userService.getUserById(userId), + description, + dayOfTheWeekList, + dosingTimeList.stream().map(t -> new MedicationDosingTime(t)).collect(Collectors.toList()), + notificationStatus + ) ); - + return MedicationDTO.entityToDto(medication); } @Transactional @@ -54,7 +54,8 @@ public MedicationDTO modifyMedication( Long medicationId, String description, String dayOfTheWeekList, - LocalTime dosingTime + List dosingTimeList, + Boolean notificationStatus ) { Medication medication = medicationRepository.findById(medicationId) @@ -73,8 +74,18 @@ public MedicationDTO modifyMedication( medication.setDayOfTheWeekList(dayOfTheWeekList); } - if (dosingTime != null) { - medication.setDosingTime(dosingTime); + if (dosingTimeList != null) { + medication.setDosingTimeList( + dosingTimeList + .stream() + .map(t -> {return new MedicationDosingTime(t);}) + .collect(Collectors.toList() + ) + ); + } + + if (notificationStatus != null) { + medication.setNotificationStatus(notificationStatus); } return MedicationDTO.entityToDto(medication); diff --git a/src/main/java/com/sswugdsc4a/withparents/service/PhotoService.java b/src/main/java/com/sswugdsc4a/withparents/service/PhotoService.java new file mode 100644 index 0000000..117084e --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/service/PhotoService.java @@ -0,0 +1,99 @@ +package com.sswugdsc4a.withparents.service; + +import com.sswugdsc4a.withparents.dto.dto.photo.PhotoDTO; +import com.sswugdsc4a.withparents.entity.Family; +import com.sswugdsc4a.withparents.entity.Photo; +import com.sswugdsc4a.withparents.entity.User; +import com.sswugdsc4a.withparents.exception.CustomException; +import com.sswugdsc4a.withparents.repository.PhotoRepository; +import lombok.RequiredArgsConstructor; +import org.hibernate.boot.model.naming.IllegalIdentifierException; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class PhotoService { + + private final PhotoRepository photoRepository; + private final UserService userService; + + public PhotoDTO uploadPhoto( + String imageUrl, + String description + ){ + User user = userService.getUser(); + + if (userService.getUser().getFamily() == null) { + throw new CustomException("Family id does not exist"); + } + + return PhotoDTO.entityToDto( + photoRepository.save(new Photo( + null, + user, + user.getFamily(), + imageUrl, + description, + null + )) + ); + } + + @Transactional + public PhotoDTO modifyPhoto( + Long photoId, + String imageUrl, + String description + ){ + Photo photo = photoRepository.findById(photoId) + .orElseThrow(()-> new CustomException("invalid photo id")); + + if (photoId == null){ + throw new CustomException("Please enter photoId"); + } + + if(!userService.areTheyAFamily(photo.getCreator().getId())){ + throw new CustomException("Family id is different"); + } + if(imageUrl != null){ + photo.setImageUrl(imageUrl); + } + if(description != null){ + photo.setDescription(description); + } + return PhotoDTO.entityToDto(photo); + } + + @Transactional + public void deletePhoto( + Long photoId + ){ + Photo photo = photoRepository.findById(photoId) + .orElseThrow(()-> new CustomException("invalid photo id")); + + if (photo.getCreator().getId() != userService.getUser().getId()){ + throw new CustomException("Only photo creator can be deleted"); + } + photoRepository.delete(photo); + } + public List getPhotoList(Long familyId) { + + User user = userService.getUser(); + + if (userService.getUser().getFamily() == null){ + throw new CustomException("Family id does not exist"); + } + return photoRepository.getRecentPhotos(userService.getUser().getFamily().getId()) + .stream() + .map(e-> PhotoDTO.entityToDto(e)) + .collect(Collectors.toList()); + } + +} + + + diff --git a/src/main/java/com/sswugdsc4a/withparents/service/ScheduleService.java b/src/main/java/com/sswugdsc4a/withparents/service/ScheduleService.java new file mode 100644 index 0000000..a4b118e --- /dev/null +++ b/src/main/java/com/sswugdsc4a/withparents/service/ScheduleService.java @@ -0,0 +1,123 @@ +package com.sswugdsc4a.withparents.service; + +import com.sswugdsc4a.withparents.dto.dto.ScheduleDTO; +import com.sswugdsc4a.withparents.entity.Schedule; +import com.sswugdsc4a.withparents.exception.CustomException; +import com.sswugdsc4a.withparents.repository.ScheduleRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.time.LocalDate; +import java.time.LocalTime; +import java.util.List; +import java.util.stream.Collectors; + + +@Service +@RequiredArgsConstructor +public class ScheduleService { + + private final ScheduleRepository scheduleRepository; + private final UserService userService; + + @Transactional + public ScheduleDTO createSchedule( + String title, + LocalDate date, + LocalTime time, + Boolean notificationStatus + ) { + if (userService.getUser().getFamily() == null) { + throw new CustomException("Family id does not exist"); + } + + return ScheduleDTO.entityToDTO( + scheduleRepository.save( + new Schedule( + null, + userService.getUser().getFamily(), + userService.getUser(), + null, + title, + date, + time, + notificationStatus + ) + ) + ); + } + + @Transactional + public ScheduleDTO modifySchedule( + Long scheduleId, + String title, + LocalDate date, + LocalTime time, + Boolean notificationStatus + ){ + Schedule schedule = scheduleRepository.findById(scheduleId) + .orElseThrow(() -> new CustomException("invalid schedule id")); + + if (!userService.areTheyAFamily(schedule.getCreator().getId())){ + throw new CustomException("Family id is different"); + } + if(title != null){ + schedule.setTitle(title); + } + if(date != null){ + schedule.setDate(date); + } + if(time != null){ + schedule.setTime(time); + } + if(notificationStatus != null){ + schedule.setNotificationState(notificationStatus); + } + return ScheduleDTO.entityToDTO(schedule); + + } + + @Transactional + public void deleteSchedule( + Long scheduleId + ){ + Schedule schedule = scheduleRepository.findById(scheduleId) + .orElseThrow(()->new CustomException("invalid schedule id")); + + if (schedule.getCreator().getId() != userService.getUser().getId()) { + throw new CustomException("Only schedule creator can be deleted"); + } + + scheduleRepository.delete(schedule); + } + + public List getScheduleList( + int year, + int month + ) { + + if (userService.getUser().getFamily() == null) { + throw new CustomException("Family id does not exist"); + } + + return scheduleRepository.getScheduleByYearAndMonth( + userService.getUser().getFamily().getId(), year, month) + .stream() + .map(e -> {return ScheduleDTO.entityToDTO(e);}) + .collect(Collectors.toList()); + } + + public List getTodayScheduleList(){ + + if (userService.getUser().getFamily() == null) { + throw new CustomException("Family id does not exist"); + } + + return scheduleRepository.getTodaySchedule(userService.getUser().getFamily().getId(), LocalDate.now()) + .stream() + .map(e -> {return ScheduleDTO.entityToDTO(e);}) + .collect(Collectors.toList()); + + } +} diff --git a/src/main/java/com/sswugdsc4a/withparents/service/UserService.java b/src/main/java/com/sswugdsc4a/withparents/service/UserService.java index 008f52f..2e1f320 100644 --- a/src/main/java/com/sswugdsc4a/withparents/service/UserService.java +++ b/src/main/java/com/sswugdsc4a/withparents/service/UserService.java @@ -1,21 +1,22 @@ package com.sswugdsc4a.withparents.service; -import com.sswugdsc4a.withparents.dto.dto.user.FamilyDTO; -import com.sswugdsc4a.withparents.dto.dto.user.LocationInfoDTO; -import com.sswugdsc4a.withparents.dto.dto.user.SimpleUserInfoDTO; -import com.sswugdsc4a.withparents.dto.dto.user.UserDTO; +import com.sswugdsc4a.withparents.dto.dto.ScheduleDTO; +import com.sswugdsc4a.withparents.dto.dto.medication.MedicationDTO; +import com.sswugdsc4a.withparents.dto.dto.photo.PhotoDTO; +import com.sswugdsc4a.withparents.dto.dto.user.*; import com.sswugdsc4a.withparents.entity.Family; +import com.sswugdsc4a.withparents.entity.LastApiCallTime; import com.sswugdsc4a.withparents.entity.LocationInfo; import com.sswugdsc4a.withparents.entity.User; import com.sswugdsc4a.withparents.exception.CustomException; -import com.sswugdsc4a.withparents.repository.FamilyRepository; -import com.sswugdsc4a.withparents.repository.LocationInfoRepository; -import com.sswugdsc4a.withparents.repository.UserRepository; +import com.sswugdsc4a.withparents.repository.*; import lombok.RequiredArgsConstructor; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import javax.transaction.Transactional; +import java.time.LocalDate; +import java.util.Calendar; import java.util.List; import java.util.stream.Collectors; @@ -26,6 +27,10 @@ public class UserService { private final UserRepository userRepository; private final FamilyRepository familyRepository; private final LocationInfoRepository locationInfoRepository; + private final MedicationRepository medicationRepository; + private final LastApiCallTimeRepository lastApiCallTimeRepository; + private final ScheduleRepository scheduleRepository; + private final PhotoRepository photoRepository; public User getUser(){ String userId = SecurityContextHolder.getContext().getAuthentication().getName(); @@ -129,7 +134,8 @@ public UserDTO modifyUserInfo( } public LocationInfoDTO setLocationInfo( - String locationInfo + String latitude, + String longitude ) { return LocationInfoDTO.entityToDTO( @@ -137,27 +143,47 @@ public LocationInfoDTO setLocationInfo( new LocationInfo( getUser().getId(), null, - locationInfo + latitude, + longitude ) ) ); } - public LocationInfoDTO getLocationInfo(Long userId) { + public List getLocationInfo() { - if (!areTheyAFamily(userId)) { - throw new CustomException("Family id is different"); + User user = getUser(); + + if (user.getFamily() == null) { + throw new CustomException("Family id does not exist"); } - LocationInfo locationInfo = locationInfoRepository.findById(userId).orElse(null); + List parents = userRepository.getParents(user.getFamily().getId()); - if (locationInfo == null) { - return null; - } + return parents + .stream() + .map(p -> { + LocationInfo locationInfo = locationInfoRepository.findById(p.getId()).orElse(null); - return LocationInfoDTO.entityToDTO(locationInfo); + if (locationInfo == null) { + return new LocationAndNicknameDTO( + p.getId(), + p.getNickname(), + null, + null + ); + } + + return new LocationAndNicknameDTO( + p.getId(), + p.getNickname(), + locationInfo.getLatitude(), + locationInfo.getLongitude() + ); + }) + .collect(Collectors.toList()); } public List getFamilyMemberList(){ @@ -175,4 +201,70 @@ public List getFamilyMemberList(){ } + public HomeInfoDTO getHomeInfo() { + + HomeInfoDTO response = new HomeInfoDTO(); + response.setUserList(getFamilyMemberList()); + response.setTodayMedicationList(getTodayMedicationList()); + response.setTodayScheduleList(getTodayScheduleList()); + response.setRecentPhotoList(getRecentPhotoList()); + + return response; + + } + + private List getTodayMedicationList() { + + int dayOfWeekNumber = switch (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) { + case 1 -> 6; // sun + case 2 -> 0; // mon + case 3 -> 1; // tue + case 4 -> 2; // wed + case 5 -> 3; // thu + case 6 -> 4; // fri + case 7 -> 5; // sat + default -> 8; // ??? + }; + + return medicationRepository.findAllByUserId(getUser().getId()) + .stream() + .filter(e -> { + return e.getDayOfTheWeekList().charAt(dayOfWeekNumber) == '1'; + }) + .map(e -> MedicationDTO.entityToDto(e)) + .collect(Collectors.toList()); + + } + + private List getTodayScheduleList(){ + + return scheduleRepository.getTodaySchedule(getUser().getFamily().getId(), LocalDate.now()) + .stream() + .map(e -> {return ScheduleDTO.entityToDTO(e);}) + .collect(Collectors.toList()); + + } + + private List getRecentPhotoList(){ + return photoRepository.getRecentPhotos(getUser().getFamily().getId()) + .stream() + .map(e -> PhotoDTO.entityToDto(e)) + .collect(Collectors.toList()); + } + + public List getParentsLastApiCallTime() { + + User user = getUser(); + + if (user.getFamily() == null) { + throw new CustomException("Family id does not exist"); + } + + return userRepository.getParents(user.getFamily().getId()) + .stream() + .map(e -> lastApiCallTimeRepository.findById(e.getId()) + .orElse(new LastApiCallTime(e.getId(), null))) + .collect(Collectors.toList()); + + } }