< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.CountryService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/CountryService.cs
Covered lines:5
Uncovered lines:44
Coverable lines:49
Total lines:88
Line coverage:10.2% (5 of 49)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)

Metrics

MethodLine coverage Branch coverage
.ctor(...)100%100%
GetCountry()0%100%
GetCountry()0%100%
FindCountry()0%100%
GetCountryByCode()0%100%
FindCountryByName()0%100%
GetCountries()0%100%
GetCountriesCount()0%100%
CreateCountry()0%0%
UpdateCountry()0%0%
DeleteCountry()0%0%
CountryExists()0%100%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/CountryService.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using System.Threading.Tasks;
 4using Microsoft.EntityFrameworkCore;
 5using Microsoft.Extensions.Logging;
 6using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 7using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 8using WinSolutions.Sveta.Server.Services.Interfaces;
 9using WinSolutions.Sveta.Common;
 10using System;
 11using WinSolutions.Sveta.Common.Extensions;
 12
 13namespace WinSolutions.Sveta.Server.Services.Implements
 14{
 15    public class CountryService : SvetaServiceBase, ICountryService
 16    {
 17        private readonly SvetaDbContext _db;
 18        private readonly ILogger<CountryService> _logger;
 19
 20        public CountryService(SvetaDbContext db, ILogger<CountryService> logger, IAuthenticationService authenticationSe
 121            : base(authenticationService)
 122        {
 123            _db = db;
 124            _logger = logger;
 125        }
 26
 027        public async Task<Country> GetCountry(long countryId) => await _db.refCountries.Where(e => !e.IsDeleted).FirstOr
 028        public async Task<Country> GetCountry(string name) => await _db.refCountries.Where(e => !e.IsDeleted).FirstOrDef
 29
 030        public async Task<Country> FindCountry(long countryId) => await _db.refCountries.Where(x => !x.IsDeleted).FirstO
 31
 032        public async Task<Country> GetCountryByCode(int code) => await _db.refCountries
 033            .Include(d => d.RecState)
 034            .AsNoTracking()
 035            .Where(e => !e.IsDeleted)
 036            .FirstOrDefaultAsync(d => d.Code == code);
 37
 038        public async Task<Country> FindCountryByName(string name) => await _db.refCountries
 039            .Include(d => d.RecState)
 040            .Where(e => !e.IsDeleted)
 041            .FirstOrDefaultAsync(d => d.Name.ToLower() == name.NormalizeName().ToLower());
 42
 043        public async Task<List<Country>> GetCountries() => await _db.refCountries
 044            .Include(d => d.RecState)
 045            .AsNoTracking()
 046            .Where(e => !e.IsDeleted)
 047            .OrderBy(x => x.Name)
 048            .ToListAsync();
 49
 50        public async Task<int> GetCountriesCount()
 051        {
 052            var count = (await GetCountries()).Count();
 053            return count;
 054        }
 55
 56        public async Task CreateCountry(Country countryIn)
 057        {
 058            if (countryIn.RecState != null)
 059                _db.Entry(countryIn.RecState).State = countryIn.RecState.Id == 0 ? EntityState.Detached : EntityState.Un
 060            await _db.refCountries.AddAsync(countryIn);
 061            await _db.SaveChangesAsync(CurrentUserId);
 062        }
 63
 64        public async Task UpdateCountry(Country data)
 065        {
 066            if (!(await CountryExists(data.Id)))
 067            {
 068                throw new ArgumentException($"Record #{data.Id} not found");
 69            }
 070            _db.refCountries.Update(data);
 071            await _db.SaveChangesAsync(CurrentUserId);
 072        }
 73
 74        public async Task DeleteCountry(long countryId)
 075        {
 076            Country rec = await _db.refCountries.FindAsync(countryId);
 077            if (rec == null)
 078            {
 079                throw new KeyNotFoundException($"Record #{countryId} not found");
 80            }
 081            rec.IsDeleted = true;
 082            await _db.SaveChangesAsync(CurrentUserId);
 083        }
 84
 085        public async Task<bool> CountryExists(long countryId) => await _db.refCountries.AsNoTracking().Where(e => !e.IsD
 86    }
 87
 88}