Files
PIbd-22_Buslaev.R.V_TwoFrom…/TwoFromTheCasketContracts/TwoFromTheCasketTests/Infrastructure/TwoFromTheCasketDbContextExtensions.cs

184 lines
6.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using TwoFromTheCasketContracts.Enums;
using TwoFromTheCasketContracts.Infastructure.SalaryConfiguration;
using TwoFromTheCasketDatabase;
using TwoFromTheCasketDatabase.Models;
namespace TwoFromTheCasketTests.Infrastructure;
public static class TwoFromTheCasketDbContextExtensions
{
public static Worker InsertWorkerToDatabaseAndReturn(
this TwoFromTheCasketDbContext db,
string? id = null,
string fio = "Иванов Иван",
string specializationId = "spec1",
SalaryConfiguration? configuration = null,
string? phone = null,
DateTime? birthday = null,
DateTime? validFrom = null,
SalaryConfiguration? _configuration = null)
{
var config = _configuration ?? new SalaryConfiguration { Rate = 1 };
var worker = new Worker
{
Id = id ?? Guid.NewGuid().ToString(),
FIO = fio,
SpecializationId = specializationId,
PhoneNumber = phone ?? $"+7999{Random.Shared.Next(1000000, 9999999)}",
DateBirthDay = birthday.HasValue
? DateTime.SpecifyKind(birthday.Value, DateTimeKind.Utc)
: DateTime.UtcNow.AddYears(-20),
ValidFrom = validFrom.HasValue
? DateTime.SpecifyKind(validFrom.Value, DateTimeKind.Utc)
: DateTime.UtcNow,
IsCurrent = 1,
Configuration = config
};
db.Workers.Add(worker);
db.SaveChanges();
return worker;
}
public static Room InsertRoomToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, string address = "ул. Пушкина, д. 1", string owner = "ИП Тест", double space = 10.0, TypeRoom type = TypeRoom.PublicBuilding)
{
var room = new Room
{
Id = id ?? Guid.NewGuid().ToString(),
OwnerFIO = owner,
Address = address,
Space = space,
Type = type
};
db.Rooms.Add(room);
db.SaveChanges();
return room;
}
public static RoomHistory InsertRoomHistoryToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, string roomId = "room1", TypeRoom type = TypeRoom.Industrial, string owner = "Старый владелец", DateTime? date = null)
{
var history = new RoomHistory
{
Id = id ?? Guid.NewGuid().ToString(),
RoomId = roomId,
OwnerFIO = owner,
Type = type,
ChangeDate = date ?? DateTime.UtcNow
};
db.RoomHistories.Add(history);
db.SaveChanges();
return history;
}
public static Salary InsertSalaryToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, string workerId = "worker1", int sum = 10000, DateTime? date = null)
{
var salary = new Salary
{
Id = id ?? Guid.NewGuid().ToString(),
WorkerId = workerId,
Sum = sum,
Date = date.HasValue
? DateTime.SpecifyKind(date.Value, DateTimeKind.Utc)
: DateTime.UtcNow
};
db.Salaries.Add(salary);
db.SaveChanges();
return salary;
}
public static Specialization InsertSpecializationToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, string specializationName = "Сантехник", double salary = 1000, bool isActual = true, DateTime? changeDate = null)
{
var specialization = new Specialization
{
Id = id ?? Guid.NewGuid().ToString(),
SpecializationName = specializationName,
Salary = salary,
IsActual = isActual,
ChangeDate = changeDate.HasValue
? DateTime.SpecifyKind(changeDate.Value, DateTimeKind.Utc)
: DateTime.UtcNow
};
db.Specializations.Add(specialization);
db.SaveChanges();
return specialization;
}
public static Work InsertWorkToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, TypeWork type = TypeWork.Plaster, string desc = "Замена крана", DateTime? date = null)
{
var work = new Work
{
Id = id ?? Guid.NewGuid().ToString(),
Type = type,
Description = desc,
Date = date ?? DateTime.UtcNow.AddDays(10)
};
db.Works.Add(work);
db.SaveChanges();
return work;
}
public static ComplitedWork InsertComplitedWorkToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, string roomId = "room1", string workId = "work1", DateTime? date = null, WorkerComplitedWork? workerComplitedWork = null)
{
var complited = new ComplitedWork
{
Id = id ?? Guid.NewGuid().ToString(),
RoomId = roomId,
WorkId = workId,
Date = date ?? DateTime.UtcNow
};
if (workerComplitedWork != null)
complited.WorkersComplitedWorks.Add(workerComplitedWork);
db.ComplitedWorks.Add(complited);
db.SaveChanges();
return complited;
}
public static WorkerComplitedWork InsertWorkerComplitedWorkToDatabaseAndReturn(this TwoFromTheCasketDbContext db,
string? id = null, string complitedWorkId = "complited1", string workerId = "worker1", double numberOfWorkingHour = 14.2)
{
var wcw = new WorkerComplitedWork
{
ComplitedWorkId = complitedWorkId,
WorkerId = workerId,
NumberOfWorkingHours = numberOfWorkingHour
};
db.WorkerComplitedWorks.Add(wcw);
db.SaveChanges();
return wcw;
}
public static Room? GetRoomFromDatabase(this TwoFromTheCasketDbContext db, string id)
{
db.ChangeTracker.Clear();
return db.Rooms.AsNoTracking().FirstOrDefault(x => x.Id == id);
}
public static void ClearTables(this TwoFromTheCasketDbContext db)
{
db.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"Rooms\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"RoomHistories\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"Specializations\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"Works\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"ComplitedWorks\" CASCADE;");
db.ExecuteSqlRaw("TRUNCATE \"WorkerComplitedWorks\" CASCADE;");
}
private static void ExecuteSqlRaw(this TwoFromTheCasketDbContext db, string sql)
=> db.Database.ExecuteSqlRaw(sql);
}