登録/変更/削除/照会springboot投稿API-02
27698 ワード
投稿APIの変更/参照
他のPostsの作成
package com.momenting.book.springboot.domain.posts;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Getter
@NoArgsConstructor
@Entity // 테이블과 링크될 클래스임을 나타냄 테이블이름은 posts
public class Posts {
@Id //해당 테이블의 PK필드가 될 녀셕에게 붙힌다.
@GeneratedValue //PK생성 규칙을 나타냄
private Long id;
@Column(length = 500, nullable = false)
private String title;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;
private String author;
@Builder
public Posts(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}
//이번글에서 추가 작성된 구문
public void update(String title, String content) {
this.title = title;
this.content = content;
}
}
Postsクラスにupdateという別のジェネレータメソッドを作成します.
PostsUpdateRequestDtoの作成
src -> main -> java -> com.momenting.book.springboot -> web.dto->PostsUpdateRequestDtoクラスの作成と作成package com.momenting.book.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;
}
}
PostsResponseDtoの作成
src -> main -> java -> com.momenting.book.springboot -> web.dto ->
PostsResponseDtoクラスを作成して作成package com.momenting.book.springboot.web.dto;
import com.momenting.book.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();
}
}
PostsResponseDtoはEntityの一部のフィールドのみを使用するため、コンストラクション関数を使用してEntityをフィールドに入力します.
PostsServiceの追加の作成
package com.momenting.book.springboot.service.posts;
import com.momenting.book.springboot.domain.posts.Posts;
import com.momenting.book.springboot.domain.posts.PostsRepository;
import com.momenting.book.springboot.web.dto.PostsResponseDto;
import com.momenting.book.springboot.web.dto.PostsSaveRequestDto;
import com.momenting.book.springboot.web.dto.PostsUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Service
public class PostsService {
private final PostsRepository postsRepository;
@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());
return id;
}
public PostsResponseDto findById (Long id) {
Posts entity = postsRepository.findById(id).orElseThrow( () -> new IllegalArgumentException("해당 게시글이 없습니다. id =" + id));
return new PostsResponseDto(entity);
}
}
他のPostsApiコントローラの作成
package com.momenting.book.springboot.web;
import com.momenting.book.springboot.service.posts.PostsService;
import com.momenting.book.springboot.web.dto.PostsResponseDto;
import com.momenting.book.springboot.web.dto.PostsSaveRequestDto;
import com.momenting.book.springboot.web.dto.PostsUpdateRequestDto;
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);
}
@PutMapping("api/v1/posts/{id}")
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);
}
}
プロセスの整理
要求フローの変更
PostsApiコントローラの更新からPostsServiceの更新を呼び出す->PostsServiceからPostsRepositoryを呼び出す.findById(id)に変更する投稿に移動->Postsを呼び出す更新方法
クエリー要求フロー
PostsApi ControlのFindByIdからPostServiceのFindBy->PostServiceからPostRepositoryを呼び出します.findById(id)でクエリーする投稿に移動->PostsResponseDtoのEntityオブジェクトとしてクエリーする投稿に戻ります.
Reference
この問題について(登録/変更/削除/照会springboot投稿API-02), 我々は、より多くの情報をここで見つけました
https://velog.io/@dudgns1086/spring-boot-게시글-등록수정삭제조회-API-만들기-02
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
package com.momenting.book.springboot.domain.posts;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Getter
@NoArgsConstructor
@Entity // 테이블과 링크될 클래스임을 나타냄 테이블이름은 posts
public class Posts {
@Id //해당 테이블의 PK필드가 될 녀셕에게 붙힌다.
@GeneratedValue //PK생성 규칙을 나타냄
private Long id;
@Column(length = 500, nullable = false)
private String title;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;
private String author;
@Builder
public Posts(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}
//이번글에서 추가 작성된 구문
public void update(String title, String content) {
this.title = title;
this.content = content;
}
}
package com.momenting.book.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;
}
}
package com.momenting.book.springboot.web.dto;
import com.momenting.book.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();
}
}
package com.momenting.book.springboot.service.posts;
import com.momenting.book.springboot.domain.posts.Posts;
import com.momenting.book.springboot.domain.posts.PostsRepository;
import com.momenting.book.springboot.web.dto.PostsResponseDto;
import com.momenting.book.springboot.web.dto.PostsSaveRequestDto;
import com.momenting.book.springboot.web.dto.PostsUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Service
public class PostsService {
private final PostsRepository postsRepository;
@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());
return id;
}
public PostsResponseDto findById (Long id) {
Posts entity = postsRepository.findById(id).orElseThrow( () -> new IllegalArgumentException("해당 게시글이 없습니다. id =" + id));
return new PostsResponseDto(entity);
}
}
package com.momenting.book.springboot.web;
import com.momenting.book.springboot.service.posts.PostsService;
import com.momenting.book.springboot.web.dto.PostsResponseDto;
import com.momenting.book.springboot.web.dto.PostsSaveRequestDto;
import com.momenting.book.springboot.web.dto.PostsUpdateRequestDto;
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);
}
@PutMapping("api/v1/posts/{id}")
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);
}
}
Reference
この問題について(登録/変更/削除/照会springboot投稿API-02), 我々は、より多くの情報をここで見つけました https://velog.io/@dudgns1086/spring-boot-게시글-등록수정삭제조회-API-만들기-02テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol