Add LeaveType Seeder
This commit is contained in:
parent
ef66c28f0e
commit
8037ad7e1e
11 changed files with 1111 additions and 2 deletions
47
BMA.EHR.Infrastructure/Persistence/LeaveSeeder.cs
Normal file
47
BMA.EHR.Infrastructure/Persistence/LeaveSeeder.cs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
using BMA.EHR.Application.Repositories.Leaves.LeaveRequests;
|
||||
using BMA.EHR.Domain.Models.Leave.Commons;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using OfficeOpenXml;
|
||||
|
||||
namespace BMA.EHR.Infrastructure.Persistence
|
||||
{
|
||||
public static class LeaveSeeder
|
||||
{
|
||||
public static async Task SeedLeaveType(WebApplication app)
|
||||
{
|
||||
using var scope = app.Services.CreateScope();
|
||||
var service = scope.ServiceProvider.GetRequiredService<LeaveTypeRepository>();
|
||||
|
||||
if ((await service.GetAllAsync()).Count() == 0)
|
||||
{
|
||||
// read excels into object
|
||||
var excelFile = "SeedLeaveData.xlsx";
|
||||
using (var excel = new ExcelPackage(new FileInfo(excelFile)))
|
||||
{
|
||||
var workSheet = excel.Workbook.Worksheets.FirstOrDefault(x => x.Name.ToLower() == "leavetype");
|
||||
var totalRows = workSheet?.Dimension.Rows;
|
||||
|
||||
int row = 2;
|
||||
|
||||
while (row <= totalRows)
|
||||
{
|
||||
var cell1 = workSheet?.Cells[row, 1]?.GetValue<string>();
|
||||
if (cell1 == "" || cell1 == null) break;
|
||||
|
||||
var inserted = new LeaveType
|
||||
{
|
||||
Id = Guid.Parse(workSheet?.Cells[row, 1]?.GetValue<string>()!),
|
||||
Name = workSheet?.Cells[row, 2]?.GetValue<string>()!,
|
||||
Code = workSheet?.Cells[row, 3]?.GetValue<string>()!,
|
||||
};
|
||||
|
||||
await service.AddAsync(inserted);
|
||||
|
||||
row++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue