Skip to content

Commit

Permalink
#40 [FEAT] : <룸리스트> get api , <룸,멤버 정보 + 멤버 로그> get api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hye-on committed Sep 20, 2023
1 parent 84b0fed commit 6915ce0
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public interface AlarmRepository extends JpaRepository<Alarm, Integer> {
@Query("update Alarm a set a.room.id = :roomId where a.id = :alarmId")
void updateRoomIdByAlarmId(@Param("alarmId") Integer alarmId, @Param("roomId") Integer roomId);

Integer countByUserId(Integer userId);


void deleteByUserIdAndRoomIdIsNull(Integer userId);
Alarm findFirstByRoom_Id(Integer roomId);


@Query("select a from Alarm a where a.room.id IN :roomIds")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wakeUpTogetUp.togetUp.api.alarm;

import com.wakeUpTogetUp.togetUp.api.alarm.dto.request.PatchAlarmReq;
import com.wakeUpTogetUp.togetUp.api.alarm.dto.response.GetAlarmRes;
import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm;
import com.wakeUpTogetUp.togetUp.api.alarm.dto.request.PostAlarmReq;
import com.wakeUpTogetUp.togetUp.api.mission.MissionObjectRepository;
Expand All @@ -14,11 +13,13 @@
import com.wakeUpTogetUp.togetUp.api.mission.model.Mission;
import com.wakeUpTogetUp.togetUp.api.users.UserRepository;
import com.wakeUpTogetUp.togetUp.api.users.model.User;
import com.wakeUpTogetUp.togetUp.utils.mapper.EntityDtoMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Service
@RequiredArgsConstructor
public class AlarmService {
Expand Down Expand Up @@ -131,4 +132,12 @@ public int deleteAlarm(Integer alarmId) {

return alarm.getId();
}
//String 인 alarmTime을 LocalTime으로 변경
public LocalTime alarmTimeStringToLocalTime(String alarmTime)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.parse(alarmTime, formatter);
return localTime;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

@Repository
public interface MissionLogRepository extends JpaRepository<MissionLog, Integer> {
@Query("SELECT MCL FROM MissionLog MCL WHERE MCL.user.id = :userId")
List<MissionLog> findAllByUserId(@Param("userId") Integer userId);

@Query("SELECT MSL " +
"FROM MissionLog MSL " +
"WHERE (MSL.room.id, MSL.createdAt) IN " +
"(SELECT ML.room.id, MAX(ML.createdAt) " +
"FROM MissionLog ML " +
"WHERE ML.room.id IN :roomIds " +
"GROUP BY ML.room.id)")
List<MissionLog> findMostRecentMissionLogsByRoomIds(@Param("roomIds") List<Integer> roomIds);




List<MissionLog> findAllByRoom_IdAndCreatedAtContaining(Integer roomId, LocalDate localDate);


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.wakeUpTogetUp.togetUp.api.auth.AuthUser;
import com.wakeUpTogetUp.togetUp.api.mission.MissionLogRepository;
import com.wakeUpTogetUp.togetUp.api.mission.model.MissionLog;
import com.wakeUpTogetUp.togetUp.api.room.dto.request.RoomUserLogReq;
import com.wakeUpTogetUp.togetUp.api.room.dto.request.RoomReq;
import com.wakeUpTogetUp.togetUp.api.room.dto.response.RoomUserLogRes;
import com.wakeUpTogetUp.togetUp.api.room.dto.response.RoomRes;
import com.wakeUpTogetUp.togetUp.exception.BaseException;
import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse;
import com.wakeUpTogetUp.togetUp.common.Status;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -14,7 +14,6 @@

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "그룹 (room)")
Expand All @@ -32,11 +31,11 @@ public class RoomController {
public BaseResponse<Status> create(@AuthUser Integer userId, @RequestBody RoomReq roomReq ) {

roomService.createRoom(userId,roomReq);
return new BaseResponse(Status.SUCCESS);
return new BaseResponse(Status.SUCCESS_CREATED);

}


@Operation(description = "그룹 리스트 불러오기")
@GetMapping()
public BaseResponse<List<RoomRes>> getList(@AuthUser Integer userId ) {

Expand All @@ -45,13 +44,14 @@ public BaseResponse<List<RoomRes>> getList(@AuthUser Integer userId ) {

}

// @GetMapping
// public BaseResponse<List<RoomRes>> getRoomDetailByDate(@AuthUser Integer userId ) {
//
//
// return new BaseResponse(Status.SUCCESS,roomService.getRoomList(userId));
//
// }
@Operation(description = "그룹의 멤버의 미션기록 보기")
@GetMapping("/user/mission-log")
public BaseResponse<RoomUserLogRes> getRoomDetailByDate(@AuthUser Integer userId, @RequestBody RoomUserLogReq roomLogReq) {


return new BaseResponse(Status.SUCCESS,roomService.getRoomUserLogList(userId,roomLogReq));

}



Expand Down
114 changes: 91 additions & 23 deletions src/main/java/com/wakeUpTogetUp/togetUp/api/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.wakeUpTogetUp.togetUp.api.alarm.AlarmService;
import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm;
import com.wakeUpTogetUp.togetUp.api.mission.MissionLogRepository;
import com.wakeUpTogetUp.togetUp.api.mission.MissionRepository;
import com.wakeUpTogetUp.togetUp.api.mission.model.Mission;
import com.wakeUpTogetUp.togetUp.api.mission.model.MissionLog;
import com.wakeUpTogetUp.togetUp.api.room.dto.request.RoomUserLogReq;
import com.wakeUpTogetUp.togetUp.api.room.dto.response.RoomUserLogRes;
import com.wakeUpTogetUp.togetUp.api.room.dto.response.RoomRes;
import com.wakeUpTogetUp.togetUp.api.room.model.Room;
import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser;
import com.wakeUpTogetUp.togetUp.api.users.UserRepository;
import com.wakeUpTogetUp.togetUp.api.users.model.User;
import com.wakeUpTogetUp.togetUp.common.Constant;
import com.wakeUpTogetUp.togetUp.common.Status;
import com.wakeUpTogetUp.togetUp.exception.BaseException;
import com.wakeUpTogetUp.togetUp.api.room.dto.request.RoomReq;
Expand All @@ -20,10 +21,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.Objects;

import com.wakeUpTogetUp.togetUp.utils.mapper.EntityDtoMapper;

@Service
Expand All @@ -36,7 +39,6 @@ public class RoomService {
private final UserRepository userRepository;
private final RoomUserRepository roomUserRepository;
private final AlarmRepository alarmRepository;
private final MissionRepository missionRepository;
private final MissionLogRepository missionLogRepository;
@Transactional
public void createRoom(Integer userId, RoomReq roomReq){
Expand Down Expand Up @@ -71,33 +73,99 @@ public void createRoom(Integer userId, RoomReq roomReq){


public List<RoomRes> getRoomList(Integer userId) {
List<Alarm> alarmList = alarmRepository.findAllByUser_IdAndRoom_IdIsNotNull(userId);
List<Alarm> alarmList = alarmRepository.findAllByUser_IdAndRoom_IdIsNotNullOrderByAlarmTime(userId);

return EntityDtoMapper.INSTANCE.toRoomResList(alarmList);
}

@Transactional
public RoomRes editGroup(Integer groupId , RoomReq roomReq){
// 그룹수정 이름, 인트로
Optional<Room> group = Optional.ofNullable(roomRepository.findById(groupId)
.orElseThrow(
() -> new BaseException(Status.INVALID_GROUP_ID)
));
group.get().setName(roomReq.getName());
group.get().setIntro(roomReq.getIntro());

//저장
Room modifiedRoom = roomRepository.save(group.get());
public RoomUserLogRes getRoomUserLogList(Integer userId, RoomUserLogReq roomUserLogReq){
Integer roomId = roomUserLogReq.getRoomId();
LocalDateTime localDateTime = roomUserLogReq.getLocalDateTime();
boolean isAlarmActive = roomUserLogReq.getIsAlarmActive();

List<RoomUser> roomUserList = roomUserRepository.findAllByRoom_Id(roomId);
//크기가 0이면 예외처리
if(roomUserList.isEmpty())
throw new BaseException(Status.ROOM_USER_NOT_FOUND);
//룸이름, userId,userName 매핑 (항상 반환하는 정보)
RoomUserLogRes roomUserLogRes =new RoomUserLogRes();
roomUserLogRes.setName(roomUserList.get(0).getRoom().getName());
roomUserLogRes.setUserLogList(EntityDtoMapper.INSTANCE.toUserLogDataList(roomUserList));

//반환
RoomRes roomRes = GroupMapper.INSTANCE.toGroupRes(modifiedRoom);
//isMyLog, missionPicLink, userCompleteType 는 각케이스에 맞게 설정
return setUserLogData(roomUserLogRes, userId, roomId, isAlarmActive, localDateTime);

return roomRes;
}



public RoomUserLogRes setUserLogData(RoomUserLogRes roomUserLogRes,Integer userId,Integer roomId,boolean isAlarmActive,LocalDateTime localDateTime){
if(!isAlarmActive)
{
for (RoomUserLogRes.UserLogData userLogData : roomUserLogRes.getUserLogList()) {
userLogData.setUserCompleteType(UserCompleteType.NOT_MISSION);
userLogData.setMissionPicLink(Constant.ROOM_USER_MISSION_IMG_NOT_MISSION);
if(userLogData.getUserId()==userId)
{
userLogData.setIsMyLog(true);
}

}
return roomUserLogRes;
}
//missionLog에서 날짜로 가져옴 .
List<MissionLog> missionLogList = missionLogRepository.findAllByRoom_IdAndCreatedAtContaining(roomId,localDateTime.toLocalDate());
//오늘인지 아닌지
boolean isToday = localDateTime.toLocalDate().isEqual(LocalDate.now());

// for 문돌면서 : 유저 Id로 비교 :
for (RoomUserLogRes.UserLogData userLogData : roomUserLogRes.getUserLogList()) {
// 유저 Id가 있으면 (미션 수행한 기록이 있으면) -> 미션 수행사진 매핑
if(userLogData.getUserId()==userId)
{
userLogData.setIsMyLog(true);
}
for(MissionLog missionLog:missionLogList){
if(userLogData.getUserId()==missionLog.getUser().getId()) {
userLogData.setMissionPicLink(missionLog.getMissionPicLink());
userLogData.setUserCompleteType(UserCompleteType.SUCCESS);
}
}
//유저 Id가 없으면 (미션 수행한 기록이 없으면)
// -> 오늘날짜이고 알람끝나기 전이면 waiting. 오늘날짜이고 알람이 끝나면 fail (다시울림 포함해서 알람이 끝나면)
//과거 : fail
if(Objects.isNull(userLogData.getMissionPicLink()))
{
if(isToday)
{
//알람의 다시 울림 시간을 계산함.
Alarm alarm = alarmRepository.findFirstByRoom_Id(roomId);
LocalTime alarmLocalTime = alarmService.alarmTimeStringToLocalTime(alarm.getAlarmTime());
LocalTime alarmOffTime = alarmLocalTime.withMinute(alarmLocalTime.getMinute() + alarm.getSnoozeCnt()*alarm.getSnoozeInterval());
//알람이 끝나기 전인지 여부
boolean isBeforeAlarmEnd = localDateTime.toLocalTime().isBefore(alarmOffTime);

//알람이 끝나기 전이라면 waiting 상태로 설정
if(isBeforeAlarmEnd)
{
userLogData.setMissionPicLink(Constant.ROOM_USER_MISSION_IMG_WAITING);
userLogData.setUserCompleteType(UserCompleteType.WAITING);
}else{ //알람이 끝났다면 실패상태로 설정
userLogData.setMissionPicLink(Constant.ROOM_USER_MISSION_IMG_FAIL);
userLogData.setUserCompleteType(UserCompleteType.FAIL);
}

}else {//오늘이 아니라 과거라면 실패
userLogData.setMissionPicLink(Constant.ROOM_USER_MISSION_IMG_FAIL);
userLogData.setUserCompleteType(UserCompleteType.FAIL);
}
}

}



return roomUserLogRes;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@


import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser;
import feign.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -13,11 +11,13 @@
public interface RoomUserRepository extends JpaRepository<RoomUser, Integer> {

List<RoomUser> findByUserId(Integer userId);
List<RoomUser> findAllByRoom_Id(Integer roomId);


Integer countByUserId(Integer userId);
void deleteByUserId(Integer userId);

//



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wakeUpTogetUp.togetUp.api.room;

public enum UserCompleteType {
SUCCESS,
FAIL,
NOT_MISSION,
WAITING
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.wakeUpTogetUp.togetUp.api.room.dto.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;

@ToString
@Schema(description = "room 로그 요청")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RoomUserLogReq {

@Schema(description = "room id")
Integer roomId;

@Schema(description = "기록을 가져올 날의 LocalDateTime ")
@NotBlank(message = "기록 시간은 공백일 수 없습니다.")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime localDateTime;

@Schema(description = "알람이 울리는 날인지 여부")
@NotBlank(message = "알람이 울리는 날인지 여부는 공백일 수 없습니다.")
Boolean isAlarmActive;



}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
@Builder
public class RoomRes {

private Integer roomId; //룸
private String icon; //알람
private String name; //룸
private String mission; //알람
// private String recentlyMissionLog;
//private boolean isRead;
private Integer roomId;
private String icon;
private String name;
private String mission;

}
Loading

0 comments on commit 6915ce0

Please sign in to comment.