From b3c7e4a508d528ca824109074e1309ad0b91cc35 Mon Sep 17 00:00:00 2001 From: PedroVidalDev Date: Tue, 7 May 2024 21:24:30 -0300 Subject: [PATCH] feat: creating a relationship between consumer and permission --- .../dtos/consumer/ConsumerDataComplete.java | 6 ++++-- .../java/com/pedro/sphynx/domain/AccessService.java | 2 +- .../com/pedro/sphynx/domain/ConsumerService.java | 12 +++++++++++- .../sphynx/infrastructure/entities/Consumer.java | 10 +++++++--- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java b/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java index de352a3..78f1b6d 100644 --- a/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java +++ b/src/main/java/com/pedro/sphynx/application/dtos/consumer/ConsumerDataComplete.java @@ -1,10 +1,12 @@ package com.pedro.sphynx.application.dtos.consumer; +import com.pedro.sphynx.application.dtos.permission.PermissionDataComplete; import com.pedro.sphynx.infrastructure.entities.Consumer; +import com.pedro.sphynx.infrastructure.entities.Permission; -public record ConsumerDataComplete (Long id, String ra, String tag, int permission){ +public record ConsumerDataComplete (Long id, String ra, String tag, PermissionDataComplete permission){ public ConsumerDataComplete(Consumer consumer){ - this(consumer.getId(), consumer.getRa(), consumer.getTag(), consumer.getPermission()); + this(consumer.getId(), consumer.getRa(), consumer.getTag(), new PermissionDataComplete(consumer.getPermission())); } } diff --git a/src/main/java/com/pedro/sphynx/domain/AccessService.java b/src/main/java/com/pedro/sphynx/domain/AccessService.java index fbc5d8c..784d6d4 100644 --- a/src/main/java/com/pedro/sphynx/domain/AccessService.java +++ b/src/main/java/com/pedro/sphynx/domain/AccessService.java @@ -46,7 +46,7 @@ public AccessDataComplete validateCreation(AccessDataInput data){ } LocalDataComplete local = new LocalDataComplete(localRepository.findByMac(data.mac())); - if(local.permission().level() < consumer.permission()){ + if(local.permission().level() < consumer.permission().level()){ var access = new Access(null, consumerRepository.findByTag(data.tag()), localRepository.findByMac(data.mac()), false, LocalDateTime.now()); accessRepository.save(access); diff --git a/src/main/java/com/pedro/sphynx/domain/ConsumerService.java b/src/main/java/com/pedro/sphynx/domain/ConsumerService.java index 91b1827..1f48e1b 100644 --- a/src/main/java/com/pedro/sphynx/domain/ConsumerService.java +++ b/src/main/java/com/pedro/sphynx/domain/ConsumerService.java @@ -4,8 +4,10 @@ import com.pedro.sphynx.application.dtos.consumer.ConsumerDataEditInput; import com.pedro.sphynx.application.dtos.consumer.ConsumerDataInput; import com.pedro.sphynx.infrastructure.entities.Consumer; +import com.pedro.sphynx.infrastructure.entities.Permission; import com.pedro.sphynx.infrastructure.exceptions.Validation; import com.pedro.sphynx.infrastructure.repository.ConsumerRepository; +import com.pedro.sphynx.infrastructure.repository.PermissionRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -20,6 +22,9 @@ public class ConsumerService{ @Autowired private ConsumerRepository consumerRepository; + @Autowired + private PermissionRepository permissionRepository; + public ConsumerDataComplete createVerify(ConsumerDataInput data, String language){ ResourceBundle messages = defineMessagesLanguage(language); @@ -27,8 +32,13 @@ public ConsumerDataComplete createVerify(ConsumerDataInput data, String language throw new Validation(messages.getString("error.raAlreadyExists")); } + if(!permissionRepository.existsByLevel(data.permission())){ + throw new Validation(messages.getString("error.permissionNotExists")); + } + else{ - Consumer consumer = new Consumer(data); + Permission permission = permissionRepository.getReferenceByLevel(data.permission()); + Consumer consumer = new Consumer(data, permission); consumerRepository.save(consumer); return new ConsumerDataComplete(consumer); } diff --git a/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java b/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java index 988fedc..e7b506c 100644 --- a/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java +++ b/src/main/java/com/pedro/sphynx/infrastructure/entities/Consumer.java @@ -22,15 +22,19 @@ public class Consumer { private String ra; private String tag; - private int permission; + + @ManyToOne + @JoinColumn(name = "permission_id") + private Permission permission; + private LocalDateTime dtcreate; private LocalDateTime dtupdate; - public Consumer(ConsumerDataInput data){ + public Consumer(ConsumerDataInput data, Permission permission){ this.id = null; this.ra = data.ra(); this.tag = data.tag(); - this.permission = data.permission(); + this.permission = permission; this.dtcreate = LocalDateTime.now(); this.dtupdate = null; }