forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
143 lines
8.7 KiB
C#
143 lines
8.7 KiB
C#
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Infrastructure.PostConfigurations;
|
|
using MagicCarpetDatabase;
|
|
using MagicCarpetDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.Infrastructure;
|
|
|
|
internal static class MagicCarpetDbContextExtensions
|
|
{
|
|
public static Client InsertClientToDatabaseAndReturn(this MagicCarpetDbContext 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 MagicCarpetDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.TravelAgent, PostConfiguration? config = null, bool isActual = true, DateTime? changeDate = null)
|
|
{
|
|
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Configuration = config ?? new PostConfiguration() { Rate = 100 }, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
|
|
dbContext.Posts.Add(post);
|
|
dbContext.SaveChanges();
|
|
return post;
|
|
}
|
|
|
|
public static Tour InsertTourToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string tourName = "test", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1)
|
|
{
|
|
var tour = new Tour() { Id = id ?? Guid.NewGuid().ToString(), TourName = tourName, TourCountry = tourCountry, TourType = tourType, Price = price };
|
|
dbContext.Tours.Add(tour);
|
|
dbContext.SaveChanges();
|
|
return tour;
|
|
}
|
|
|
|
public static TourHistory InsertTourHistoryToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string tourId, double price = 10, DateTime? changeDate = null)
|
|
{
|
|
var tourHistory = new TourHistory() { Id = Guid.NewGuid().ToString(), TourId = tourId, OldPrice = price, ChangeDate = changeDate ?? DateTime.UtcNow };
|
|
dbContext.TourHistories.Add(tourHistory);
|
|
dbContext.SaveChanges();
|
|
return tourHistory;
|
|
}
|
|
|
|
public static Salary InsertSalaryToDatabaseAndReturn(this MagicCarpetDbContext 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 MagicCarpetDbContext 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)>? tours = null)
|
|
{
|
|
var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleTours = [] };
|
|
if (tours is not null)
|
|
{
|
|
foreach (var elem in tours)
|
|
{
|
|
sale.SaleTours.Add(new SaleTour { TourId = elem.Item1, SaleId = sale.Id, Count = elem.Item2, Price = elem.Item3 });
|
|
}
|
|
}
|
|
dbContext.Sales.Add(sale);
|
|
dbContext.SaveChanges();
|
|
return sale;
|
|
}
|
|
|
|
public static Employee InsertEmployeeToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string fio = "test", string email = "abc@gmail.com", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false, DateTime? dateDelete = null)
|
|
{
|
|
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, DateOfDelete = dateDelete };
|
|
dbContext.Employees.Add(employee);
|
|
dbContext.SaveChanges();
|
|
return employee;
|
|
}
|
|
|
|
public static Agency InsertAgencyToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, TourType tourType = TourType.Beach, int count = 20, List<(string, int)>? tours = null)
|
|
{
|
|
var agency = new Agency() { Id = id ?? Guid.NewGuid().ToString(), TourType = tourType, Count = count, Tours = [] };
|
|
if (tours is not null)
|
|
{
|
|
foreach (var elem in tours)
|
|
{
|
|
agency.Tours.Add(new TourAgency { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
|
}
|
|
}
|
|
dbContext.Agencies.Add(agency);
|
|
dbContext.SaveChanges();
|
|
return agency;
|
|
}
|
|
|
|
public static Supplies InsertSuppliesToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, TourType type = TourType.Beach, int count = 20, DateTime? date = null, List<(string, int)>? tours = null)
|
|
{
|
|
var supply = new Supplies() { Id = id ?? Guid.NewGuid().ToString(), Type = type, Count = count, ProductuionDate = date ?? DateTime.UtcNow, Tours = [] };
|
|
if (tours is not null)
|
|
{
|
|
foreach (var elem in tours)
|
|
{
|
|
supply.Tours.Add(new TourSupplies { SuppliesId = supply.Id, TourId = elem.Item1, Count = elem.Item2 });
|
|
}
|
|
}
|
|
dbContext.Supplieses.Add(supply);
|
|
dbContext.SaveChanges();
|
|
return supply;
|
|
}
|
|
|
|
public static Client? GetClientFromDatabase(this MagicCarpetDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Post? GetPostFromDatabaseByPostId(this MagicCarpetDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
|
|
|
public static Post[] GetPostsFromDatabaseByPostId(this MagicCarpetDbContext dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)];
|
|
|
|
public static Tour? GetTourFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Tours.FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Salary[] GetSalariesFromDatabaseByEmployeeId(this MagicCarpetDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.EmployeeId == id)];
|
|
|
|
public static Sale? GetSaleFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Sales.Include(x => x.SaleTours).FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Sale[] GetSalesByClientId(this MagicCarpetDbContext dbContext, string? clientId) => [.. dbContext.Sales.Include(x => x.SaleTours).Where(x => x.ClientId == clientId)];
|
|
|
|
public static Employee? GetEmployeeFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Employees.FirstOrDefault(x => x.Id == id);
|
|
public static Agency? GetAgencyFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Agencies.Include(x => x.Tours).FirstOrDefault(x => x.Id == id);
|
|
public static Supplies? GetSuppliesFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Supplieses.Include(x => x.Tours).FirstOrDefault(x => x.Id == id);
|
|
|
|
public static void RemoveClientsFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
|
|
|
public static void RemovePostsFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
|
|
|
public static void RemoveToursFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
|
|
|
public static void RemoveSalariesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
|
|
|
public static void RemoveSalesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
|
|
|
|
public static void RemoveEmployeesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
|
|
|
public static void RemoveAgenciesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Agencies\" CASCADE;");
|
|
public static void RemoveSuppliesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
|
|
|
private static void ExecuteSqlRaw(this MagicCarpetDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
|
} |