-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/org/rf/rfserver/party/controller/FavoritePartyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/org/rf/rfserver/party/dto/favoriteparty/FavoritePartyReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/rf/rfserver/party/dto/favoriteparty/FavoritePartyRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/rf/rfserver/party/repository/FavoritePartyRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/rf/rfserver/party/service/FavoritePartyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters