-
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.
week2 기본 과제 first init #3
- Loading branch information
Showing
5 changed files
with
140 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
37 changes: 37 additions & 0 deletions
37
...ss/SecondSeminar/src/main/java/sopt/org/SecondSeminar/controller/post/PostController.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,37 @@ | ||
package sopt.org.SecondSeminar.controller.post; // controller 클라이언트에게 받고 보내고만 | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import sopt.org.SecondSeminar.controller.post.dto.request.SaveRequestDto; | ||
import sopt.org.SecondSeminar.service.PostService; | ||
import static sopt.org.SecondSeminar.SecondSeminarApplication.postList; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class PostController { | ||
private final PostService postService; | ||
|
||
@PostMapping("/post") | ||
public String register(@RequestBody final SaveRequestDto request) { | ||
|
||
Long userId = postService.save(request); // 컨트롤러가 service한테 요청. 일을 대신 시킴 | ||
System.out.println(postList); // servics는 글에 대한 서비스 컨트롤러는 어디로 향하는지 중간다리 | ||
|
||
return userId + "번 글 등록이 완료됐습니다."; | ||
} | ||
|
||
@GetMapping("/post/{userId}") // 아이디로 게시물을 찾는 메서드 | ||
public String getOne(@PathVariable final Long userId) { | ||
System.out.println(userId + "의 게시물 조회" + postList.get(userId.intValue()-1)); // 최고 | ||
|
||
|
||
return postService.getPostInfo(userId); // 보내주기만 한다. | ||
} | ||
|
||
@GetMapping("/post/search") // 제목으로 글을 찾는 메서드 | ||
public String search(@RequestParam final String title) { | ||
System.out.println("글 제목으로 검색: " + title); | ||
|
||
return postService.getByTitle(title); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...inar/src/main/java/sopt/org/SecondSeminar/controller/post/dto/request/SaveRequestDto.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,12 @@ | ||
package sopt.org.SecondSeminar.controller.post.dto.request; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class SaveRequestDto { | ||
private String title; | ||
private String content; | ||
} |
32 changes: 32 additions & 0 deletions
32
SecondClass/SecondSeminar/src/main/java/sopt/org/SecondSeminar/domain/post/Post.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,32 @@ | ||
package sopt.org.SecondSeminar.domain.post; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter // 객체에 맞는 클래스 정의 | ||
public class Post { // 하나의 엔티티 Post 라는 테이블에서 세가지 속성을 가지겠다. | ||
|
||
private long id; | ||
private String title; | ||
private String content; | ||
|
||
public Post(String title, String content) { // 게시물을 불러올 메서드 | ||
this.title = title; | ||
this.content = content; | ||
} | ||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "id: " + this.id + "\n" + | ||
"title: " + this.title + "\n" + | ||
"content: " + this.content + "\n"; | ||
|
||
} | ||
public String idToString(){ // 제목과 글 내용만 불러오고 싶어서 따로 만들어 줬습니다. | ||
return "제목: " + this.title + "\n" + | ||
"글내용: " + this.content + "\n"; | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
SecondClass/SecondSeminar/src/main/java/sopt/org/SecondSeminar/service/PostService.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,54 @@ | ||
package sopt.org.SecondSeminar.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import sopt.org.SecondSeminar.controller.post.dto.request.SaveRequestDto; | ||
import sopt.org.SecondSeminar.domain.post.Post; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static sopt.org.SecondSeminar.SecondSeminarApplication.postList; | ||
|
||
|
||
|
||
@Service // 요청 처리 find, save 이런식으로 행위는 서비스 | ||
public class PostService { | ||
public Long save(SaveRequestDto requestDto){ // 저장해주는 메서드 | ||
Post newPost = new Post( | ||
requestDto.getTitle(), | ||
requestDto.getContent() | ||
); | ||
|
||
postList.add(newPost); | ||
newPost.setId((long) postList.size()); | ||
|
||
//저장한 유저 아이디 값 반환 | ||
return newPost.getId(); | ||
} | ||
|
||
public String getPostInfo(Long UserId) { // 아이디로 글을 찾는 메서드 | ||
Post post = postList.get(UserId.intValue() - 1); | ||
Post postInfo = new Post( // title과 content만 불러오고 싶어서 따로 만들었습니다. | ||
post.getTitle(), | ||
post.getContent() | ||
); | ||
|
||
return postInfo.idToString(); | ||
} | ||
|
||
public String getByTitle(String title) { // 찾는 제목을 포함하는 게시물을 찾는 메서드 | ||
ArrayList<String> titlePost = new ArrayList<>(); // 일회성 Array를 만들어주고 싶었습니다. | ||
for(int i = 0 ; i < postList.size() ; i++) { | ||
Post post = postList.get(i); | ||
if (post.getTitle().contains(title)){ // 검색한 String과 일치하는 타이틀 조건 | ||
titlePost.add(post.toString()); // id,title,content 모두 나오게 설정 | ||
} | ||
} | ||
return "해당 제목을 포함하는 게시물: " + titlePost; | ||
} | ||
|
||
}; // 조회 함수를 새로 만들어서 역할 위임하는 거 | ||
|
||
//controller, service >> 이해됨 | ||
// Application, domain | ||
// | ||
|