Web/SpringBoot

[SpringBoot/AWS EC2] MongoDB - 3 Update

hjkongkong 2021. 12. 12. 21:58

https://hjkongkong.tistory.com/12

 

[SpringBoot/AWS EC2] MongoDB - 2 등록

https://hjkongkong.tistory.com/11 [SpringBoot/AWS EC2] MongoDB - 1 (Auto-Generated Field) 본격적으로 개발을 앞서 오늘 사용 할 환경정보이다. JAVA 1.8 springBoot 2.1.9.RELEASE gradle 7.1 spring data j..

hjkongkong.tistory.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.springboot.web.dto;
 
import com.example.springboot.domain.posts.Posts;
import lombok.Getter;
 
@Getter
public class PostsResponseDto {
    
    private Long id;
    private String title;
    private String content;
    private String author;
    
    public PostsResponseDto(Posts entity){
        this.id = entity.getId();
        this.title = entity.getTitle();
        this.content = entity.getContent();
        this.author = entity.getAuthor();
    }
 
}
 
cs

 PostsResponseDto.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.springboot.web.dto;
 
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
 
@Getter
@NoArgsConstructor
public class PostsUpdateRequestDto {
 
    private String title;
    private String content;
 
    @Builder
    public PostsUpdateRequestDto(String title, String content){
        this.title = title;
        this.content = content;
    }
}
 
cs

PostsUpdateRequestDto.java

 

public void update(String title, String content){
        this.title = title;
        this.content = content;
    }

Posts.java에 update 코드 추가(사용 안함)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.springboot.service;
 
import com.example.springboot.domain.posts.Posts;
import com.example.springboot.domain.posts.PostsRepository;
import com.example.springboot.web.dto.PostsResponseDto;
import com.example.springboot.web.dto.PostsSaveRequestDto;
import com.example.springboot.web.dto.PostsUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@RequiredArgsConstructor
@Service
public class PostsService {
    private final PostsRepository postsRepository;
 
    @Autowired
    private MongoTemplate mongoTemplate; //update에 사용
 
    @Transactional // 기능을 하나로 묶어서 실행(데이터 무결성)
    public Long Save(PostsSaveRequestDto requestDto){
        return postsRepository.save(requestDto.toEntity()).getId();
    }
 
    @Transactional
    public Long update(Long id, PostsUpdateRequestDto requestDto) {
        Posts posts = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + id));
//        posts.update(requestDto.getTitle(), requestDto.getContent());
 
        // update 쿼리 수행을 위해 MongoTemplate 사용
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Update update = new Update();
 
        update.set("title", requestDto.getTitle());
        update.set("content",requestDto.getContent());
        mongoTemplate.updateFirst(query, update, Posts.class);
 
        return id;
    }
 
    @Transactional(readOnly = true//조회 기능만(속도 개선)
    public PostsResponseDto findById(Long id){
        Posts document = postsRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id ="+id));
 
        return new PostsResponseDto(document);
    }
}
 
cs

PostsService.java에 Update를 위한 코드 추가

JPA에서 사용하듯 Posts.java에 update 코드를 추가했었는데(JPA 영속성 컨텍스트), 

업데이트 쿼리를 작성하지 않다보니 수행이 안됐다.

MongoTemplate을 이용하여 업데이트를 수행하였다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.example.springboot.web;
 
import com.example.springboot.service.PostsService;
import com.example.springboot.web.dto.PostsResponseDto;
import com.example.springboot.web.dto.PostsSaveRequestDto;
import com.example.springboot.web.dto.PostsUpdateRequestDto;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
@RequiredArgsConstructor
@RestController
public class PostsApiController {
 
    private final PostsService postsService;
 
    // 등록
    @PostMapping("/api/v1/posts")
    public Long Save(@RequestBody PostsSaveRequestDto requestDto){
        return postsService.Save(requestDto);
    }
 
    //수정
    public Long Update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto){
        return postsService.update(id, requestDto);
    }
 
    @GetMapping("/api/v1/posts/{id}")
    public PostsResponseDto findById(@PathVariable Long id){
        return postsService.findById(id);
    }
}
 
cs

PostsApiController.java에 Update를 위한 API 생성

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.example.springboot.web;
 
import com.example.springboot.domain.posts.Posts;
import com.example.springboot.domain.posts.PostsRepository;
import com.example.springboot.web.dto.PostsSaveRequestDto;
import com.example.springboot.web.dto.PostsUpdateRequestDto;
import org.junit.After;
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.HttpEntity;
import org.springframework.http.HttpMethod;
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 PostsApiControllerTest {
 
    @LocalServerPort
    private int port;
 
    @Autowired
    private TestRestTemplate restTemplate;
 
    @Autowired
    private PostsRepository postsRepository;
 
    @After
    public void tearDown() throws Exception{
        postsRepository.deleteAll();
    }
 
    @Test
    public void Posts_등록된다() throws Exception{
        //given
        String title = "title";
        String content = "content";
        PostsSaveRequestDto requestDto = PostsSaveRequestDto.builder()
                .title(title)
                .content(content)
                .author("author")
                .build();
 
        String url = "http://localhost:"+port+"/api/v1/posts";
 
        //when
        ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url,requestDto,Long.class);
 
        //then
        assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(responseEntity.getBody()).isGreaterThan(0L);
 
        List<Posts> all = postsRepository.findAll();
        assertThat(all.get(0).getTitle()).isEqualTo(title);
        assertThat(all.get(0).getContent()).isEqualTo(content);
    }
 
    @Test
    public void Posts_수정() throws Exception{
        //given
        Posts savedPosts = postsRepository.save(Posts.builder()
                .title("title")
                .content("content")
                .author("author")
                .build());
 
        Long updateId = savedPosts.getId();
        String expectedTitle = "title2";
        String expectedContent = "content2";
 
        PostsUpdateRequestDto requestDto = PostsUpdateRequestDto.builder()
                .title(expectedTitle)
                .content(expectedContent)
                .build();
 
        String url = "http://localhost:"+port+"/api/v1/posts"+updateId;
 
        HttpEntity<PostsUpdateRequestDto> requestEntity = new HttpEntity<>(requestDto);
 
        //when
        ResponseEntity<Long> responseEntity = restTemplate
                .exchange(url, HttpMethod.PUT, requestEntity,Long.class);
 
        //then
        assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(responseEntity.getBody()).isGreaterThan(0L);
 
        List<Posts> all = postsRepository.findAll();
        assertThat(all.get(0).getTitle()).isEqualTo(expectedTitle);
        assertThat(all.get(0).getContent()).isEqualTo(expectedContent);
    }
}
 
cs

Update 쿼리(수정 기능 테스트) test를 위한 PostsApiControllerTest.java 수정