112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
|
using Contracts.BindingModels;
|
|||
|
using Contracts.SearchModels;
|
|||
|
using Contracts.StorageContracts;
|
|||
|
using Contracts.ViewModels;
|
|||
|
using DatabaseImplements.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace DatabaseImplements.Implements
|
|||
|
{
|
|||
|
public class DeliveryStorage : IDeliveryStorage
|
|||
|
{
|
|||
|
public DeliveryViewModel? Delete(DeliveryBindingModel model)
|
|||
|
{
|
|||
|
using var context = new DeliveryDataBase();
|
|||
|
var element = context.Deliverys.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Deliverys.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public DeliveryViewModel? GetElement(DeliverySearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new DeliveryDataBase();
|
|||
|
return context.Deliverys.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public List<DeliveryViewModel> GetFilteredList(DeliverySearchModel model)
|
|||
|
{
|
|||
|
using var context = new DeliveryDataBase();
|
|||
|
return context.Deliverys
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<DeliveryViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new DeliveryDataBase();
|
|||
|
return context.Deliverys
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public DeliveryViewModel? Insert(DeliveryBindingModel model)
|
|||
|
{
|
|||
|
using var context = new DeliveryDataBase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var newDl = Delivery.Create(model);
|
|||
|
if (newDl == null)
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Deliverys.Add(newDl);
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
context.Database.CommitTransaction();
|
|||
|
|
|||
|
return newDl.GetViewModel;
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public DeliveryViewModel? Update(DeliveryBindingModel model)
|
|||
|
{
|
|||
|
using var context = new DeliveryDataBase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var dl = context.Deliverys.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (dl == null)
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
return null;
|
|||
|
}
|
|||
|
dl.Update(model);
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
context.Database.CommitTransaction();
|
|||
|
|
|||
|
return dl.GetViewModel;
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|