Last fix ProductList
This commit is contained in:
parent
134f7d6cef
commit
99b0e691c4
@ -17,9 +17,9 @@ namespace ElectronicsShopDataBaseImplement
|
||||
}
|
||||
public virtual DbSet<CategoryProduct> CategoryProducts { set; get; }
|
||||
public virtual DbSet<User> Users { set; get; }
|
||||
public virtual DbSet<Route> Routes { set; get; }
|
||||
public virtual DbSet<RoutePreserve> RoutePreserves { set; get; }
|
||||
public virtual DbSet<Preserve> Preserves { set; get; }
|
||||
public virtual DbSet<Role> Roles { set; get; }
|
||||
public virtual DbSet<Product> Products { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,15 @@ namespace ElectronicsShopDataBaseImplement.Implements
|
||||
{
|
||||
public CategoryProductViewModel? Delete(CategoryProductBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
using var context = new Database();
|
||||
var element = context.CategoryProducts.FirstOrDefault(rec => rec.ID == model.ID);
|
||||
if (element != null)
|
||||
{
|
||||
context.CategoryProducts.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public CategoryProductViewModel? GetElement(CategoryProductSearchModel model)
|
||||
@ -60,7 +68,15 @@ namespace ElectronicsShopDataBaseImplement.Implements
|
||||
|
||||
public CategoryProductViewModel? Update(CategoryProductBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
using var context = new Database();
|
||||
var component = context.CategoryProducts.FirstOrDefault(x => x.ID == model.ID);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,84 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.SearchModels;
|
||||
using ElectronicsShopContracts.StorageContracts;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataBaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Implements
|
||||
{
|
||||
public class ProductStorage : IProductStorage
|
||||
{
|
||||
public ProductViewModel? Delete(ProductBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var element = context.Products.FirstOrDefault(rec => rec.ID == model.ID);
|
||||
if (element != null)
|
||||
{
|
||||
context.Products.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProductViewModel? GetElement(ProductSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ProductName) && !model.ID.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Products.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.ProductName) && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ProductName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
|
||||
public List<ProductViewModel> GetFullList()
|
||||
{
|
||||
using var context = new Database();
|
||||
return context.Products.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public ProductViewModel? Insert(ProductBindingModel model)
|
||||
{
|
||||
var newComponent = Product.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
context.Products.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
|
||||
public ProductViewModel? Update(ProductBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var component = context.Products.FirstOrDefault(x => x.ID == model.ID);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.SearchModels;
|
||||
using ElectronicsShopContracts.StorageContracts;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataBaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Implements
|
||||
{
|
||||
public class RoleStorage: IRoleStorage
|
||||
{
|
||||
public RoleViewModel? Delete(RoleBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var element = context.Roles.FirstOrDefault(rec => rec.ID == model.ID);
|
||||
if (element != null)
|
||||
{
|
||||
context.Roles.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RoleViewModel? GetElement(RoleSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Roles.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<RoleViewModel> GetFilteredList(RoleSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Roles.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<RoleViewModel> GetFullList()
|
||||
{
|
||||
using var context = new Database();
|
||||
return context.Roles.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public RoleViewModel? Insert(RoleBindingModel model)
|
||||
{
|
||||
var newComponent = Role.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
context.Roles.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
public RoleViewModel? Update(RoleBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var component = context.Roles.FirstOrDefault(x => x.ID == model.ID);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.SearchModels;
|
||||
using ElectronicsShopContracts.StorageContracts;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataBaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Implements
|
||||
{
|
||||
public class UserStorage : IUserStorage
|
||||
{
|
||||
public UserViewModel? Delete(UserBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var element = context.Users.FirstOrDefault(rec => rec.ID == model.ID);
|
||||
if (element != null)
|
||||
{
|
||||
context.Users.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserViewModel? GetElement(UserSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Login) && !model.ID.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Users.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login ||
|
||||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<UserViewModel> GetFilteredList(UserSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Login))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Users.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<UserViewModel> GetFullList()
|
||||
{
|
||||
using var context = new Database();
|
||||
return context.Users.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public UserViewModel? Insert(UserBindingModel model)
|
||||
{
|
||||
var newComponent = User.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
context.Users.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
public UserViewModel? Update(UserBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var component = context.Users.FirstOrDefault(x => x.ID == model.ID);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -44,8 +44,7 @@ namespace ElectronicsShopDataBaseImplement.Models
|
||||
{
|
||||
return;
|
||||
}
|
||||
model.ID = ID;
|
||||
model.Name = Name;
|
||||
Name = model.Name;
|
||||
}
|
||||
|
||||
public CategoryProductViewModel GetViewModel => new()
|
||||
|
@ -0,0 +1,88 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataModels.Enums;
|
||||
using ElectronicsShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
[Required]
|
||||
public double Sum { get; set; }
|
||||
[Required]
|
||||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||||
[Required]
|
||||
public PaymeantOption PaymeantOption { get; set; } = PaymeantOption.Неизвестно;
|
||||
[Required]
|
||||
public DateTime DateCreate { get; set; }= DateTime.Now;
|
||||
[Required]
|
||||
public DateTime? DateImplemet { get; set; } = null;
|
||||
[Required]
|
||||
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
{
|
||||
ID = model.ID,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
PaymeantOption = model.PaymeantOption,
|
||||
DateCreate = model.DateCreate,
|
||||
//todo ProductList
|
||||
};
|
||||
}
|
||||
public static Order? Create(OrderViewModel model)
|
||||
{
|
||||
return new Order
|
||||
{
|
||||
ID = model.ID,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
PaymeantOption = model.PaymeantOption,
|
||||
DateCreate = model.DateCreate,
|
||||
//todo ProductList
|
||||
};
|
||||
}
|
||||
public void Update(OrderBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ID = model.ID;
|
||||
Sum = model.Sum;
|
||||
PaymeantOption = model.PaymeantOption;
|
||||
Status = model.Status;
|
||||
DateCreate = model.DateCreate;
|
||||
if (model.DateImplemet != null)
|
||||
{
|
||||
DateImplemet = model.DateImplemet;
|
||||
}
|
||||
//todo ProductList
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
Sum = Sum,
|
||||
PaymeantOption = PaymeantOption,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplemet=DateImplemet,
|
||||
ProductList = ProductList
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Models
|
||||
{
|
||||
public class Product : IProductModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
[Required]
|
||||
public string ProductName { get; set; }=string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
[Required]
|
||||
public int CategoryID { get; set; }
|
||||
|
||||
public static Product? Create(ProductBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Product()
|
||||
{
|
||||
ID = model.ID,
|
||||
ProductName = model.ProductName,
|
||||
Price = model.Price,
|
||||
Count = model.Count,
|
||||
CategoryID = model.CategoryID
|
||||
};
|
||||
}
|
||||
public static Product? Create(ProductViewModel model)
|
||||
{
|
||||
return new Product
|
||||
{
|
||||
ID = model.ID,
|
||||
ProductName = model.ProductName,
|
||||
Price = model.Price,
|
||||
Count = model.Count,
|
||||
CategoryID = model.CategoryID
|
||||
};
|
||||
}
|
||||
public void Update(ProductBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ProductName = model.ProductName;
|
||||
Price = model.Price;
|
||||
Count = model.Count;
|
||||
CategoryID = model.CategoryID;
|
||||
}
|
||||
|
||||
public ProductViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
ProductName = ProductName,
|
||||
Price = Price,
|
||||
Count = Count,
|
||||
CategoryID = CategoryID
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Models
|
||||
{
|
||||
public class Role : IRoleModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public static Role? Create(RoleBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Role()
|
||||
{
|
||||
ID = model.ID,
|
||||
Name = model.Name
|
||||
};
|
||||
}
|
||||
public static Role? Create(RoleViewModel model)
|
||||
{
|
||||
return new Role
|
||||
{
|
||||
ID = model.ID,
|
||||
Name = model.Name
|
||||
};
|
||||
}
|
||||
public void Update(RoleBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
}
|
||||
|
||||
public RoleViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
Name = Name
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
using ElectronicsShopContracts.BindingModels;
|
||||
using ElectronicsShopContracts.ViewModels;
|
||||
using ElectronicsShopDataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronicsShopDataBaseImplement.Models
|
||||
{
|
||||
public class User : IUserModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
[Required]
|
||||
public string FirstName { get; set; }=string.Empty;
|
||||
[Required]
|
||||
public string LastName {get; set; }=string.Empty;
|
||||
[Required]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string PhoneNumber { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string Login { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public int RoleID { get; set; }
|
||||
|
||||
public static User? Create(UserBindingModel model)
|
||||
{
|
||||
if(model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new User()
|
||||
{
|
||||
ID = model.ID,
|
||||
FirstName = model.FirstName,
|
||||
LastName = model.LastName,
|
||||
Password = model.Password,
|
||||
PhoneNumber = model.PhoneNumber,
|
||||
Login = model.Login,
|
||||
Email = model.Email,
|
||||
RoleID = model.RoleID
|
||||
};
|
||||
}
|
||||
public static User Create(UserViewModel model)
|
||||
{
|
||||
return new User
|
||||
{
|
||||
ID = model.ID,
|
||||
FirstName = model.FirstName,
|
||||
LastName = model.LastName,
|
||||
Password = model.Password,
|
||||
PhoneNumber = model.PhoneNumber,
|
||||
Login = model.Login,
|
||||
Email = model.Email,
|
||||
RoleID = model.RoleID
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(UserBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FirstName = model.FirstName;
|
||||
LastName = model.LastName;
|
||||
Password = model.Password;
|
||||
PhoneNumber = model.PhoneNumber;
|
||||
Login = model.Login;
|
||||
Email = model.Email;
|
||||
RoleID = model.RoleID;
|
||||
}
|
||||
public UserViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
FirstName = FirstName,
|
||||
LastName = LastName,
|
||||
Password = Password,
|
||||
PhoneNumber = PhoneNumber,
|
||||
Login = Login,
|
||||
Email = Email,
|
||||
RoleID = RoleID
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user