102 lines
6.1 KiB
C#
102 lines
6.1 KiB
C#
using SquirrelContract.Enums;
|
|
using SquirrelDatabase;
|
|
using SquirrelDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace SquirrelTests.Infrastructure;
|
|
|
|
internal static class SquirrelDbContextExtensions
|
|
{
|
|
public static Client InsertClientToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
|
|
{
|
|
var client = new Client() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = discountSize };
|
|
dbContext.Clients.Add(client);
|
|
dbContext.SaveChanges();
|
|
return client;
|
|
}
|
|
|
|
public static Post InsertPostToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Manager, 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 Cocktail InsertCocktailToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string cocktailName = "test", AlcoholType baseAlcohol = AlcoholType.Vodka, double price = 1)
|
|
{
|
|
var cocktail = new Cocktail() { Id = id ?? Guid.NewGuid().ToString(), CocktailName = cocktailName, BaseAlcohol = baseAlcohol, Price = price };
|
|
dbContext.Cocktails.Add(cocktail);
|
|
dbContext.SaveChanges();
|
|
return cocktail;
|
|
}
|
|
|
|
public static CocktailHistory InsertCocktailHistoryToDatabaseAndReturn(this SquirrelDbContext dbContext, string cocktailId, double price = 10, DateTime? changeDate = null)
|
|
{
|
|
var cocktailHistory = new CocktailHistory() { Id = Guid.NewGuid().ToString(), CocktailId = cocktailId, OldPrice = price, ChangeDate = changeDate ?? DateTime.UtcNow };
|
|
dbContext.CocktailHistories.Add(cocktailHistory);
|
|
dbContext.SaveChanges();
|
|
return cocktailHistory;
|
|
}
|
|
|
|
public static Salary InsertSalaryToDatabaseAndReturn(this SquirrelDbContext dbContext, string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
|
|
{
|
|
var salary = new Salary() { EmployeeId = employeeId, EmployeeSalary = employeeSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
|
|
dbContext.Salaries.Add(salary);
|
|
dbContext.SaveChanges();
|
|
return salary;
|
|
}
|
|
|
|
public static Sale InsertSaleToDatabaseAndReturn(this SquirrelDbContext dbContext, string employeeId, string? clientId = null, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.None, double discount = 0, bool isCancel = false, List<(string, int, double)>? cocktails = null)
|
|
{
|
|
var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleCocktails = [] };
|
|
if (cocktails is not null)
|
|
{
|
|
foreach (var elem in cocktails)
|
|
{
|
|
sale.SaleCocktails.Add(new SaleCocktail { CocktailId = elem.Item1, SaleId = sale.Id, Count = elem.Item2, Price = elem.Item3 });
|
|
}
|
|
}
|
|
dbContext.Sales.Add(sale);
|
|
dbContext.SaveChanges();
|
|
return sale;
|
|
}
|
|
|
|
public static Employee InsertEmployeeToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string fio = "test", string email = "abc@gmail.com", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
|
|
{
|
|
var employee = new Employee() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
|
|
dbContext.Employees.Add(employee);
|
|
dbContext.SaveChanges();
|
|
return employee;
|
|
}
|
|
|
|
public static Client? GetClientFromDatabase(this SquirrelDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Post? GetPostFromDatabaseByPostId(this SquirrelDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
|
|
|
public static Post[] GetPostsFromDatabaseByPostId(this SquirrelDbContext dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)];
|
|
|
|
public static Cocktail? GetCocktailFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Cocktails.FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Salary[] GetSalariesFromDatabaseByEmployeeId(this SquirrelDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.EmployeeId == id)];
|
|
|
|
public static Sale? GetSaleFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Sales.Include(x => x.SaleCocktails).FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Sale[] GetSalesByClientId(this SquirrelDbContext dbContext, string? clientId) => [.. dbContext.Sales.Include(x => x.SaleCocktails).Where(x => x.ClientId == clientId)];
|
|
|
|
public static Employee? GetEmployeeFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Employees.FirstOrDefault(x => x.Id == id);
|
|
|
|
public static void RemoveClientsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
|
|
|
public static void RemovePostsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
|
|
|
public static void RemoveCocktailsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Cocktails\" CASCADE;");
|
|
|
|
public static void RemoveSalariesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
|
|
|
public static void RemoveSalesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
|
|
|
|
public static void RemoveEmployeesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
|
|
|
private static void ExecuteSqlRaw(this SquirrelDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
|
} |