-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Ewha-thon-Melting-Pot/feat/result
[feat] 논의 결과 등록 및 수정
- Loading branch information
Showing
7 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/melting_pot/ewha_sinmungo/result/controller/ResultController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/melting_pot/ewha_sinmungo/result/converter/ResultConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
src/main/java/melting_pot/ewha_sinmungo/result/dto/requestDto/ResultRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/main/java/melting_pot/ewha_sinmungo/result/entity/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/melting_pot/ewha_sinmungo/result/repository/ResultRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/melting_pot/ewha_sinmungo/result/service/ResultService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
|
||
} |