Skip to content

Commit c30c223

Browse files
committed
add persist data layer
1 parent a452932 commit c30c223

File tree

7 files changed

+277
-1
lines changed

7 files changed

+277
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ repositories {
2525

2626

2727
dependencies {
28+
compile('org.springframework.boot:spring-boot-starter-data-jpa')
2829
compile('org.springframework.boot:spring-boot-starter-security')
2930
compile('org.springframework.boot:spring-boot-starter-web')
3031
runtime('mysql:mysql-connector-java')
3132
testCompile('org.springframework.boot:spring-boot-starter-test')
32-
testCompile('org.springframework.security:spring-security-test')
3333
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.streaming.configuration;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.boot.autoconfigure.domain.EntityScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
import org.springframework.transaction.annotation.EnableTransactionManagement;
8+
9+
@Configuration
10+
@EnableAutoConfiguration
11+
@EntityScan(basePackages = {"com.streaming.domains"})
12+
@EnableJpaRepositories(basePackages = {"com.streaming.repositories"})
13+
@EnableTransactionManagement
14+
public class RepositoryConfiguration {
15+
16+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.streaming.domains;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
import java.util.Date;
6+
import java.util.Objects;
7+
import java.util.Set;
8+
9+
@Entity
10+
@Table(name = "users")
11+
public class User implements Serializable {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
@Column(unique = true)
16+
private String username;
17+
private String password;
18+
private Date created;
19+
private Date modified;
20+
21+
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
22+
private Set<Video> videos;
23+
24+
public User() { }
25+
26+
public User(String username, String password) {
27+
this.username = username;
28+
this.password = password;
29+
this.created = new Date();
30+
this.modified = new Date();
31+
}
32+
33+
public Long getId() {
34+
return id;
35+
}
36+
37+
public void setId(Long id) {
38+
this.id = id;
39+
}
40+
41+
public String getUsername() {
42+
return username;
43+
}
44+
45+
public void setUsername(String username) {
46+
this.username = username;
47+
}
48+
49+
public String getPassword() {
50+
return password;
51+
}
52+
53+
public void setPassword(String password) {
54+
this.password = password;
55+
}
56+
57+
public Date getCreated() {
58+
return created;
59+
}
60+
61+
public void setCreated(Date created) {
62+
this.created = created;
63+
}
64+
65+
public Date getModified() {
66+
return modified;
67+
}
68+
69+
public void setModified(Date modified) {
70+
this.modified = modified;
71+
}
72+
73+
public Set<Video> getVideos() {
74+
return videos;
75+
}
76+
77+
public void setVideos(Set<Video> videos) {
78+
this.videos = videos;
79+
}
80+
81+
@Override
82+
public int hashCode() {
83+
if (this.id == null) {
84+
return super.hashCode();
85+
}
86+
return id.hashCode();
87+
}
88+
89+
@Override
90+
public boolean equals(Object obj) {
91+
if (this == obj) {
92+
return true;
93+
}
94+
if (obj == null) {
95+
return false;
96+
}
97+
if (!(obj instanceof User)) {
98+
return false;
99+
}
100+
final User other = (User) obj;
101+
102+
return Objects.equals(this.id, other.getId());
103+
}
104+
105+
@Override
106+
public String toString() {
107+
return "User{" +
108+
"id=" + id +
109+
", username='" + username + '\'' +
110+
", password='" + password + '\'' +
111+
", created=" + created +
112+
", modified=" + modified +
113+
'}';
114+
}
115+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.streaming.domains;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
import java.util.Date;
6+
import java.util.Objects;
7+
8+
@Entity
9+
@Table(name = "videos")
10+
public class Video implements Serializable {
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.IDENTITY)
13+
private Long id;
14+
@ManyToOne(fetch = FetchType.LAZY)
15+
@JoinColumn(name = "user_id")
16+
private User user;
17+
private String title;
18+
private double size;
19+
private String url;
20+
private Date created;
21+
private Date modified;
22+
23+
public Video() {}
24+
25+
public Video(User user, String title, double size, String url) {
26+
this.user = user;
27+
this.title = title;
28+
this.size = size;
29+
this.url = url;
30+
this.created = new Date();
31+
this.modified = new Date();
32+
}
33+
34+
public Long getId() {
35+
return id;
36+
}
37+
38+
public void setId(Long id) {
39+
this.id = id;
40+
}
41+
42+
public User getUser() {
43+
return user;
44+
}
45+
46+
public void setUser(User user) {
47+
this.user = user;
48+
}
49+
50+
public String getTitle() {
51+
return title;
52+
}
53+
54+
public void setTitle(String title) {
55+
this.title = title;
56+
}
57+
58+
public double getSize() {
59+
return size;
60+
}
61+
62+
public void setSize(double size) {
63+
this.size = size;
64+
}
65+
66+
public String getUrl() {
67+
return url;
68+
}
69+
70+
public void setUrl(String url) {
71+
this.url = url;
72+
}
73+
74+
public Date getCreated() {
75+
return created;
76+
}
77+
78+
public void setCreated(Date created) {
79+
this.created = created;
80+
}
81+
82+
public Date getModified() {
83+
return modified;
84+
}
85+
86+
public void setModified(Date modified) {
87+
this.modified = modified;
88+
}
89+
90+
@Override
91+
public int hashCode() {
92+
if (this.id == null) {
93+
return super.hashCode();
94+
}
95+
return id.hashCode();
96+
}
97+
98+
@Override
99+
public boolean equals(Object obj) {
100+
if (this == obj) {
101+
return true;
102+
}
103+
if (obj == null) {
104+
return false;
105+
}
106+
if (!(obj instanceof Video)) {
107+
return false;
108+
}
109+
final Video other = (Video) obj;
110+
111+
return Objects.equals(this.id, other.getId());
112+
}
113+
114+
@Override
115+
public String toString() {
116+
return "Video{" +
117+
"id=" + id +
118+
", title='" + title + '\'' +
119+
", size=" + size +
120+
", url='" + url + '\'' +
121+
", created=" + created +
122+
", modified=" + modified +
123+
'}';
124+
}
125+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.streaming.repositories;
2+
3+
import com.streaming.domains.User;
4+
import org.springframework.data.repository.PagingAndSortingRepository;
5+
6+
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
7+
User findByUsername(String username);
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.streaming.repositories;
2+
3+
import com.streaming.domains.Video;
4+
import org.springframework.data.repository.PagingAndSortingRepository;
5+
6+
public interface VideoRepository extends PagingAndSortingRepository<Video, Long> {
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#MySQL data source properties
2+
spring.datasource.url=jdbc:mysql://localhost/live-streaming?useUnicode=true&characterEncoding=UTF-8&useSSL=false
3+
spring.datasource.username=root
4+
spring.datasource.password=
5+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

0 commit comments

Comments
 (0)