-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from Leets-Official/feature-#55
#55 Feat: 패널티 기능 구현
- Loading branch information
Showing
9 changed files
with
212 additions
and
1 deletion.
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
23 changes: 23 additions & 0 deletions
23
src/main/java/leets/weeth/domain/penalty/controller/PenaltyController.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,23 @@ | ||
package leets.weeth.domain.penalty.controller; | ||
|
||
import leets.weeth.domain.penalty.dto.ResponsePenalty; | ||
import leets.weeth.domain.penalty.service.PenaltyService; | ||
import leets.weeth.global.auth.annotation.CurrentUser; | ||
import leets.weeth.global.common.response.CommonResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/penalty") | ||
public class PenaltyController { | ||
private final PenaltyService penaltyService; | ||
@GetMapping() | ||
public CommonResponse<List<ResponsePenalty>> showMyPenalty(@CurrentUser Long userId){ | ||
List<ResponsePenalty> myPenalties= penaltyService.getMyPenalties(userId); | ||
return CommonResponse.createSuccess(myPenalties); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/leets/weeth/domain/penalty/dto/RequestPenalty.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 leets.weeth.domain.penalty.dto; | ||
|
||
import lombok.*; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@ToString | ||
@Setter | ||
public class RequestPenalty { | ||
private Long userId; | ||
private String penaltyDescription; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/leets/weeth/domain/penalty/dto/ResponsePenalty.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,31 @@ | ||
package leets.weeth.domain.penalty.dto; | ||
|
||
import leets.weeth.domain.penalty.entity.Penalty; | ||
import lombok.*; | ||
import java.time.LocalDateTime; | ||
|
||
|
||
@AllArgsConstructor | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@ToString | ||
public class ResponsePenalty { | ||
private Long userId; | ||
private String userName; | ||
private Long penaltyId; | ||
private String penaltyDescription; | ||
private LocalDateTime time; | ||
private int penaltyCount; | ||
|
||
public static ResponsePenalty createResponsePenaltyDTO(Penalty penalty) { | ||
return ResponsePenalty.builder() | ||
.userId(penalty.getUser().getId()) | ||
.userName(penalty.getUser().getName()) | ||
.penaltyId(penalty.getId()) | ||
.penaltyDescription(penalty.getPenaltyDescription()) | ||
.time(penalty.getCreatedAt()) | ||
.penaltyCount(penalty.getUser().getPenalties().size()) | ||
.build(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/leets/weeth/domain/penalty/entity/Penalty.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,36 @@ | ||
package leets.weeth.domain.penalty.entity; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import leets.weeth.domain.penalty.dto.RequestPenalty; | ||
import leets.weeth.domain.user.entity.User; | ||
import leets.weeth.global.common.entity.BaseEntity; | ||
import lombok.*; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
@Entity | ||
@Getter | ||
@Table | ||
public class Penalty extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "penalty_id") | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@NotEmpty | ||
private String penaltyDescription; | ||
|
||
public static Penalty toEntity(RequestPenalty dto, User user){ | ||
return Penalty.builder() | ||
.user(user) | ||
.penaltyDescription(dto.getPenaltyDescription()) | ||
.build(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/leets/weeth/domain/penalty/repository/PenaltyRepository.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,11 @@ | ||
package leets.weeth.domain.penalty.repository; | ||
|
||
import leets.weeth.domain.penalty.entity.Penalty; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface PenaltyRepository extends JpaRepository<Penalty, Long> { | ||
List<Penalty> findByUserId(Long userId, Sort sort); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/leets/weeth/domain/penalty/service/PenaltyService.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,54 @@ | ||
package leets.weeth.domain.penalty.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import leets.weeth.domain.penalty.dto.RequestPenalty; | ||
import leets.weeth.domain.penalty.dto.ResponsePenalty; | ||
import leets.weeth.domain.penalty.entity.Penalty; | ||
import leets.weeth.domain.penalty.repository.PenaltyRepository; | ||
import leets.weeth.domain.user.entity.User; | ||
import leets.weeth.domain.user.repository.UserRepository; | ||
import leets.weeth.global.common.error.exception.custom.PenaltyNotFoundException; | ||
import leets.weeth.global.common.error.exception.custom.UserNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Service | ||
public class PenaltyService { | ||
private final PenaltyRepository penaltyRepository; | ||
private final UserRepository userRepository; | ||
@Transactional | ||
public void assignPenalty(RequestPenalty requestPenalty){ | ||
User userToBan = userRepository.findById(requestPenalty.getUserId()).orElseThrow(UserNotFoundException::new); | ||
Penalty penalty = penaltyRepository.save(Penalty.toEntity(requestPenalty, userToBan)); | ||
userToBan.addPenalty(penalty); | ||
} | ||
|
||
public List<ResponsePenalty> getMyPenalties(Long userId){ | ||
User currentUser = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); | ||
currentUser.getPenalties().sort(Comparator.comparing(Penalty::getId)); | ||
|
||
return currentUser.getPenalties().stream() | ||
.map(ResponsePenalty::createResponsePenaltyDTO) // Post -> ResponsePostDTO 변환 | ||
.toList(); | ||
} | ||
|
||
public void removePenalty(Long penaltyId) { | ||
Penalty penaltyToRemove = penaltyRepository.findById(penaltyId) | ||
.orElseThrow(PenaltyNotFoundException::new); | ||
penaltyRepository.delete(penaltyToRemove); | ||
} | ||
|
||
public List<ResponsePenalty> getAllPenaltiesSortedByUserId() { | ||
List<Penalty> allPenalties = penaltyRepository.findAll(); | ||
return allPenalties.stream() | ||
.sorted(Comparator.comparing(Penalty-> Penalty.getUser().getId())) | ||
.map(ResponsePenalty::createResponsePenaltyDTO) | ||
.toList(); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/leets/weeth/global/common/error/exception/custom/PenaltyNotFoundException.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,9 @@ | ||
package leets.weeth.global.common.error.exception.custom; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
|
||
public class PenaltyNotFoundException extends EntityNotFoundException { | ||
public PenaltyNotFoundException() { | ||
super("존재하지 않는 패널티입니다."); | ||
} | ||
} |