174 lines
7.7 KiB
C#
174 lines
7.7 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SmallSoftwareContracts.Enums;
|
|
using SmallSoftwareDatabase;
|
|
using SmallSoftwareDatabase.Models;
|
|
using static NUnit.Framework.Internal.OSPlatform;
|
|
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using SmallSoftwareContracts.Infrastructure.PostConfigurations;
|
|
namespace SmallSoftwareTests.Infrastructure;
|
|
|
|
internal static class SmallSoftwareDbContextExtensions
|
|
{
|
|
public static Manufacturer InsertManufacturerToDatabaseAndReturn(this SmallSoftwareDbContext 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 SmallSoftwareDbContext
|
|
dbContext, string? id = null, string postName = "test", PostType postType =
|
|
PostType.SoftInstaller, 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 Software InsertSoftwareToDatabaseAndReturn(this
|
|
SmallSoftwareDbContext dbContext, string manufacturerId, string? id = null, string
|
|
softwareName = "test", SoftwareType softwareType = SoftwareType.Windows, double price =
|
|
1, bool isDeleted = false)
|
|
{
|
|
var software = new Software()
|
|
{
|
|
Id = id ?? Guid.NewGuid().ToString(),
|
|
ManufacturerId = manufacturerId,
|
|
SoftwareName = softwareName,
|
|
SoftwareType = softwareType,
|
|
Price = price,
|
|
IsDeleted = isDeleted
|
|
};
|
|
dbContext.Softwares.Add(software);
|
|
dbContext.SaveChanges();
|
|
return software;
|
|
}
|
|
|
|
public static SoftwareHistory InsertSoftwareHistoryToDatabaseAndReturn(this
|
|
SmallSoftwareDbContext dbContext, string softwareId, double price = 10, DateTime?
|
|
changeDate = null)
|
|
{
|
|
var softwareHistory = new SoftwareHistory()
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
SoftwareId = softwareId,
|
|
OldPrice = price,
|
|
ChangeDate = changeDate ?? DateTime.UtcNow
|
|
};
|
|
dbContext.SoftwareHistories.Add(softwareHistory);
|
|
dbContext.SaveChanges();
|
|
return softwareHistory;
|
|
}
|
|
public static Salary InsertSalaryToDatabaseAndReturn(this SmallSoftwareDbContext 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 Request InsertRequestToDatabaseAndReturn(this SmallSoftwareDbContext dbContext,
|
|
string workerId, string email = "test@mail.ru", double sum = 1, bool isCancel = false,
|
|
List<(string, int, double)>? softwares = null, DateTime? requestDate = null)
|
|
{
|
|
var request = new Request()
|
|
{
|
|
WorkerId = workerId,
|
|
Email = email,
|
|
Sum = sum,
|
|
IsCancel = isCancel,
|
|
RequestDate = (requestDate ?? DateTime.UtcNow).ToUniversalTime(),
|
|
InstallationRequests = []
|
|
};
|
|
|
|
if (softwares is not null)
|
|
{
|
|
foreach (var elem in softwares)
|
|
{
|
|
var software = dbContext.Softwares.Find(elem.Item1);
|
|
request.InstallationRequests.Add(new InstallationRequest
|
|
{
|
|
SoftwareId = elem.Item1,
|
|
Software = software!,
|
|
RequestId = request.Id,
|
|
Count = elem.Item2,
|
|
Price = elem.Item3
|
|
});
|
|
}
|
|
}
|
|
|
|
dbContext.Requests.Add(request);
|
|
dbContext.SaveChanges();
|
|
return request;
|
|
}
|
|
|
|
public static Worker InsertWorkerToDatabaseAndReturn(this SmallSoftwareDbContext dbContext,
|
|
string? id = null, string fio = "test", string? postId = null, DateTime? birthDate = null,
|
|
DateTime? employmentDate = null, bool isDeleted = false, PostConfiguration? config = null, DateTime? dateDelete = null)
|
|
{
|
|
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,
|
|
Configuration = config ?? new PostConfiguration() { Rate = 100 },
|
|
DateOfDelete = dateDelete
|
|
};
|
|
dbContext.Workers.Add(worker);
|
|
dbContext.SaveChanges();
|
|
return worker;
|
|
}
|
|
public static Manufacturer? GetManufacturerFromDatabase(this
|
|
SmallSoftwareDbContext dbContext, string id) => dbContext.Manufacturers.FirstOrDefault(x => x.Id == id);
|
|
public static Post? GetPostFromDatabaseByPostId(this SmallSoftwareDbContext
|
|
dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
|
public static Post[] GetPostsFromDatabaseByPostId(this SmallSoftwareDbContext
|
|
dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)];
|
|
public static Software? GetSoftwareFromDatabaseById(this SmallSoftwareDbContext
|
|
dbContext, string id) => dbContext.Softwares.FirstOrDefault(x => x.Id == id);
|
|
public static Salary[] GetSalariesFromDatabaseByWorkerId(this
|
|
SmallSoftwareDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.WorkerId == id)];
|
|
public static Request? GetRequestFromDatabaseById(this SmallSoftwareDbContext
|
|
dbContext, string id) => dbContext.Requests.Include(x => x.InstallationRequests).FirstOrDefault(x => x.Id == id);
|
|
|
|
public static Worker? GetWorkerFromDatabaseById(this SmallSoftwareDbContext
|
|
dbContext, string id) => dbContext.Workers.FirstOrDefault(x => x.Id == id);
|
|
public static void RemoveManufacturersFromDatabase(this SmallSoftwareDbContext
|
|
dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Manufacturers\" CASCADE;");
|
|
public static void RemovePostsFromDatabase(this SmallSoftwareDbContext
|
|
dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
|
public static void RemoveSoftwaresFromDatabase(this SmallSoftwareDbContext
|
|
dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Softwares\" CASCADE;");
|
|
public static void RemoveSalariesFromDatabase(this SmallSoftwareDbContext
|
|
dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
|
public static void RemoveRequestsFromDatabase(this SmallSoftwareDbContext
|
|
dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Requests\" CASCADE;");
|
|
public static void RemoveWorkersFromDatabase(this SmallSoftwareDbContext
|
|
dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
|
private static void ExecuteSqlRaw(this SmallSoftwareDbContext dbContext,
|
|
string command) => dbContext.Database.ExecuteSqlRaw(command);
|
|
}
|