ru.foodtechlab.lib.auth.service.domain.role.usecases.CreateRoleUseCase Maven / Gradle / Ivy
package ru.foodtechlab.lib.auth.service.domain.role.usecases;
import com.rcore.domain.commons.usecase.AbstractCreateUseCase;
import com.rcore.domain.commons.usecase.UseCase;
import com.rcore.domain.commons.usecase.model.SingletonEntityOutputValues;
import com.rcore.domain.commons.validators.ValidationDomain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import ru.foodtechlab.lib.auth.service.domain.Domain;
import ru.foodtechlab.lib.auth.service.domain.role.entity.RoleEntity;
import ru.foodtechlab.lib.auth.service.domain.role.exception.RoleAlreadyExistException;
import ru.foodtechlab.lib.auth.service.domain.role.port.RoleIdGenerator;
import ru.foodtechlab.lib.auth.service.domain.role.port.RoleRepository;
import ru.foodtechlab.lib.auth.service.domain.roleAccess.entity.RoleAccessEntity;
import ru.foodtechlab.lib.auth.service.domain.roleAccess.port.RoleAccessRepository;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CreateRoleUseCase extends AbstractCreateUseCase, RoleRepository, CreateRoleUseCase.InputValues> {
private final RoleAccessRepository roleAccessRepository;
public CreateRoleUseCase(RoleRepository repository, RoleIdGenerator idGenerator, RoleAccessRepository roleAccessRepository) {
super(repository, idGenerator);
this.roleAccessRepository = roleAccessRepository;
}
@Override
public SingletonEntityOutputValues execute(InputValues inputValues) {
if (repository.findByCode(inputValues.getCode()).isPresent())
throw new RoleAlreadyExistException();
List accesses = null;
if (inputValues.getAccessIds() != null) {
accesses = inputValues.getAccessIds()
.stream()
.map(roleAccessRepository::findById)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}
RoleEntity roleEntity = new RoleEntity();
roleEntity.setId(idGenerator.generate());
roleEntity.setCode(inputValues.getCode());
roleEntity.setName(inputValues.getName());
roleEntity.setAccesses(accesses);
roleEntity.setRegistrationAllowed(inputValues.isRegistrationAllowed);
return SingletonEntityOutputValues.of(repository.save(roleEntity));
}
@ValidationDomain(domainName = Domain.ROLE)
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Data
public static class InputValues implements UseCase.InputValues {
private String name;
@NotBlank
private String code;
private List accessIds;
private boolean isRegistrationAllowed;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy