Skip to content

Commit 6d18b90

Browse files
committed
Add the NoteDTO
1 parent 02adf46 commit 6d18b90

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/main/java/lk/ijse/dep/note/api/UserController.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lk.ijse.dep.note.service.exception.NotFoundException;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.http.HttpStatus;
10+
import org.springframework.validation.Errors;
1011
import org.springframework.validation.annotation.Validated;
1112
import org.springframework.web.bind.annotation.*;
1213
import org.springframework.web.server.ResponseStatusException;
@@ -21,14 +22,17 @@ public class UserController {
2122

2223
@ResponseStatus(HttpStatus.CREATED)
2324
@PostMapping(consumes = "application/json", produces = "application/json")
24-
public UserDTO registerUser(@RequestBody @Valid UserDTO user) {
25+
public UserDTO registerUser(@RequestBody @Validated UserDTO user, Errors errors) {
2526

27+
if(errors.hasFieldErrors()){
28+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,errors.getFieldErrors().get(0).getDefaultMessage());
29+
}
2630
userService.registerUser(user);
2731

2832
return user;
2933
}
3034

31-
@GetMapping(path = "{userId:[A-Fa-f0-\\~]{36}}}", produces = "application/json")
35+
@GetMapping(path = "/{userId:[A-Fa-f0-9\\-]{36}}", produces = "application/json")
3236
public UserDTO getUserInfo(@PathVariable String userId) {
3337
System.out.println("get" + userId);
3438

@@ -37,18 +41,20 @@ public UserDTO getUserInfo(@PathVariable String userId) {
3741
}
3842

3943
@ResponseStatus(HttpStatus.NO_CONTENT)
40-
@DeleteMapping("{userId:[A-Fa-f0-\\~]{36}}")
44+
@DeleteMapping("/{userId:[A-Fa-f0-9\\-]{36}}")
4145
public void deleteuser(@PathVariable String userId) {
4246

4347
userService.deleteUser(userId);
4448

4549
}
4650

4751
@ResponseStatus(HttpStatus.NO_CONTENT)
48-
@PatchMapping(path = "{userId:[A-Fa-f0-\\~]{36}}", consumes = "application/json")
49-
public void updateUser(@PathVariable String userId, @RequestBody @Valid UserDTO user) {
50-
52+
@PatchMapping(path = "/{userId:[A-Fa-f0-9\\-]{36}}", consumes = "application/json")
53+
public void updateUser(@PathVariable String userId, @RequestBody @Valid UserDTO user, Errors errors) {
5154

55+
if(errors.hasFieldErrors()){
56+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,errors.getFieldErrors().get(0).getDefaultMessage());
57+
}
5258

5359
user.setId(userId);
5460
userService.updateUser(user);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package lk.ijse.dep.note.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import javax.validation.constraints.NotBlank;
8+
import javax.validation.constraints.Null;
9+
import javax.validation.constraints.Pattern;
10+
import java.io.Serializable;
11+
12+
@NoArgsConstructor @AllArgsConstructor @Data
13+
public class NoteDTO implements Serializable {
14+
@Null(message = "Id cannot be set")
15+
private int id;
16+
@NotBlank(message = "Text cannot be empty")
17+
private String text;
18+
@Pattern(regexp = "[A-Fa-F0-9\\-]{36}",message = "Invalid user id")
19+
private String userId;
20+
}

src/main/java/lk/ijse/dep/note/dto/UserDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class UserDTO implements Serializable {
2323
private String email;
2424

2525
@NotNull(message = "Password cannot be empty")
26-
@Length(max = 6,message = "Password must have minimum of 6 characters")
26+
@Length(min = 6,message = "Password must have minimum of 6 characters")
2727
@NotNull(message = "Password cannot be null")
2828
private String password;
2929

0 commit comments

Comments
 (0)