본문 바로가기

Java/Spring Boot

04-Spring Data JPA - 게시판 등록, 수정, 삭제

01. 등록

1) com.face.home.springboot.web.dto/ReviewboardRequestDto.java

package com.face.home.springboot.web.dto;

import com.face.home.springboot.domain.reviewboard.ReviewBoard;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ReviewboardRequestDto {

    private String title;
    private String content;
    private String author;

    @Builder
    public ReviewboardRequestDto(String title, String content, String author) {
        this.title = title;
        this.content = content;
        this.author = author;
    }

    public ReviewBoard toEntity() {
        return ReviewBoard.builder().title(title).content(content).author(author).build();
    }

}

- Entity 클래스(Reviewboard.java)와 유사하지만 Entity 클래스를 Request/Response 클래스로 사용해서 안되기 때문에 dto 추가 생성

- Entity 클래스는 데이터베이스와 맞닿은 핵심 클래스로 테이블이 생성되고 스키마가 변경됨

 

2) com.face.home.springboot.service.reviewboard/ReviewboardService.java

package com.face.home.springboot.service.reviewboard;

import com.face.home.springboot.domain.reviewboard.ReviewboardRepository;
import com.face.home.springboot.web.dto.ReviewboardRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@RequiredArgsConstructor
@Service
public class ReviewboardService {

    private final ReviewboardRepository reviewboardRepository;

    @Transactional
    public Long save(ReviewboardRequestDto reviewboardRequestDto) {
        return reviewboardRepository.save(reviewboardRequestDto.toEntity()).getId();
    }

}

 

3) com.face.home.springboot.web/ReviewboardApiController.java

package com.face.home.springboot.web;

import com.face.home.springboot.service.reviewboard.ReviewboardService;
import com.face.home.springboot.web.dto.ReviewboardRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class ReviewboardApiController {

    private final ReviewboardService reviewboardService;

    @PostMapping("/api/v1/reviewboard")
    public Long save(@RequestBody ReviewboardRequestDto reviewboardRequestDto) {
        return reviewboardService.save(reviewboardRequestDto);
    }

}

01-2. 등록 테스트 코드

package com.face.home.springboot.web;

import com.face.home.springboot.domain.reviewboard.ReviewBoard;
import com.face.home.springboot.domain.reviewboard.ReviewboardRepository;
import com.face.home.springboot.web.dto.ReviewboardRequestDto;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ReviewboardApiControllerTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Autowired
    private ReviewboardRepository reviewboardRepository;

    @Test
    public void 등록된다() throws Exception {

        // given
        String title = "title";
        String content = "content";
        String author = "author";
        ReviewboardRequestDto requestDto = ReviewboardRequestDto.builder().title(title).content(content).author(author).build();

        String url = "http://localhost:" + port + "/api/v1/reviewboard";

        // when
        ResponseEntity<Long> responseEntity = testRestTemplate.postForEntity(url, requestDto, Long.class);

        // then
        assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(responseEntity.getBody()).isGreaterThan(0L);

        List<ReviewBoard> all = reviewboardRepository.findAll();
        assertThat(all.get(0).getTitle()).isEqualTo(title);
        assertThat(all.get(0).getContent()).isEqualTo(content);
        assertThat(all.get(0).getAuthor()).isEqualTo(author);

    }


}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

참고

스프링 부트와 AWS로 혼자 구현하는 웹서비스

반응형

'Java > Spring Boot' 카테고리의 다른 글

03-Spring Data JPA  (0) 2020.05.21
02-Lombok  (0) 2020.05.20
01-IntelliJ 설치 및 환경설정 (gradle, git)  (0) 2020.05.20
SpringBoot-011-RestTemplate  (0) 2019.09.06
SpringBoot-010-CORS (Cross Origin Resource Sharing)  (0) 2019.09.05