getAll() {
23 | return postRepository.findAll();
24 | }
25 |
26 | public Post save(Post post) {
27 | if (post.getId() == null) {
28 | post.setCreatedAt(LocalDateTime.now());
29 | }
30 | post.setUpdatedAt(LocalDateTime.now());
31 | return postRepository.save(post);
32 | }
33 |
34 | public void delete(Post post) {
35 | postRepository.delete(post);
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # use the default server port
2 | server.port=8080
3 |
4 | # enabling flyway to manage migrations
5 | spring.flyway.enabled=true
6 |
7 | spring.jpa.hibernate.ddl-auto=update
8 | spring.jpa.show-sql=true
9 | spring.jpa.open-in-view=false
10 |
11 | # setup some mysql database configs from .env variables
12 | spring.datasource.url=jdbc:mysql://localhost:3306/${MYSQL_DATABASE}
13 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
14 | spring.datasource.username=${MYSQL_USER}
15 | spring.datasource.password=${MYSQL_PASSWORD}
16 |
17 |
--------------------------------------------------------------------------------
/src/main/resources/db/migration/V1__initial_migration.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS post
2 | (
3 | id BIGINT NOT NULL AUTO_INCREMENT,
4 | PRIMARY KEY (id),
5 | title VARCHAR(255) NOT NULL,
6 | body VARCHAR(5000),
7 | created_at TIMESTAMP,
8 | updated_at TIMESTAMP,
9 | account_id BIGINT NOT NULL
10 | );
11 |
12 | CREATE TABLE IF NOT EXISTS authority
13 | (
14 | name VARCHAR(16) PRIMARY KEY
15 | );
16 |
17 | CREATE TABLE IF NOT EXISTS account
18 | (
19 | id BIGINT NOT NULL AUTO_INCREMENT,
20 | PRIMARY KEY(id),
21 | email VARCHAR(255) NOT NULL,
22 | password VARCHAR(255) NOT NULL,
23 | first_name VARCHAR(255),
24 | last_name VARCHAR(255),
25 | created_at TIMESTAMP,
26 | updated_at TIMESTAMP
27 | );
28 |
29 | CREATE TABLE IF NOT EXISTS account_authority
30 | (
31 | account_id BIGINT,
32 | PRIMARY KEY(account_id),
33 | authority_name VARCHAR(16)
34 | );
35 |
--------------------------------------------------------------------------------
/src/main/resources/db/migration/V2__add_imageFilePathToPost.sql:
--------------------------------------------------------------------------------
1 | ALTER TABLE post ADD COLUMN image_file_path VARCHAR(255) AFTER body;
2 |
3 | -- backfill our sample posts in the db with updated column value
4 | UPDATE post SET image_file_path='pexels-adrien-olichon-16059681.jpg' WHERE id=1;
5 | UPDATE post SET image_file_path='pexels-adrien-olichon-16059681.jpg' WHERE id=2;
6 |
--------------------------------------------------------------------------------
/src/main/resources/templates/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Blog :: 404
8 |
9 |
10 |
11 |
12 |
Sorry, Post Not Found
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/main/resources/templates/error.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Blog :: 403
8 |
9 |
10 |
11 |
12 |
Sorry, some kind of error happened!
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/main/resources/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 | Blog :: Home
9 |
10 |
11 |
12 |
13 |
Spring Boot Blog Application
14 |
15 |
18 |
19 |
20 |
![]()
21 |
22 |
Account First Name
23 |
Created At
24 |
Updated At
25 |
body text
26 |
27 |
28 |
29 |
30 |
34 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/src/main/resources/templates/login.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Blog :: Login
8 |
9 |
10 |
11 |
12 |
Spring Boot Blog Application
13 |
14 |
Home
15 |
Login
16 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/main/resources/templates/post.html:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 | Blog :: Post
10 |
11 |
12 |
13 |
14 |
Home
15 |
16 |
![]()
17 |
Title
18 |
Created At
19 |
Updated At
20 |
body text
21 |
22 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/main/resources/templates/post_edit.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Blog :: Update Post
8 |
9 |
10 |
11 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/src/main/resources/templates/post_new.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Blog :: New Post
8 |
9 |
10 |
11 |
12 |
Home
13 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/src/main/resources/templates/register.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Blog :: Register
8 |
9 |
10 |
11 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/test/java/com/example/springbootblogapplication/SpringBootBlogApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.springbootblogapplication;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class SpringBootBlogApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/uploads/pexels-adrien-olichon-16059681.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wazooinc/spring-boot-blog-application/cb88d96ed8984afa7ea9b6cfbbbd889165705071/uploads/pexels-adrien-olichon-16059681.jpg
--------------------------------------------------------------------------------