Skip to content

Commit

Permalink
[게시글 생성/조회/수정/삭제] 지노 미션 제출합니다. (#45)
Browse files Browse the repository at this point in the history
* feat: thymeleaf 추가, html 파일 templates 디렉토리로 이동

* feat: Test 주석추가

* feat: ArticleController 추가

* feat: Article 클래스 추가

* feat: 게시글 수정, 삭제 기능 구현

* refactor: 코드 리팩토링

* refactor: 함수명 수정

* refactor: Controller 메서드명 수정

* refactor: 코드 리팩토링

* refactor: 매서드 위치 수정

* refactor: 리뷰 반영

* refactor: 리뷰 반영 : setter 메서드명 번경

* refactor: 리뷰 반영, ArticleRepository.class update 메서드 삭제

* refactor: 리뷰 반영, ArticleRepository.class 수정에 따른 테스트 코드 삭제

* refactor: 리뷰 반영, controller ModelAnvView 메서드 내부에서 수정

* refactor: 테스트 코드 수정

* refactor 피드백 반영

* refactor 피드백 반영
  • Loading branch information
qpwoeiru789 authored and woowahan-pjs committed Jul 16, 2019
1 parent ca74828 commit 72e0dc0
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 140 deletions.
16 changes: 9 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}

apply plugin: 'io.spring.dependency-management'
Expand All @@ -10,14 +10,16 @@ version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit'
}
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
}
15 changes: 13 additions & 2 deletions src/main/java/techcourse/myblog/HelloWorldController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package techcourse.myblog;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
public class HelloWorldController {
@GetMapping("/helloworld")
public String temp(String blogName) {
return blogName;
}

@PostMapping("/helloworld")
public String asd(@RequestBody String blogName) {
return blogName;
}
}
1 change: 0 additions & 1 deletion src/main/java/techcourse/myblog/MyblogApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class MyblogApplication {
public static void main(String[] args) {
SpringApplication.run(MyblogApplication.class, args);
}

}
68 changes: 68 additions & 0 deletions src/main/java/techcourse/myblog/domain/Article.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
package techcourse.myblog.domain;

import java.util.Objects;

public class Article {
private int id;
private String title;
private String coverUrl;
private String contents;

public Article(String title, String coverUrl, String contents) {
this.title = title;
this.coverUrl = coverUrl;
this.contents = contents;
}

public boolean checkId(int id) {
return this.id == id;
}

public void update(Article article) {
this.title = article.title;
this.coverUrl = article.coverUrl;
this.contents = article.contents;
}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public String getTitle() {
return title;
}

public String getCoverUrl() {
return coverUrl;
}

public String getContents() {
return contents;
}

@Override
public String toString() {
return "Article{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", coverUrl='" + coverUrl + '\'' +
", contents='" + contents + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Article article = (Article) o;
return id == article.id &&
Objects.equals(title, article.title) &&
Objects.equals(coverUrl, article.coverUrl) &&
Objects.equals(contents, article.contents);
}

@Override
public int hashCode() {
return Objects.hash(id, title, coverUrl, contents);
}
}
20 changes: 19 additions & 1 deletion src/main/java/techcourse/myblog/domain/ArticleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,26 @@
@Repository
public class ArticleRepository {
private List<Article> articles = new ArrayList<>();
private int id = 0;

public List<Article> findAll() {
return articles;
return new ArrayList<>(articles);
}

public void save(Article article) {
id = id + 1;
article.setId(id);
articles.add(article);
}

public Article findArticleById(int id) {
return articles.stream()
.filter(x -> x.checkId(id))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 ID의 기사가 없습니다."));
}

public void removeById(int id) {
articles.remove(findArticleById(id));
}
}
72 changes: 71 additions & 1 deletion src/main/java/techcourse/myblog/web/ArticleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,80 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import techcourse.myblog.domain.Article;
import techcourse.myblog.domain.ArticleRepository;

@Controller
public class ArticleController {
@Autowired

private ArticleRepository articleRepository;

@Autowired
public ArticleController(ArticleRepository articleRepository) {
this.articleRepository = articleRepository;
}

@GetMapping("/")
public ModelAndView index(String blogName) {
if (blogName == null) {
blogName = "누구게?";
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("blogName", blogName);
modelAndView.addObject("articles", articleRepository.findAll());
return modelAndView;
}

@GetMapping("/writing")
public String writeForm() {
return "article-edit";
}

@PostMapping("/articles")
public ModelAndView save(Article article) {
articleRepository.save(article);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("article");
modelAndView.addObject("article", article);
return modelAndView;
}

@GetMapping("/article/{articleId}")
public ModelAndView show(@PathVariable String articleId) {
Article article = articleRepository.findArticleById(Integer.parseInt(articleId));
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("article");
modelAndView.addObject("article", article);
return modelAndView;
}

@GetMapping("/articles/{articleId}/edit")
public ModelAndView writeForm(@PathVariable String articleId) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("article-edit");
Article article = articleRepository.findArticleById(Integer.parseInt(articleId));
modelAndView.addObject("article", article);
return modelAndView;
}

@PutMapping("/articles/{articleId}")
public RedirectView update(@PathVariable String articleId, Article newArticle) {
RedirectView redirectView = new RedirectView();
Article article = articleRepository.findArticleById(Integer.parseInt(articleId));
article.update(newArticle);
redirectView.setUrl("/");
return redirectView;
}

@DeleteMapping("/articles/{articleId}")
public RedirectView delete(@PathVariable String articleId) {
RedirectView redirectView = new RedirectView();
articleRepository.removeById(Integer.parseInt(articleId));
redirectView.setUrl("/");
return redirectView;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

spring.devtools.livereload.enabled=true
spring.devtools.restart.enabled=true
spring.thymeleaf.cache=false
15 changes: 15 additions & 0 deletions src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
,--,
,---._ ,----.. ,--. ,--, ,----.. ,---.'|
.-- -.' \ ,---, .--.--. / / \ ,--.'| ,--.'| / / \ | | : ,---, ,----..
| | : ' .' \ / / '. / . : ,--,: : | ,--, | : / . : : : | ,`--.' | / / \
: ; | / ; '. | : /`. / . / ;. \,`--.'`| ' : ,---.'| : ' . / ;. \| ' : | : :| : :
: |: : \ ; | |--` . ; / ` ;| : : | | | | : _' |. ; / ` ;; ; ' : | '. | ;. /
| : :: | /\ \| : ;_ ; | ; \ ; |: | \ | : : : |.' |; | ; \ ; |' | |__ | : |. ; /--`
: | : ' ;. :\ \ `. | : | ; | '| : ' '; | | ' ' ; :| : | ; | '| | :.'|' ' ;; | ;
| ; || | ;/ \ \`----. \. | ' ' ' :' ' ;. ; ' | .'. |. | ' ' ' :' : ;| | || : |
___ l ' : | \ \ ,'__ \ \ |' ; \; / || | | \ | | | : | '' ; \; / || | ./ ' : ;. | '___
/ /\ J :| | ' '--' / /`--' / \ \ ', / ' : | ; .' ' : | : ; \ \ ', / ; : ; | | '' ; : .'|
/ ../ `..- ,| : : '--'. / ; : / | | '`--' | | ' ,/ ; : / | ,/ ' : |' | '/ :
\ \ ; | | ,' `--'---' \ \ .' ' : | ; : ;--' \ \ .' '---' ; |.' | : /
\ \ ,' `--'' `---` ; |.' | ,/ `---` '---' \ \ .'
"---....--' '---' '---' `---`
Binary file modified src/main/resources/static/images/pages/index/bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/main/resources/static/images/pages/index/study.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 72e0dc0

Please sign in to comment.