Skip to content

Commit

Permalink
Merge pull request #20 from KNU-HAEDAL/issue/#14
Browse files Browse the repository at this point in the history
Issue/#14
  • Loading branch information
bayy1216 authored Jun 1, 2024
2 parents 2a44c31 + a2ed34b commit f4440be
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@
import org.haedal.zzansuni.controller.auth.AuthReq;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.domain.auth.AuthService;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Tag(name = "admin", description = "관리자 API")
@RequiredArgsConstructor
@RestController
public class AdminController {
private final AuthService authService;
private final ChallengeGroupService challengeGroupService;

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary= "매니저 등록", description = "매니저를 등록한다.")
@PostMapping("/api/auth/manager")
@PostMapping("/api/admin/auth/manager")
public ApiResponse<Void> createManager(@RequestBody @Valid AuthReq.EmailSignupRequest request) {
authService.createManager(request.toCommand());
return ApiResponse.success(null, "매니저 등록 성공");
}

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "챌린지 그룹 생성", description = "챌린지 그룹과 해당하는 챌린지를 생성합니다")
@PostMapping("/api/admin/challengeGroups")
public ApiResponse<Void> createChallengeGroup(@RequestBody @Valid AdminReq.CreateChallengeGroupRequest request) {
challengeGroupService.createChallengeGroup(request.toCommand());
return ApiResponse.success(null, "챌린지 그룹 생성 성공");
}

@Operation(summary = "챌린지 그룹 삭제", description = "챌린지 그룹을 삭제합니다")
@DeleteMapping("/api/admin/challengeGroups/{challengeGroupId}")
public ApiResponse<Void> deleteChallengeGroup(@PathVariable Long challengeGroupId) {
challengeGroupService.deleteChallengeGroup(challengeGroupId);
return ApiResponse.success(null, "챌린지 그룹 삭제 성공");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.haedal.zzansuni.controller.admin;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.haedal.zzansuni.domain.challengegroup.ChallengeCategory;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupCommand;
import org.haedal.zzansuni.domain.challengegroup.DayType;

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

public class AdminReq {
public record CreateChallengeGroupRequest(
@NotBlank(message = "title은 필수값입니다.")
String title,
@NotBlank(message = "content는 필수값입니다.")
String content,
@NotBlank(message = "guide는 필수값입니다.")
String guide,
@NotNull(message = "category는 필수값입니다.")
ChallengeCategory category,
@NotNull(message = "challenges는 필수값입니다.")
List<CreateChallengeRequest> challenges
){
public ChallengeGroupCommand.Create toCommand() {
return ChallengeGroupCommand.Create.builder()
.title(title)
.content(content)
.guide(guide)
.category(category)
.createChallenges(challenges.stream().map(CreateChallengeRequest::toCommand).toList())
.build();
}
}

public record CreateChallengeRequest(
@NotNull(message = "startDate는 필수값입니다.")
LocalDate startDate,
@NotNull(message = "endDate는 필수값입니다.")
LocalDate endDate,
@NotNull(message = "dayType은 필수값입니다.")
DayType dayType,
@NotNull(message = "requiredCount는 필수값입니다.")
Integer requiredCount,
@NotNull(message = "onceExp는 필수값입니다.")
Integer onceExp,
@NotNull(message = "successExp는 필수값입니다.")
Integer successExp,
@NotNull(message = "difficulty는 필수값입니다.")
Integer difficulty
){
public ChallengeGroupCommand.CreateChallenge toCommand() {
return ChallengeGroupCommand.CreateChallenge.builder()
.startDate(startDate)
.endDate(endDate)
.dayType(dayType)
.requiredCount(requiredCount)
.onceExp(onceExp)
.successExp(successExp)
.difficulty(difficulty)
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ public class ChallengeGroup extends BaseTimeEntity {

@OneToMany(mappedBy = "challengeGroup", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Challenge> challenges = new ArrayList<>();

public static ChallengeGroup create(ChallengeGroupCommand.Create command) {
List<Challenge> challenges = new ArrayList<>();
ChallengeGroup group = ChallengeGroup.builder()
.category(command.getCategory())
.title(command.getTitle())
.content(command.getContent())
.guide(command.getGuide())
.cumulativeCount(0)
.challenges(challenges)
.build();
command.getCreateChallenges().stream().map(challenge -> Challenge.create(challenge, group))
.forEach(challenges::add);
return group;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.haedal.zzansuni.domain.challengegroup;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

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

public class ChallengeGroupCommand {
@Builder
@Getter
public static class Create {
@NotBlank(message = "title은 필수값입니다.")
private final String title;
@NotBlank(message = "content는 필수값입니다.")
private final String content;
@NotBlank(message = "guide는 필수값입니다.")
private final String guide;
@NotNull(message = "category는 필수값입니다.")
private final ChallengeCategory category;
@NotNull(message = "challenges는 필수값입니다.")
private final List<CreateChallenge> createChallenges;

}

@Builder
@Getter
public static class CreateChallenge {
@NotNull(message = "startDate는 필수값입니다.")
private final LocalDate startDate;
@NotNull(message = "endDate는 필수값입니다.")
private final LocalDate endDate;
@NotNull(message = "dayType은 필수값입니다.")
private final DayType dayType;
@NotNull(message = "requiredCount는 필수값입니다.")
private final Integer requiredCount;
@NotNull(message = "onceExp는 필수값입니다.")
private final Integer onceExp;
@NotNull(message = "successExp는 필수값입니다.")
private final Integer successExp;
@NotNull(message = "difficulty는 필수값입니다.")
private final Integer difficulty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.haedal.zzansuni.domain.challengegroup;

public interface ChallengeGroupReader {
ChallengeGroup getById(Long challengeGroupId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.haedal.zzansuni.domain.challengegroup;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ChallengeGroupService {
private final ChallengeGroupStore challengeGroupStore;
private final ChallengeGroupReader challengeGroupReader;

@Transactional
public void createChallengeGroup(ChallengeGroupCommand.Create command) {
ChallengeGroup challengeGroup = ChallengeGroup.create(command);
challengeGroupStore.save(challengeGroup);
}

@Transactional
public void deleteChallengeGroup(Long challengeGroupId) {
ChallengeGroup challengeGroup = challengeGroupReader.getById(challengeGroupId);
challengeGroupStore.delete(challengeGroup.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.haedal.zzansuni.domain.challengegroup;

public interface ChallengeGroupStore {
ChallengeGroup save(ChallengeGroup challengeGroup);
void delete(Long challengeGroupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.domain.BaseTimeEntity;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupCommand;
import org.haedal.zzansuni.domain.challengegroup.DayType;

@Entity
Expand Down Expand Up @@ -50,4 +51,16 @@ public class Challenge extends BaseTimeEntity {
private LocalDate endDate;


public static Challenge create(ChallengeGroupCommand.CreateChallenge command, ChallengeGroup group) {
return Challenge.builder()
.challengeGroup(group)
.requiredCount(command.getRequiredCount())
.dayType(command.getDayType())
.onceExp(command.getOnceExp())
.successExp(command.getSuccessExp())
.difficulty(command.getDifficulty())
.startDate(command.getStartDate())
.endDate(command.getEndDate())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.haedal.zzansuni.infrastructure.challengegroup;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupReader;
import org.springframework.stereotype.Component;

import java.util.NoSuchElementException;

@Component
@RequiredArgsConstructor
public class ChallengeGroupReaderImpl implements ChallengeGroupReader {
private final ChallengeGroupRepository challengeGroupRepository;
@Override
public ChallengeGroup getById(Long challengeGroupId) {
return challengeGroupRepository
.findById(challengeGroupId)
.orElseThrow(NoSuchElementException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.haedal.zzansuni.infrastructure.challengegroup;

import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChallengeGroupRepository extends JpaRepository<ChallengeGroup, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.haedal.zzansuni.infrastructure.challengegroup;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroup;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupStore;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ChallengeGroupStoreImpl implements ChallengeGroupStore {
private final ChallengeGroupRepository challengeGroupRepository;
@Override
public ChallengeGroup save(ChallengeGroup challengeGroup) {
return challengeGroupRepository.save(challengeGroup);
}

@Override
public void delete(Long challengeGroupId) {
challengeGroupRepository.deleteById(challengeGroupId);
}
}

0 comments on commit f4440be

Please sign in to comment.