73 lines
2.1 KiB
C#
73 lines
2.1 KiB
C#
|
using Contracts.BindingModels;
|
|||
|
using Contracts.SearchModels;
|
|||
|
using Contracts.StorageContracts;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace DatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class ProductStorage : IProductStorage
|
|||
|
{
|
|||
|
public ProductBindingModel? Delete(ProductSearchModel model)
|
|||
|
{
|
|||
|
if (model.Id is null && model.Name is null && model.Price is null && model.Rate is null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
var context = new Database();
|
|||
|
var product = context.Products.FirstOrDefault(p =>
|
|||
|
(model.Id.HasValue && p.Id == model.Id) || (!string.IsNullOrEmpty(p.Name) && p.Name.Contains(model.Name)));
|
|||
|
|
|||
|
if (product is null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Remove(product);
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
return product.GetBindingModel();
|
|||
|
}
|
|||
|
|
|||
|
public ProductBindingModel? GetElement(ProductSearchModel model)
|
|||
|
{
|
|||
|
if (model.Id is null && string.IsNullOrWhiteSpace(model.Name))
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
var context = new Database();
|
|||
|
return context.Products
|
|||
|
.FirstOrDefault(prod => (model.Id.HasValue && prod.Id == model.Id)
|
|||
|
|| (!string.IsNullOrWhiteSpace(model.Name) && prod.Name.Contains(model.Name)))?.GetBindingModel();
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<ProductBindingModel> GetList(ProductSearchModel? model)
|
|||
|
{
|
|||
|
if (model.Id is null && string.IsNullOrWhiteSpace(model.Name) || model.RateFrom > model.RateTo)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
var context = new Database();
|
|||
|
return context.Products
|
|||
|
.Where(prod => (model.Id.HasValue && prod.Id == model.Id)
|
|||
|
|| (!string.IsNullOrWhiteSpace(model.Name) && prod.Name.Contains(model.Name))
|
|||
|
|| ((model.RateFrom.HasValue && model.RateTo.HasValue) && ((model.RateFrom < prod.Rate && prod.Rate < model.RateTo)))
|
|||
|
|| ((model.PriceFrom.HasValue && model.PriceTo.HasValue) && ((model.PriceFrom < prod.Price && prod.Price < model.PriceTo))))
|
|||
|
.Select(r => r.GetBindingModel()); ;
|
|||
|
}
|
|||
|
|
|||
|
public ProductBindingModel? Insert(ProductBindingModel model)
|
|||
|
{
|
|||
|
throw new NotImplementedException();
|
|||
|
}
|
|||
|
|
|||
|
public ProductBindingModel? Update(ProductBindingModel model)
|
|||
|
{
|
|||
|
throw new NotImplementedException();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|