Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
Backend fix pet mapper (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: ozfox <[email protected]>
Co-authored-by: Leo <[email protected]>
  • Loading branch information
3 people authored Jun 24, 2024
1 parent 7dac8e5 commit 95ff62f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -49,6 +51,9 @@ public class PetRestSelfService {
@Inject
protected UcManageUser ucManageUser;

@Inject
protected UcFindPetType ucFindPetType;

/**
* Get all pets.
*
Expand All @@ -72,14 +77,15 @@ public List<PetDTO> 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);
if (owner == null) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<PetEntity> mapTransferObjectToEntity(List<PetDTO> petDtos);

Expand All @@ -61,13 +63,24 @@ public interface PetMapper {
*/
List<PetDTO> mapEntityToTransferObject(List<PetEntity> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

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;
import haw.teamagochi.backend.pet.service.rest.v1.model.PetDTO;
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;
Expand Down Expand Up @@ -38,6 +40,12 @@ public class PetMapperTest {
@Inject
UcManagePetType ucManagePetType;

@Inject
UcFindUser ucFindUser;

@Inject
UcFindPetType ucFindPetType;

PetMapper petMapper = PetMapper.MAPPER;

private Map<String, PetEntity> petEntities;
Expand Down Expand Up @@ -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());
Expand All @@ -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()));
}
}

0 comments on commit 95ff62f

Please sign in to comment.