< Summary

Information
Class: FAU.Logica.Services.BancoService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/BancoService.cs
Line coverage
93%
Covered lines: 59
Uncovered lines: 4
Coverable lines: 63
Total lines: 150
Line coverage: 93.6%
Branch coverage
95%
Covered branches: 23
Total branches: 24
Branch coverage: 95.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetByIdAsync()100%11100%
GetByCodigoAsync()100%11100%
GetPagedAsync()83.33%66100%
GetAllAsync()100%11100%
CreateAsync()100%6688.23%
UpdateAsync()100%121287.5%
DeleteAsync()100%11100%

File(s)

/home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/BancoService.cs

#LineLine coverage
 1using FAU.DataAccess.Repositories;
 2using FAU.Entidades;
 3using FAU.Logica.DTOs;
 4
 5namespace FAU.Logica.Services;
 6
 7public class BancoService : IBancoService
 8{
 9    private readonly IBancoRepository _bancoRepository;
 10
 1711    public BancoService(IBancoRepository bancoRepository)
 12    {
 1713        _bancoRepository = bancoRepository;
 1714    }
 15
 16    public async Task<Banco?> GetByIdAsync(long id)
 17    {
 118        return await _bancoRepository.GetByIdAsync(id);
 119    }
 20
 21    public async Task<Banco?> GetByCodigoAsync(string codigo)
 22    {
 123        return await _bancoRepository.GetByCodigoAsync(codigo);
 124    }
 25
 26    public async Task<PagedResult<BancoDto>> GetPagedAsync(
 27        int page,
 28        int pageSize,
 29        string? searchQuery = null,
 30        bool? vigente = null)
 31    {
 32        // Validar parámetros de paginación
 333        if (page < 1) page = 1;
 234        if (pageSize < 1) pageSize = 10;
 335        if (pageSize > 100) pageSize = 100;
 36
 237        var (items, totalCount) = await _bancoRepository.GetPagedAsync(page, pageSize, searchQuery, vigente);
 38
 439        var bancosDto = items.Select(b => new BancoDto
 440        {
 441            Id = b.Id,
 442            Codigo = b.Codigo,
 443            Nombre = b.Nombre,
 444            Vigente = b.Vigente
 445        }).ToList();
 46
 247        return new PagedResult<BancoDto>
 248        {
 249            Items = bancosDto,
 250            Page = page,
 251            PageSize = pageSize,
 252            TotalCount = totalCount
 253        };
 254    }
 55
 56    public async Task<IEnumerable<Banco>> GetAllAsync()
 57    {
 158        return await _bancoRepository.GetAllAsync();
 159    }
 60
 61    public async Task<(bool Success, Banco? Banco, string? Error)> CreateAsync(
 62        string nombre,
 63        string codigo,
 64        bool vigente = true)
 65    {
 66        try
 67        {
 68            // Validaciones
 669            if (string.IsNullOrWhiteSpace(nombre))
 70            {
 171                return (false, null, "El nombre es requerido");
 72            }
 73
 574            if (string.IsNullOrWhiteSpace(codigo))
 75            {
 176                return (false, null, "El código es requerido");
 77            }
 78
 79            // Verificar si ya existe un banco con ese código
 480            if (await _bancoRepository.ExistsAsync(codigo))
 81            {
 182                return (false, null, "Ya existe un banco con ese código");
 83            }
 84
 385            var nuevoBanco = new Banco
 386            {
 387                Nombre = nombre.Trim(),
 388                Codigo = codigo.Trim().ToUpper(),
 389                Vigente = vigente
 390            };
 91
 392            var bancoCreado = await _bancoRepository.CreateAsync(nuevoBanco);
 393            return (true, bancoCreado, null);
 94        }
 095        catch (Exception ex)
 96        {
 097            return (false, null, $"Error al crear banco: {ex.Message}");
 98        }
 699    }
 100
 101    public async Task<(bool Success, Banco? Banco, string? Error)> UpdateAsync(
 102        long id,
 103        string? nombre = null,
 104        string? codigo = null,
 105        bool? vigente = null)
 106    {
 107        try
 108        {
 5109            var banco = await _bancoRepository.GetByIdAsync(id);
 5110            if (banco == null)
 111            {
 1112                return (false, null, "Banco no encontrado");
 113            }
 114
 115            // Actualizar solo los campos que se especifican
 4116            if (!string.IsNullOrWhiteSpace(nombre))
 117            {
 2118                banco.Nombre = nombre.Trim();
 119            }
 120
 4121            if (!string.IsNullOrWhiteSpace(codigo))
 122            {
 123                // Verificar si el nuevo código ya existe (si es diferente al actual)
 2124                if (banco.Codigo != codigo.Trim().ToUpper() && await _bancoRepository.ExistsAsync(codigo))
 125                {
 1126                    return (false, null, "Ya existe un banco con ese código");
 127                }
 1128                banco.Codigo = codigo.Trim().ToUpper();
 129            }
 130
 3131            if (vigente.HasValue)
 132            {
 2133                banco.Vigente = vigente.Value;
 134            }
 135
 3136            var bancoActualizado = await _bancoRepository.UpdateAsync(banco);
 3137            return (true, bancoActualizado, null);
 138        }
 0139        catch (Exception ex)
 140        {
 0141            return (false, null, $"Error al actualizar banco: {ex.Message}");
 142        }
 5143    }
 144
 145    public async Task DeleteAsync(long id)
 146    {
 1147        await _bancoRepository.DeleteAsync(id);
 1148    }
 149}
 150