forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
149 lines
4.3 KiB
C#
149 lines
4.3 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using MagicCarpetDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetDatabase.Implementations;
|
|
|
|
public class AgencyStorageContract : IAgencyStorageContract
|
|
{
|
|
private readonly MagicCarpetDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public AgencyStorageContract(MagicCarpetDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.AddMaps(typeof(TourAgency));
|
|
cfg.AddMaps(typeof(Agency));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<AgencyDataModel> GetList()
|
|
{
|
|
try
|
|
{
|
|
return [.. _dbContext.Agencies.Select(x => _mapper.Map<AgencyDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public AgencyDataModel GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<AgencyDataModel>(GetAgencyById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void AddElement(AgencyDataModel agencyDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Agencies.Add(_mapper.Map<Agency>(agencyDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void UpdElement(AgencyDataModel agencyDataModel)
|
|
{
|
|
try
|
|
{
|
|
var element = GetAgencyById(agencyDataModel.Id) ?? throw new ElementNotFoundException(agencyDataModel.Id);
|
|
_dbContext.Agencies.Update(_mapper.Map(agencyDataModel, element));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetAgencyById(id) ?? throw new ElementNotFoundException(id);
|
|
_dbContext.Agencies.Remove(element);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
//второе требование
|
|
public bool CheckComponents(SaleDataModel saleDataModel)
|
|
{
|
|
using (var transaction = _dbContext.Database.BeginTransaction())
|
|
{
|
|
var componentTasks = saleDataModel.Tours
|
|
.AsParallel()
|
|
.Select(async saleComponent =>
|
|
{
|
|
var component = await _dbContext.Tours.FirstOrDefaultAsync(x => x.Id == saleComponent.TourId);
|
|
var agency = await _dbContext.Agencies.FirstOrDefaultAsync(x => x.TourType == component.TourType && x.Count >= saleComponent.Count);
|
|
if (agency == null)
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
if (agency.Count - saleComponent.Count == 0)
|
|
{
|
|
DelElement(agency.Id);
|
|
}
|
|
else
|
|
{
|
|
agency.Count -= saleComponent.Count;
|
|
_dbContext.Agencies.Update(agency);
|
|
}
|
|
|
|
return true;
|
|
})
|
|
.ToList();
|
|
|
|
Task.WaitAll(componentTasks.ToArray());
|
|
transaction.Commit();
|
|
_dbContext.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private Agency? GetAgencyById(string id) => _dbContext.Agencies.FirstOrDefault(x => x.Id == id);
|
|
}
|