From 62947e18cb88771ee78308f477dbb7f9985cb1a1 Mon Sep 17 00:00:00 2001 From: VanKHoiPham Date: Mon, 17 Jun 2024 18:04:43 +0200 Subject: [PATCH] create user if userId not found in db --- .../backend/device/logic/UcManageDeviceImpl.java | 7 ++++++- .../pet/service/rest/v1/PetRestSelfService.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/logic/UcManageDeviceImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/device/logic/UcManageDeviceImpl.java index 4f4978d8..71f80e77 100644 --- a/web_backend/src/main/java/haw/teamagochi/backend/device/logic/UcManageDeviceImpl.java +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/logic/UcManageDeviceImpl.java @@ -6,6 +6,7 @@ import haw.teamagochi.backend.device.logic.registrationmanager.RegistrationManager; import haw.teamagochi.backend.user.dataaccess.model.UserEntity; import haw.teamagochi.backend.user.logic.UcFindUser; +import haw.teamagochi.backend.user.logic.UcManageUser; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.transaction.Transactional; @@ -22,6 +23,9 @@ public class UcManageDeviceImpl implements UcManageDevice { @Inject UcFindUser ucFindUser; + @Inject + UcManageUser ucManageUser; + @Inject UcManageDevice ucManageDevice; @@ -81,7 +85,8 @@ public DeviceEntity registerDevice(String registrationCode, String deviceName, S UserEntity owner = ucFindUser.find(uuid); if (owner == null) { - throw new IllegalArgumentException("User does not exist"); + ucManageUser.create(uuid); // create userId in database + owner = ucFindUser.find(uuid); } DeviceEntity device = new DeviceEntity(deviceName, DeviceType.FROG); 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 48e01f1f..8b083410 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 @@ -6,6 +6,9 @@ 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; +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.security.identity.SecurityIdentity; import jakarta.inject.Inject; import jakarta.ws.rs.GET; @@ -38,6 +41,12 @@ public class PetRestSelfService { @Inject protected UcManagePet ucManagePet; + @Inject + protected UcFindUser ucFindUser; + + @Inject + protected UcManageUser ucManageUser; + /** * Get all pets. * @@ -63,6 +72,10 @@ public List getAllPets() { @APIResponse(responseCode = "200") 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); ucManagePet.create(entity);