77import org .mapstruct .Mapping ;
88import org .mapstruct .factory .Mappers ;
99
10+ import java .text .ParseException ;
11+ import java .text .SimpleDateFormat ;
12+ import java .util .Date ;
13+
1014@ Mapper (componentModel = "spring" )
1115public interface RecordMapper {
1216 RecordMapper INSTANCE = Mappers .getMapper (RecordMapper .class );
1317
1418 @ Mapping (source = "id" , target = "id" , qualifiedByName = "stringToObjectId" )
1519 @ Mapping (source = "crop" , target = "crop" , qualifiedByName = "stringToObjectId" )
20+ @ Mapping (source = "date" , target = "date" , qualifiedByName = "stringToDate" )
1621 History toEntity (RecordDTO recordDTO );
1722
1823 @ Mapping (source = "id" , target = "id" , qualifiedByName = "objectIdToString" )
@@ -28,4 +33,14 @@ default String objectIdToString(ObjectId objectId) {
2833 default ObjectId stringToObjectId (String id ) {
2934 return id != null ? new ObjectId (id ) : null ;
3035 }
36+
37+ @ org .mapstruct .Named ("stringToDate" )
38+ default Date stringToDate (String date ) {
39+ if (date == null ) return null ;
40+ try {
41+ return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ).parse (date );
42+ } catch (ParseException e ) {
43+ throw new RuntimeException ("Invalid date format: " + date );
44+ }
45+ }
3146}
0 commit comments