Skip to content

Commit

Permalink
Merge pull request #88 from KNU-HAEDAL/issue/#79
Browse files Browse the repository at this point in the history
Issue/#79
  • Loading branch information
kwonssshyeon authored Sep 12, 2024
2 parents 44fb89a + e605148 commit 6aae7bf
Show file tree
Hide file tree
Showing 30 changed files with 581 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.auth.controller.AuthReq;
import org.haedal.zzansuni.auth.domain.AuthService;
import org.haedal.zzansuni.challengegroup.controller.ChallengeGroupReq;
import org.haedal.zzansuni.challengegroup.domain.application.ChallengeGroupService;
import org.haedal.zzansuni.common.controller.PagingRequest;
import org.haedal.zzansuni.common.controller.PagingResponse;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.user.domain.UserModel;
import org.haedal.zzansuni.user.domain.UserService;
import org.haedal.zzansuni.userchallenge.controller.ChallengeRes;
import org.haedal.zzansuni.userchallenge.domain.ChallengeVerificationStatus;
import org.haedal.zzansuni.userchallenge.domain.application.ChallengeVerificationService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "admin", description = "관리자 API")
@RequiredArgsConstructor
@RestController
public class AdminController {

private final AuthService authService;
private final ChallengeGroupService challengeGroupService;
private final UserService userService;
private final ChallengeVerificationService challengeVerificationService;

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "매니저 등록", description = "매니저를 등록한다.")
Expand All @@ -27,6 +39,15 @@ public ApiResponse<Void> createManager(@RequestBody @Valid AuthReq.EmailSignupRe
return ApiResponse.success(null, "매니저 등록 성공");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "매니저, 어드민 계정 조회", description = "매니저, 어드민 계정을 조회한다.")
@GetMapping("/api/admin/auth/manager")
public ApiResponse<List<AdminRes.ManagerAndAdmin>> getAdminAndManager() {
List<UserModel.Main> managerAndAdmin = userService.getManagerAndAdmin();
return ApiResponse.success(AdminRes.ManagerAndAdmin.from(managerAndAdmin), "매니저, 어드민 계정 조회 성공");
}


@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 그룹 생성", description = "챌린지 그룹과 해당하는 챌린지를 생성합니다")
@PostMapping("/api/admin/challengeGroups")
Expand All @@ -42,4 +63,32 @@ public ApiResponse<Void> deleteChallengeGroup(@PathVariable Long challengeGroupI
challengeGroupService.deleteChallengeGroup(challengeGroupId);
return ApiResponse.success(null, "챌린지 그룹 삭제 성공");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "챌린지 그룹 수정", description = "챌린지 그룹을 수정합니다. 새로운 챌린지를 추가할땐 id=-1로 설정합니다.")
@PutMapping("/api/admin/challengeGroups/{challengeGroupId}")
public ApiResponse<Void> updateChallengeGroup(@Valid @RequestBody ChallengeGroupReq.Update request) {
challengeGroupService.updateChallengeGroup(request.toCommand());
return ApiResponse.success(null, "챌린지 그룹 수정 성공");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "챌린지 인증 조회", description = "챌린지 인증을 페이징 조회합니다. 챌린지 이름으로 검색이 가능합니다.")
@GetMapping("/api/admin/challenges/verifications")
public ApiResponse<PagingResponse<ChallengeRes.ChallengeVerification>> getChallengeVerifications(
@Valid PagingRequest pagingRequest,
@RequestParam(required = false) String challengeTitle){
var verificationPage = challengeVerificationService.getChallengeVerifications(pagingRequest.toPageable(), challengeTitle);
return ApiResponse.success(PagingResponse.from(verificationPage, ChallengeRes.ChallengeVerification::from), "챌린지 인증 조회 성공");
}

@ResponseStatus(HttpStatus.OK)
@Operation(summary = "챌린지 인증 승인/거절", description = "챌린지 인증을 승인/거절합니다. 거절시 경험치가 취소됩니다.")
@PatchMapping("/api/admin/challenges/verifications/{challengeVerificationId}")
public ApiResponse<Void> approveChallengeVerification(@PathVariable Long challengeVerificationId,
@Valid @RequestParam ChallengeVerificationStatus status) {
challengeVerificationService.confirm(challengeVerificationId, status);
return ApiResponse.success(null, "챌린지 인증 승인/거절 성공");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public record CreateChallengeGroupRequest(
String guide,
@NotNull(message = "category는 필수값입니다.")
ChallengeCategory category,
@NotNull(message = "joinStartDate는 필수값입니다.")
LocalDate joinStartDate,
@NotNull(message = "joinEndDate는 필수값입니다.")
LocalDate joinEndDate,
@NotNull(message = "challenges는 필수값입니다.")
List<CreateChallengeRequest> challenges
){
Expand All @@ -27,30 +31,32 @@ public ChallengeGroupCommand.Create toCommand() {
.content(content)
.guide(guide)
.category(category)
.joinStartDate(joinStartDate)
.joinEndDate(joinEndDate)
.createChallenges(challenges.stream().map(CreateChallengeRequest::toCommand).toList())
.build();
}
}

public record CreateChallengeRequest(
@NotNull(message = "startDate는 필수값입니다.")
LocalDate startDate,
@NotNull(message = "endDate는 필수값입니다.")
@NotNull(message = "requiredCount는 필수값입니다.")
Integer requiredCount,
@NotNull(message = "onceExp는 필수값입니다.")
Integer onceExp,
@NotNull(message = "successExp는 필수값입니다.")
Integer successExp,
@NotNull(message = "difficulty는 필수값입니다.")
Integer difficulty
Integer difficulty,
@NotNull(message = "activePeriod는 필수값입니다.")
Integer activePeriod
){
public ChallengeGroupCommand.CreateChallenge toCommand() {
return ChallengeGroupCommand.CreateChallenge.builder()
.requiredCount(requiredCount)
.onceExp(onceExp)
.successExp(successExp)
.difficulty(difficulty)
.activePeriod(activePeriod)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.haedal.zzansuni.admin.controller;

import lombok.Builder;
import org.haedal.zzansuni.user.domain.UserModel;

import java.time.LocalDateTime;
import java.util.List;

public class AdminRes {
@Builder
public record ManagerAndAdmin(
Long id,
String email,
String name,
String role,
LocalDateTime createdAt
) {
public static ManagerAndAdmin from(UserModel.Main userMain) {
return ManagerAndAdmin.builder()
.id(userMain.id())
.email(userMain.email())
.name(userMain.nickname())
.role(userMain.role().name())
.createdAt(userMain.createdAt())
.build();
}

public static List<ManagerAndAdmin> from(List<UserModel.Main> userMainList) {
return userMainList.stream()
.map(ManagerAndAdmin::from)
.toList();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
package org.haedal.zzansuni.challengegroup.controller;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.haedal.zzansuni.admin.controller.AdminReq;
import org.haedal.zzansuni.challengegroup.domain.ChallengeCategory;
import org.haedal.zzansuni.challengegroup.domain.ChallengeGroupCommand;

import java.time.LocalDate;
import java.util.List;

public class ChallengeGroupReq {
public record Update(
@NotNull(message = "id는 필수값입니다.")
Long id,
@NotBlank(message = "title은 필수값입니다.")
String title,
@NotBlank(message = "content는 필수값입니다.")
String content,
@NotBlank(message = "guide는 필수값입니다.")
String guide,
@NotNull(message = "category는 필수값입니다.")
ChallengeCategory category,
@NotNull(message = "joinStartDate는 필수값입니다.")
LocalDate joinStartDate,
@NotNull(message = "joinEndDate는 필수값입니다.")
LocalDate joinEndDate,
List<UpdateChallenge> updateChallenges,
List<AdminReq.CreateChallengeRequest> createChallenges
) {
public ChallengeGroupCommand.Update toCommand() {
return ChallengeGroupCommand.Update.builder()
.id(id)
.title(title)
.content(content)
.guide(guide)
.category(category)
.joinStartDate(joinStartDate)
.joinEndDate(joinEndDate)
.updateChallenges(updateChallenges.stream().map(UpdateChallenge::toCommand).toList())
.createChallenges(createChallenges.stream().map(AdminReq.CreateChallengeRequest::toCommand).toList())
.build();
}
}


public record UpdateChallenge(
@NotNull(message = "id는 필수값입니다.")
Long id,
@NotNull(message = "activePeriod는 필수값입니다.")
Integer activePeriod,
@NotNull(message = "requiredCount는 필수값입니다.")
Integer requiredCount,
@NotNull(message = "onceExp는 필수값입니다.")
Integer onceExp,
@NotNull(message = "successExp는 필수값입니다.")
Integer successExp,
@NotNull(message = "difficulty는 필수값입니다.")
Integer difficulty
){
public ChallengeGroupCommand.UpdateChallenge toCommand() {
return ChallengeGroupCommand.UpdateChallenge.builder()
.id(id)
.activePeriod(activePeriod)
.requiredCount(requiredCount)
.onceExp(onceExp)
.successExp(successExp)
.difficulty(difficulty)
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.persistence.*;

import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -53,6 +52,15 @@ public static Challenge create(ChallengeGroupCommand.CreateChallenge command, Ch
.build();
}

public Challenge update(ChallengeGroupCommand.UpdateChallenge command) {
this.onceExp = command.getOnceExp();
this.successExp = command.getSuccessExp();
this.difficulty = command.getDifficulty();
this.requiredCount = command.getRequiredCount();
this.activePeriod = command.getActivePeriod();
return this;
}

/**
* 챌린지 그룹 아이디 반환
* FK는 LAZY 여도 이미 영속성 컨텍스트에 존재하므로 추가 조회 없이 바로 접근 가능
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -65,10 +66,36 @@ public static ChallengeGroup create(ChallengeGroupCommand.Create command) {
.content(command.getContent())
.guide(command.getGuide())
.cumulativeCount(0)
.joinStartDate(command.getJoinStartDate())
.joinEndDate(command.getJoinEndDate())
.challenges(challenges)
.build();
command.getCreateChallenges().stream().map(challenge -> Challenge.create(challenge, group))
.forEach(challenges::add);
return group;
}

public ChallengeGroup update(ChallengeGroupCommand.Update command) {
this.category = command.getCategory();
this.title = command.getTitle();
this.content = command.getContent();
this.guide = command.getGuide();
this.joinStartDate = command.getJoinStartDate();
this.joinEndDate = command.getJoinEndDate();
return this;
}

public void addChallenges(List<Challenge> challenges) {
this.challenges.addAll(challenges);
}

public void removeChallenges(List<Challenge> challenges) {
this.challenges.removeAll(challenges);
}

public Optional<Challenge> getChallengeById(Long challengeId) {
return this.challenges.stream()
.filter(challenge -> challenge.getId().equals(challengeId))
.findFirst();
}
}
Loading

0 comments on commit 6aae7bf

Please sign in to comment.