Skip to content

Commit

Permalink
feat: [#114]: 충돌 해결 및 모임찜, 모임찜 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
kuk6933 committed Sep 2, 2023
1 parent 700c984 commit 442cd55
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum BaseResponseStatus {
INVALID_TOKEN(false, 2013, "유효하지 않은 토큰입니다."),
INVALID_LOGIN_IR_OR_PASSWORD(false, 2014, "아이디 또는 비밀번호가 일치하지 않습니다"),
NOT_OWNER(false, 2015, "모임장이 아니면 강퇴할 수 없습니다"),
NOT_FAVORITE_PARTY(false, 2016, "해당 유저는 이 모임을 찜하지 않았습니다"),


/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/rf/rfserver/domain/FavoriteParty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.rf.rfserver.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FavoriteParty {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
private User user;
@ManyToOne
private Party party;

public FavoriteParty(User user, Party party) {
this.user = user;
this.party = party;
user.getFavoriteParties().add(this);
}

public void deleteFavoriteParty() {
user.getFavoriteParties().remove(this);
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/rf/rfserver/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ public class User extends BaseEntity implements UserDetails {
private LifeStyle lifeStyle;
@OneToMany(mappedBy = "user")
private List<UserParty> userParties;
@OneToMany(mappedBy = "user")
private List<FavoriteParty> favoriteParties;

@OneToMany(mappedBy = "blockerUser")
@JsonManagedReference // 이 엔티티를 직렬화 할 때 관련된 BlockParty 엔티티를 포함
private List<BlockParty> blockedParties;



@Builder
public User(String loginId, String password, int entrance, University university, String nickName
, Country country, List<Language> interestingLanguages, String introduce, Mbti mbti
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.rf.rfserver.party.controller;

import lombok.RequiredArgsConstructor;
import org.rf.rfserver.config.BaseException;
import org.rf.rfserver.config.BaseResponse;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyReq;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyRes;
import org.rf.rfserver.party.service.FavoritePartyService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/favoriteParty")
public class FavoritePartyController {

private final FavoritePartyService favoritePartyService;

@PostMapping("/favorite")
public BaseResponse<FavoritePartyRes> toggleFavoriteParty(@RequestBody FavoritePartyReq favoritePartyReq) {
try {
return new BaseResponse<>(favoritePartyService.favoriteParty(favoritePartyReq));

} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import org.rf.rfserver.constant.Interest;
import org.rf.rfserver.constant.PreferAges;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyReq;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyRes;
import org.rf.rfserver.party.dto.party.*;
import org.rf.rfserver.party.dto.partyjoin.PostApproveJoinRes;
import org.rf.rfserver.party.dto.partyjoin.PostDenyJoinRes;
import org.rf.rfserver.party.dto.partyjoinapply.PostJoinApplicationReq;
import org.rf.rfserver.party.dto.partyjoinapply.PostJoinApplicationRes;
import org.rf.rfserver.party.repository.FavoritePartyRepository;
import org.rf.rfserver.party.service.PartyService;

import org.rf.rfserver.config.PageDto;
Expand Down Expand Up @@ -185,5 +188,4 @@ public BaseResponse<PageDto<List<GetInterestPartyRes>>> recommendPersonalParties
return new BaseResponse<>(e.getStatus());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.rf.rfserver.party.dto.favoriteparty;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class FavoritePartyReq {
Long userId;
Long partyId;
Boolean isFavorite;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.rf.rfserver.party.dto.favoriteparty;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class FavoritePartyRes {
Boolean done;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.rf.rfserver.party.repository;

import org.rf.rfserver.domain.FavoriteParty;
import org.rf.rfserver.domain.Party;
import org.rf.rfserver.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface FavoritePartyRepository extends JpaRepository<FavoriteParty, Long> {
Optional<FavoriteParty> findByUserAndParty(User user, Party party);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.rf.rfserver.party.service;

import lombok.RequiredArgsConstructor;
import org.rf.rfserver.config.BaseException;
import org.rf.rfserver.config.BaseResponseStatus;
import org.rf.rfserver.domain.FavoriteParty;
import org.rf.rfserver.domain.Party;
import org.rf.rfserver.domain.User;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyReq;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyRes;
import org.rf.rfserver.party.repository.FavoritePartyRepository;
import org.rf.rfserver.user.service.UserService;
import org.springframework.stereotype.Service;

import static org.rf.rfserver.config.BaseResponseStatus.*;

@RequiredArgsConstructor
@Service
public class FavoritePartyService {
private final FavoritePartyRepository favoritePartyRepository;
private final UserService userService;
private final PartyService partyService;

public FavoritePartyRes favoriteParty(FavoritePartyReq favoritePartyReq) throws BaseException {
User user = userService.findUserById(favoritePartyReq.getUserId());
Party party = partyService.findPartyById(favoritePartyReq.getPartyId());
if(favoritePartyReq.getIsFavorite()) {
return addFavoriteParty(user, party);
}
return deleteFavoriteParty(user, party);
}

public FavoritePartyRes addFavoriteParty(User user, Party party) {
FavoriteParty favoriteParty = new FavoriteParty(user, party);
favoritePartyRepository.save(favoriteParty);
return new FavoritePartyRes(true);
}

public FavoritePartyRes deleteFavoriteParty(User user, Party party) throws BaseException {
FavoriteParty favoriteParty = favoritePartyRepository.findByUserAndParty(user, party)
.orElseThrow(() -> new BaseException(NOT_FAVORITE_PARTY));
favoriteParty.deleteFavoriteParty();
favoritePartyRepository.delete(favoriteParty);
return new FavoritePartyRes(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import org.rf.rfserver.constant.PushNotificationType;
import org.rf.rfserver.constant.PreferAges;
import org.rf.rfserver.domain.*;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyReq;
import org.rf.rfserver.party.dto.favoriteparty.FavoritePartyRes;
import org.rf.rfserver.party.dto.party.*;
import org.rf.rfserver.party.dto.partyjoin.PostApproveJoinRes;
import org.rf.rfserver.party.dto.partyjoin.PostDenyJoinRes;
import org.rf.rfserver.party.dto.partyjoinapply.PostJoinApplicationReq;
import org.rf.rfserver.party.dto.partyjoinapply.PostJoinApplicationRes;
import org.rf.rfserver.party.repository.FavoritePartyRepository;
import org.rf.rfserver.party.repository.PartyJoinApplicationRepository;
import org.rf.rfserver.party.repository.PartyRepository;
import org.rf.rfserver.party.repository.UserPartyRepository;
Expand Down Expand Up @@ -52,6 +55,8 @@ public class PartyService {
private final S3Uploader s3Uploader;
private final PartyidUseridService partyidUseridService;
private final ApnsService apnsService;
private final FavoritePartyRepository favoritePartyRepository;


public PostPartyRes createParty(PostPartyReq postPartyReq, MultipartFile file) throws BaseException {
try {
Expand Down Expand Up @@ -211,7 +216,7 @@ public PostApproveJoinRes approveJoin(Long partyJoinApplicationId) throws BaseEx
return new PostApproveJoinRes(partyJoinApplicationId);
}

public void makeUserParty(User user, Party party) throws BaseException {
public void makeUserParty(User user, Party party) {
UserParty userParty = new UserParty(party, user);
userPartyRepository.save(userParty);
partyidUseridService.setPartyidUserid(party.getId(), user.getId());
Expand Down

0 comments on commit 442cd55

Please sign in to comment.