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

Commit

Permalink
Added ChangePet Functionality (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
VanKhoiPham authored Jul 1, 2024
2 parents 6a73aba + 191a2e0 commit d8795ce
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package haw.teamagochi.backend.device.logic;

import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;

public interface UcChangePet {

/**
*
* @param deviceId id of the device
* @param petId id of pet to be put on the device
* @return the Device Entity
*/
DeviceEntity changePet(long deviceId, long petId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package haw.teamagochi.backend.device.logic;

import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;
import haw.teamagochi.backend.device.dataaccess.repository.DeviceRepository;
import haw.teamagochi.backend.pet.dataaccess.model.PetEntity;
import haw.teamagochi.backend.pet.logic.UcFindPet;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.NotFoundException;

@ApplicationScoped
public class UcChangePetImpl implements UcChangePet{

@Inject
DeviceRepository deviceRepository;

@Inject
UcFindDevice ucFindDevice;
@Inject
UcFindPet findPet;

@Override
@Transactional
public DeviceEntity changePet(long deviceId, long petId) {
DeviceEntity device = ucFindDevice.find(deviceId);
PetEntity pet = findPet.find(petId);
if(device == null || pet == null){
return null;
}
device.setPet(pet);
//deviceRepository.persist(device); --> nicht da als @Transactional gekennzeichnet
return device;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package haw.teamagochi.backend.device.service.rest.v1;

import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;
import haw.teamagochi.backend.device.logic.UcChangePet;
import haw.teamagochi.backend.device.logic.UcFindDevice;
import haw.teamagochi.backend.device.logic.UcManageDevice;
import haw.teamagochi.backend.device.service.rest.v1.mapper.DeviceMapper;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class DeviceRestSelfService {

@Inject
protected UcManageDevice ucManageDevice;
@Inject
UcChangePet ucChangePet;

/**
* Get all devices.
Expand Down Expand Up @@ -101,4 +104,24 @@ public DeviceDTO registerDevice(
}
throw new NotFoundException();
}

@POST
@Path("/changepet/{deviceID}/{PetID}")
@Operation(summary = "Set Pet for the Device")
@APIResponse(responseCode = "200")
@APIResponse(responseCode = "404", description = "Not Found")
public DeviceDTO changePet(@PathParam("deviceID") long deviceId, @PathParam("PetID") long petId){
String uuid = SecurityUtil.getExternalUserId(identity);
DeviceEntity device = ucFindDevice.find(deviceId);
if(device != null
&& device.getOwner() != null
&& Objects.equals(device.getOwner().getExternalID().toString(), uuid)){//request authorised
device = ucChangePet.changePet(deviceId, petId);
if (device != null) {
return deviceMapper.mapEntityToTransferObject(device);
}//if
}//if
throw new NotFoundException();
}//method

}

0 comments on commit d8795ce

Please sign in to comment.