Skip to content

Commit 71e6950

Browse files
Update RecordMapper.java
1 parent 69afe31 commit 71e6950

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/app/smartpot/api/Records/Mapper/RecordMapper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
import org.mapstruct.Mapping;
88
import 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")
1115
public 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

Comments
 (0)