Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
[#9] Request 기능 구현 (#48)
Browse files Browse the repository at this point in the history
* feat: 등하원 도우미 신청 서비스 및 엔드 포인트 정의

#9

* feat: 포스트 신청 게시글 관련 DTO 정의

#9

* feat: 비즈니스 예외처리 로직 적용

#9

* test: 테스트 코드 추가

#9

* fix: 코드리뷰 반영

- url 변경
- 로그인여부 검증

* fix: 불필요한 NotNull 어노테이션 제거
  • Loading branch information
oksusutea authored Aug 4, 2021
1 parent e377581 commit 6c0f888
Show file tree
Hide file tree
Showing 7 changed files with 612 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.flab.kidsafer.controller;

import com.flab.kidsafer.config.auth.LoginUser;
import com.flab.kidsafer.config.auth.dto.SessionUser;
import com.flab.kidsafer.domain.enums.UserType;
import com.flab.kidsafer.dto.PostRequestDTO;
import com.flab.kidsafer.error.exception.OperationNotAllowedException;
import com.flab.kidsafer.error.exception.UserNotSignInException;
import com.flab.kidsafer.service.PostRequestService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/posts/{postId}/requests")
public class PostRequestController {

@Autowired
private PostRequestService postRequestService;

@GetMapping("/mine")
public ResponseEntity<PostRequestDTO> getSingleRequest(@PathVariable int postId,
@LoginUser SessionUser user) {
checkUserSignIn(user);
return ResponseEntity.ok(postRequestService.getSingleRequest(postId, user.getId()));
}

@GetMapping
public ResponseEntity<List<PostRequestDTO>> getAllRequests(@PathVariable int postId,
@LoginUser SessionUser user) {
checkUserSignIn(user);
return ResponseEntity.ok(postRequestService.getAllRequests(postId));
}

@PostMapping
public void applyRequest(@RequestBody PostRequestDTO postRequestDTO, @PathVariable int postId,
@LoginUser SessionUser user) {
if (user.getType() != UserType.SAFER) {
throw new OperationNotAllowedException();
}
postRequestService.applyRequest(postRequestDTO);
}

@DeleteMapping
public void cancelRequest(@PathVariable int postId, @LoginUser SessionUser user) {
if (user.getType() != UserType.SAFER) {
throw new OperationNotAllowedException();
}
postRequestService.cancelRequest(postId, user.getId());
}

public void checkUserSignIn(SessionUser user) {
if (user == null) {
throw new UserNotSignInException();
}
}
}
101 changes: 101 additions & 0 deletions src/main/java/com/flab/kidsafer/dto/PostRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.flab.kidsafer.dto;

import java.time.LocalDateTime;
import javax.validation.constraints.NotNull;

public class PostRequestDTO {

private int id;

@NotNull
private int postId;

@NotNull
private int userId;

private String contents;
private LocalDateTime registerDate;

public PostRequestDTO(int id, int postId,
int userId, String contents, LocalDateTime registerDate) {
this.id = id;
this.postId = postId;
this.userId = userId;
this.contents = contents;
this.registerDate = registerDate;
}

public PostRequestDTO(PostRequestDTO.Builder builder) {
this.id = builder.id;
this.postId = builder.postId;
this.userId = builder.userId;
this.contents = builder.contents;
this.registerDate = builder.registerDate;
}

public int getId() {
return id;
}

public int getPostId() {
return postId;
}

public int getUserId() {
return userId;
}

public String getContents() {
return contents;
}

public LocalDateTime getRegisterDate() {
return registerDate;
}

public static class Builder {

private int id;
private int postId;
private int userId;
private String contents;
private LocalDateTime registerDate;

public Builder() {
}

public Builder(int id) {
this.id = id;
}

public PostRequestDTO.Builder id(int id) {
this.id = id;
return this;
}

public PostRequestDTO.Builder postId(int postId) {
this.postId = postId;
return this;
}

public PostRequestDTO.Builder userId(int userId) {
this.userId = userId;
return this;
}

public PostRequestDTO.Builder contents(String contents) {
this.contents = contents;
return this;
}

public PostRequestDTO.Builder registerDate(LocalDateTime registerDate) {
this.registerDate = registerDate;
return this;
}

public PostRequestDTO build() {
return new PostRequestDTO(this);
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/flab/kidsafer/mapper/PostRequestMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.flab.kidsafer.mapper;

import com.flab.kidsafer.dto.PostRequestDTO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface PostRequestMapper {

List<PostRequestDTO> getAllRequests(int postId);

PostRequestDTO getSingleRequest(int postId, int userId);

void applyRequest(PostRequestDTO postRequestDTO);

void cancelRequest(int postId, int userId);

}
50 changes: 50 additions & 0 deletions src/main/java/com/flab/kidsafer/service/PostRequestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.flab.kidsafer.service;

import com.flab.kidsafer.dto.PostRequestDTO;
import com.flab.kidsafer.error.exception.OperationNotAllowedException;
import com.flab.kidsafer.error.exception.PostNotFoundException;
import com.flab.kidsafer.mapper.PostMapper;
import com.flab.kidsafer.mapper.PostRequestMapper;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PostRequestService {

@Autowired
private PostRequestMapper postRequestMapper;

@Autowired
private PostMapper postMapper;

public List<PostRequestDTO> getAllRequests(int postId) {
if (postMapper.findPostById(postId) == null) {
throw new PostNotFoundException();
}
return postRequestMapper.getAllRequests(postId);
}

public PostRequestDTO getSingleRequest(int postId, int userId) {
if (postMapper.findPostById(postId) == null) {
throw new PostNotFoundException();
}
return postRequestMapper.getSingleRequest(postId, userId);
}


public void applyRequest(PostRequestDTO postRequestDTO) {
if (getSingleRequest(postRequestDTO.getPostId(), postRequestDTO.getUserId()) != null) {
throw new OperationNotAllowedException();
}
postRequestMapper.applyRequest(postRequestDTO);
}

public void cancelRequest(int postId, int userId) {
if (getSingleRequest(postId, userId) == null) {
throw new OperationNotAllowedException();
}
postRequestMapper.cancelRequest(postId, userId);
}

}
28 changes: 28 additions & 0 deletions src/main/resources/mapper/PostRequestMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.flab.kidsafer.mapper.PostRequestMapper">
<select id="getAllRequests" resultType="com.flab.kidsafer.dto.PostRequestDTO">
SELECT id, postId, userId, contents, registerDate
FROM posts_request
WHERE postID = #{postId}
</select>

<select id="getSingleRequest" resultType="com.flab.kidsafer.dto.PostRequestDTO">
SELECT id, postId, userId, contents, registerDate
FROM posts_request
WHERE postID = #{postId}
AND userId = #{userId}
</select>

<insert id="applyRequest" parameterType="com.flab.kidsafer.dto.PostRequestDTO">
INSERT INTO posts_request (postId, userId, contents, registerDate)
VALUES (#{postId},#{userId}, #{contents}, #{registerDate});
</insert>

<delete id="cancelRequest">
DELETE FROM posts_request
WHERE postId = #{postId}
AND userId = #{userId}
</delete>
</mapper>
Loading

0 comments on commit 6c0f888

Please # to comment.