Skip to content

Commit

Permalink
Merge pull request #122 from team-Ollie/develop
Browse files Browse the repository at this point in the history
[develop] ํ”„๋กœ๊ทธ๋žจ ๋“ฑ๋ก ๋กœ์ง ๊ฐœ์„ 
  • Loading branch information
JoongHyun-Kim authored Jul 3, 2024
2 parents d018a67 + 643bd8e commit 601ad67
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import ollie.wecare.program.dto.PostProgramReq;
import ollie.wecare.program.dto.ProgramListResponse;
import ollie.wecare.program.service.ProgramService;
import ollie.wecare.user.service.AuthService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static ollie.wecare.common.base.BaseResponseStatus.SUCCESS;
import static ollie.wecare.common.constants.RequestURI.program;

@RestController
Expand All @@ -22,6 +22,7 @@
@Slf4j
public class ProgramController {
private final ProgramService programService;
private final AuthService authService;

// ํ”„๋กœ๊ทธ๋žจ ์กฐํšŒ (์›”๋ณ„)
@GetMapping
Expand All @@ -44,7 +45,6 @@ public BaseResponse<GetProgramDetailRes> getProgram(@PathVariable(value = "progr
// ํ”„๋กœ๊ทธ๋žจ ๋“ฑ๋ก
@PostMapping
public BaseResponse<String> saveProgram(@RequestBody PostProgramReq postProgramReq) throws BaseException {
programService.saveTag(postProgramReq, programService.saveProgram(postProgramReq).getProgram());
return new BaseResponse<>(SUCCESS);
return programService.saveProgram(authService.getUserIdx(), postProgramReq);
}
}
12 changes: 1 addition & 11 deletions src/main/java/ollie/wecare/program/dto/PostProgramReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,21 @@
@Getter
public class PostProgramReq {
private String name;

private DateDto dueDate;

private DateDto openDate;

private String location;

private String locatedPlace;

private String host;

private String schedule;

private String description;

private String category;

public static Program toProgram(PostProgramReq postProgramReq) {
return Program.builder().name(postProgramReq.getName())
.dueDate(convertToLocalDateTime(postProgramReq.getDueDate()))
.openDate(convertToLocalDateTime(postProgramReq.getOpenDate()))
.host(postProgramReq.getHost())
.locatedPlace(postProgramReq.getLocatedPlace())
.host(postProgramReq.getHost())
.schedule(postProgramReq.getSchedule())
.description(postProgramReq.getDescription())
.build();
Expand All @@ -40,7 +32,5 @@ private static LocalDateTime convertToLocalDateTime(DateDto dateDto) {
if(dateDto == null) return null;
return LocalDateTime.of(dateDto.year(), dateDto.month(), dateDto.day(), 0, 0, 0);
}


}

38 changes: 26 additions & 12 deletions src/main/java/ollie/wecare/program/service/ProgramService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ollie.wecare.program.repository.ProgramRepository;
import ollie.wecare.program.repository.TagRepository;
import ollie.wecare.user.entity.User;
import ollie.wecare.user.service.UserService;
import ollie.wecare.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -36,10 +36,10 @@
@Transactional
public class ProgramService {

private final UserService userService;
private final ProgramRepository programRepository;
private final ChallengeRepository challengeRepository;
private final TagRepository tagRepository;
private final UserRepository userRepository;

// ํ”„๋กœ๊ทธ๋žจ ์›”๋ณ„ ์กฐํšŒ
public List<GetProgramRes> getPrograms(Long year, Long month) {
Expand Down Expand Up @@ -68,22 +68,36 @@ public GetProgramDetailRes getProgram(Long programIdx) {
programRepository.findById(programIdx).orElseThrow(()-> new BaseException(INVALID_PROGRAM_IDX)));
}

//TODO : ์—ฐ๊ด€๊ด€๊ณ„ ์ˆ˜์ •
public Challenge saveProgram(PostProgramReq postProgramReq) {
User user = userService.getUserWithValidation();
if(!user.getRole().equals(Role.Admin)) throw new BaseException(INVALID_ROLE);
return this.saveChallenge(PostProgramReq.toProgram(postProgramReq), user);
// ํ”„๋กœ๊ทธ๋žจ ๋“ฑ๋ก
@Transactional(rollbackFor = Exception.class)
public BaseResponse<String> saveProgram(Long userIdx, PostProgramReq postProgramReq) {
User user = userRepository.findByUserIdx(userIdx).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
if (!user.getRole().equals(Role.Admin)) throw new BaseException(INVALID_ROLE);

Program newProgram = PostProgramReq.toProgram(postProgramReq);
programRepository.save(newProgram);

saveChallenge(newProgram, user);
saveTag(postProgramReq, newProgram);
return new BaseResponse<>(SUCCESS);
}

public void saveTag(PostProgramReq postProgramReq, Program program) {
private void saveTag(PostProgramReq postProgramReq, Program program) {
tagRepository.save(Tag.builder().name(TagEnum.getEnumByName(postProgramReq.getCategory())).program(program).build());
tagRepository.save(Tag.builder().name(TagEnum.getEnumByName(postProgramReq.getLocation())).program(program).build());
}

public Challenge saveChallenge(Program program, User user) {
private void saveChallenge(Program program, User user) {
Duration duration = Duration.between(program.getOpenDate(), program.getDueDate());
Integer totalNum = Math.toIntExact(duration.toDays() / 7);//TODO : ํŠน์ • ์š”์ผ ํšŸ์ˆ˜ ๋ฐ˜์˜
return challengeRepository.save(Challenge.builder().program(program).name(program.getName()).attendanceRate(0)
.admin(user).host(program.getHost()).totalNum(totalNum).build());
Integer totalNum = Math.toIntExact(duration.toDays() / 7); //TODO : ํŠน์ • ์š”์ผ ํšŸ์ˆ˜ ๋ฐ˜์˜
Challenge newChallenge = Challenge.builder()
.program(program)
.name(program.getName())
.attendanceRate(0)
.admin(user)
.host(program.getHost())
.totalNum(totalNum).build();
challengeRepository.save(newChallenge);
}

}

0 comments on commit 601ad67

Please sign in to comment.