Skip to content

Commit 963aed4

Browse files
committed
add Post entity, repo, and react component
1 parent 0530f1b commit 963aed4

File tree

12 files changed

+246
-26192
lines changed

12 files changed

+246
-26192
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ target/
1818
src/main/resources/config/application.properties
1919
node/
2020
src/main/resources/static/node_modules/
21+
src/main/resources/static/built/

src/main/java/com/shadowaterservices/thehub/controllers/AppController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
import org.springframework.web.bind.annotation.RestController;
66
import org.springframework.web.servlet.ModelAndView;
77

8+
import com.shadowaterservices.thehub.data.repositories.PostRepository;
89
import com.shadowaterservices.thehub.data.repositories.UserRepository;
910

1011
@RestController
1112
public class AppController {
1213
@Autowired
1314
UserRepository userRepository;
15+
16+
@Autowired
17+
PostRepository postRepository;
1418

1519
@RequestMapping("/")
1620
public ModelAndView index() {
@@ -19,4 +23,10 @@ public ModelAndView index() {
1923
return mav;
2024
}
2125

26+
@RequestMapping("/posts")
27+
public ModelAndView getPosts() {
28+
ModelAndView mav = new ModelAndView("posts");
29+
mav.addObject("posts", postRepository.findAll());
30+
return mav;
31+
}
2232
}
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package com.shadowaterservices.thehub.data;
22

3+
import java.util.Calendar;
4+
import java.util.Date;
5+
36
import org.springframework.beans.factory.annotation.Autowired;
47
import org.springframework.boot.CommandLineRunner;
58
import org.springframework.stereotype.Component;
69

10+
import com.shadowaterservices.thehub.data.entities.Post;
711
import com.shadowaterservices.thehub.data.entities.User;
12+
import com.shadowaterservices.thehub.data.repositories.PostRepository;
813
import com.shadowaterservices.thehub.data.repositories.UserRepository;
914

1015
@Component
1116
public class DatabaseLoader implements CommandLineRunner {
1217

13-
private final UserRepository repository;
18+
private final UserRepository userRepository;
19+
private final PostRepository postRepository;
1420

1521
@Autowired
16-
public DatabaseLoader(UserRepository repository) {
17-
this.repository = repository;
22+
public DatabaseLoader(UserRepository userRepository, PostRepository postRepository) {
23+
this.userRepository = userRepository;
24+
this.postRepository = postRepository;
1825
}
1926

2027
@Override
2128
public void run(String... strings) throws Exception {
22-
this.repository.save(new User("Harry", "Hub"));
29+
this.userRepository.save(new User("Harry", "Hub"));
30+
this.postRepository.save(new Post("This Is A Title","This is a body for the greatest blog post of all time. Since Tuesday.","http://google.com",Calendar.getInstance()));
2331
}
2432
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.shadowaterservices.thehub.data.entities;
2+
3+
4+
import java.util.Calendar;
5+
6+
import javax.persistence.Column;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
import javax.persistence.Lob;
11+
import javax.persistence.Temporal;
12+
import javax.persistence.TemporalType;
13+
14+
@Entity
15+
public class Post {
16+
@Id
17+
@GeneratedValue
18+
private Long id;
19+
20+
private String title;
21+
22+
@Lob
23+
@Column(columnDefinition = "text")
24+
private String body;
25+
26+
private String uri;
27+
28+
@Column(name = "DATETIME_FIELD")
29+
@Temporal(TemporalType.TIMESTAMP)
30+
private Calendar publishDate;
31+
32+
private Post() {};
33+
34+
public Post(String title, String body, String uri, Calendar publishDate) {
35+
this.setTitle(title);
36+
this.setBody(body);
37+
this.setUri(uri);
38+
this.setPublishDate(publishDate);
39+
}
40+
41+
public String getTitle() {
42+
return title;
43+
}
44+
45+
public void setTitle(String title) {
46+
this.title = title;
47+
}
48+
49+
public String getBody() {
50+
return body;
51+
}
52+
53+
public void setBody(String body) {
54+
this.body = body;
55+
}
56+
57+
public String getUri() {
58+
return uri;
59+
}
60+
61+
public void setUri(String uri) {
62+
this.uri = uri;
63+
}
64+
65+
public Calendar getPublishDate() {
66+
return this.publishDate;
67+
}
68+
69+
public void setPublishDate(Calendar publishDate) {
70+
this.publishDate = publishDate;
71+
}
72+
73+
public Long getId() {
74+
return id;
75+
}
76+
77+
public void setId(Long id) {
78+
this.id = id;
79+
}
80+
81+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.shadowaterservices.thehub.data.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
import com.shadowaterservices.thehub.data.entities.Post;
6+
7+
public interface PostRepository extends CrudRepository<Post, Long> {
8+
9+
}

src/main/resources/static/app.js

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,18 @@
11
'use strict';
22

33
const React = require('react');
4-
const client = require('./client');
5-
6-
class App extends React.Component {
7-
8-
constructor(props) {
9-
super(props);
10-
this.state = {users: []};
11-
}
12-
13-
componentDidMount() {
14-
client({method: 'GET', path: '/api/users'}).done(response => {
15-
this.setState({users: response.entity._embedded.users});
16-
});
17-
}
18-
19-
render() {
20-
return (
21-
<UserList users={this.state.users}/>
22-
)
23-
}
24-
}
25-
26-
class UserList extends React.Component{
27-
render() {
28-
var users = this.props.users.map(user =>
29-
<User key={user._links.self.href} user={user}/>
30-
);
31-
return (
32-
<table>
33-
<tr>
34-
<th>First Name</th>
35-
<th>Last Name</th>
36-
</tr>
37-
{users}
38-
</table>
39-
)
40-
}
41-
}
42-
43-
class User extends React.Component{
44-
render() {
45-
return (
46-
<tr>
47-
<td>{this.props.user.firstName}</td>
48-
<td>{this.props.user.lastName}</td>
49-
</tr>
50-
)
51-
}
52-
}
4+
const UserList = require('./users');
5+
const PostList = require('./posts');
536

547
React.render(
55-
<App />,
8+
<div>
9+
<div className="element">
10+
<PostList />
11+
</div>
12+
<div className="element">
13+
<UserList />
14+
</div>
15+
</div>,
5616
document.getElementById('react')
5717
)
5818

0 commit comments

Comments
 (0)