Skip to content

Commit

Permalink
[DEV-9] 동아리 평가 점수 조회 로직 수정 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
5uhwann authored Jul 27, 2024
1 parent e97fe04 commit 771826d
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<AllActivityReportResponse> getAll() {

@Transactional(readOnly = true)
public List<AllActivityReportResponse> getMyActivityReports(final User user) {
Club club = clubService.findClubByClubId(user.getId());
Club club = clubService.getByUserId(user.getId());

List<ActivityReport> activityReports = activityReportRepository.findByClubName(
club.getName());
Expand All @@ -73,7 +73,7 @@ public List<DetailActivityReportResponse> getActivityReport(final String term,
public Long register(final User user,
final RegisterActivityReportRequest registerActivityReportRequest) {

Club club = clubService.findClubByUserId(user.getId());
Club club = clubService.getByUserId(user.getId());
ActivityReport activityReport = registerActivityReportRequest.toEntity(club);

ActivityReport savedActivityReport = activityReportRepository.save(activityReport);
Expand All @@ -83,7 +83,7 @@ public Long register(final User user,

public List<ActivityReportDto> update(final User user, final String term,
final List<UpdateActivityReportRequest> requests) {
Club club = clubService.findClubByUserId(user.getId());
Club club = clubService.getByUserId(user.getId());

List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(
club.getName(), term);
Expand All @@ -96,7 +96,7 @@ public List<ActivityReportDto> update(final User user, final String term,
}

public List<ActivityReportDto> delete(final User user, final String term) {
Club club = clubService.findClubByUserId(user.getId());
Club club = clubService.getByUserId(user.getId());

List<ActivityReport> activityReports = activityReportRepository.findByClubNameAndTerm(
club.getName(), term);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public class Club extends BaseEntity {
private Score score;

@Builder
public Club(User user, String name, String category, String tag, String leader, Location location,
public Club(Long id, User user, String name, String category, String tag, String leader, Location location,
PhoneNumber phoneNumber, Score score) {
this.id = id;
this.user = user;
this.name = name;
this.category = category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -122,10 +121,6 @@ public Long update(Long userId, UpdateClubRequest request) {
return club.getId();
}

public Optional<Club> findByUserId(final Long userId) {
return clubRepository.findByUserId(userId);
}

public Club getByUserId(final Long userId) {
return clubRepository.findByUserId(userId)
.orElseThrow(() -> new NoSuchElementException(NO_SUCH_CLUB.getText()));
Expand Down Expand Up @@ -180,7 +175,7 @@ private RecruitmentStatus checkRecruit(LocalDateTime now, Club club) {
return BEFORE_RECRUIT;
}

return club.getEndRecruitPeriod().isAfter(now) ? RECRUITING : END_RECRUIT;
return club.getEndRecruitPeriod().isAfter(now) ? RECRUITING : END_RECRUIT;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ddingdong.ddingdongBE.domain.scorehistory.api;

import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.RegisterScoreRequest;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseStatus;

@Tag(name = "ScoreHistory - Admin", description = "ScoreHistory Admin API")
@RequestMapping("/server/admin/{clubId}/score")
public interface AdminScoreHistoryApi {

@Operation(summary = "어드민 점수 등록 API")
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@SecurityRequirement(name = "AccessToken")
void register(@PathVariable Long clubId, @RequestBody RegisterScoreRequest registerScoreRequest);

@Operation(summary = "어드민 동아리 점수 내역 목록 조회 API")
@GetMapping
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "AccessToken")
ScoreHistoryFilterByClubResponse getScoreHistories(@PathVariable Long clubId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ddingdong.ddingdongBE.domain.scorehistory.api;

import ddingdong.ddingdongBE.auth.PrincipalDetails;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.RegisterScoreRequest;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseStatus;

@Tag(name = "ScoreHistory - Club", description = "ScoreHistory Club API")
@RequestMapping("/server/club/my/score")
public interface ClubScoreHistoryApi {

@Operation(summary = "동아리 내 점수 내역 목록 조회 API")
@GetMapping
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "AccessToken")
ScoreHistoryFilterByClubResponse getMyScoreHistories(@AuthenticationPrincipal PrincipalDetails principalDetails);

}
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
package ddingdong.ddingdongBE.domain.scorehistory.controller;

import ddingdong.ddingdongBE.domain.club.entity.Club;
import ddingdong.ddingdongBE.domain.club.service.ClubService;
import ddingdong.ddingdongBE.domain.scorehistory.api.AdminScoreHistoryApi;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.RegisterScoreRequest;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse.ScoreHistoryResponse;
import ddingdong.ddingdongBE.domain.scorehistory.service.ScoreHistoryService;

import java.util.List;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

@RestController
@RequiredArgsConstructor
@RequestMapping("/server/admin/{clubId}/score")
public class AdminScoreHistoryController {
public class AdminScoreHistoryController implements AdminScoreHistoryApi {

private final ClubService clubService;
private final ScoreHistoryService scoreHistoryService;

@PostMapping
public void register(
@PathVariable Long clubId,
@RequestBody RegisterScoreRequest registerScoreRequest
) {
public void register(Long clubId, RegisterScoreRequest registerScoreRequest) {
scoreHistoryService.register(clubId, registerScoreRequest);
}

@GetMapping
public List<ScoreHistoryFilterByClubResponse> getScoreHistories(
@PathVariable Long clubId
) {
return scoreHistoryService.getScoreHistories(clubId);
public ScoreHistoryFilterByClubResponse getScoreHistories(Long clubId) {
Club club = clubService.getByClubId(clubId);
List<ScoreHistoryResponse> scoreHistoryResponses = scoreHistoryService.getScoreHistories(clubId).stream()
.map(ScoreHistoryResponse::from)
.toList();
return ScoreHistoryFilterByClubResponse.of(club, scoreHistoryResponses);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package ddingdong.ddingdongBE.domain.scorehistory.controller;

import ddingdong.ddingdongBE.auth.PrincipalDetails;
import ddingdong.ddingdongBE.domain.club.entity.Club;
import ddingdong.ddingdongBE.domain.club.service.ClubService;
import ddingdong.ddingdongBE.domain.scorehistory.api.ClubScoreHistoryApi;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse.ScoreHistoryResponse;
import ddingdong.ddingdongBE.domain.scorehistory.service.ScoreHistoryService;
import java.util.List;

import lombok.RequiredArgsConstructor;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/server/club/my/score")
public class ClubScoreHistoryController {
public class ClubScoreHistoryController implements ClubScoreHistoryApi {

private final ClubService clubService;
private final ScoreHistoryService scoreHistoryService;

@GetMapping
public List<ScoreHistoryFilterByClubResponse> getMyScoreHistories(
@AuthenticationPrincipal PrincipalDetails principalDetails
) {
return scoreHistoryService.getMyScoreHistories(principalDetails.getUser().getId());
public ScoreHistoryFilterByClubResponse getMyScoreHistories(PrincipalDetails principalDetails) {
Club club = clubService.getByUserId(principalDetails.getUser().getId());
List<ScoreHistoryResponse> scoreHistoryResponses = scoreHistoryService.getMyScoreHistories(club.getId())
.stream()
.map(ScoreHistoryResponse::from)
.toList();
return ScoreHistoryFilterByClubResponse.of(club, scoreHistoryResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public class RegisterScoreRequest {

private float amount;

public ScoreHistory toEntity(Club club, float remainingScore) {
public ScoreHistory toEntity(Club club) {
return ScoreHistory.builder()
.club(club)
.amount(amount)
.scoreCategory(ScoreCategory.of(scoreCategory))
.reason(reason)
.remainingScore(remainingScore)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
package ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import ddingdong.ddingdongBE.domain.club.entity.Club;
import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Getter
public class ScoreHistoryFilterByClubResponse {
private String scoreCategory;

private String reason;

private float amount;
@Schema(
name = "ScoreHistoryFilterByClubResponse",
description = "어드민 - 동아리 점수 변동 내역 목록 응답"
)
@Builder
public record ScoreHistoryFilterByClubResponse(

private float remainingScore;
@Schema(description = "동아리 총 점수", example = "50")
float totalScore,
@ArraySchema(schema = @Schema(description = "점수내역 목록", implementation = ScoreHistoryResponse.class))
List<ScoreHistoryResponse> scoreHistories
) {

private LocalDateTime createdAt;
public static ScoreHistoryFilterByClubResponse of(Club club, List<ScoreHistoryResponse> scoreHistories) {
return ScoreHistoryFilterByClubResponse.builder()
.totalScore(club.getScore().getValue())
.scoreHistories(scoreHistories)
.build();
}

@Schema(
name = "ScoreHistoryResponse",
description = "점수 변동 내역 응답"
)
@Builder
public ScoreHistoryFilterByClubResponse(String scoreCategory, String reason, float amount, float remainingScore, LocalDateTime createdAt) {
this.scoreCategory = scoreCategory;
this.reason = reason;
this.amount = amount;
this.remainingScore = remainingScore;
this.createdAt = createdAt;
}
public record ScoreHistoryResponse(

@Schema(description = "점수 내역 카테고리", example = "활동보고서")
String scoreCategory,
@Schema(description = "점수 내역 이유", example = "활동보고서 작성")
String reason,
@Schema(description = "변동 점수", example = "10")
float amount,
@Schema(description = "작성일", example = "2024-01-01")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime createdAt
) {

public static ScoreHistoryResponse from(ScoreHistory scoreHistory) {
return ScoreHistoryResponse.builder()
.scoreCategory(scoreHistory.getScoreCategory().getCategory())
.reason(scoreHistory.getReason())
.amount(scoreHistory.getAmount())
.createdAt(scoreHistory.getCreatedAt())
.build();
}

public static ScoreHistoryFilterByClubResponse of(final ScoreHistory scoreHistory) {
return ScoreHistoryFilterByClubResponse.builder()
.scoreCategory(scoreHistory.getScoreCategory().getCategory())
.reason(scoreHistory.getReason())
.amount(scoreHistory.getAmount())
.remainingScore(scoreHistory.getRemainingScore())
.createdAt(scoreHistory.getCreatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public static ScoreCategory of(String category) {
}
throw new IllegalArgumentException(ILLEGAL_SCORE_CATEGORY.getText());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ public class ScoreHistory extends BaseEntity {

private String reason;

private float remainingScore;

@Builder
public ScoreHistory(Club club, float amount, ScoreCategory scoreCategory, String reason, float remainingScore) {
public ScoreHistory(Club club, float amount, ScoreCategory scoreCategory, String reason) {
this.club = club;
this.amount = amount;
this.scoreCategory = scoreCategory;
this.reason = reason;
this.remainingScore = remainingScore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ddingdong.ddingdongBE.domain.club.entity.Club;
import ddingdong.ddingdongBE.domain.club.service.ClubService;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.RegisterScoreRequest;
import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse;
import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory;
import ddingdong.ddingdongBE.domain.scorehistory.repository.ScoreHistoryRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,27 +22,20 @@ public void register(final Long clubId, RegisterScoreRequest registerScoreReques
Club club = clubService.getByClubId(clubId);

float score = roundToThirdPoint(registerScoreRequest.getAmount());

float remainingScore = clubService.editClubScore(clubId, score);

scoreHistoryRepository.save(registerScoreRequest.toEntity(club, remainingScore));
clubService.editClubScore(clubId, score);
scoreHistoryRepository.save(registerScoreRequest.toEntity(club));
}

@Transactional(readOnly = true)
public List<ScoreHistoryFilterByClubResponse> getScoreHistories(final Long clubId) {
public List<ScoreHistory> getScoreHistories(final Long clubId) {

return scoreHistoryRepository.findByClubId(clubId).stream()
.map(ScoreHistoryFilterByClubResponse::of)
.toList();
return scoreHistoryRepository.findByClubId(clubId);
}

@Transactional(readOnly = true)
public List<ScoreHistoryFilterByClubResponse> getMyScoreHistories(final Long userId) {
public List<ScoreHistory> getMyScoreHistories(final Long userId) {
Club club = clubService.getByUserId(userId);

return scoreHistoryRepository.findByClubId(club.getId()).stream()
.map(ScoreHistoryFilterByClubResponse::of)
.toList();
return scoreHistoryRepository.findByClubId(club.getId());
}

private static float roundToThirdPoint(float value) {
Expand Down
Loading

0 comments on commit 771826d

Please sign in to comment.