Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week3] 기본과제 & 심화과제 제출 #6

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMG_2574
p5: 이거 전에 제가 공부했던 내용인데 어떤 순서로 동작하는지 이해할 때 참고하시면 좋을 것 같아 남겨둡니다 ㅎㅎ

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미쳤다 ㄷㄷ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 저 지금 봤어요

IMG_2574 p5: 이거 전에 제가 공부했던 내용인데 어떤 순서로 동작하는지 이해할 때 참고하시면 좋을 것 같아 남겨둡니다 ㅎㅎ

진짜...대박이예요...저...진짜 감동받았어요.........

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sopt.org.SecondSeminar.controller.post;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import sopt.org.SecondSeminar.controller.post.dto.request.SaveRequestDto;
import sopt.org.SecondSeminar.controller.post.dto.request.UpdateRequestDto;
import sopt.org.SecondSeminar.service.PostService;
import static sopt.org.SecondSeminar.SecondSeminarApplication.postList;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1") // api 버전 처리

public class PostController {
private final PostService postService;

@PostMapping("/posts")
public String register(@RequestBody final SaveRequestDto request) {

Long userId = postService.save(request); // 컨트롤러가 service한테 요청. 일을 대신 시킴
System.out.println(postList); // // servics는 글에 대한 서비스 컨트롤러는 어디로 향하는지 중간다리

return userId + "번 글 등록이 완료됐습니다.";
}

@GetMapping("/posts/{userId}") // 아이디로 게시물을 찾는 메서드
public String getOne(@PathVariable final Long userId) {
System.out.println(userId + "의 게시물 조회" + postList.get(userId.intValue()-1)); // 최고


return postService.getPostInfo(userId); // 보내주기만 한다.
}

@GetMapping("/posts") // 제목으로 글을 찾는 메서드

public String search(@RequestParam final String title) {
System.out.println("글 제목으로 검색: " + title);

return postService.getByTitle(title);
}

@PutMapping("/posts/{userId}") // 글을 수정하는 메서드
public String update(@PathVariable final Long userId, @RequestBody UpdateRequestDto update) {
System.out.println(userId + " 님의 글을 수정했습니다.");

return postService.updatePost(userId, update);
}

@DeleteMapping("/posts/{userId}") // API에 행위가 들어가면 안된다는 피드백으로 변경
public String delete(@PathVariable final Long userId){
System.out.println(userId + " 님의 글을 삭제했습니다.");

return postService.deletePost(userId);
}

}
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sopt.org.SecondSeminar.controller.post.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class UpdateRequestDto {
private String title;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;
}

public void setTitle(String title) {this.title = title;}

public void setContent(String content) {this.content = content;}


@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";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sopt.org.SecondSeminar.service;

import org.springframework.stereotype.Service;
import sopt.org.SecondSeminar.controller.post.dto.request.SaveRequestDto;
import sopt.org.SecondSeminar.controller.post.dto.request.UpdateRequestDto;
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(Post post: postList) {
if (post.getTitle().contains(title)){
titlePost.add(post.toString());
}
}
return "해당 제목을 포함하는 게시물: " + titlePost;
}
public String updatePost(Long UserId, UpdateRequestDto requestDto) {
Post post = postList.get(UserId.intValue() - 1);
post.setContent(requestDto.getContent()); // UpdateRequestDto에서 받은것을 post에서 할당
post.setTitle(requestDto.getTitle());

return "게시물 수정 완료: " + post;
}

public String deletePost(Long UserId) {
if (UserId -1 >= postList.size()) { // 유저 아이디가 postList안에 존재하지 않을때
return "삭제할 글이 존재하지 않습니다";
}
postList.remove(UserId.intValue() - 1); // 해당 post 삭제
return "게시물 삭제 완료";

}
}
8 changes: 8 additions & 0 deletions ThirdClass/ThirdSeminar/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions ThirdClass/ThirdSeminar/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions ThirdClass/ThirdSeminar/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions ThirdClass/ThirdSeminar/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Getting Started

### Reference Documentation
For further reference, please consider the following sections:

* [Official Gradle documentation](https://docs.gradle.org)
* [Spring Boot Gradle Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.7.11/gradle-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.7.11/gradle-plugin/reference/html/#build-image)
* [Spring Web](https://docs.spring.io/spring-boot/docs/2.7.11/reference/htmlsingle/#web)

### Guides
The following guides illustrate how to use some features concretely:

* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)

### Additional Links
These additional references should also help you:

* [Gradle Build Scans – insights for your project's build](https://scans.gradle.com#gradle)

34 changes: 34 additions & 0 deletions ThirdClass/ThirdSeminar/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.11'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'sopt.org'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'mysql:mysql-connector-java:8.0.32'
implementation 'org.springframework.boot:spring-boot-starter-validation'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading