Skip to content

Commit 3099ad0

Browse files
committed
Replace @Data with records for DTOs
1 parent 4ea43f5 commit 3099ad0

17 files changed

Lines changed: 245 additions & 325 deletions

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/controller/MovieControllerImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public ResponseEntity<SuccessResponse<Movie>> createMovie(
163163

164164
SuccessResponse<Movie> response = SuccessResponse.<Movie>builder()
165165
.success(true)
166-
.message("Movie '" + request.getTitle() + "' created successfully")
166+
.message("Movie '" + request.title() + "' created successfully")
167167
.data(movie)
168168
.timestamp(Instant.now().toString())
169169
.build();
@@ -183,7 +183,7 @@ public ResponseEntity<SuccessResponse<BatchInsertResponse>> createMoviesBatch(
183183

184184
SuccessResponse<BatchInsertResponse> response = SuccessResponse.<BatchInsertResponse>builder()
185185
.success(true)
186-
.message("Successfully created " + result.getInsertedCount() + " movies")
186+
.message("Successfully created " + result.insertedCount() + " movies")
187187
.data(result)
188188
.timestamp(Instant.now().toString())
189189
.build();
@@ -236,8 +236,8 @@ public ResponseEntity<SuccessResponse<BatchUpdateResponse>> updateMoviesBatch(
236236

237237
SuccessResponse<BatchUpdateResponse> response = SuccessResponse.<BatchUpdateResponse>builder()
238238
.success(true)
239-
.message("Update operation completed. Matched " + result.getMatchedCount() +
240-
" documents, modified " + result.getModifiedCount() + " documents.")
239+
.message("Update operation completed. Matched " + result.matchedCount() +
240+
" documents, modified " + result.modifiedCount() + " documents.")
241241
.data(result)
242242
.timestamp(Instant.now().toString())
243243
.build();
@@ -302,7 +302,7 @@ public ResponseEntity<SuccessResponse<DeleteResponse>> deleteMoviesBatch(
302302

303303
SuccessResponse<DeleteResponse> response = SuccessResponse.<DeleteResponse>builder()
304304
.success(true)
305-
.message("Delete operation completed. Removed " + result.getDeletedCount() + " documents.")
305+
.message("Delete operation completed. Removed " + result.deletedCount() + " documents.")
306306
.data(result)
307307
.timestamp(Instant.now().toString())
308308
.build();
@@ -328,7 +328,7 @@ public ResponseEntity<SuccessResponse<List<MovieWithCommentsResult>>> getMoviesW
328328

329329
// Calculate total comments across all movies
330330
int totalComments = results.stream()
331-
.mapToInt(result -> result.getTotalComments() != null ? result.getTotalComments() : 0)
331+
.mapToInt(result -> result.totalComments() != null ? result.totalComments() : 0)
332332
.sum();
333333

334334
String message = movieId != null
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package com.mongodb.samplemflix.model.dto;
22

33
import java.util.Collection;
4-
import lombok.AllArgsConstructor;
5-
import lombok.Data;
6-
import lombok.NoArgsConstructor;
74
import org.bson.BsonValue;
85

96
/**
107
* Response DTO for batch insert operations.
118
*/
12-
@Data
13-
@NoArgsConstructor
14-
@AllArgsConstructor
15-
public class BatchInsertResponse {
16-
private int insertedCount;
17-
private Collection<BsonValue> insertedIds;
18-
}
9+
public record BatchInsertResponse (
10+
int insertedCount,
11+
Collection<BsonValue> insertedIds) {}
1912

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
package com.mongodb.samplemflix.model.dto;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
5-
import lombok.NoArgsConstructor;
6-
73
/**
84
* Response DTO for batch update operations.
95
*/
10-
@Data
11-
@NoArgsConstructor
12-
@AllArgsConstructor
13-
public class BatchUpdateResponse {
14-
private long matchedCount;
15-
private long modifiedCount;
16-
}
17-
6+
public record BatchUpdateResponse(
7+
long matchedCount,
8+
long modifiedCount) {}

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/model/dto/CreateMovieRequest.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,81 @@
22

33
import jakarta.validation.constraints.NotBlank;
44
import java.util.List;
5-
import lombok.AllArgsConstructor;
65
import lombok.Builder;
7-
import lombok.Data;
8-
import lombok.NoArgsConstructor;
96

107
/**
118
* Data Transfer Object for creating a new movie.
129
*
1310
* <p>This DTO is used for POST /api/movies requests.
1411
* It includes validation annotations to ensure required fields are present.
15-
* Only the title field is required; all other fields are optional.
12+
* Only the title field is required, all other fields are optional.
1613
*/
17-
@Data
1814
@Builder
19-
@NoArgsConstructor
20-
@AllArgsConstructor
21-
public class CreateMovieRequest {
15+
public record CreateMovieRequest (
2216

2317
/**
2418
* Movie title (required).
2519
* Must not be blank.
2620
*/
2721
@NotBlank(message = "Title is required")
28-
private String title;
22+
String title,
2923

3024
/**
3125
* Release year (optional).
3226
*/
33-
private Integer year;
27+
Integer year,
3428

3529
/**
3630
* Short plot summary (optional).
3731
*/
38-
private String plot;
32+
String plot,
3933

4034
/**
4135
* Full plot description (optional).
4236
*/
43-
private String fullplot;
37+
String fullplot,
4438

4539
/**
4640
* List of genres (optional).
4741
*/
48-
private List<String> genres;
42+
List<String> genres,
4943

5044
/**
5145
* List of directors (optional).
5246
*/
53-
private List<String> directors;
47+
List<String> directors,
5448

5549
/**
5650
* List of writers (optional).
5751
*/
58-
private List<String> writers;
52+
List<String> writers,
5953

6054
/**
6155
* List of cast members (optional).
6256
*/
63-
private List<String> cast;
57+
List<String> cast,
6458

6559
/**
6660
* List of countries (optional).
6761
*/
68-
private List<String> countries;
62+
List<String> countries,
6963

7064
/**
7165
* List of languages (optional).
7266
*/
73-
private List<String> languages;
67+
List<String> languages,
7468

7569
/**
7670
* Movie rating (optional).
7771
*/
78-
private String rated;
72+
String rated,
7973

8074
/**
8175
* Runtime in minutes (optional).
8276
*/
83-
private Integer runtime;
77+
Integer runtime,
8478

8579
/**
8680
* Poster image URL (optional).
8781
*/
88-
private String poster;
89-
}
82+
String poster) {}
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package com.mongodb.samplemflix.model.dto;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
5-
import lombok.NoArgsConstructor;
6-
73
/**
84
* Response DTO for delete operations.
95
*/
10-
@Data
11-
@NoArgsConstructor
12-
@AllArgsConstructor
13-
public class DeleteResponse {
14-
private long deletedCount;
15-
}
6+
public record DeleteResponse (
7+
long deletedCount) {}
168

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
package com.mongodb.samplemflix.model.dto;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4-
import lombok.AllArgsConstructor;
54
import lombok.Builder;
6-
import lombok.Data;
7-
import lombok.NoArgsConstructor;
85

96
/**
107
* DTO for director statistics aggregation result.
118
*
129
* <p>This class represents the result of the reportingByDirectors aggregation
1310
* which finds directors with the most movies and their statistics.
1411
*/
15-
@Data
16-
@Builder
17-
@NoArgsConstructor
18-
@AllArgsConstructor
1912
@JsonInclude(JsonInclude.Include.NON_NULL)
20-
public class DirectorStatisticsResult {
21-
13+
@Builder
14+
public record DirectorStatisticsResult (
2215
/**
2316
* Director name.
2417
*/
25-
private String director;
18+
String director,
2619

2720
/**
2821
* Number of movies directed by this director.
2922
*/
30-
private Integer movieCount;
23+
Integer movieCount,
3124

3225
/**
3326
* Average IMDB rating of this director's movies.
3427
*/
35-
private Double averageRating;
36-
}
28+
Double averageRating) {}
3729

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,60 @@
11
package com.mongodb.samplemflix.model.dto;
22

3-
import lombok.AllArgsConstructor;
43
import lombok.Builder;
5-
import lombok.Data;
6-
import lombok.NoArgsConstructor;
74

85
/**
96
* Data Transfer Object for movie search query parameters.
107
*
118
* <p>This DTO is used to parse and validate query parameters for GET /api/movies requests.
129
* It supports full-text search, filtering by genre/year/rating, sorting, and pagination.
1310
*/
14-
@Data
1511
@Builder
16-
@NoArgsConstructor
17-
@AllArgsConstructor
18-
public class MovieSearchQuery {
12+
public record MovieSearchQuery(
1913

2014
/**
2115
* Full-text search query.
2216
* Searches across plot, title, and fullplot fields using MongoDB text index.
2317
*/
24-
private String q;
18+
String q,
2519

2620
/**
2721
* Filter by genre (case-insensitive partial match).
2822
*/
29-
private String genre;
23+
String genre,
3024

3125
/**
3226
* Filter by exact year.
3327
*/
34-
private Integer year;
28+
Integer year,
3529

3630
/**
3731
* Minimum IMDB rating (inclusive).
3832
*/
39-
private Double minRating;
33+
Double minRating,
4034

4135
/**
4236
* Maximum IMDB rating (inclusive).
4337
*/
44-
private Double maxRating;
38+
Double maxRating,
4539

4640
/**
4741
* Number of results to return (default: 20, max: 100).
4842
*/
49-
private Integer limit;
43+
Integer limit,
5044

5145
/**
5246
* Number of results to skip for pagination (default: 0).
5347
*/
54-
private Integer skip;
48+
Integer skip,
5549

5650
/**
5751
* Field to sort by (e.g., "title", "year", "imdb.rating").
5852
* Default: "title"
5953
*/
60-
private String sortBy;
54+
String sortBy,
6155

6256
/**
6357
* Sort order: "asc" or "desc".
6458
* Default: "asc"
6559
*/
66-
private String sortOrder;
67-
}
60+
String sortOrder) {}

0 commit comments

Comments
 (0)