Skip to content

Commit

Permalink
Merge pull request #578 from SWM-Morandi/feat/#576
Browse files Browse the repository at this point in the history
Feat/#576 DTO 클래스 Request, Response 구분
  • Loading branch information
miiiinju1 authored Nov 5, 2023
2 parents a2b1857 + c5f1f4a commit a82a597
Show file tree
Hide file tree
Showing 50 changed files with 195 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import swm_nm.morandi.domain.codeSubmit.dto.*;
import swm_nm.morandi.domain.codeSubmit.service.BaekjoonSubmitService;
Expand All @@ -14,34 +14,39 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/submit")
@Tag(name = "CodeSubmitController", description = "코드 제출과 관련된 컨트롤러")
@Tag(name = "CodeSubmitController", description = "백준 코드 제출과 관련된 컨트롤러")
public class CodeSubmitController {

private final BaekjoonSubmitService submitService;

private final SaveSubmitResultService saveSubmitResultService;
@PostMapping("/baekjoon")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "문제 번호, 언어이름, 소스코드를 백준에 제출하는 컨트롤러 ", description = "사용자가 테스트 중 코드를 제출하는 경우 백준에 제출하는 컨트롤러입니다.")
public ResponseEntity<SolutionIdResponse> submit(@RequestBody @Valid SubmitCodeDto submitCodeDto) {
return submitService.submit(submitCodeDto);
public SolutionIdResponse submit(@RequestBody @Valid SubmitCodeRequest submitCodeRequest) {
return submitService.submit(submitCodeRequest);
}
@PostMapping("/baekjoon/practice")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "문제 번호, 언어 이름, 소스코드를 백준에 제출하는 컨트롤러", description = "사용자가 연습 문제를 제출하는 경우 백준에 제출하는 컨트롤러입니다.")
public ResponseEntity<SolutionIdResponse> submit(@RequestBody @Valid PracticeProblemSubmitCodeRequest practiceProblemSubmitCodeRequest) {
public SolutionIdResponse submit(@RequestBody @Valid PracticeProblemSubmitCodeRequest practiceProblemSubmitCodeRequest) {
return submitService.submit(practiceProblemSubmitCodeRequest);
}
@PostMapping("/cookie")
public String loginForSubmit(@RequestBody @Valid BaekjoonUserDto userDto) {
return submitService.saveBaekjoonInfo(userDto);
@ResponseStatus(HttpStatus.OK)
public String loginForSubmit(@RequestBody @Valid BaekjoonUserRequest baekjoonUserRequest) {
return submitService.saveBaekjoonInfo(baekjoonUserRequest);
}
@PostMapping("/result")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "testId, bojProblemId, 정답여부를 전달하는 컨트롤러", description = "제출 후 채점 결과에 대한 내용을 백엔드에 전달하는 컨트롤러입니다.")
public String saveSubmitResult(@RequestBody @Valid AttemptProblemResultDto attemptProblemResultDto){
public String saveSubmitResult(@RequestBody @Valid AttemptProblemResultRequest attemptProblemResultDto){
return saveSubmitResultService.saveSubmitResult(attemptProblemResultDto);
}
@PostMapping("/result/practice")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "정답여부를 전달하는 컨트롤러", description = "제출 후 연습 문제에 대한 내용을 백엔드에 전달하는 컨트롤러입니다.")
public String savePracticeProblemSubmitResult(@RequestBody @Valid PracticeProblemResultDto practiceProblemResultDto) {
return saveSubmitResultService.savePracticeProblemSubmit(practiceProblemResultDto);
public String savePracticeProblemSubmitResult(@RequestBody @Valid PracticeProblemResultRequest practiceProblemResultRequest) {
return saveSubmitResultService.savePracticeProblemSubmit(practiceProblemResultRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javax.validation.constraints.NotNull;

@Getter @Setter
public class AttemptProblemResultDto {
public class AttemptProblemResultRequest {

@NotNull(message = "testId는 null일 수 없습니다, 정수형")
private Long testId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@Getter
@Setter
public class BaekjoonUserDto {
public class BaekjoonUserRequest {
@NotNull(message = "백준 아이디를 입력해주세요")
public String bojId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import lombok.Setter;

@Getter @Setter
public class PracticeProblemResultDto {
public class PracticeProblemResultRequest {
private Long practiceProblemId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Getter
@Setter
public class SubmitCodeDto {
public class SubmitCodeRequest {

@NotNull(message = "백준 문제 번호를 입력해주세요")
public String bojProblemId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import org.springframework.web.client.RestTemplate;
import swm_nm.morandi.aop.annotation.MemberLock;
import swm_nm.morandi.domain.codeSubmit.constants.CodeVisuabilityConstants;
import swm_nm.morandi.domain.codeSubmit.dto.BaekjoonUserDto;
import swm_nm.morandi.domain.codeSubmit.dto.BaekjoonUserRequest;
import swm_nm.morandi.domain.codeSubmit.dto.PracticeProblemSubmitCodeRequest;
import swm_nm.morandi.domain.codeSubmit.dto.SolutionIdResponse;
import swm_nm.morandi.domain.codeSubmit.dto.SubmitCodeDto;
import swm_nm.morandi.domain.codeSubmit.dto.SubmitCodeRequest;
import swm_nm.morandi.domain.common.Language;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.member.repository.MemberRepository;
Expand Down Expand Up @@ -65,20 +65,20 @@ public class BaekjoonSubmitService {
//백준 로그인용 쿠키 저장
//Redis에 현재 로그인한 사용자의 백준 제출용 쿠키를 저장
@Transactional
public String saveBaekjoonInfo(BaekjoonUserDto baekjoonUserDto) {
public String saveBaekjoonInfo(BaekjoonUserRequest baekjoonUserRequest) {
Long memberId = SecurityUtils.getCurrentMemberId();
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MorandiException(MemberErrorCode.EXTENSION_MEMBER_NOT_FOUND));

//validateBojId(member, baekjoonUserDto.getBojId());

saveCookieToRedis(memberId, baekjoonUserDto.getCookie());
saveCookieToRedis(memberId, baekjoonUserRequest.getCookie());

//Member에 백준 아이디 초기화
// if(member.getBojId()==null) {
updateMemberInfo(member, baekjoonUserDto.getBojId());
updateMemberInfo(member, baekjoonUserRequest.getBojId());
// }
return baekjoonUserDto.getCookie();
return baekjoonUserRequest.getCookie();
}

private void validateBojId(Member member, String bojId) {
Expand Down Expand Up @@ -108,24 +108,24 @@ private void updateMemberInfo(Member member, String bojId){
}
@MemberLock
@Transactional
public ResponseEntity<SolutionIdResponse> submit(SubmitCodeDto submitCodeDto) {
validateBojProblemId(submitCodeDto.getBojProblemId());
public SolutionIdResponse submit(SubmitCodeRequest submitCodeRequest) {
validateBojProblemId(submitCodeRequest.getBojProblemId());

Long memberId = SecurityUtils.getCurrentMemberId();
String cookie = getCookieFromRedis(generateKey(memberId));
String CSRFKey = getCSRFKey(cookie, submitCodeDto.getBojProblemId());
String CSRFKey = getCSRFKey(cookie, submitCodeRequest.getBojProblemId());

SolutionIdResponse solutionId = sendSubmitRequest(cookie, CSRFKey, submitCodeDto.getBojProblemId(), submitCodeDto.getLanguage(), submitCodeDto.getSourceCode());
SolutionIdResponse solutionId = sendSubmitRequest(cookie, CSRFKey, submitCodeRequest.getBojProblemId(), submitCodeRequest.getLanguage(), submitCodeRequest.getSourceCode());


//제출한 코드 정보를 저장
saveSubmitTempCode(submitCodeDto);
saveSubmitTempCode(submitCodeRequest);

return ResponseEntity.status(HttpStatus.OK).body(solutionId);
return solutionId;
}
@MemberLock
@Transactional
public ResponseEntity<SolutionIdResponse> submit(PracticeProblemSubmitCodeRequest practiceProblemSubmitCodeRequest) {
public SolutionIdResponse submit(PracticeProblemSubmitCodeRequest practiceProblemSubmitCodeRequest) {
validateBojProblemId(practiceProblemSubmitCodeRequest.getBojProblemId());

Long memberId = SecurityUtils.getCurrentMemberId();
Expand All @@ -138,7 +138,7 @@ public ResponseEntity<SolutionIdResponse> submit(PracticeProblemSubmitCodeReques
//제출한 코드 정보를 저장
savePracticeProblemSubmitTempCode(practiceProblemSubmitCodeRequest);

return ResponseEntity.status(HttpStatus.OK).body(solutionId);
return solutionId;
}


Expand Down Expand Up @@ -260,7 +260,7 @@ private SolutionIdResponse sendSubmitRequest(String cookie, String CSRFKey, Stri
private MultiValueMap<String, String> createParameters(String bojProblemId, Language language, String sourceCode, String CSRFKey) {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("problem_id", bojProblemId);
parameters.add("language", SubmitCodeDto.getLanguageId(language));
parameters.add("language", SubmitCodeRequest.getLanguageId(language));
parameters.add("code_open", CodeVisuabilityConstants.CLOSE.getCodeVisuability());
parameters.add("source", sourceCode);
parameters.add("csrf_key", CSRFKey);
Expand Down Expand Up @@ -290,15 +290,15 @@ private SolutionIdResponse extractSolutionIdFromHtml(String html) {
}
}

private void saveSubmitTempCode(SubmitCodeDto submitCodeDto)
private void saveSubmitTempCode(SubmitCodeRequest submitCodeRequest)
{
String ongoingTestKey = redisKeyGenerator.generateOngoingTestKey();
Long testId = ((TestInfo) Optional.ofNullable(redisTemplate.opsForValue().get(ongoingTestKey))
.orElseThrow(() -> new MorandiException(SubmitErrorCode.TEST_NOT_EXIST))).getTestId();

tempCodeService.saveTempCode(testId, submitCodeDto.getProblemNumber(), submitCodeDto.getLanguage(), submitCodeDto.getSourceCode());
tempCodeService.saveTempCode(testId, submitCodeRequest.getProblemNumber(), submitCodeRequest.getLanguage(), submitCodeRequest.getSourceCode());

saveSubmitCodeToDatabase(testId, submitCodeDto);
saveSubmitCodeToDatabase(testId, submitCodeRequest);
}

private void savePracticeProblemSubmitTempCode(PracticeProblemSubmitCodeRequest practiceProblemSubmitCodeRequest) {
Expand All @@ -309,12 +309,12 @@ private void savePracticeProblemSubmitTempCode(PracticeProblemSubmitCodeRequest
savePracticeProblemToDatabase(practiceProblemId, practiceProblemSubmitCodeRequest);
}

private void saveSubmitCodeToDatabase(Long testId, SubmitCodeDto submitCodeDto) {
AttemptProblem attemptProblem = attemptProblemRepository.findByTest_TestIdAndProblem_BojProblemId(testId,Long.parseLong(submitCodeDto.getBojProblemId()))
private void saveSubmitCodeToDatabase(Long testId, SubmitCodeRequest submitCodeRequest) {
AttemptProblem attemptProblem = attemptProblemRepository.findByTest_TestIdAndProblem_BojProblemId(testId,Long.parseLong(submitCodeRequest.getBojProblemId()))
.orElseThrow(() -> new MorandiException(SubmitErrorCode.TEST_NOT_EXIST));

attemptProblem.setSubmitCode(submitCodeDto.getSourceCode());
attemptProblem.setSubmitLanguage(submitCodeDto.getLanguage());
attemptProblem.setSubmitCode(submitCodeRequest.getSourceCode());
attemptProblem.setSubmitLanguage(submitCodeRequest.getLanguage());
//TODO
//attemptProblem에 마지막으로 제출한 language의 정보가 빠져있음 -> 이거 나중에 추가하던지 해야함
attemptProblemRepository.save(attemptProblem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import swm_nm.morandi.domain.codeSubmit.dto.AttemptProblemResultDto;
import swm_nm.morandi.domain.codeSubmit.dto.PracticeProblemResultDto;
import swm_nm.morandi.domain.practice.dto.PracticeProblemCode;
import swm_nm.morandi.domain.codeSubmit.dto.AttemptProblemResultRequest;
import swm_nm.morandi.domain.codeSubmit.dto.PracticeProblemResultRequest;
import swm_nm.morandi.domain.practice.entity.PracticeProblem;
import swm_nm.morandi.domain.practice.repository.PracticeProblemRepository;
import swm_nm.morandi.domain.testDuring.dto.TestStatus;
Expand All @@ -29,12 +28,14 @@ public class SaveSubmitResultService {

private final PracticeProblemRepository practiceProblemRepository;


// 시험 중 정답으로 판명된 문제를 정답으로 처리하는 메서드
@Transactional
public String saveSubmitResult(AttemptProblemResultDto attemptProblemResultDto) {
public String saveSubmitResult(AttemptProblemResultRequest attemptProblemResultRequest) {
Long memberId = SecurityUtils.getCurrentMemberId();
AttemptProblem attemptProblem =
attemptProblemRepository.findByMember_MemberIdAndTest_testIdAndTest_TestStatusAndProblem_BojProblemId(memberId,
attemptProblemResultDto.getTestId(), TestStatus.IN_PROGRESS,attemptProblemResultDto.getBojProblemId())
attemptProblemResultRequest.getTestId(), TestStatus.IN_PROGRESS,attemptProblemResultRequest.getBojProblemId())
.orElseThrow(() -> new MorandiException(AttemptProblemErrorCode.ATTEMPT_PROBLEM_NOT_FOUND_DURING_TEST));

if (attemptProblem.getIsSolved())
Expand All @@ -57,8 +58,8 @@ public String saveSubmitResult(AttemptProblemResultDto attemptProblemResultDto)
}

@Transactional
public String savePracticeProblemSubmit(PracticeProblemResultDto practiceProblemResultDto) {
PracticeProblem practiceProblem = practiceProblemRepository.findById(practiceProblemResultDto.getPracticeProblemId())
public String savePracticeProblemSubmit(PracticeProblemResultRequest practiceProblemResultRequest) {
PracticeProblem practiceProblem = practiceProblemRepository.findById(practiceProblemResultRequest.getPracticeProblemId())
.orElseThrow(() -> new MorandiException(PracticeProblemErrorCode.PRACTICE_PROBLEM_NOT_FOUND));
practiceProblem.setIsSolved(true);
practiceProblemRepository.save(practiceProblem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import swm_nm.morandi.domain.member.dto.MemberInfoDto;
import swm_nm.morandi.domain.member.dto.MemberListRequestDto;
import swm_nm.morandi.domain.member.dto.MemberListResponseDto;
import swm_nm.morandi.domain.member.dto.RegisterInfoDto;
import swm_nm.morandi.domain.member.dto.MemberInfoRequest;
import swm_nm.morandi.domain.member.dto.MemberListRequest;
import swm_nm.morandi.domain.member.dto.MemberListResponse;
import swm_nm.morandi.domain.member.dto.RegisterInfoRequest;
import swm_nm.morandi.domain.member.service.MemberEditService;
import swm_nm.morandi.domain.member.service.MemberInfoService;
import swm_nm.morandi.domain.member.service.MemberInitService;
Expand All @@ -31,33 +30,37 @@ public class MemberController {

private final MemberListService memberListService;
@GetMapping("/check")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "백준 id 등록되어있는지 확인", description ="백준 id가 등록되어있는지 확인합니다. 200반환 시 정상," +
" \"code\": \"BAEKJOON_ID_NULL\" 반환 시 백준 id가 등록되지 않은 상태, 토큰 오류 시 403")
public ResponseEntity<?> checkMemberInitialized(){
return new ResponseEntity<>(HttpStatus.OK);
public void checkMemberInitialized(){
}
@PostMapping("/register-info")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "사용자 최초 등록", description = "사용자 최초 등록시 백준 아이디가 필수적으로 필요합니다.")
public ResponseEntity<RegisterInfoDto> memberInitialize(@RequestBody @Valid RegisterInfoDto registerInfoDto) {
return new ResponseEntity<>(memberInitService.memberInitialize(registerInfoDto), HttpStatus.OK);
public RegisterInfoRequest memberInitialize(@RequestBody @Valid RegisterInfoRequest registerInfoRequest) {
return memberInitService.memberInitialize(registerInfoRequest);
}
@GetMapping("/info")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "사용자 정보", description = "사용자의 닉네임과 백준 아이디를 보여줍니다.")
public ResponseEntity<MemberInfoDto> memberInfo() {
return new ResponseEntity<>(memberInfoService.getMemberInfo(), HttpStatus.OK);
public MemberInfoRequest memberInfo() {
return memberInfoService.getMemberInfo();
}
@PostMapping("/edit")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "사용자 정보 수정", description = "사용자의 닉네임 또는 백준 아이디를 수정합니다.")
public ResponseEntity<MemberInfoDto> editProfile(@RequestBody MemberInfoDto memberInfoDto) {
memberEditService.editProfile(memberInfoDto.getIntroduceInfo(), memberInfoDto.getBojId());
return new ResponseEntity(memberInfoDto, HttpStatus.OK);
public MemberInfoRequest editProfile(@RequestBody MemberInfoRequest memberInfoRequest) {
memberEditService.editProfile(memberInfoRequest.getIntroduceInfo(), memberInfoRequest.getBojId());
return memberInfoRequest;
}

@GetMapping("/list")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "사용자 전체 리스트 정보", description = "서비스를 이용하는 모든 사용자 정보를 보여줍니다. page = (페이지 번호), size = (한 페이지 당 크기) " +
"ex. page = 1, size = 10 -> 1페이지에 10명의 유저 리스트를 보여줍니다.")
public ResponseEntity<List<MemberListResponseDto>> memberList(MemberListRequestDto memberListRequestDto) {
List<MemberListResponseDto> memberListResponseDtos = memberListService.findAll(memberListRequestDto);
return new ResponseEntity<>(memberListResponseDtos, HttpStatus.OK);
public List<MemberListResponse> memberList(MemberListRequest memberListRequest) {
return memberListService.findAll(memberListRequest);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class MemberInfoDto {
public class MemberInfoRequest {
private String introduceInfo;
private String bojId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Getter @Setter
@RequiredArgsConstructor
public class MemberListRequestDto {
public class MemberListRequest {
private Integer page = 1;
private Integer size = 15;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MemberListResponseDto {
public class MemberListResponse {

private Long rank;
private String bojId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RegisterInfoDto {
public class RegisterInfoRequest {
@NotBlank
private String bojId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import swm_nm.morandi.domain.member.dto.MemberInfoDto;
import swm_nm.morandi.domain.member.dto.MemberInfoRequest;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.member.repository.MemberRepository;
import swm_nm.morandi.global.exception.MorandiException;
Expand All @@ -16,10 +16,10 @@
public class MemberInfoService {

private final MemberRepository memberRepository;
public MemberInfoDto getMemberInfo() {
public MemberInfoRequest getMemberInfo() {
Long memberId = SecurityUtils.getCurrentMemberId();
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MorandiException(MemberErrorCode.MEMBER_NOT_FOUND));
MemberInfoDto memberInfoDto = new MemberInfoDto();
MemberInfoRequest memberInfoDto = new MemberInfoRequest();
memberInfoDto.setIntroduceInfo(member.getIntroduceInfo());
memberInfoDto.setBojId(member.getBojId());
return memberInfoDto;
Expand Down
Loading

0 comments on commit a82a597

Please sign in to comment.