Skip to content

Commit

Permalink
debug test : test fail in action
Browse files Browse the repository at this point in the history
  • Loading branch information
adorahelen committed Dec 1, 2024
1 parent 4d46b9f commit 3593a9b
Show file tree
Hide file tree
Showing 65 changed files with 2,545 additions and 292 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ dependencies {
// Logging
implementation 'org.springframework.boot:spring-boot-starter-logging'

// mysql
implementation 'mysql:mysql-connector-java:8.0.29'



// H2 데이터베이스 의존성
implementation 'com.h2database:h2'

Expand All @@ -54,6 +53,7 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testAnnotationProcessor 'org.projectlombok:lombok'

// 시큐리티
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
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);
}
}

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


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.example.springbootblog.article.domain;
package edu.example.springbootblog.Blog.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -13,8 +13,8 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name="articles")
public class Article {
@Table(name="blog")
public class Blog {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // prime key auto 1+
Expand All @@ -28,7 +28,7 @@ public class Article {
private String content;

@Builder // make the object==객체 of Builder pattern
public Article(String title, String content) {
public Blog(String title, String content) {
this.title = title;
this.content = content;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package edu.example.springbootblog.article.dto.apidto;
package edu.example.springbootblog.Blog.dto.apidto;


import edu.example.springbootblog.article.domain.Article;
import edu.example.springbootblog.Blog.domain.Blog;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor // add general constructor
@AllArgsConstructor // all filed value get parameter in constructor
@Getter
public class AddArticleRequest {
public class AddBlogRequest {

private String title;
private String content;

public Article toEntity(){
return Article.builder()
public Blog toEntity(){
return Blog.builder()
.title(title)
.content(content)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package edu.example.springbootblog.article.dto.apidto;
package edu.example.springbootblog.Blog.dto.apidto;

import edu.example.springbootblog.article.domain.Article;
import edu.example.springbootblog.Blog.domain.Blog;
import lombok.Getter;

@Getter
public class ArticleResponse {
public class BlogResponse {

private final String title;
private final String content;

public ArticleResponse(Article article) {
public BlogResponse(Blog blog) {
// 엔티티를 인수로 받는 생성자
this.title = article.getTitle();
this.content = article.getContent();
this.title = blog.getTitle();
this.content = blog.getContent();
}
}
// 응답을 위한 dto 코드
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.example.springbootblog.article.dto.apidto;
package edu.example.springbootblog.Blog.dto.apidto;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -7,7 +7,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UpdateArticleRequest {
public class UpdateBlogRequest {
private String title;
private String content;
}
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();
}
}// 뷰에게 데이터를 전달하기 위한 객체
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();
}

}
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> {

}


Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package edu.example.springbootblog.article.service;
package edu.example.springbootblog.Blog.service;

import edu.example.springbootblog.article.domain.Article;
import edu.example.springbootblog.article.dto.apidto.AddArticleRequest;
import edu.example.springbootblog.article.dto.apidto.UpdateArticleRequest;
import edu.example.springbootblog.article.repository.BlogRepository;
import edu.example.springbootblog.Blog.domain.Blog;
import edu.example.springbootblog.Blog.dto.apidto.AddBlogRequest;
import edu.example.springbootblog.Blog.dto.apidto.UpdateBlogRequest;
import edu.example.springbootblog.Blog.repository.BlogRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -15,18 +15,18 @@
public class BlogService {
private final BlogRepository blogRepository;

public Article save(AddArticleRequest request){
public Blog save(AddBlogRequest request){
return blogRepository.save(request.toEntity());
}

// Read ALL
public List<Article> findAll(){
public List<Blog> findAll(){
return blogRepository.findAll();
//jpa 지원 메서드 파인드올 호출하여, 아티클 테이블에 저장되어 있는 모든 데이터를 조회
}

// Read 1
public Article findById(Long id){ // Long & long 둘 다 가능
public Blog findById(Long id){ // Long & long 둘 다 가능
return blogRepository.findById(id)
.orElseThrow(()-> new IllegalArgumentException("Not Found : " + id));
}
Expand All @@ -37,11 +37,11 @@ public void delete(Long id){
}

@Transactional // 트랜잭션용 메서드
public Article update(long id, UpdateArticleRequest request){ // 설마 이거 하나?
Article article = blogRepository.findById(id)
public Blog update(long id, UpdateBlogRequest request){ // 설마 이거 하나?
Blog blog = blogRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Not Found : " + id));

article.update(request.getTitle(), request.getContent());
return article;
blog.update(request.getTitle(), request.getContent());
return blog;
}
}

This file was deleted.

Loading

0 comments on commit 3593a9b

Please sign in to comment.