-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d46b9f
commit 3593a9b
Showing
65 changed files
with
2,545 additions
and
292 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
58 changes: 58 additions & 0 deletions
58
src/main/java/edu/example/springbootblog/Blog/controller/BlogApiController.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,58 @@ | ||
package edu.example.springbootblog.Blog.controller; | ||
|
||
import edu.example.springbootblog.Blog.domain.Blog; | ||
import edu.example.springbootblog.Blog.dto.apidto.AddBlogRequest; | ||
import edu.example.springbootblog.Blog.dto.apidto.BlogResponse; | ||
import edu.example.springbootblog.Blog.dto.apidto.UpdateBlogRequest; | ||
import edu.example.springbootblog.Blog.service.BlogService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class BlogApiController { | ||
|
||
private final BlogService blogService; | ||
|
||
@PostMapping("/api/blogs") | ||
public ResponseEntity<Blog> addBlog(@RequestBody AddBlogRequest request) { | ||
Blog saveBlog = blogService.save(request); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(saveBlog); | ||
} | ||
|
||
@GetMapping("/api/blogs") | ||
public ResponseEntity<List<BlogResponse>> findAllBlog() { | ||
List<BlogResponse> blog = blogService.findAll() | ||
.stream() | ||
.map(BlogResponse::new) | ||
.toList(); // dto에 있는 아티클 리스폰스로 파싱해서 바디에 담는다. | ||
|
||
return ResponseEntity.ok().body(blog); | ||
} | ||
|
||
@GetMapping("/api/blogs/{id}") // 슬래시 하나 빼먹어서, 계속 에러 | ||
public ResponseEntity<BlogResponse> findBlog(@PathVariable long id) { | ||
Blog blog = blogService.findById(id); | ||
|
||
return ResponseEntity.ok().body(new BlogResponse(blog)); | ||
} | ||
|
||
@DeleteMapping("/api/blogs/{id}") | ||
public ResponseEntity<Void> deleteBlog(@PathVariable long id) { | ||
blogService.delete(id); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PutMapping("/api/blogs/{id}") // 여기서 사용된 DTO(UpdateArticleR) 에는 id가 없기에 @PathVariable 로 받음 | ||
public ResponseEntity<Blog> updateBlog(@PathVariable Long id, @RequestBody UpdateBlogRequest request) { | ||
Blog updatedBlog = blogService.update(id, request); | ||
|
||
return ResponseEntity.ok().body(updatedBlog); | ||
} | ||
} | ||
|
58 changes: 58 additions & 0 deletions
58
src/main/java/edu/example/springbootblog/Blog/controller/BlogViewController.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,58 @@ | ||
package edu.example.springbootblog.Blog.controller; | ||
|
||
import edu.example.springbootblog.Blog.domain.Blog; | ||
import edu.example.springbootblog.Blog.dto.viewdto.BlogListViewResponse; | ||
import edu.example.springbootblog.Blog.dto.viewdto.BlogViewResponse; | ||
import edu.example.springbootblog.Blog.service.BlogService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class BlogViewController { | ||
private final BlogService blogService; | ||
|
||
// @Value("${kakao.map.api.key}") | ||
// private String kakaoApiKey; | ||
|
||
@GetMapping("/") | ||
public String blogs(Model model) { | ||
// model.addAttribute("kakaoApiKey", kakaoApiKey); | ||
List<BlogListViewResponse> blogs = blogService.findAll().stream() | ||
.map(BlogListViewResponse::new) | ||
.toList(); | ||
model.addAttribute("blogs", blogs); | ||
// 모델 내부 메서드를 통해 값을 모델에 저장한다. (글 리스트) 반환값은 뷰파일 이름을 적어 찾으라는 뜻이다. | ||
// 다른 파일은, 들어오는 요청에 대한 엔드포인트를 정의한 것 | ||
|
||
return "blog/mainPage"; // == mainPage.html | ||
} | ||
|
||
@GetMapping("/blogs/{id}") | ||
public String getBlog(@PathVariable Long id, Model model) { | ||
Blog blog = blogService.findById(id); | ||
model.addAttribute("blog", new BlogViewResponse(blog)); | ||
|
||
return "blog/blogSee"; | ||
} | ||
|
||
@GetMapping("/new-blog") | ||
public String newBlog(@RequestParam(required = false) Long id, Model model) { | ||
if (id == null) { | ||
model.addAttribute("blog", new BlogViewResponse()); | ||
|
||
}else { | ||
Blog blog = blogService.findById(id); | ||
model.addAttribute("blog", new BlogViewResponse(blog)); | ||
} | ||
return "blog/newBlog"; | ||
} | ||
|
||
|
||
} |
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
10 changes: 5 additions & 5 deletions
10
...article/dto/apidto/AddArticleRequest.java → ...tblog/Blog/dto/apidto/AddBlogRequest.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
12 changes: 6 additions & 6 deletions
12
...g/article/dto/apidto/ArticleResponse.java → ...ootblog/Blog/dto/apidto/BlogResponse.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
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
17 changes: 17 additions & 0 deletions
17
src/main/java/edu/example/springbootblog/Blog/dto/viewdto/BlogListViewResponse.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,17 @@ | ||
package edu.example.springbootblog.Blog.dto.viewdto; | ||
|
||
import edu.example.springbootblog.Blog.domain.Blog; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class BlogListViewResponse { | ||
private final Long id; | ||
private final String title; | ||
private final String content; | ||
|
||
public BlogListViewResponse(Blog blog) { | ||
this.id = blog.getId(); | ||
this.title = blog.getTitle(); | ||
this.content = blog.getContent(); | ||
} | ||
}// 뷰에게 데이터를 전달하기 위한 객체 |
24 changes: 24 additions & 0 deletions
24
src/main/java/edu/example/springbootblog/Blog/dto/viewdto/BlogViewResponse.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,24 @@ | ||
package edu.example.springbootblog.Blog.dto.viewdto; | ||
|
||
import edu.example.springbootblog.Blog.domain.Blog; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@NoArgsConstructor | ||
@Data | ||
public class BlogViewResponse { | ||
private Long id; | ||
private String title; | ||
private String content; | ||
private LocalDateTime createdAt; | ||
|
||
public BlogViewResponse(Blog blog) { | ||
this.id = blog.getId(); | ||
this.title = blog.getTitle(); | ||
this.content = blog.getContent(); | ||
this.createdAt = blog.getCreatedAt(); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/edu/example/springbootblog/Blog/repository/BlogRepository.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 edu.example.springbootblog.Blog.repository; | ||
|
||
import edu.example.springbootblog.Blog.domain.Blog; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface BlogRepository extends JpaRepository<Blog, Long> { | ||
|
||
} | ||
|
||
|
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
58 changes: 0 additions & 58 deletions
58
src/main/java/edu/example/springbootblog/article/controller/BlogApiController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.