Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3636f25

Browse files
author
Ivan Franchin
committed
Convert some DTOs to record; remove mappers to simplify the project
1 parent 21a7612 commit 3636f25

File tree

10 files changed

+40
-126
lines changed

10 files changed

+40
-126
lines changed

‎book-api/src/main/java/com/ivanfranchin/bookapi/mapper/BookMapper.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎book-api/src/main/java/com/ivanfranchin/bookapi/mapper/BookMapperImpl.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎book-api/src/main/java/com/ivanfranchin/bookapi/mapper/UserMapper.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎book-api/src/main/java/com/ivanfranchin/bookapi/mapper/UserMapperImpl.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎book-api/src/main/java/com/ivanfranchin/bookapi/rest/AuthController.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class AuthController {
2828

2929
@PostMapping("/authenticate")
3030
public ResponseEntity<AuthResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
31-
Optional<User> userOptional = userService.validUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
31+
Optional<User> userOptional = userService.validUsernameAndPassword(loginRequest.username(), loginRequest.password());
3232
if (userOptional.isPresent()) {
3333
User user = userOptional.get();
3434
return ResponseEntity.ok(new AuthResponse(user.getId(), user.getName(), user.getRole()));
@@ -39,11 +39,11 @@ public ResponseEntity<AuthResponse> login(@Valid @RequestBody LoginRequest login
3939
@ResponseStatus(HttpStatus.CREATED)
4040
@PostMapping("/signup")
4141
public AuthResponse signUp(@Valid @RequestBody SignUpRequest signUpRequest) {
42-
if (userService.hasUserWithUsername(signUpRequest.getUsername())) {
43-
throw new DuplicatedUserInfoException(String.format("Username %s is already been used", signUpRequest.getUsername()));
42+
if (userService.hasUserWithUsername(signUpRequest.username())) {
43+
throw new DuplicatedUserInfoException(String.format("Username %s is already been used", signUpRequest.username()));
4444
}
45-
if (userService.hasUserWithEmail(signUpRequest.getEmail())) {
46-
throw new DuplicatedUserInfoException(String.format("Email %s is already been used", signUpRequest.getEmail()));
45+
if (userService.hasUserWithEmail(signUpRequest.email())) {
46+
throw new DuplicatedUserInfoException(String.format("Email %s is already been used", signUpRequest.email()));
4747
}
4848

4949
User user = userService.saveUser(createUser(signUpRequest));
@@ -52,10 +52,10 @@ public AuthResponse signUp(@Valid @RequestBody SignUpRequest signUpRequest) {
5252

5353
private User createUser(SignUpRequest signUpRequest) {
5454
User user = new User();
55-
user.setUsername(signUpRequest.getUsername());
56-
user.setPassword(signUpRequest.getPassword());
57-
user.setName(signUpRequest.getName());
58-
user.setEmail(signUpRequest.getEmail());
55+
user.setUsername(signUpRequest.username());
56+
user.setPassword(signUpRequest.password());
57+
user.setName(signUpRequest.name());
58+
user.setEmail(signUpRequest.email());
5959
user.setRole(SecurityConfig.USER);
6060
return user;
6161
}

‎book-api/src/main/java/com/ivanfranchin/bookapi/rest/BookController.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ivanfranchin.bookapi.rest;
22

3-
import com.ivanfranchin.bookapi.mapper.BookMapper;
43
import com.ivanfranchin.bookapi.model.Book;
54
import com.ivanfranchin.bookapi.rest.dto.BookDto;
65
import com.ivanfranchin.bookapi.rest.dto.CreateBookRequest;
@@ -31,30 +30,37 @@
3130
public class BookController {
3231

3332
private final BookService bookService;
34-
private final BookMapper bookMapper;
3533

3634
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
3735
@GetMapping
3836
public List<BookDto> getBooks(@RequestParam(value = "text", required = false) String text) {
3937
List<Book> books = (text == null) ? bookService.getBooks() : bookService.getBooksContainingText(text);
4038
return books.stream()
41-
.map(bookMapper::toBookDto)
39+
.map(this::toBookDto)
4240
.collect(Collectors.toList());
4341
}
4442

4543
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
4644
@ResponseStatus(HttpStatus.CREATED)
4745
@PostMapping
4846
public BookDto createBook(@Valid @RequestBody CreateBookRequest createBookRequest) {
49-
Book book = bookMapper.toBook(createBookRequest);
50-
return bookMapper.toBookDto(bookService.saveBook(book));
47+
Book book = toBook(createBookRequest);
48+
return toBookDto(bookService.saveBook(book));
5149
}
5250

5351
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
5452
@DeleteMapping("/{isbn}")
5553
public BookDto deleteBook(@PathVariable String isbn) {
5654
Book book = bookService.validateAndGetBook(isbn);
5755
bookService.deleteBook(book);
58-
return bookMapper.toBookDto(book);
56+
return toBookDto(book);
57+
}
58+
59+
private Book toBook(CreateBookRequest createBookRequest) {
60+
return new Book(createBookRequest.isbn(), createBookRequest.title());
61+
}
62+
63+
private BookDto toBookDto(Book book) {
64+
return new BookDto(book.getIsbn(), book.getTitle());
5965
}
6066
}

‎book-api/src/main/java/com/ivanfranchin/bookapi/rest/UserController.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ivanfranchin.bookapi.rest;
22

3-
import com.ivanfranchin.bookapi.mapper.UserMapper;
43
import com.ivanfranchin.bookapi.model.User;
54
import com.ivanfranchin.bookapi.rest.dto.UserDto;
65
import com.ivanfranchin.bookapi.security.CustomUserDetails;
@@ -26,33 +25,36 @@
2625
public class UserController {
2726

2827
private final UserService userService;
29-
private final UserMapper userMapper;
3028

3129
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
3230
@GetMapping("/me")
3331
public UserDto getCurrentUser(@AuthenticationPrincipal CustomUserDetails currentUser) {
34-
return userMapper.toUserDto(userService.validateAndGetUserByUsername(currentUser.getUsername()));
32+
return toUserDto(userService.validateAndGetUserByUsername(currentUser.getUsername()));
3533
}
3634

3735
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
3836
@GetMapping
3937
public List<UserDto> getUsers() {
4038
return userService.getUsers().stream()
41-
.map(userMapper::toUserDto)
39+
.map(this::toUserDto)
4240
.collect(Collectors.toList());
4341
}
4442

4543
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
4644
@GetMapping("/{username}")
4745
public UserDto getUser(@PathVariable String username) {
48-
return userMapper.toUserDto(userService.validateAndGetUserByUsername(username));
46+
return toUserDto(userService.validateAndGetUserByUsername(username));
4947
}
5048

5149
@Operation(security = {@SecurityRequirement(name = BASIC_AUTH_SECURITY_SCHEME)})
5250
@DeleteMapping("/{username}")
5351
public UserDto deleteUser(@PathVariable String username) {
5452
User user = userService.validateAndGetUserByUsername(username);
5553
userService.deleteUser(user);
56-
return userMapper.toUserDto(user);
54+
return toUserDto(user);
55+
}
56+
57+
public UserDto toUserDto(User user) {
58+
return new UserDto(user.getId(), user.getUsername(), user.getName(), user.getEmail(), user.getRole());
5759
}
5860
}

‎book-api/src/main/java/com/ivanfranchin/bookapi/rest/dto/CreateBookRequest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22

33
import io.swagger.v3.oas.annotations.media.Schema;
44
import jakarta.validation.constraints.NotBlank;
5-
import lombok.Data;
65

7-
@Data
8-
public class CreateBookRequest {
9-
10-
@Schema(example = "9781849518260")
11-
@NotBlank
12-
private String isbn;
13-
14-
@Schema(example = "Spring Security 3.1")
15-
@NotBlank
16-
private String title;
6+
public record CreateBookRequest(
7+
@Schema(example = "9781849518260") @NotBlank String isbn,
8+
@Schema(example = "Spring Security 3.1") @NotBlank String title) {
179
}

‎book-api/src/main/java/com/ivanfranchin/bookapi/rest/dto/LoginRequest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22

33
import io.swagger.v3.oas.annotations.media.Schema;
44
import jakarta.validation.constraints.NotBlank;
5-
import lombok.Data;
65

7-
@Data
8-
public class LoginRequest {
9-
10-
@Schema(example = "user")
11-
@NotBlank
12-
private String username;
13-
14-
@Schema(example = "user")
15-
@NotBlank
16-
private String password;
6+
public record LoginRequest(
7+
@Schema(example = "user") @NotBlank String username,
8+
@Schema(example = "user") @NotBlank String password) {
179
}

‎book-api/src/main/java/com/ivanfranchin/bookapi/rest/dto/SignUpRequest.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,10 @@
33
import io.swagger.v3.oas.annotations.media.Schema;
44
import jakarta.validation.constraints.Email;
55
import jakarta.validation.constraints.NotBlank;
6-
import lombok.Data;
76

8-
@Data
9-
public class SignUpRequest {
10-
11-
@Schema(example = "user3")
12-
@NotBlank
13-
private String username;
14-
15-
@Schema(example = "user3")
16-
@NotBlank
17-
private String password;
18-
19-
@Schema(example = "User3")
20-
@NotBlank
21-
private String name;
22-
23-
@Schema(example = "user3@mycompany.com")
24-
@Email
25-
private String email;
7+
public record SignUpRequest(
8+
@Schema(example = "user3") @NotBlank String username,
9+
@Schema(example = "user3") @NotBlank String password,
10+
@Schema(example = "User3") @NotBlank String name,
11+
@Schema(example = "user3@mycompany.com") @Email String email) {
2612
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /