using AutoMapper; using AutoMapper.QueryableExtensions; using Microsoft.EntityFrameworkCore; using Server.Data; using Server.Models; using SharedModels.DataTransferObjects; namespace Server.Services; public class CountryManagementService : ICountryManagementService { private readonly ApplicationDbContext _dbContext; private readonly IMapper _mapper; public CountryManagementService(ApplicationDbContext dbContext, IMapper mapper) { _dbContext = dbContext; _mapper = mapper; } public async Task<(bool isSucceed, string message, CountryDto country)> AddCountry(CreateCountryDto createCountryDto) { var country = _mapper.Map(createCountryDto); await _dbContext.Countries.AddAsync(country); await _dbContext.SaveChangesAsync(); return (true, String.Empty, _mapper.Map(country)); } public async Task<(bool isSucceed, string message, IEnumerable countries)> GetCountries() { throw new NotImplementedException(); } public async Task<(bool isSucceed, string message, CountryDto country)> GetCountry(int id) { var dbCountry = await _dbContext.Countries.Where(c => c.Id == id) .ProjectTo(_mapper.ConfigurationProvider) .FirstOrDefaultAsync(); if (dbCountry == null) { return (false, $"Country doesn't exist", null)!; } return (true, "", dbCountry); } public async Task<(bool isSucceed, string message, CountryDto country)> UpdateCountry(UpdateCountryDto updateCountryDto) { var country = _mapper.Map(updateCountryDto); _dbContext.Entry(country).State = EntityState.Modified; try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!await IsCountryExists(updateCountryDto.Id)) { return (false, $"Country with id:{updateCountryDto.Id} doesn't exist", null)!; } throw; } var dbCountry = await _dbContext.Countries.FirstOrDefaultAsync(c => c.Id == country.Id); return (true, String.Empty, _mapper.Map(dbCountry)); } public async Task<(bool isSucceed, string message)> DeleteCountry(int id) { var dbCountry = await _dbContext.Countries.FirstOrDefaultAsync(c => c.Id == id); if (dbCountry == null) { return (false, $"Country with id:{id} doesn't exist"); } _dbContext.Countries.Remove(dbCountry); await _dbContext.SaveChangesAsync(); return (true, String.Empty); } public async Task IsCountryExists(int id) { return await _dbContext.Countries.AnyAsync(c => c.Id == id); } }