File tree Expand file tree Collapse file tree
mflix/server/java-spring/src
main/java/com/mongodb/samplemflix
test/java/com/mongodb/samplemflix/controller Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99import org .springframework .context .annotation .Bean ;
1010import org .springframework .context .annotation .Configuration ;
1111import org .springframework .data .mongodb .config .AbstractMongoClientConfiguration ;
12+ import org .springframework .data .mongodb .core .convert .MongoCustomConversions ;
1213import org .springframework .data .mongodb .repository .config .EnableMongoRepositories ;
1314import org .springframework .lang .NonNull ;
1415
@@ -119,4 +120,9 @@ public MongoDatabase mongoDatabase() {
119120
120121 return client .getDatabase (databaseName );
121122 }
123+
124+ @ Bean
125+ public MongoCustomConversions customConversions () {
126+ return MongoCustomConversions .create (MongoCustomConversions .MongoConverterConfigurationAdapter ::useNativeDriverJavaTimeCodecs );
127+ }
122128}
Original file line number Diff line number Diff line change 11package com .mongodb .samplemflix .model ;
22
33import com .fasterxml .jackson .annotation .JsonProperty ;
4- import java .util .Date ;
4+ import java .time .Instant ;
5+ import java .time .LocalDate ;
56import java .util .List ;
67import lombok .AccessLevel ;
78import lombok .AllArgsConstructor ;
@@ -111,9 +112,14 @@ private Fields() {
111112 private String fullplot ;
112113
113114 /**
114- * Release date.
115+ * Release date as a calendar date (no time-of-day or time zone).
116+ *
117+ * <p>A movie release date is a "date only" concept (e.g. "1999-03-31"), not a specific
118+ * moment in time. We use {@link LocalDate} because it represents exactly that: a date
119+ * without time-of-day or time zone information. Spring Data MongoDB maps this via the
120+ * Jsr310 {@code LocalDateCodec}.
115121 */
116- private Date released ;
122+ private LocalDate released ;
117123
118124 /**
119125 * Runtime in minutes.
@@ -270,9 +276,12 @@ public static class Tomatoes {
270276 private String production ;
271277
272278 /**
273- * Last updated date.
279+ * Timestamp of the last update to Tomatoes ratings.
280+ *
281+ * <p>Stored as BSON DateTime in MongoDB. Uses {@link Instant} for an immutable,
282+ * UTC-only representation of this point-in-time event.
274283 */
275- private Date lastUpdated ;
284+ private Instant lastUpdated ;
276285
277286 /**
278287 * Nested class for viewer ratings.
Original file line number Diff line number Diff line change 11package com .mongodb .samplemflix .model .dto ;
22
33import com .fasterxml .jackson .annotation .JsonInclude ;
4- import java .util . Date ;
4+ import java .time . Instant ;
55import java .util .List ;
66import lombok .Builder ;
77
@@ -61,9 +61,12 @@ public record MovieWithCommentsResult (
6161 Integer totalComments ,
6262
6363 /**
64- * Date of the most recent comment.
64+ * Timestamp of the most recent comment as a UTC instant.
65+ *
66+ * <p>Uses {@link Instant} for an immutable, unambiguous UTC representation.
67+ * BSON DateTime values are converted via {@code Date.toInstant()}.
6568 */
66- Date mostRecentCommentDate ) {
69+ Instant mostRecentCommentDate ) {
6770
6871 /**
6972 * Nested record for comment information.
@@ -91,8 +94,11 @@ public record CommentInfo (
9194 String text ,
9295
9396 /**
94- * Comment date.
97+ * Comment timestamp as a UTC instant.
98+ *
99+ * <p>Stored as BSON DateTime in MongoDB. Uses {@link Instant} for immutability
100+ * and unambiguous UTC semantics.
95101 */
96- Date date ) {}
102+ Instant date ) {}
97103}
98104
Original file line number Diff line number Diff line change @@ -574,7 +574,7 @@ private MovieWithCommentsResult mapToMovieWithCommentsResult(Document doc) {
574574 .name (commentDoc .getString ("name" ))
575575 .email (commentDoc .getString ("email" ))
576576 .text (commentDoc .getString ("text" ))
577- .date (commentDoc .getDate ("date" ))
577+ .date (commentDoc .get ( "date" ) != null ? commentDoc . getDate ("date" ). toInstant () : null )
578578 .build ())
579579 .collect (Collectors .toList ());
580580 }
@@ -598,7 +598,7 @@ private MovieWithCommentsResult mapToMovieWithCommentsResult(Document doc) {
598598 .imdbRating (imdbRating )
599599 .recentComments (recentComments )
600600 .totalComments (doc .getInteger ("totalComments" ))
601- .mostRecentCommentDate (doc .getDate ("mostRecentCommentDate" ))
601+ .mostRecentCommentDate (doc .getDate ("mostRecentCommentDate" ) != null ? doc . getDate ( "mostRecentCommentDate" ). toInstant () : null )
602602 .build ();
603603 }
604604
Original file line number Diff line number Diff line change 2222import com .mongodb .samplemflix .model .dto .UpdateMovieRequest ;
2323import com .mongodb .samplemflix .model .dto .VectorSearchResult ;
2424import com .mongodb .samplemflix .service .MovieService ;
25+ import java .time .Instant ;
2526import java .util .Arrays ;
26- import java .util .Date ;
2727import java .util .HashMap ;
2828import java .util .List ;
2929import java .util .Map ;
@@ -343,7 +343,7 @@ void testGetMoviesWithMostComments_Success() throws Exception {
343343 .name ("John Doe" )
344344 .email ("john@example.com" )
345345 .text ("Great movie!" )
346- .date (new Date ())
346+ .date (Instant . now ())
347347 .build ();
348348
349349 MovieWithCommentsResult result = MovieWithCommentsResult .builder ()
@@ -356,7 +356,7 @@ void testGetMoviesWithMostComments_Success() throws Exception {
356356 .imdbRating (8.5 )
357357 .recentComments (Arrays .asList (comment ))
358358 .totalComments (5 )
359- .mostRecentCommentDate (new Date ())
359+ .mostRecentCommentDate (Instant . now ())
360360 .build ();
361361
362362 when (movieService .getMoviesWithMostRecentComments (anyInt (), isNull ())).thenReturn (Arrays .asList (result ));
You can’t perform that action at this time.
0 commit comments