| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 8 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 9 | | using WinSolutions.Sveta.Common; |
| | | 10 | | using System; |
| | | 11 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 12 | | |
| | | 13 | | namespace 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 |
| | 1 | 21 | | : base(authenticationService) |
| | 1 | 22 | | { |
| | 1 | 23 | | _db = db; |
| | 1 | 24 | | _logger = logger; |
| | 1 | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | public async Task<Country> GetCountry(long countryId) => await _db.refCountries.Where(e => !e.IsDeleted).FirstOr |
| | 0 | 28 | | public async Task<Country> GetCountry(string name) => await _db.refCountries.Where(e => !e.IsDeleted).FirstOrDef |
| | | 29 | | |
| | 0 | 30 | | public async Task<Country> FindCountry(long countryId) => await _db.refCountries.Where(x => !x.IsDeleted).FirstO |
| | | 31 | | |
| | 0 | 32 | | public async Task<Country> GetCountryByCode(int code) => await _db.refCountries |
| | 0 | 33 | | .Include(d => d.RecState) |
| | 0 | 34 | | .AsNoTracking() |
| | 0 | 35 | | .Where(e => !e.IsDeleted) |
| | 0 | 36 | | .FirstOrDefaultAsync(d => d.Code == code); |
| | | 37 | | |
| | 0 | 38 | | public async Task<Country> FindCountryByName(string name) => await _db.refCountries |
| | 0 | 39 | | .Include(d => d.RecState) |
| | 0 | 40 | | .Where(e => !e.IsDeleted) |
| | 0 | 41 | | .FirstOrDefaultAsync(d => d.Name.ToLower() == name.NormalizeName().ToLower()); |
| | | 42 | | |
| | 0 | 43 | | public async Task<List<Country>> GetCountries() => await _db.refCountries |
| | 0 | 44 | | .Include(d => d.RecState) |
| | 0 | 45 | | .AsNoTracking() |
| | 0 | 46 | | .Where(e => !e.IsDeleted) |
| | 0 | 47 | | .OrderBy(x => x.Name) |
| | 0 | 48 | | .ToListAsync(); |
| | | 49 | | |
| | | 50 | | public async Task<int> GetCountriesCount() |
| | 0 | 51 | | { |
| | 0 | 52 | | var count = (await GetCountries()).Count(); |
| | 0 | 53 | | return count; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task CreateCountry(Country countryIn) |
| | 0 | 57 | | { |
| | 0 | 58 | | if (countryIn.RecState != null) |
| | 0 | 59 | | _db.Entry(countryIn.RecState).State = countryIn.RecState.Id == 0 ? EntityState.Detached : EntityState.Un |
| | 0 | 60 | | await _db.refCountries.AddAsync(countryIn); |
| | 0 | 61 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task UpdateCountry(Country data) |
| | 0 | 65 | | { |
| | 0 | 66 | | if (!(await CountryExists(data.Id))) |
| | 0 | 67 | | { |
| | 0 | 68 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 69 | | } |
| | 0 | 70 | | _db.refCountries.Update(data); |
| | 0 | 71 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | public async Task DeleteCountry(long countryId) |
| | 0 | 75 | | { |
| | 0 | 76 | | Country rec = await _db.refCountries.FindAsync(countryId); |
| | 0 | 77 | | if (rec == null) |
| | 0 | 78 | | { |
| | 0 | 79 | | throw new KeyNotFoundException($"Record #{countryId} not found"); |
| | | 80 | | } |
| | 0 | 81 | | rec.IsDeleted = true; |
| | 0 | 82 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | public async Task<bool> CountryExists(long countryId) => await _db.refCountries.AsNoTracking().Where(e => !e.IsD |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | } |