diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java index 270fbdda..ec8ed3e6 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java @@ -4,6 +4,7 @@ import haw.teamagochi.backend.user.dataaccess.model.UserEntity; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.annotation.Nullable; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java index b118d7ca..7c3b6a80 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/PetRestSelfService.java @@ -3,6 +3,7 @@ import haw.teamagochi.backend.general.security.SecurityUtil; import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; import haw.teamagochi.backend.pet.logic.UcFindPet; +import haw.teamagochi.backend.pet.logic.UcFindPetType; import haw.teamagochi.backend.pet.logic.UcManagePet; import haw.teamagochi.backend.pet.service.rest.v1.mapper.PetMapper; import haw.teamagochi.backend.pet.service.rest.v1.model.PetDTO; @@ -11,6 +12,7 @@ import haw.teamagochi.backend.user.logic.UcManageUser; import io.quarkus.security.identity.SecurityIdentity; import jakarta.inject.Inject; +import jakarta.transaction.Transactional; import jakarta.ws.rs.GET; import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.POST; @@ -49,6 +51,9 @@ public class PetRestSelfService { @Inject protected UcManageUser ucManageUser; + @Inject + protected UcFindPetType ucFindPetType; + /** * Get all pets. * @@ -72,6 +77,7 @@ public List getAllPets() { @POST @Operation(summary = "Create a pet") @APIResponse(responseCode = "200") + @Transactional public PetDTO createPet(PetDTO dto) { String uuid = SecurityUtil.getExternalUserId(identity); UserEntity owner = ucFindUser.find(uuid); @@ -79,7 +85,7 @@ public PetDTO createPet(PetDTO dto) { ucManageUser.create(uuid); // create userId in database } dto.setOwnerId(uuid); - PetEntity entity = petMapper.mapTransferObjectToEntity(dto); + PetEntity entity = petMapper.mapTransferObjectToEntity(dto, ucFindUser, ucFindPetType); ucManagePet.create(entity); return petMapper.mapEntityToTransferObject(entity); } diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/mapper/PetMapper.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/mapper/PetMapper.java index fc7bb180..8cc172ea 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/mapper/PetMapper.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/service/rest/v1/mapper/PetMapper.java @@ -7,10 +7,12 @@ import haw.teamagochi.backend.user.dataaccess.model.UserEntity; import haw.teamagochi.backend.user.logic.UcFindUser; import java.util.List; +import org.mapstruct.AfterMapping; import org.mapstruct.Context; import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; import org.mapstruct.ObjectFactory; import org.mapstruct.factory.Mappers; @@ -40,10 +42,10 @@ public interface PetMapper { @Mapping(source = "state.cleanliness", target = "cleanliness") @Mapping(source = "state.fun", target = "fun") @Mapping(source = "state.xp", target = "xp") - PetEntity mapTransferObjectToEntity(PetDTO petDto); + PetEntity mapTransferObjectToEntity(PetDTO petDto, @Context UcFindUser ucFindUser, @Context UcFindPetType ucFindPetType); /** - * See {@link PetMapper#mapTransferObjectToEntity(PetDTO)}. + * See {@link PetMapper#mapTransferObjectToEntity(PetDTO, UcFindUser, UcFindPetType)}. */ List mapTransferObjectToEntity(List petDtos); @@ -61,13 +63,24 @@ public interface PetMapper { */ List mapEntityToTransferObject(List petEntities); - @ObjectFactory - default UserEntity lookup(String uuid, @Context UcFindUser ucFindUser) { - return ucFindUser.find(uuid); + @AfterMapping + default void findOwner(PetDTO dto, @MappingTarget PetEntity entity, @Context UcFindUser ucFindUser) { + if (dto.getOwnerId() == null || ucFindUser == null) return; + + UserEntity dbEntity = ucFindUser.find(dto.getOwnerId()); + if (dbEntity != null) { + entity.setOwner(dbEntity); + } } - @ObjectFactory - default PetTypeEntity lookup(Long id, @Context UcFindPetType ucFindPetType) { - return ucFindPetType.find(id); + @AfterMapping + default void findPetTyp( PetDTO dto,@MappingTarget PetEntity entity, @Context UcFindPetType ucFindPetTyp) { + if (dto.getType() == null || ucFindPetTyp == null) return; + + PetTypeEntity dbEntity = ucFindPetTyp.find(dto.getType()); + if (dbEntity != null) { + entity.setPetType(dbEntity); + } } + } diff --git a/web_backend/src/test/java/haw/teamagochi/backend/pet/service/mapper/PetMapperTest.java b/web_backend/src/test/java/haw/teamagochi/backend/pet/service/mapper/PetMapperTest.java index a91feb89..b41f5b23 100644 --- a/web_backend/src/test/java/haw/teamagochi/backend/pet/service/mapper/PetMapperTest.java +++ b/web_backend/src/test/java/haw/teamagochi/backend/pet/service/mapper/PetMapperTest.java @@ -4,6 +4,7 @@ import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import haw.teamagochi.backend.pet.logic.UcFindPetType; import haw.teamagochi.backend.pet.logic.UcManagePet; import haw.teamagochi.backend.pet.logic.UcManagePetType; import haw.teamagochi.backend.pet.service.rest.v1.mapper.PetMapper; @@ -11,6 +12,7 @@ import haw.teamagochi.backend.pet.service.rest.v1.model.PetStateDTO; import haw.teamagochi.backend.pet.service.rest.v1.model.PetTypeDTO; import haw.teamagochi.backend.user.dataaccess.model.UserEntity; +import haw.teamagochi.backend.user.logic.UcFindUser; import haw.teamagochi.backend.user.logic.UcManageUser; import io.quarkus.test.junit.QuarkusTest; import jakarta.inject.Inject; @@ -38,6 +40,12 @@ public class PetMapperTest { @Inject UcManagePetType ucManagePetType; + @Inject + UcFindUser ucFindUser; + + @Inject + UcFindPetType ucFindPetType; + PetMapper petMapper = PetMapper.MAPPER; private Map petEntities; @@ -114,7 +122,7 @@ void testMapTransferObjectToEntity() { petStateDto); // When - PetEntity entity = petMapper.mapTransferObjectToEntity(dto); + PetEntity entity = petMapper.mapTransferObjectToEntity(dto, ucFindUser, ucFindPetType); // Then assertEquals(dto.getId(), entity.getId()); @@ -131,4 +139,26 @@ void testMapTransferObjectToEntity() { assertEquals(dto.getState().getFun(), entity.getFun()); assertEquals(dto.getState().getXp(), entity.getXp()); } + @Test + public void testMapTransferObjectToEntity_JustWithNameAndType() { + // Given + PetEntity sourceEntity = petEntities.get("pet1"); + PetTypeDTO petTypeDto = + new PetTypeDTO(sourceEntity.getPetType().getId(), sourceEntity.getPetType().getName()); + PetDTO dto = new PetDTO( + null, + sourceEntity.getName(), + petTypeDto.getId(), + String.valueOf(owner.getExternalID()), + null, + null); + + // When + PetEntity entity = petMapper.mapTransferObjectToEntity(dto, ucFindUser, ucFindPetType); + + assertEquals(dto.getName(), entity.getName()); + assertEquals(dto.getType(), entity.getPetType().getId()); + assertEquals("Frog", entity.getPetType().getName()); + assertEquals(dto.getOwnerId(), String.valueOf(entity.getOwner().getExternalID())); + } }