Skip to content

Commit

Permalink
[backend-application-default] better BaseMapper.java.hbs
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Aug 1, 2024
1 parent e5f564b commit 778a020
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ package {{coreImplementationPackage}}.mappers;
import org.mapstruct.Mapper;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

@Mapper
public interface BaseMapper {

default OffsetDateTime asOffsetDateTime(Instant value) {
return value == null ? null : OffsetDateTime.ofInstant(value, OffsetDateTime.now().getOffset());
default Instant asInstant(OffsetDateTime date) {
return date != null? date.toInstant() : null;
}

default OffsetDateTime asOffsetDateTime(Instant date) {
return date != null? OffsetDateTime.ofInstant(date, ZoneOffset.UTC) : null;
}

default LocalDateTime map(OffsetDateTime value) {
return value != null ? value.toLocalDateTime() : null;
}

default OffsetDateTime map(LocalDateTime value) {
return value != null ? OffsetDateTime.of(value, ZoneOffset.UTC) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
log.debug("[CRUD] Request to save {{entity.className}}: {}", input);
var {{entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{entity.instanceName}} = {{entity.instanceName}}Repository.save({{entity.instanceName}});
// TODO: you may need to reload the entity here to fetch relationships 'mapped by id'
// TODO: may need to reload the entity to fetch relationships 'mapped by id'
{{~> (partial '../withEvents')}}
return {{wrapWithMapper entity}};
{{~else if (isCrudMethod 'list' method=method entity=entity )}}
Expand Down

0 comments on commit 778a020

Please sign in to comment.