135 lines
4.1 KiB
C#
135 lines
4.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using OnlineShopContracts.BindingModels;
|
|
using OnlineShopContracts.ViewModels;
|
|
using OnlineShopDatabase.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace OnlineShopDatabase.Storages
|
|
{
|
|
public class OrderStorage
|
|
{
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
using var context = new OnlineShopDatabase();
|
|
return context.Orders.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderBindingModel model)
|
|
{
|
|
using var context = new OnlineShopDatabase();
|
|
//либо поиск по ФИО заказчика
|
|
if (!string.IsNullOrEmpty(model.FIO))
|
|
{
|
|
return context.Orders
|
|
.Where(x => x.FIO == model.FIO)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
//либо по статусу заказа
|
|
else if (!string.IsNullOrEmpty(model.Status))
|
|
{
|
|
return context.Orders
|
|
.Where(x => x.Status == model.Status)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
//либо по стоимости
|
|
else if (model.Sum.HasValue)
|
|
{
|
|
return context.Orders
|
|
.Where(x => x.Sum == model.Sum)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
return new();
|
|
}
|
|
|
|
//получение по id заказа
|
|
public OrderViewModel? GetElement(OrderBindingModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new OnlineShopDatabase();
|
|
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public bool Insert(OrderBindingModel model)
|
|
{
|
|
var newOrder = Order.Create(model);
|
|
|
|
if (newOrder == null)
|
|
{
|
|
return false;
|
|
}
|
|
using var context = new OnlineShopDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
context.Orders.Add(newOrder);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public bool Update(OrderBindingModel model)
|
|
{
|
|
using var context = new OnlineShopDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
throw new Exception("Заказ не найден");
|
|
}
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool Delete(OrderBindingModel model)
|
|
{
|
|
using var context = new OnlineShopDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
throw new Exception("Заказ не найден");
|
|
}
|
|
context.Orders.Remove(order);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|