Skip to content

Commit a121d04

Browse files
committed
[feat] entity 생성,수정 시간 추가, mustache index 생성
1 parent 47d8585 commit a121d04

File tree

10 files changed

+134
-6
lines changed

10 files changed

+134
-6
lines changed

build.gradle

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ repositories {
2121
dependencies {
2222
implementation 'org.springframework.boot:spring-boot-starter-web'
2323
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
24-
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
2524
testImplementation 'junit:junit:4.13.1'
2625
compileOnly 'org.projectlombok:lombok'
27-
compileOnly 'org.springframework.boot:spring-boot-starter-data-jpa'
28-
//compileOnly 'com.h2database:h2'
2926
developmentOnly 'org.springframework.boot:spring-boot-devtools'
3027
annotationProcessor 'org.projectlombok:lombok'
3128
testImplementation 'org.springframework.boot:spring-boot-starter-test'
32-
29+
compileOnly 'org.springframework.boot:spring-boot-starter-mustache'
30+
compileOnly('com.h2database:h2')
3331
}
3432

3533
tasks.named('test') {

src/main/java/com/devian/springboot/Application.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.devian.springboot;
22
import org.springframework.boot.SpringApplication;
33
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
5+
6+
@EnableJpaAuditing
47
@SpringBootApplication
58
public class Application {
69
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.devian.springboot.domain;
2+
3+
import jakarta.persistence.EntityListeners;
4+
import jakarta.persistence.MappedSuperclass;
5+
import lombok.Getter;
6+
import org.springframework.data.annotation.CreatedDate;
7+
import org.springframework.data.annotation.LastModifiedDate;
8+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
9+
10+
import java.time.LocalDateTime;
11+
12+
@Getter
13+
@MappedSuperclass
14+
@EntityListeners(AuditingEntityListener.class)
15+
public abstract class BaseTimeEntity {
16+
17+
@CreatedDate
18+
private LocalDateTime createdDate;
19+
20+
@LastModifiedDate
21+
private LocalDateTime modifiedDate;
22+
23+
}

src/main/java/com/devian/springboot/domain/posts/Posts.java

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ public void update(String title, String content) {
3333
this.title = title;
3434
this.content = content;
3535
}
36+
3637
}

src/main/java/com/devian/springboot/service/posts/PostsService.java src/main/java/com/devian/springboot/service/PostsService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.devian.springboot.service.posts;
1+
package com.devian.springboot.service;
22

33
import com.devian.springboot.domain.posts.Posts;
44
import com.devian.springboot.domain.posts.PostsRepository;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.devian.springboot.web;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class IndexController {
8+
@GetMapping("/")
9+
public String index() {
10+
return "index";
11+
}
12+
}

src/main/java/com/devian/springboot/web/PostsApiController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.devian.springboot.web;
22

3-
import com.devian.springboot.service.posts.PostsService;
3+
import com.devian.springboot.service.PostsService;
44
import com.devian.springboot.web.dto.PostsResponseDto;
55
import com.devian.springboot.web.dto.PostsSaveRequestDto;
66
import com.devian.springboot.web.dto.PostsUpdateRequestDto;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{>layout/header}}
2+
3+
<h1>스프링부트로 시작하는 웹 서비스 Ver.2</h1>
4+
<div class="col-md-12">
5+
<div class="row">
6+
<div class="col-md-6">
7+
<a href="/posts/save" role="button" class="btn btn-primary">글 등록</a>
8+
{{#userName}}
9+
Logged in as: <span id="user">{{userName}}</span>
10+
<a href="/logout" class="btn btn-info active" role="button">Logout</a>
11+
{{/userName}}
12+
{{^userName}}
13+
<a href="/oauth2/authorization/google" class="btn btn-success active" role="button">Google Login</a>
14+
<a href="/oauth2/authorization/naver" class="btn btn-secondary active" role="button">Naver Login</a>
15+
{{/userName}}
16+
</div>
17+
</div>
18+
<br>
19+
<!-- 목록 출력 영역 -->
20+
<table class="table table-horizontal table-bordered">
21+
<thead class="thead-strong">
22+
<tr>
23+
<th>게시글번호</th>
24+
<th>제목</th>
25+
<th>작성자</th>
26+
<th>최종수정일</th>
27+
</tr>
28+
</thead>
29+
<tbody id="tbody">
30+
{{#posts}}
31+
<tr>
32+
<td>{{id}}</td>
33+
<td><a href="/posts/update/{{id}}">{{title}}</a></td>
34+
<td>{{author}}</td>
35+
<td>{{modifiedDate}}</td>
36+
</tr>
37+
{{/posts}}
38+
</tbody>
39+
</table>
40+
</div>
41+
42+
{{>layout/footer}}

src/test/java/com/devian/springboot/domain/posts/PostsRepositoryTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.boot.test.context.SpringBootTest;
88
import org.springframework.test.context.junit4.SpringRunner;
99

10+
import java.time.LocalDateTime;
1011
import java.util.List;
1112

1213
import static org.assertj.core.api.Assertions.assertThat;
@@ -44,4 +45,24 @@ public void cleanup() {
4445
assertThat(posts.getTitle()).isEqualTo(title);
4546
assertThat(posts.getContent()).isEqualTo(content);
4647
}
48+
@Test
49+
public void BaseTimeEntity_등록() {
50+
//given
51+
LocalDateTime now = LocalDateTime.of(2019, 6, 4, 0, 0, 0);
52+
postsRepository.save(Posts.builder()
53+
.title("title")
54+
.content("content")
55+
.author("author")
56+
.build());
57+
//when
58+
List<Posts> postsList = postsRepository.findAll();
59+
60+
//then
61+
Posts posts = postsList.get(0);
62+
63+
//System.out.println(">>>>>>>>> createDate=" + posts.getContent() + ", modifiedDate=" + posts.getModifiedDate());
64+
65+
//assertThat(posts.getCreatedDate()).isAfter(now);
66+
//assertThat(posts.getModifiedDate()).isAfter(now);
67+
}
4768
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.devian.springboot.web;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.web.client.TestRestTemplate;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest(webEnvironment = RANDOM_PORT)
15+
public class IndexControllerTest {
16+
17+
@Autowired
18+
private TestRestTemplate restTemplate;
19+
20+
@Test
21+
public void 메인페이지_로딩() {
22+
//when
23+
String body = this.restTemplate.getForObject("/", String.class);
24+
25+
//then
26+
assertThat(body).contains("스프링부트로 시작하는 웹 서비스");
27+
}
28+
}

0 commit comments

Comments
 (0)