Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions server/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1057,3 +1057,23 @@ include::{snippets}/api/plants/info/1/response-fields.adoc[]
==== 실패 Response
실패 1.
include::{snippets}/api/plants/info/2/http-response.adoc[]


=== **14. 달력에서 그린룸 활동 정보 조회**

특정 날짜의 모든 그린룸 활동 내역들을 조회함.

==== Request
include::{snippets}/api/greenroom/calendar/1/http-request.adoc[]

==== Request Headers
include::{snippets}/api/greenroom/calendar/1/request-headers.adoc[]

==== Request Query Parameter Fields
include::{snippets}/api/greenroom/calendar/1/query-parameters.adoc[]

==== 성공 Response
include::{snippets}/api/greenroom/calendar/1/http-response.adoc[]

==== Response Body Fields
include::{snippets}/api/greenroom/calendar/1/response-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class BaseTime {
@Column(updatable = false,insertable = false)
protected LocalDateTime createDate;


@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@Column(insertable = false,updatable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@RestController
Expand Down Expand Up @@ -100,6 +101,10 @@ public ResponseEntity<ApiResponse> updateActivity(@PathVariable(value = "greenro
return ResponseEntity.ok(ApiResponse.success(ResponseCodeEnum.SUCCESS,greenroomService.updateActivity(activityInfoUpdateRequestDto,greenroomId)));
}

@GetMapping("/calendar")
public ResponseEntity<ApiResponse> getCalender(@AuthenticationPrincipal User user, @RequestParam(value = "date") LocalDate date, @RequestParam (value = "type",required = false) Long activityId){
return ResponseEntity.ok(ApiResponse.success(ResponseCodeEnum.SUCCESS,greenroomService.getGreenroomInfoFromCalendar(user.getUsername(),date,activityId)));
}



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.greenroom.server.api.domain.greenroom.dto.out;

import com.greenroom.server.api.domain.greenroom.entity.*;
import com.greenroom.server.api.global.config.PropertiesHolder;

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;

public record GreenroomCalendarResponseDto(DateInfo dateInfo, List<CalendarInfo> mainInfo) {
public static GreenroomCalendarResponseDto from(LocalDate date, List<CalendarInfo> mainInfo){
return new GreenroomCalendarResponseDto(DateInfo.from(date),mainInfo);
}

public record CalendarInfo(GreenroomInfo greenroomInfo, List<TodoInfo> activity , List<DiaryInfo> diary){
public static CalendarInfo of(GreenroomInfo greenroomInfo,List<TodoInfo> activity, List<DiaryInfo> diary){
return new CalendarInfo(greenroomInfo,activity,diary);
}
}

public record GreenroomInfo(Long greenroomId, String greenroomName){
public static GreenroomInfo from(GreenRoom greenRoom){
return new GreenroomInfo(greenRoom.getGreenroomId(),greenRoom.getName());
}
}
public record DateInfo(Integer year, Integer month, Integer date,String day){
public static DateInfo from(LocalDate date){
return new DateInfo(date.getYear(),date.getMonthValue(),date.getDayOfMonth(),date.getDayOfWeek()
.getDisplayName(TextStyle.SHORT, Locale.KOREAN));
}
}
public record TodoInfo(Long activityId, String activityName, Integer remainingDays, Boolean isCompleted){
public static TodoInfo from(Todo todo){
Integer remainingDays = todo.getNextTodoDate().getDayOfYear() - LocalDate.now().getDayOfYear();
return new TodoInfo(todo.getActivity().getActivityId(),todo.getActivity().getActivityName(),remainingDays,false);
}
public static TodoInfo from(TodoLog todoLog){
Activity activity = todoLog.getActivity();
return new TodoInfo(activity.getActivityId(),activity.getActivityName(),null,true);
}
}
public record DiaryInfo(Long diaryId, String title, String body, String imageUrl){
public static DiaryInfo from(Diary diary){
String imageUrl = diary.getDiaryPictureUrl()==null?null:PropertiesHolder.CDN_PATH+"/"+diary.getDiaryPictureUrl();
return new DiaryInfo(diary.getDiaryId(),diary.getTitle(),diary.getContent(),imageUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
public enum GreenRoomStatus {

ENABLED("활성화됨"),
DISABLED("비활성화됨"),
DELETED("삭제됨"),
OTHER("기타");
DISABLED("비활성화됨");

private final String description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.greenroom.server.api.domain.greenroom.dto.in.DiaryImageSimpleDto;
import com.greenroom.server.api.domain.greenroom.entity.Diary;
import com.greenroom.server.api.domain.greenroom.entity.GreenRoom;
import com.greenroom.server.api.domain.greenroom.entity.TodoLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

Expand All @@ -26,4 +28,7 @@ public interface DiaryRepository extends JpaRepository<Diary,Long> {

void deleteAllByGreenRoom(GreenRoom greenRoom);

@Query("select d from Diary d where FUNCTION('DATE', d.createDate)=:date And d.greenRoom.greenroomId in :greenRooms ")
List<Diary> findByCreateDateAndGreenRoomIn(@Param("date")LocalDate date, @Param("greenRooms") List<Long> greenRoom);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public interface GreenRoomRepository extends JpaRepository<GreenRoom,Long> {
void deleteAllByGreenroomId(@Param("ids")Collection<Long> greenroomId);



@EntityGraph(attributePaths = {"plant"})
List<GreenRoom> findGreenRoomByUserAndGreenroomStatus(User user, GreenRoomStatus greenRoomStatus);

@EntityGraph(attributePaths = {"plant"})
List<GreenRoom> findGreenRoomByUser(User user);

@EntityGraph(attributePaths = {"plant"})
Optional<GreenRoom> findByGreenroomIdAndGreenroomStatus(Long greenroomId, GreenRoomStatus greenRoomStatus);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.greenroom.server.api.domain.greenroom.repository;

import com.greenroom.server.api.domain.greenroom.entity.GreenRoom;
import com.greenroom.server.api.domain.greenroom.entity.TodoLog;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Repository
public interface TodoLogRepository extends JpaRepository<TodoLog,Long> {
Expand All @@ -18,4 +23,8 @@ public interface TodoLogRepository extends JpaRepository<TodoLog,Long> {
@Query("delete from TodoLog tl where tl.greenRoom.greenroomId =(:id)")
@Modifying
void deleteAllByGreenRoomGreenroomId(@Param("id")Long greenroomId);

@EntityGraph(attributePaths ={"activity"})
@Query("select tl from TodoLog tl where FUNCTION('DATE', tl.createDate) =:date And tl.greenRoom.greenroomId in (:greenRooms) ")
List<TodoLog> findByCreateDateAndGreenRoomIn( @Param("date") LocalDate date, @Param("greenRooms") List<Long> greenRooms);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.greenroom.server.api.domain.greenroom.entity.GreenRoom;
import com.greenroom.server.api.domain.greenroom.entity.Todo;
import com.greenroom.server.api.domain.greenroom.entity.TodoLog;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

Expand All @@ -26,4 +28,11 @@ public interface TodoRepository extends JpaRepository<Todo, Long> {
List<Todo> findAllByGreenRoomAndActivity(@Param("greenroomId")Long greenroomId,@Param("activityIds") List<Long> activityIds );

List<Todo> findAllByGreenRoom(GreenRoom greenRoom);

@EntityGraph(attributePaths ={"activity"})
@Query("select t from Todo t where FUNCTION('DATE', t.nextTodoDate)<=:date And t.greenRoom.greenroomId in :greenRooms ")
List<Todo> findByNextTodoDateAndGreenRoomIn(@Param("date") LocalDate nextTodoDate,@Param("greenRooms")List<Long> greenRoomList);
@EntityGraph(attributePaths ={"activity"})
@Query("select t from Todo t where FUNCTION('DATE', t.nextTodoDate) =:date And t.greenRoom.greenroomId in :greenRooms ")
List<Todo> findByNextTodoDateAndGreenRoomInFuture(@Param("date") LocalDate date,@Param("greenRooms")List<Long> greenRoomList);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.greenroom.server.api.domain.greenroom.service;

import com.amazonaws.util.StringUtils;
import com.greenroom.server.api.domain.greenroom.entity.Diary;
import com.greenroom.server.api.domain.greenroom.entity.GreenRoom;
import com.greenroom.server.api.domain.greenroom.repository.DiaryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -29,4 +32,8 @@ public List<String> deleteAllByGreenRoom(List<Long> greenroomIdList){
diaryRepository.deleteAllByIdInBatch(deletedDiaryIdList);
return imageDeleteList;
}

public List<Diary> getAllDiariesByGreenroomAndDate(List<GreenRoom>greenRoomList, LocalDate date){
return diaryRepository.findByCreateDateAndGreenRoomIn(date,greenRoomList.stream().map(GreenRoom::getGreenroomId).toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.greenroom.server.api.domain.greenroom.service;

import com.amazonaws.util.StringUtils;
import com.greenroom.server.api.domain.greenroom.dto.in.*;
import com.greenroom.server.api.domain.greenroom.dto.out.*;
import com.greenroom.server.api.domain.greenroom.entity.Activity;
import com.greenroom.server.api.domain.greenroom.entity.GreenRoom;
import com.greenroom.server.api.domain.greenroom.entity.Plant;
import com.greenroom.server.api.domain.greenroom.entity.Todo;
import com.greenroom.server.api.domain.greenroom.entity.*;
import com.greenroom.server.api.domain.greenroom.enums.GreenRoomStatus;
import com.greenroom.server.api.domain.greenroom.repository.GreenRoomRepository;
import com.greenroom.server.api.domain.user.entity.User;
Expand All @@ -18,7 +14,6 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
Expand All @@ -27,6 +22,7 @@
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -338,5 +334,104 @@ public void updateTodo(Todo todo, ActivityInfoUpdateRequestDto.ActivityInfoUpdat
todo.updateNextTodoDate(baseDate.plusDays(term));

}

public GreenroomCalendarResponseDto getGreenroomInfoFromCalendar(String email, LocalDate date, Long activityId){

User user = customUserDetailService.findUserByEmail(email);

if(date.isBefore(LocalDate.now())){
return GreenroomCalendarResponseDto.from(date,getGreenroomInfoPast(user,date,activityId));
}
else if(date.isEqual(LocalDate.now())){
return GreenroomCalendarResponseDto.from(date,getGreenroomInfoPresent(user,date,activityId));
}
else{
return GreenroomCalendarResponseDto.from(date,getGreenroomInfoFuture(user,date,activityId));
}
}

private List<GreenRoom> getEnabledGreenRooms(User user) {
return greenRoomRepository.findGreenRoomByUserAndGreenroomStatus(user, GreenRoomStatus.ENABLED);
}

private List<GreenRoom> getAllGreenRooms(User user) {
return greenRoomRepository.findGreenRoomByUser(user);
}

private Map<GreenRoom, List<TodoLog>> getTodoLogs(List<GreenRoom> rooms, LocalDate date, Long activityId) {
return todoLogService.getAllTodoLogByGreenroomAndDate(rooms, date).stream()
.filter(log -> activityId == null || Objects.equals(log.getActivity().getActivityId(), activityId))
.collect(Collectors.groupingBy(TodoLog::getGreenRoom));
}

private Map<GreenRoom, List<Todo>> getTodos(List<GreenRoom> rooms, LocalDate date, Long activityId) {
return todoService.getAllTodoByGreenroomAndDate(rooms, date).stream()
.filter(todo -> activityId == null || Objects.equals(todo.getActivity().getActivityId(), activityId))
.collect(Collectors.groupingBy(Todo::getGreenRoom));
}

private Map<GreenRoom, List<Diary>> getDiaries(List<GreenRoom> rooms, LocalDate date) {
return diaryService.getAllDiariesByGreenroomAndDate(rooms, date).stream()
.collect(Collectors.groupingBy(Diary::getGreenRoom));
}

private GreenroomCalendarResponseDto.CalendarInfo buildCalendarInfo(GreenRoom greenRoom, List<TodoLog> todoLogs, List<Todo> todos, List<Diary> diaries) {

List<GreenroomCalendarResponseDto.TodoInfo> todoInfos = new ArrayList<>();
if (todoLogs != null) todoInfos.addAll(todoLogs.stream().map(GreenroomCalendarResponseDto.TodoInfo::from).toList());
if (todos != null) todoInfos.addAll(todos.stream().map(GreenroomCalendarResponseDto.TodoInfo::from).toList());

List<GreenroomCalendarResponseDto.DiaryInfo> diaryInfos = diaries != null ? diaries.stream().map(GreenroomCalendarResponseDto.DiaryInfo::from).toList() : new ArrayList<>();

GreenroomCalendarResponseDto.GreenroomInfo greenroomInfo = GreenroomCalendarResponseDto.GreenroomInfo.from(greenRoom);
return GreenroomCalendarResponseDto.CalendarInfo.of(greenroomInfo, todoInfos, diaryInfos);
}

public List<GreenroomCalendarResponseDto.CalendarInfo> getGreenroomInfoPast(User user, LocalDate date, Long activityId){
List<GreenRoom> greenRoomListForTodoLogAndDiary = getAllGreenRooms(user);
if(greenRoomListForTodoLogAndDiary.isEmpty()){return List.of();}

Map<GreenRoom,List<TodoLog>> todoLogList = getTodoLogs(greenRoomListForTodoLogAndDiary,date,activityId);
Map<GreenRoom,List<Diary>> diaryList = getDiaries(greenRoomListForTodoLogAndDiary,date);

Set<GreenRoom> resultGreenroomList = new HashSet<>();
resultGreenroomList.addAll(todoLogList.keySet());resultGreenroomList.addAll(diaryList.keySet());


return resultGreenroomList.isEmpty()?List.of():resultGreenroomList.stream()
.map(greenroom -> buildCalendarInfo(greenroom, todoLogList.getOrDefault(greenroom, null), null, diaryList.getOrDefault(greenroom, null)))
.toList();

}
public List<GreenroomCalendarResponseDto.CalendarInfo> getGreenroomInfoPresent(User user, LocalDate date, Long activityId){
List<GreenRoom> greenRoomListForTodoLogAndDiary = getAllGreenRooms(user);
List<GreenRoom> greenRoomListForTodo = getEnabledGreenRooms(user);

if(greenRoomListForTodoLogAndDiary.isEmpty()||greenRoomListForTodo.isEmpty()){return List.of();}

Map<GreenRoom,List<TodoLog>> todoLogList = getTodoLogs(greenRoomListForTodoLogAndDiary,date,activityId);
Map<GreenRoom,List<Todo>> todoList = getTodos(greenRoomListForTodo,date,activityId);
Map<GreenRoom,List<Diary>> diaryList = getDiaries(greenRoomListForTodoLogAndDiary,date);

Set<GreenRoom> resultGreenroomList = new HashSet<>();
resultGreenroomList.addAll(todoList.keySet()); resultGreenroomList.addAll(todoLogList.keySet());resultGreenroomList.addAll(diaryList.keySet());


return resultGreenroomList.isEmpty()?List.of():resultGreenroomList.stream()
.map(greenroom -> buildCalendarInfo(greenroom, todoLogList.getOrDefault(greenroom, null), todoList.getOrDefault(greenroom, null) ,diaryList.getOrDefault(greenroom, null)))
.toList();
}

public List<GreenroomCalendarResponseDto.CalendarInfo> getGreenroomInfoFuture(User user, LocalDate date, Long activityId){
List<GreenRoom> greenRoomListForTodo = getEnabledGreenRooms(user);
if(greenRoomListForTodo.isEmpty()){return List.of();}

Map<GreenRoom,List<Todo>> todoList = getTodos(greenRoomListForTodo,date,activityId);


return todoList.isEmpty()?List.of():todoList.keySet().stream()
.map(greenroom -> buildCalendarInfo(greenroom, null, todoList.getOrDefault(greenroom, null), null))
.toList();
}
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.greenroom.server.api.domain.greenroom.service;

import com.greenroom.server.api.domain.greenroom.entity.GreenRoom;
import com.greenroom.server.api.domain.greenroom.entity.TodoLog;
import com.greenroom.server.api.domain.greenroom.repository.TodoLogRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Service
Expand All @@ -24,5 +26,7 @@ public void deleteAllByGreenroom(List<Long> greenroomIdList){
todoLogRepository.deleteAllByGreenRoom(greenroomIdList);
}


public List<TodoLog> getAllTodoLogByGreenroomAndDate(List<GreenRoom> greenRooms, LocalDate date){
return todoLogRepository.findByCreateDateAndGreenRoomIn(date, greenRooms.stream().map(GreenRoom::getGreenroomId).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,11 @@ public List<Todo> findAllByGreenroom(GreenRoom greenRoom){
return todoRepository.findAllByGreenRoom(greenRoom);
}

public List<Todo> getAllTodoByGreenroomAndDate(List<GreenRoom> greenRoomList, LocalDate date){
if(date.isAfter(LocalDate.now())){
return todoRepository.findByNextTodoDateAndGreenRoomInFuture(date, greenRoomList.stream().map(GreenRoom::getGreenroomId).toList());
}
return todoRepository.findByNextTodoDateAndGreenRoomIn(date, greenRoomList.stream().map(GreenRoom::getGreenroomId).toList());
}

}
Loading
Loading