Files
ProektstuZhenechka/TheCatHasPawsProject/CatHasPawsTests/Infrastructure/CatHasPawsDbContextExtensions.cs

114 lines
6.9 KiB
C#

using CatHasPawsContratcs.Enums;
using CatHasPawsDatabase;
using CatHasPawsDatabase.Models;
using Microsoft.EntityFrameworkCore;
namespace CatHasPawsTests.Infrastructure;
internal static class CatHasPawsDbContextExtensions
{
public static Buyer InsertBuyerToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string? id = null, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
{
var buyer = new Buyer() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = discountSize };
dbContext.Buyers.Add(buyer);
dbContext.SaveChanges();
return buyer;
}
public static Manufacturer InsertManufacturerToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string? id = null, string manufacturerName = "test", string prevManufacturerName = "prev", string prevPrevManufacturerName = "prevPrev")
{
var manufacturer = new Manufacturer() { Id = id ?? Guid.NewGuid().ToString(), ManufacturerName = manufacturerName, PrevManufacturerName = prevManufacturerName, PrevPrevManufacturerName = prevPrevManufacturerName };
dbContext.Manufacturers.Add(manufacturer);
dbContext.SaveChanges();
return manufacturer;
}
public static Post InsertPostToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Assistant, double salary = 10, bool isActual = true, DateTime? changeDate = null)
{
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
dbContext.Posts.Add(post);
dbContext.SaveChanges();
return post;
}
public static Product InsertProductToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string manufacturerId, string? id = null, string productName = "test", ProductType productType = ProductType.Feed, double price = 1, bool isDeleted = false)
{
var product = new Product() { Id = id ?? Guid.NewGuid().ToString(), ManufacturerId = manufacturerId, ProductName = productName, ProductType = productType, Price = price, IsDeleted = isDeleted };
dbContext.Products.Add(product);
dbContext.SaveChanges();
return product;
}
public static ProductHistory InsertProductHistoryToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string productId, double price = 10, DateTime? changeDate = null)
{
var productHistory = new ProductHistory() { Id = Guid.NewGuid().ToString(), ProductId = productId, OldPrice = price, ChangeDate = changeDate ?? DateTime.UtcNow };
dbContext.ProductHistories.Add(productHistory);
dbContext.SaveChanges();
return productHistory;
}
public static Salary InsertSalaryToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string workerId, double workerSalary = 1, DateTime? salaryDate = null)
{
var salary = new Salary() { WorkerId = workerId, WorkerSalary = workerSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
dbContext.Salaries.Add(salary);
dbContext.SaveChanges();
return salary;
}
public static Sale InsertSaleToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string workerId, string? buyerId = null, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.OnSale, double discount = 0, bool isCancel = false, List<(string, int, double)>? products = null)
{
var sale = new Sale() { WorkerId = workerId, BuyerId = buyerId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleProducts = [] };
if (products is not null)
{
foreach (var elem in products)
{
sale.SaleProducts.Add(new SaleProduct { ProductId = elem.Item1, SaleId = sale.Id, Count = elem.Item2, Price = elem.Item3 });
}
}
dbContext.Sales.Add(sale);
dbContext.SaveChanges();
return sale;
}
public static Worker InsertWorkerToDatabaseAndReturn(this CatHasPawsDbContext dbContext, string? id = null, string fio = "test", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
{
var worker = new Worker() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
dbContext.Workers.Add(worker);
dbContext.SaveChanges();
return worker;
}
public static Buyer? GetBuyerFromDatabase(this CatHasPawsDbContext dbContext, string id) => dbContext.Buyers.FirstOrDefault(x => x.Id == id);
public static Manufacturer? GetManufacturerFromDatabase(this CatHasPawsDbContext dbContext, string id) => dbContext.Manufacturers.FirstOrDefault(x => x.Id == id);
public static Post? GetPostFromDatabaseByPostId(this CatHasPawsDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
public static Post[] GetPostsFromDatabaseByPostId(this CatHasPawsDbContext dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)];
public static Product? GetProductFromDatabaseById(this CatHasPawsDbContext dbContext, string id) => dbContext.Products.FirstOrDefault(x => x.Id == id);
public static Salary[] GetSalariesFromDatabaseByWorkerId(this CatHasPawsDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.WorkerId == id)];
public static Sale? GetSaleFromDatabaseById(this CatHasPawsDbContext dbContext, string id) => dbContext.Sales.Include(x => x.SaleProducts).FirstOrDefault(x => x.Id == id);
public static Sale[] GetSalesByBuyerId(this CatHasPawsDbContext dbContext, string? buyerId) => [.. dbContext.Sales.Include(x => x.SaleProducts).Where(x => x.BuyerId == buyerId)];
public static Worker? GetWorkerFromDatabaseById(this CatHasPawsDbContext dbContext, string id) => dbContext.Workers.FirstOrDefault(x => x.Id == id);
public static void RemoveBuyersFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Buyers\" CASCADE;");
public static void RemoveManufacturersFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Manufacturers\" CASCADE;");
public static void RemovePostsFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
public static void RemoveProductsFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE;");
public static void RemoveSalariesFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
public static void RemoveSalesFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
public static void RemoveWorkersFromDatabase(this CatHasPawsDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
private static void ExecuteSqlRaw(this CatHasPawsDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
}