что-то намутила

This commit is contained in:
Whoisthatjulia 2024-04-28 19:31:02 +04:00
parent 8550d059f2
commit a2bcf9e35d
7 changed files with 417 additions and 38 deletions

View File

@ -15,7 +15,7 @@ namespace BankDatabaseImplement
Port=5432;
Database=BankFullNew;
Username=postgres;
Password=55256636a;");
Password=postgres;");
}
base.OnConfiguring(optionsBuilder);
}

View File

@ -1,12 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BankDatabaseImplement.Implements
{
internal class CostStorage
public class CostStorage : ICostStorage
{
private void CheckSearchModel(CostSearchModel model)
{
if (model == null)
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
if (!model.Id.HasValue && !model.EmployeeId.HasValue)
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
}
public CostViewModel? Delete(CostBindingModel model)
{
using var context = new BankDB();
var element = context.Costs.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Costs.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public CostViewModel? GetElement(CostSearchModel model)
{
CheckSearchModel(model);
using var context = new BankDB();
if (!model.Id.HasValue)
{
return null;
}
return context.Costs
.Include(cost => cost.Employee)
.Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<CostViewModel> GetFilteredList(CostSearchModel model)
{
CheckSearchModel(model);
using var context = new BankDB();
return context.Costs
.Include(cost => cost.Employee)
.Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase)
.Where(x => x.EmployeeId == model.EmployeeId || x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CostViewModel> GetFullList()
{
using var context = new BankDB();
return context.Costs
.Include(cost => cost.Employee)
.Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase)
.Select(x => x.GetViewModel)
.ToList();
}
public CostViewModel? Insert(CostBindingModel model)
{
var newCost = Cost.Create(model);
if (newCost == null)
{
return null;
}
using var context = new BankDB();
context.Costs.Add(newCost);
context.SaveChanges();
return newCost.GetViewModel;
}
public CostViewModel? Update(CostBindingModel model)
{
using var context = new BankDB();
var cost = context.Costs.FirstOrDefault(x => x.Id == model.Id);
if (cost == null)
{
return null;
}
cost.Update(model);
context.SaveChanges();
cost.UpdatePurchases(context, model);
context.SaveChanges();
return cost.GetViewModel;
}
}
}

View File

@ -1,12 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
namespace BankDatabaseImplement.Implements
{
internal class EmployeeStorage
public class EmployeeStorage : IEmployeeStorage
{
private void CheckSearchModel(EmployeeSearchModel model)
{
if (model == null)
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
if (!model.Id.HasValue && string.IsNullOrEmpty(model.PhoneNumber) && string.IsNullOrEmpty(model.Password))
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
if (!model.Id.HasValue && (string.IsNullOrEmpty(model.PhoneNumber) && !string.IsNullOrEmpty(model.Password)))
throw new ArgumentException("Для нахождения соответствующего пользователя вместе с паролем нужен логин");
}
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
{
CheckSearchModel(model);
using var context = new BankDB();
return context.Employees
.FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)))?.GetViewModel;
}
public EmployeeViewModel? Insert(EmployeeBindingModel model)
{
if (model == null)
{
return null;
}
var newEmployee = Employee.Create(model);
using var context = new BankDB();
context.Employees.Add(newEmployee);
context.SaveChanges();
return newEmployee.GetViewModel;
}
}
}

View File

@ -1,12 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BankDatabaseImplement.Implements
{
internal class OperationStorage
public class OperationStorage : IOperationStorage
{
private void CheckSearchModel(OperationSearchModel model)
{
if (model == null)
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
if (!model.Id.HasValue && !model.EmployeeId.HasValue && model.PurchasesIds == null)
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
}
public OperationViewModel? Delete(OperationBindingModel model)
{
using var context = new BankDB();
var element = context.Operations.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Operations.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OperationViewModel? GetElement(OperationSearchModel model)
{
CheckSearchModel(model);
if (!model.Id.HasValue)
{
return null;
}
using var context = new BankDB();
return context.Operations
.Include(x => x.Employee)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<OperationViewModel> GetFilteredList(OperationSearchModel model)
{
CheckSearchModel(model);
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
using var context = new BankDB();
var query = context.Operations.Include(x => x.Employee);
if (model.EmployeeId.HasValue)
{
return query
.Where(x => model.EmployeeId == x.EmployeeId)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.PurchasesIds != null)
return query
.Include(x => x.Purchases)!
.ThenInclude(x => x.Purchase)
.ThenInclude(x => x.Operations)
.Where(x => x.Purchases.Any(y => model.PurchasesIds.Contains(y.PurchaseId)))
.Select(x => x.GetViewModel)
.ToList();
return new();
}
public List<OperationViewModel> GetFullList()
{
using var context = new BankDB();
return context.Operations.Include(x => x.Employee)
.Select(x => x.GetViewModel)
.ToList();
}
public OperationViewModel? Insert(OperationBindingModel model)
{
var newOperation = Operation.Create(model);
if (newOperation == null)
{
return null;
}
using var context = new BankDB();
context.Operations.Add(newOperation);
context.SaveChanges();
return newOperation.GetViewModel;
}
public OperationViewModel? Update(OperationBindingModel model)
{
using var context = new BankDB();
var car = context.Operations.FirstOrDefault(x => x.Id == model.Id);
if (car == null)
{
return null;
}
car.Update(model);
context.SaveChanges();
return car.GetViewModel;
}
}
}

View File

@ -1,12 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankContracts.BindingModels;
using BankContracts.ViewModels;
using BankDataModels;
using System.ComponentModel.DataAnnotations;
using BankDataModels.ProxyModels;
namespace BankDatabaseImplement.Models
{
internal class Cost
public class Cost : ICostModel
{
[Required]
public int EmployeeId { get; private set; }
[Required]
public string NameOfCost { get; private set; } = string.Empty;
public int Id { get; private set; }
[Required]
public double Price { get; private set; }
private Dictionary<int, CostByPurchaseModel>? _cachedPurchases;
public Dictionary<int, CostByPurchaseModel> PurchasesModels => _cachedPurchases ??= Purchases.Select(x => (CostByPurchaseModel)x).ToDictionary(x => x.PurchaseId, x => x);
[Required]
public Employee? Employee { get; private set; }
[Required]
public List<CostByPurchase> Purchases { get; private set; } = new();
public static Cost Create(CostBindingModel model)
{
if (model == null)
{
return null;
}
return new Cost()
{
EmployeeId = model.EmployeeId,
NameOfCost = model.NameOfCost,
Price = model.Price,
//PurchasesModels = model.PurchasesModels,
Id = model.Id
};
}
public CostViewModel GetViewModel => new()
{
PhoneNumber = Employee?.PhoneNumber ?? string.Empty,
PurchaseModels = PurchasesModels,
EmployeeId = EmployeeId,
NameOfCost = NameOfCost,
Price = Price,
Id = Id
};
public void Update(CostBindingModel model)
{
NameOfCost = model.NameOfCost;
Price = model.Price;
}
/// <summary>
/// Привязка статей затрат к покупкам
/// </summary>
public void UpdatePurchases(BankDB context, CostBindingModel model)
{
var oldPurchases = context.CostByPurchases.Where(x => x.CostId == model.Id).ToDictionary(x => x.PurchaseId, x => x);
var newPurchases = model.PurchasesModels.ToDictionary(
x => x.Key,
x => new CostByPurchase()
{
CostId = model.Id,
PurchaseId = x.Key,
Count = x.Value.Count,
}
);
context.RemoveRange(oldPurchases.Where(x => !newPurchases.ContainsKey(x.Key)).Select(x => x.Value));
context.AddRange(newPurchases.Where(x => !oldPurchases.ContainsKey(x.Key)).Select(x => x.Value));
oldPurchases.Where(x => newPurchases.ContainsKey(x.Key))
.Select(x => x.Value).ToList()
.ForEach(x => x.Count = newPurchases[x.PurchaseId].Count);
context.SaveChanges();
_cachedPurchases = null;
}
}
}

View File

@ -1,12 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankContracts.BindingModels;
using BankContracts.ViewModels;
using BankDataModels;
using System.ComponentModel.DataAnnotations;
namespace BankDatabaseImplement.Models
{
internal class Employee
public class Employee : IEmployeeModel
{
public int Id { get; private set; }
[Required]
public string FirstName { get; private set; } = string.Empty;
[Required]
public string LastName { get; private set; } = string.Empty;
public string? MiddleName { get; private set; }
[Required]
public string Post { get; private set; } = string.Empty;
[Required]
public string PhoneNumber { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public List<Operation>? Operations { get; private set; }
[Required]
public List<Cost>? Costs { get; private set; }
public static Employee Create(EmployeeBindingModel model)
{
if (model == null)
{
return null;
}
return new Employee()
{
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName,
PhoneNumber = model.PhoneNumber,
Password = model.Password,
Id = model.Id,
Post = model.Post,
Email = model.Email,
};
}
public EmployeeViewModel GetViewModel => new()
{
FirstName = FirstName,
LastName = LastName,
MiddleName = MiddleName,
PhoneNumber = PhoneNumber,
Password = Password,
Id = Id,
Post = Post,
Email = Email,
};
}
}
}

View File

@ -1,12 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Implements;
using BankDataModels;
using BankDataModels.ProxyModels;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace BankDatabaseImplement.Models
{
internal class Operation
public class Operation : IOperationModel
{
[Required]
public int EmployeeId { get; private set; }
[Required]
public string Model { get; private set; } = string.Empty;
[Required]
public double Price { get; private set; }
[Required]
public string Mark { get; private set; } = string.Empty;
public int Id { get; private set; }
[Required]
public Employee? Employee { get; private set; }
[Required]
public List<OperationByPurchase> Purchases { get; private set; } = new();
public static Operation Create(OperationBindingModel model)
{
if (model == null)
{
return null;
}
return new Operation()
{
EmployeeId = model.EmployeeId,
Model = model.Model,
Price = model.Price,
Mark = model.Mark,
Id = model.Id,
};
}
public OperationViewModel GetViewModel => new()
{
EmployeePhoneNumber = Employee?.PhoneNumber ?? string.Empty,
EmployeeId = EmployeeId,
Model = Model,
Price = Price,
Mark = Mark,
Id = Id,
Purchases = Purchases.Select(x => x.Purchase?.GetViewModel2).ToList(),
};
public void Update(OperationBindingModel model)
{
Price = model.Price;
}
}
}