Skip to content

Commit

Permalink
feat: creating a relationship between consumer and permission
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroVidalDev committed May 8, 2024
1 parent f09e46f commit b3c7e4a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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()));
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/pedro/sphynx/domain/AccessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/pedro/sphynx/domain/ConsumerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,15 +22,23 @@ public class ConsumerService{
@Autowired
private ConsumerRepository consumerRepository;

@Autowired
private PermissionRepository permissionRepository;

public ConsumerDataComplete createVerify(ConsumerDataInput data, String language){
ResourceBundle messages = defineMessagesLanguage(language);

if(consumerRepository.existsByRa(data.ra())){
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit b3c7e4a

Please sign in to comment.