still hacked, but now cleaning up the resource object, not the entity

This commit is contained in:
Michael Hoennig 2024-06-25 17:37:52 +02:00
parent 3566cb61b6
commit 680b67f162
2 changed files with 19 additions and 16 deletions

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.cleanup;
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.validated; import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.validated;
@RestController @RestController
@ -71,7 +72,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER);
final var saved = saveAndValidate(entityToSave); final var saved = validated(assetRepo.save(entityToSave));
final var uri = final var uri =
MvcUriComponentsBuilder.fromController(getClass()) MvcUriComponentsBuilder.fromController(getClass())
@ -126,8 +127,8 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
new HsHostingAssetEntityPatcher(em, current).apply(body); new HsHostingAssetEntityPatcher(em, current).apply(body);
final var saved = saveAndValidate(current); final var saved = validated(assetRepo.save(current));
final var mapped = mapper.map(saved, HsHostingAssetResource.class); final var mapped = mapper.map(saved, HsHostingAssetResource.class, ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
@ -145,11 +146,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
} }
}; };
HsHostingAssetEntity saveAndValidate(final HsHostingAssetEntity entity) { final BiConsumer<HsHostingAssetEntity, HsHostingAssetResource> ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
final var saved = assetRepo.save(entity); cleanup(entity, resource);
// FIXME: this is hacky, better remove the properties from the mapped resource object };
em.flush();
em.detach(saved); // validated(...) is going to remove writeOnly properties
return validated(saved);
}
} }

View File

@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.hosting.asset.validators;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.model.HsHostingAssetResource;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator; import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import net.hostsharing.hsadminng.errors.MultiValidationException; import net.hostsharing.hsadminng.errors.MultiValidationException;
@ -41,12 +42,7 @@ public class HsHostingAssetEntityValidatorRegistry {
public static List<String> doValidate(final HsHostingAssetEntity hostingAsset) { public static List<String> doValidate(final HsHostingAssetEntity hostingAsset) {
final var validator = HsHostingAssetEntityValidatorRegistry.forType(hostingAsset.getType()); final var validator = HsHostingAssetEntityValidatorRegistry.forType(hostingAsset.getType());
final var validated = validator.validate(hostingAsset); return validator.validate(hostingAsset);
//validator.cleanup()
hostingAsset.getConfig().remove("password"); // FIXME
hostingAsset.getConfig().remove("totpKey"); // FIXME
return validated;
} }
public static HsHostingAssetEntity validated(final HsHostingAssetEntity entityToSave) { public static HsHostingAssetEntity validated(final HsHostingAssetEntity entityToSave) {
@ -54,4 +50,14 @@ public class HsHostingAssetEntityValidatorRegistry {
return entityToSave; return entityToSave;
} }
public static void cleanup(final HsHostingAssetEntity entity, final HsHostingAssetResource resource) {
final var validator = HsHostingAssetEntityValidatorRegistry.forType(entity.getType());
// final var validated = validator.cleanup(hostingAsset);
//validator.cleanup()
final var config = new HashMap<>((Map<String, Object>) resource.getConfig());
config.remove("password"); // FIXME
config.remove("totpKey"); // FIXME
resource.setConfig(config);
}
} }