Skip to content

Commit

Permalink
Merge pull request #48 from TEAM-DHS/feat/registration
Browse files Browse the repository at this point in the history
[FEAT] 행사 참가 신청
  • Loading branch information
mingulmangul authored Nov 25, 2023
2 parents 668a3e0 + 2234984 commit 238923d
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import java.net.URI;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import com.efub.dhs.domain.heart.dto.HeartResponseDto;
import com.efub.dhs.domain.heart.entity.Heart;
import com.efub.dhs.domain.heart.repository.HeartRepository;
import com.efub.dhs.domain.member.entity.Member;
import com.efub.dhs.domain.member.repository.MemberRepository;
import com.efub.dhs.domain.member.service.MemberService;
import com.efub.dhs.domain.program.entity.Program;
import com.efub.dhs.domain.program.repository.ProgramRepository;
import com.efub.dhs.global.utils.SecurityUtils;

import lombok.RequiredArgsConstructor;

Expand All @@ -23,8 +24,8 @@
public class HeartService {

private final HeartRepository heartRepository;
private final MemberRepository memberRepository;
private final ProgramRepository programRepository;
private final MemberService memberService;

private Heart getHeart(Member member, Program program) {
return heartRepository.findByMemberAndProgram(member, program)
Expand All @@ -42,16 +43,14 @@ public Boolean existsByMemberAndProgram(Member member, Program program) {
}

public ResponseEntity<HeartResponseDto> heartProgram(Long programId) {
String username = SecurityUtils.getCurrentUsername();
Member currentUser = memberRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("해당 아이디의 회원을 찾을 수 없습니다."));
Member currentUser = memberService.getCurrentUser();
Program program = programRepository.findById(programId)
.orElseThrow(() -> new IllegalArgumentException("해당 ID의 행사를 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 ID의 행사를 찾을 수 없습니다."));
Heart heart = getOrCreateHeart(currentUser, program);
return createOrCancelHeart(programId, heart);
return convertHeartExist(programId, heart);
}

private ResponseEntity<HeartResponseDto> createOrCancelHeart(Long programId, Heart heart) {
private ResponseEntity<HeartResponseDto> convertHeartExist(Long programId, Heart heart) {
// 좋아요 생성
if (Boolean.FALSE.equals(heart.getExist())) {
heart.activateHeart();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.efub.dhs.domain.member.service;

import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import com.efub.dhs.domain.member.entity.Member;
import com.efub.dhs.domain.member.repository.MemberRepository;
Expand All @@ -28,7 +30,7 @@ public class AuthService {
public Member signUp(String username, String password) {
boolean exists = memberRepository.existsByUsername(username);
if (exists) {
throw new IllegalArgumentException("동일한 아이디의 유저가 존재합니다.");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "동일한 아이디의 유저가 존재합니다.");
}
String encodedPassword = passwordEncoder.encode(password);
Member member = Member.builder().username(username).password(encodedPassword).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.efub.dhs.domain.member.service;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import com.efub.dhs.domain.member.entity.Member;
import com.efub.dhs.domain.member.repository.MemberRepository;
Expand All @@ -18,6 +20,6 @@ public class MemberService {
public Member getCurrentUser() {
String username = SecurityUtils.getCurrentUsername();
return memberRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("해당 아이디의 회원을 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "해당 아이디의 회원을 찾을 수 없습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ProgramCreationResponseDto createProgram(@RequestBody @Valid ProgramCreat

@PostMapping("/{programId}")
@ResponseStatus(value = HttpStatus.CREATED)
public ProgramRegistrationResponseDto applyProgram(@PathVariable Long programId,
public ProgramRegistrationResponseDto registerProgram(@PathVariable Long programId,
@RequestBody @Valid ProgramRegistrationRequestDto requestDto) {
Registration savedRegistration = programService.registerProgram(programId, requestDto);
return ProgramRegistrationResponseDto.from(savedRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.efub.dhs.domain.program.dto.GoalDto;
import com.efub.dhs.domain.program.dto.PageInfoDto;
import com.efub.dhs.domain.program.entity.Program;
import com.efub.dhs.domain.registration.entity.RefundStatus;
import com.efub.dhs.domain.registration.entity.Registration;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -14,17 +14,17 @@
@AllArgsConstructor
public class ProgramRegisteredResponseDto {

private List<ProgramOutlineWithStatusDto> programs;
private List<ProgramOutlineWithRegistrationDto> programs;
private PageInfoDto pageInfo;

@Getter
public static class ProgramOutlineWithStatusDto extends ProgramOutlineResponseDto {
private final RefundStatus refundStatus;
public static class ProgramOutlineWithRegistrationDto extends ProgramOutlineResponseDto {
private final ProgramRegistrationResponseDto registrationInfoDto;

public ProgramOutlineWithStatusDto(
Program program, Integer remainingDays, GoalDto goal, Boolean hasLike, RefundStatus refundStatus) {
public ProgramOutlineWithRegistrationDto(
Program program, Integer remainingDays, GoalDto goal, Boolean hasLike, Registration registration) {
super(program, remainingDays, goal, hasLike);
this.refundStatus = refundStatus;
this.registrationInfoDto = ProgramRegistrationResponseDto.from(registration);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
package com.efub.dhs.domain.program.dto.response;

import com.efub.dhs.domain.program.dto.request.ProgramRegistrationRequestDto;
import java.time.LocalDateTime;

import com.efub.dhs.domain.registration.entity.RefundStatus;
import com.efub.dhs.domain.registration.entity.Registration;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ProgramRegistrationResponseDto {

private long registrationId;
private ProgramRegistrationRequestDto registrationInfo;
private String depositorName;
private String depositAmount;
private LocalDateTime depositDate;
private String registrantName;
private String registrantPhone;
private String refundBank;
private String refundAccount;
private String refundName;
private String refundStatus;

public static ProgramRegistrationResponseDto from(Registration registration) {
return new ProgramRegistrationResponseDto(
registration.getRegistrationId(),
ProgramRegistrationRequestDto.builder()
.depositorName(registration.getDepositName())
.depositAmount(registration.getDepositAmount())
.depositDate(registration.getDepositDate())
.registrantName(registration.getRegistrantName())
.registrantPhone(registration.getRegistrantPhone())
.refundBank(registration.getRefundBank())
.refundAccount(registration.getRefundAccount())
.refundName(registration.getRefundName())
.build()
);
return ProgramRegistrationResponseDto.builder()
.registrationId(registration.getRegistrationId())
.depositorName(registration.getDepositName())
.depositAmount(registration.getDepositAmount())
.depositDate(registration.getDepositDate())
.registrantName(registration.getRegistrantName())
.registrantPhone(registration.getRegistrantPhone())
.refundBank(registration.getRefundBank())
.refundAccount(registration.getRefundAccount())
.refundName(registration.getRefundName())
.refundStatus(RefundStatus.to(registration.getRefundStatus()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Arrays;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
Expand All @@ -19,7 +22,7 @@ public static Category from(String name) {
return Arrays.stream(Category.values())
.filter(category -> category.name.equals(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid Category name: " + name));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid Category name: " + name));
}

public static String to(Category category) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/efub/dhs/domain/program/entity/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public Program(Member host, String title, Category category, LocalDateTime sched
this.notices = List.of();
}

public void increaseRegistrantNumber() {
this.registrantNumber++;
}

public void decreaseRegistrantNumber() {
this.registrantNumber--;
}

public void closeProgram() {
this.isOpen = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ public ProgramRegisteredResponseDto findProgramRegistered(int page) {
Page<Registration> registrationPage = registrationRepository.findRegisteredPrograms(
currentUser, PageRequest.of(page, PAGE_SIZE));
PageInfoDto pageInfoDto = PageInfoDto.createRegistrationPageInfoDto(registrationPage);
List<ProgramOutlineWithStatusDto> programOutlineWithStatusDtoList =
convertToProgramOutlineWithStatusDtoList(registrationPage.getContent(), currentUser);
return new ProgramRegisteredResponseDto(programOutlineWithStatusDtoList, pageInfoDto);
List<ProgramOutlineWithRegistrationDto> programOutlineWithRegistrationDtoList =
convertToProgramOutlineWithRegistrationDtoList(registrationPage.getContent(), currentUser);
return new ProgramRegisteredResponseDto(programOutlineWithRegistrationDtoList, pageInfoDto);
}

public List<ProgramOutlineWithStatusDto> convertToProgramOutlineWithStatusDtoList(
public List<ProgramOutlineWithRegistrationDto> convertToProgramOutlineWithRegistrationDtoList(
List<Registration> registrationList, Member member) {
return registrationList.stream().map(registration -> {
Program program = registration.getProgram();
return new ProgramOutlineWithStatusDto(program,
return new ProgramOutlineWithRegistrationDto(program,
programService.calculateRemainingDays(program.getDeadline()),
programService.findGoalByProgram(program.getTargetNumber(), program.getRegistrantNumber()),
heartService.existsByMemberAndProgram(member, program),
registration.getRefundStatus());
registration);
}).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ProgramService {

public Program getProgram(Long programId) {
return programRepository.findById(programId)
.orElseThrow(() -> new IllegalArgumentException("해당 ID의 행사를 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 ID의 행사를 찾을 수 없습니다." + programId));
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -154,7 +154,9 @@ public Registration registerProgram(Long programId, ProgramRegistrationRequestDt
Member currentUser = memberService.getCurrentUser();
Program program = getProgram(programId);
Registration registration = requestDto.toEntity(currentUser, program);
return registrationService.saveRegistration(registration);
Registration savedRegistration = registrationService.saveRegistration(registration);
program.increaseRegistrantNumber();
return savedRegistration;
}

public ProgramListResponseDto findProgramList(int page, ProgramListRequestDto requestDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.efub.dhs.domain.registration.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.efub.dhs.domain.program.dto.response.ProgramRegistrationResponseDto;
import com.efub.dhs.domain.registration.dto.RegistrationModificationRequestDto;
import com.efub.dhs.domain.registration.entity.Registration;
import com.efub.dhs.domain.registration.service.RegistrationService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/registrations")
public class RegistrationController {

private final RegistrationService registrationService;

@GetMapping("/{registrationId}")
public ProgramRegistrationResponseDto findRegistration(@PathVariable Long registrationId) {
Registration registration = registrationService.findRegistration(registrationId);
return ProgramRegistrationResponseDto.from(registration);
}

@PatchMapping("/{registrationId}")
public ProgramRegistrationResponseDto modifyRegistration(
@PathVariable Long registrationId, @RequestBody RegistrationModificationRequestDto requestDto) {
Registration modifiedRegistration = registrationService.modifyRegistration(registrationId, requestDto);
return ProgramRegistrationResponseDto.from(modifiedRegistration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.efub.dhs.domain.registration.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class RegistrationModificationRequestDto {

private String registrantName;
private String registrantPhone;
private String refundBank;
private String refundAccount;
private String refundName;
private String refundStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Arrays;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
Expand All @@ -16,7 +19,8 @@ public static RefundStatus from(String name) {
return Arrays.stream(RefundStatus.values())
.filter(refundStatus -> refundStatus.name.equals(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid RefundStatus name: " + name));
.orElseThrow(
() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid RefundStatus name: " + name));
}

public static String to(RefundStatus refundStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.efub.dhs.domain.member.entity.Member;
import com.efub.dhs.domain.program.entity.Program;
import com.efub.dhs.domain.registration.dto.RegistrationModificationRequestDto;

import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -84,4 +85,26 @@ public Registration(Member member, Program program, String registrantName, Strin
this.refundAccount = refundAccount;
this.refundName = refundName;
}

public Registration modifyRegistration(RegistrationModificationRequestDto requestDto) {
if (requestDto.getRegistrantName() != null) {
this.registrantName = requestDto.getRegistrantName();
}
if (requestDto.getRegistrantPhone() != null) {
this.registrantPhone = requestDto.getRegistrantPhone();
}
if (requestDto.getRefundBank() != null) {
this.refundBank = requestDto.getRefundBank();
}
if (requestDto.getRefundAccount() != null) {
this.refundAccount = requestDto.getRefundAccount();
}
if (requestDto.getRefundName() != null) {
this.refundName = requestDto.getRefundName();
}
if (requestDto.getRefundStatus() != null) {
this.refundStatus = RefundStatus.from(requestDto.getRefundStatus());
}
return this;
}
}
Loading

0 comments on commit 238923d

Please sign in to comment.