Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/party 업데이트,조회 수정, 약속 시간 기능 추가 #69

Merged
merged 4 commits into from
Jan 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class PartyRequestDTO {

private String title;
private String content;
private int year;
private int month;
private int day;
private int hours;
private int minutes;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class PartyResponseDTO {
private String title;
private String username;
private String content;
private LocalDateTime dateTime; // 약속 날짜,시간
private LocalDateTime createAt;
private LocalDateTime modifiedAt;
private List<MemberResponseDTO> members;
Expand All @@ -27,6 +28,7 @@ public PartyResponseDTO(Party party) {
this.title = party.getTitle();
this.username = party.getUsername();
this.content = party.getContent();
this.dateTime = party.getDateTime();
this.createAt = party.getCreatedAt();
this.modifiedAt = party.getModifiedAt();
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/HHive/hhive/domain/party/entity/Party.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@Entity
@Builder
Expand Down Expand Up @@ -41,20 +43,29 @@ public class Party extends BaseTimeEntity {
@JoinColumn(name = "user_id")
private User user;

@Column
private LocalDateTime dateTime; // 약속 시간


public Party(Hive hive, PartyRequestDTO dto, User user) {
public Party(Hive hive, PartyRequestDTO dto, User user, LocalDateTime dateTime) {
this.hive = hive;
this.title = dto.getTitle();
this.username = user.getUsername();
this.content = dto.getContent();
this.hostId = user.getId();
this.dateTime = dateTime;
}

// 연관관계 메서드
public void setUser(User user) {
this.user = user;
}

// 날짜 설정 메서드
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}

// 서비스 메서드
public void setIsDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -35,7 +36,17 @@ public PartyResponseDTO createParty(Long hiveId, PartyRequestDTO dto, User user)
Hive hive = hiveRepository.findById(hiveId)
.orElseThrow(NotFoundHiveException::new);

Party party = new Party(hive, dto, user);
LocalDateTime dateTime = LocalDateTime.of(dto.getYear(), dto.getMonth(), dto.getDay(), dto.getHours(), dto.getMinutes());

// 현재 시간을 가져옵니다.
LocalDateTime now = LocalDateTime.now();

// 설정하려는 약속 시간이 과거인지 확인합니다.
if (dateTime.isBefore(now)) {
throw new InvalidPartyTimeException();
}

Party party = new Party(hive, dto, user, dateTime);
party.setUser(user);

var saved = partyRepository.save(party);
Expand All @@ -46,28 +57,33 @@ public PartyResponseDTO createParty(Long hiveId, PartyRequestDTO dto, User user)
//단건 조회
@Transactional
public PartyResponseDTO getPartyDto(Long partyId) {
Party party = partyRepository.findById(partyId).orElseThrow(PartyNotFoundException::new);
Party party = partyRepository.findById(partyId)
.filter(p -> !p.isDeleted()) // 삭제 되지 않은 파티만 필터링
.orElseThrow(PartyNotFoundException::new);

List<MemberResponseDTO> members = partyUserRepository.findUsersByPartyId(partyId).stream()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPartyDto메서드와 getUserPartyMap메서드에서 partyUserRepository.findUsersByPartyId를 MemberResponseDTO로 변환하는 로직이 중복되어 있는데, 이걸 메서드로 추출하지 않고 중복으로 사용한 이유가 있을까요?

.map(partyUser -> new MemberResponseDTO(partyUser.getUser().getUsername(), partyUser.getUser().getEmail()))
.collect(Collectors.toList());

return new PartyResponseDTO(party.getId(), party.getTitle(), party.getUsername(), party.getContent(), party.getCreatedAt(), party.getModifiedAt(), members);
return new PartyResponseDTO(party.getId(), party.getTitle(), party.getUsername(), party.getContent(),party.getDateTime(), party.getCreatedAt(), party.getModifiedAt(), members);
}

//전체 조회
@Transactional
public Map<UserInfoResponseDTO, List<PartyResponseDTO>> getUserPartyMap() {
Map<UserInfoResponseDTO, List<PartyResponseDTO>> userPartyMap = new HashMap<>();

List<Party> partyList = partyRepository.findAll(Sort.by(Sort.Direction.DESC, "createdAt"));
List<Party> partyList = partyRepository.findAll(Sort.by(Sort.Direction.DESC, "createdAt")).stream()
.filter(p -> !p.isDeleted())
.collect(Collectors.toList());

for (Party party : partyList) {
UserInfoResponseDTO userDto = new UserInfoResponseDTO(party.getUser());
List<MemberResponseDTO> members = partyUserRepository.findUsersByPartyId(party.getId()).stream()
.map(partyUser -> new MemberResponseDTO(partyUser.getUser().getUsername(), partyUser.getUser().getEmail()))
.collect(Collectors.toList());

PartyResponseDTO partyDto = new PartyResponseDTO(party.getId(), party.getTitle(), party.getUsername(), party.getContent(), party.getCreatedAt(), party.getModifiedAt(), members);
PartyResponseDTO partyDto = new PartyResponseDTO(party.getId(), party.getTitle(), party.getUsername(), party.getContent(),party.getDateTime(), party.getCreatedAt(), party.getModifiedAt(), members);

if (userPartyMap.containsKey(userDto)) {
userPartyMap.get(userDto).add(partyDto);
Expand All @@ -83,6 +99,10 @@ public Map<UserInfoResponseDTO, List<PartyResponseDTO>> getUserPartyMap() {
public PartyResponseDTO updateParty(Long partyId, PartyRequestDTO partyRequestDto, User user) {
Party party = getUserParty(partyId, user);

if (!party.getHostId().equals(user.getId())) {
throw new UnauthorizedAccessException();
}

party.setTitle(partyRequestDto.getTitle());
party.setContent(partyRequestDto.getContent());

Expand All @@ -92,9 +112,11 @@ public PartyResponseDTO updateParty(Long partyId, PartyRequestDTO partyRequestDt
@Transactional
public void deleteParty(Long partyId, User user) {
Party party = getUserParty(partyId, user);

if (!party.getHostId().equals(user.getId())) {
throw new UnauthorizedAccessException();
}

party.setIsDeleted(true);
partyRepository.save(party);
}
Expand All @@ -108,11 +130,19 @@ private Party getUserParty(Long partyId, User user) {
public void joinParty(Long partyId, User user) {
Party party = getUserParty(partyId, user);

// 현재 날짜를 가져옵니다.
LocalDateTime now = LocalDateTime.now();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 시간대를 다룰 경우를 대비해서 ZonedDateTime 사용은 어떠신가요?


// 파티 호스트가 파티에 가입하는 것을 방지
if (party.getHostId().equals(user.getId())) {
throw new PartyHostNotJoinException();
}

// 파티 날짜가 현재 날짜보다 이전인 경우, 가입 불가 처리
if (party.getDateTime().isBefore(now)) {
throw new EndPartyNotJoinException();
}

// 중복 가입 검사
if (partyUserRepository.existsByUserIdAndPartyId(user.getId(), partyId)) {
throw new AlreadyJoinException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public enum ErrorCode {
UNAUTHORIZED_ACCESS_EXCEPTION(403,"해당 파티에 대한 권한이 없습니다."),
PARTY_HOST_NOT_RESIGN_EXCEPTION(403,"리더는 파티 탈퇴가 불가능합니다."),
PARTY_HOST_NOT_JOIN_EXCEPTION(403,"리더는 파티 가입이 불가능합니다."),
END_PARTY_NOT_JOIN_EXCEPTION(403,"가입 기간이 끝난 파티 입니다."),
ALREADY_JOIN_EXCEPTION(403,"이미 가입 되어 있습니다."),
PARTY_NOT_RESIGN_EXCEPTION(403,"탈퇴할 파티가 없습니다."),
INVALID_PARTY_TIME_EXCEPTION(403,"설정 하려는 약속 시간이 현재 보다 과거 입니다."),

//Notification
NOT_FOUND_NOTIFICATION_EXCEPTION(401,"알림이 존재하지 않습니다"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.HHive.hhive.global.exception.party;

import com.HHive.hhive.global.exception.common.CustomException;
import com.HHive.hhive.global.exception.common.ErrorCode;

public class EndPartyNotJoinException extends CustomException {
public EndPartyNotJoinException() {
super(ErrorCode.END_PARTY_NOT_JOIN_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.HHive.hhive.global.exception.party;

import com.HHive.hhive.global.exception.common.CustomException;
import com.HHive.hhive.global.exception.common.ErrorCode;

public class InvalidPartyTimeException extends CustomException {
public InvalidPartyTimeException() {
super(ErrorCode.INVALID_PARTY_TIME_EXCEPTION);
}
}
Loading