Skip to content
This repository was archived by the owner on Oct 25, 2019. It is now read-only.

Commit 8956003

Browse files
committed
working validation and users
1 parent 60ef543 commit 8956003

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed

src/main/java/oraMessaging/models/Chat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ public String toString() {
4444
this.date);
4545
}
4646

47-
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package oramessaging.controllers;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.validation.annotation.Validated;
5+
import org.springframework.web.bind.annotation.*;
6+
import oramessaging.models.User;
7+
import oramessaging.services.UserRepo;
8+
9+
@RestController
10+
public class UserController {
11+
@Autowired
12+
private UserRepo userRepo;
13+
14+
@RequestMapping(path = "/user/{id}", method = RequestMethod.GET)
15+
public User user(@PathVariable("id") long id) {
16+
return userRepo.findOne(id);
17+
}
18+
19+
@RequestMapping(path="/users", method=RequestMethod.GET)
20+
public Iterable<User> getUsers(){
21+
return userRepo.findAll();
22+
}
23+
24+
@RequestMapping(path="/user", method=RequestMethod.POST)
25+
public User saveuser(@RequestBody User user){
26+
System.out.println("User was saved!");
27+
return userRepo.save(user);
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package oramessaging.helpers;
2+
3+
import javax.validation.*;
4+
import java.lang.annotation.*;
5+
import oramessaging.helpers.PasswordMatchesValidator;
6+
7+
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
8+
@Retention(value=RetentionPolicy.RUNTIME)
9+
@Constraint(validatedBy = PasswordMatchesValidator.class)
10+
@Documented
11+
public @interface PasswordMatches {
12+
String message() default "Passwords don't match";
13+
Class<?>[] groups() default {};
14+
Class<? extends Payload>[] payload() default {};
15+
}
16+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package oramessaging.helpers;
2+
3+
import oramessaging.helpers.PasswordMatches;
4+
import oramessaging.models.User;
5+
6+
import javax.validation.*;
7+
8+
public class PasswordMatchesValidator
9+
implements ConstraintValidator<PasswordMatches, Object> {
10+
11+
@Override
12+
public void initialize(PasswordMatches p) {
13+
14+
}
15+
16+
@Override
17+
public boolean isValid(Object obj, ConstraintValidatorContext context){
18+
User user = (User) obj;
19+
if (user.getPassword() == null || user.getConfirmedPassword() == null)
20+
return false;
21+
return user.getPassword().equals(user.getConfirmedPassword());
22+
}
23+
24+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package oramessaging.models;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.validation.constraints.NotNull;
8+
9+
import org.hibernate.validator.constraints.NotEmpty;
10+
11+
import oramessaging.helpers.PasswordMatches;
12+
13+
@Entity
14+
@PasswordMatches
15+
public class User {
16+
17+
@Id
18+
@GeneratedValue(strategy=GenerationType.AUTO)
19+
private long id;
20+
@NotNull
21+
@NotEmpty
22+
private String email;
23+
@NotNull
24+
@NotEmpty
25+
private String name;
26+
@NotNull
27+
@NotEmpty
28+
private String password;
29+
@NotNull
30+
@NotEmpty
31+
private String confirmedPassword;
32+
33+
protected User() { }
34+
35+
public long getId(){
36+
return this.id;
37+
}
38+
39+
public String getEmail(){
40+
return this.email;
41+
}
42+
43+
public String getName(){
44+
return this.name;
45+
}
46+
47+
public String getPassword(){
48+
return this.password;
49+
}
50+
51+
public String getConfirmedPassword() {
52+
return this.confirmedPassword;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return String.format(
58+
"User[id=%d, name='%s', password='%s']",
59+
this.id,
60+
this.name,
61+
this.password
62+
);
63+
}
64+
65+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package oramessaging.services;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.repository.CrudRepository;
6+
7+
import oramessaging.models.User;
8+
9+
public interface UserRepo extends CrudRepository<User, Long> {
10+
11+
// List<User> findById(long id);
12+
// <U extends User> U save(U entity);
13+
14+
}

0 commit comments

Comments
 (0)