Skip to content

Commit

Permalink
Merge pull request #24 from Ewha-thon-Melting-Pot/feat/result
Browse files Browse the repository at this point in the history
[feat] 논의 결과 등록 및 수정
  • Loading branch information
hannah0226 authored Mar 17, 2024
2 parents fad6876 + f2f5df4 commit 13cd867
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public enum ErrorStatus implements BaseErrorCode {
INVALID_REFRESH_TOKEN(HttpStatus.BAD_REQUEST, "MEMBER4005", "유효하지 않은 리프레시 토큰 입니다."),
// 게시물 관련 에러
ARTICLE_NOT_FOUND(HttpStatus.NOT_FOUND, "ARTICLE4001", "게시글이 없습니다."),
PAGE_FORMAT_BAD_REQUEST(HttpStatus.BAD_REQUEST,"PAGE4001","잘못된 페이지 번호 형식입니다");
PAGE_FORMAT_BAD_REQUEST(HttpStatus.BAD_REQUEST,"PAGE4001","잘못된 페이지 번호 형식입니다"),
// 결과 관련 에러
RESULT_NOT_FOUND(HttpStatus.NOT_FOUND, "RESULT4001","결과가 없습니다");


private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package melting_pot.ewha_sinmungo.result.controller;

import lombok.RequiredArgsConstructor;
import melting_pot.ewha_sinmungo.result.dto.requestDto.ResultRequestDto;
import melting_pot.ewha_sinmungo.result.service.ResultService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequiredArgsConstructor
public class ResultController {

private final ResultService resultService;

@PostMapping("/admin/{postId}/result")
public String postResult (@RequestBody @Valid ResultRequestDto.ResultSaveDto request, @PathVariable Long postId) {
resultService.createResult(request, postId);
return "Success";
}

@PutMapping("/admin/{postId}/result")
public String updateResult (@RequestBody @Valid ResultRequestDto.ResultSaveDto request, @PathVariable Long postId) {
resultService.updateResult(request, postId);
return "Success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package melting_pot.ewha_sinmungo.result.converter;

import lombok.RequiredArgsConstructor;
import melting_pot.ewha_sinmungo.member.entity.Member;
import melting_pot.ewha_sinmungo.member.service.MemberService;
import melting_pot.ewha_sinmungo.post.entity.Post;
import melting_pot.ewha_sinmungo.post.service.PostService;
import melting_pot.ewha_sinmungo.result.dto.requestDto.ResultRequestDto;
import melting_pot.ewha_sinmungo.result.entity.Result;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ResultConverter {
private final MemberService memberService;
private final PostService postService;
public Result toResultEntity (ResultRequestDto.ResultSaveDto request, Long postId) {
Member member = memberService.getCurrentMember();
Post post = postService.findById(postId);
Result result = Result.builder()
.content(request.getContent())
.createdDate(request.getCreatedDate())
.member(member)
.post(post)
.build();
return result;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package melting_pot.ewha_sinmungo.result.dto.requestDto;

import lombok.Getter;

import java.time.LocalDateTime;

public class ResultRequestDto {

@Getter
public static class ResultSaveDto {
LocalDateTime createdDate;
String content;
}
}
20 changes: 17 additions & 3 deletions src/main/java/melting_pot/ewha_sinmungo/result/entity/Result.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package melting_pot.ewha_sinmungo.result.entity;

import javax.persistence.*;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import melting_pot.ewha_sinmungo.member.entity.Member;
import melting_pot.ewha_sinmungo.post.entity.Post;

import java.time.LocalDateTime;

@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "result")
public class Result {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "result_id")
@Column(name = "resultId")
private Long id;

@Column(nullable = false)
private String content;

@Column(nullable = false)
private LocalDateTime createdDate;

@ManyToOne
@JoinColumn(name = "member_id",nullable = false)
@JoinColumn(name = "memberId",nullable = false)
private Member member;

@OneToOne
@JoinColumn(name = "post_id",nullable = false)
@JoinColumn(name = "postId",nullable = false)
private Post post;

public void updateContent(String content) {this.content = content;}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package melting_pot.ewha_sinmungo.result.repository;

import melting_pot.ewha_sinmungo.result.entity.Result;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ResultRepository extends JpaRepository<Result, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package melting_pot.ewha_sinmungo.result.service;

import lombok.RequiredArgsConstructor;
import melting_pot.ewha_sinmungo.global.apiResponse.code.status.ErrorStatus;
import melting_pot.ewha_sinmungo.global.apiResponse.exception.GeneralException;
import melting_pot.ewha_sinmungo.result.converter.ResultConverter;
import melting_pot.ewha_sinmungo.result.dto.requestDto.ResultRequestDto;
import melting_pot.ewha_sinmungo.result.entity.Result;
import melting_pot.ewha_sinmungo.result.repository.ResultRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ResultService {
private final ResultConverter resultConverter;
private final ResultRepository resultRepository;

@Transactional
public Result createResult(ResultRequestDto.ResultSaveDto request, Long postId) {
Result result = resultConverter.toResultEntity(request, postId);
resultRepository.save(result);
return result;
}

@Transactional
public Result updateResult(ResultRequestDto.ResultSaveDto request, Long postId) {
Result result = findById(postId);
result.updateContent(request.getContent());
return result;
}

public Result findById(Long postId) {
return resultRepository.findById(postId).orElseThrow(() -> new GeneralException(ErrorStatus.RESULT_NOT_FOUND));
}


}

0 comments on commit 13cd867

Please sign in to comment.