Commit 0df5c192 authored by Almouhannad's avatar Almouhannad

(B) Add seed employees from API

parent b35741bc
......@@ -26,6 +26,7 @@
<ItemGroup>
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Domain\Domain.csproj" />
<ProjectReference Include="..\EmployeesAPI\EmployeesAPI.csproj" />
<ProjectReference Include="..\Infrastructure\NotificationsService.csproj" />
<ProjectReference Include="..\MedicinesAPI\MedicinesAPI.csproj" />
<ProjectReference Include="..\Persistence\Persistence.csproj" />
......
......@@ -61,7 +61,8 @@ builder
.FromAssemblies(
Persistence.AssemblyReference.Assembly,
NotificationsService.AssemblyReference.Assembly,
MedicinesAPI.AssemblyReference.Assembly
MedicinesAPI.AssemblyReference.Assembly,
EmployeesAPI.AssemblyReference.Assembly
)
.AddClasses(false)
.AsImplementedInterfaces()
......@@ -133,6 +134,7 @@ var app = builder.Build();
await SeedHelper.Seed(app);
await SeedAdminUserHelper.Seed(app);
await SeedMedicinesHelper.Seed(app);
await SeedEmployeesHelper.Seed(app);
#endregion
// Configure the HTTP request pipeline.
......
using Domain.Entities.People.Employees;
using EmployeesAPI.Services;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace API.SeedDatabaseHelper;
public class SeedEmployeesHelper
{
public static async Task Seed(IApplicationBuilder applicationBuilder)
{
using (var serviceScope = applicationBuilder.ApplicationServices.CreateScope())
{
var employeesAPIServices = serviceScope.ServiceProvider.GetService<IEmployeesAPIServices>();
var context = serviceScope.ServiceProvider.GetService<ClinicsDbContext>();
if (employeesAPIServices is not null && context is not null)
{
var currentCount = (await context.Set<Employee>().ToListAsync()).Count;
if (currentCount == 0)
{
var employeesResult = await employeesAPIServices.GetAll();
if (employeesResult.IsSuccess)
{
var employees = employeesResult.Value;
foreach (var employee in employees)
{
context.Entry(employee.Patient.Gender).State = EntityState.Unchanged;
context.Set<Employee>().Add(employee);
}
await context.SaveChangesAsync();
}
else
{
Console.WriteLine($"Error seeding employees: {employeesResult.Error}");
}
}
}
}
}
}
\ No newline at end of file
......@@ -35,6 +35,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Services", "Services", "{C6
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataPersistence", "DataPersistence", "{2776718C-A689-415D-9E65-0DBE61F0C66F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmployeesAPI", "EmployeesAPI\EmployeesAPI.csproj", "{4CDBB65C-33AB-41EC-A423-5F5CF71E8D2B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -73,6 +75,10 @@ Global
{D0573185-52B8-4091-B3B2-BD8D354C715C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0573185-52B8-4091-B3B2-BD8D354C715C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0573185-52B8-4091-B3B2-BD8D354C715C}.Release|Any CPU.Build.0 = Release|Any CPU
{4CDBB65C-33AB-41EC-A423-5F5CF71E8D2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CDBB65C-33AB-41EC-A423-5F5CF71E8D2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CDBB65C-33AB-41EC-A423-5F5CF71E8D2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CDBB65C-33AB-41EC-A423-5F5CF71E8D2B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -92,6 +98,7 @@ Global
{D0573185-52B8-4091-B3B2-BD8D354C715C} = {74A202AC-94B8-40C0-B308-CCCEAED898CF}
{C6BE3A7A-3C92-414E-ACFF-4A61E829273F} = {FDA56BCD-A53D-4BA1-A59D-3F44FA32DDD7}
{2776718C-A689-415D-9E65-0DBE61F0C66F} = {FDA56BCD-A53D-4BA1-A59D-3F44FA32DDD7}
{4CDBB65C-33AB-41EC-A423-5F5CF71E8D2B} = {74A202AC-94B8-40C0-B308-CCCEAED898CF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E9FC9252-1283-485F-8F84-3574CFA12633}
......
......@@ -8,27 +8,11 @@ public static class Genders
public static int Count => 2;
public static Gender Male
{
get
{
var result = Gender.Create("ذكر", 1);
if (result.IsFailure)
throw new InvalidValuesDomainException<Gender>();
return result.Value;
}
}
private readonly static Gender _male = Gender.Create("ذكر", 1).Value;
public static Gender Male => _male;
public static Gender Female
{
get
{
var result = Gender.Create("أنثى", 2);
if (result.IsFailure)
throw new InvalidValuesDomainException<Gender>();
return result.Value;
}
}
private readonly static Gender _female = Gender.Create("أنثى", 2).Value;
public static Gender Female => _female;
#endregion
}
using System.Reflection;
namespace EmployeesAPI;
public class AssemblyReference
{
public static readonly Assembly Assembly = typeof(AssemblyReference).Assembly;
}
namespace EmployeesAPI.Configuration;
public static class APILink
{
public static readonly string Link = "http://localhost:9002/employees";
}
using Domain.Entities.People.Employees;
using Domain.Errors;
using Domain.Shared;
namespace EmployeesAPI.Contracts;
public class EmployeeResponse
{
public string FirstName { get; set; } = null!;
public string MiddleName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string DateOfBirth { get; set; } = null!;
public string Gender { get; set; } = null!;
public string SerialNumber { get; set; } = null!;
public string CenterStatus { get; set; } = null!;
public string Id { get; set; } = null!;
public Result<Employee> GetEmployee()
{
DateOnly dateOfBirth=new();
var dateOfBirthResult = DateOnly.TryParse(DateOfBirth, out dateOfBirth);
if (!dateOfBirthResult)
return Result.Failure<Employee>(DomainErrors.InvalidValuesError);
var employeeCreateResult = Employee.Create(FirstName, MiddleName, LastName, dateOfBirth, Gender, SerialNumber, CenterStatus);
if (employeeCreateResult.IsFailure)
return Result.Failure<Employee>(employeeCreateResult.Error);
return employeeCreateResult.Value;
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
</Project>
using Domain.Entities.People.Employees;
using Domain.Errors;
using Domain.Shared;
using EmployeesAPI.Configuration;
using EmployeesAPI.Contracts;
using System.Text.Json;
namespace EmployeesAPI.Services;
internal class EmployeesAPIServices : IEmployeesAPIServices
{
#region Http client DI
private readonly HttpClient _httpClient;
public EmployeesAPIServices(HttpClient httpClient)
{
_httpClient = httpClient;
}
#endregion
public async Task<Result<ICollection<Employee>>> GetAll()
{
try
{
// Get response
var response = await _httpClient.GetAsync(APILink.Link);
response.EnsureSuccessStatusCode();
// Parse response
var responseBody = await response.Content.ReadAsStringAsync();
var employeesResponses = JsonSerializer.Deserialize<EmployeeResponse[]>(
responseBody,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true } // id in Json, Id in contract class
);
List<Employee> employees = new();
if (employeesResponses is not null)
{
foreach (var employeeResponse in employeesResponses)
{
Result<Employee> employeeResult = employeeResponse.GetEmployee();
if (employeeResult.IsFailure)
return Result.Failure<ICollection<Employee>>(employeeResult.Error);
employees.Add(employeeResult.Value);
}
return employees;
}
else
{
return Result.Failure<ICollection<Employee>>(APIErrors.NoData);
}
}
catch (Exception)
{
return Result.Failure<ICollection<Employee>>(APIErrors.UnableToConnect);
}
}
}
using Domain.Entities.People.Employees;
using Domain.Shared;
namespace EmployeesAPI.Services;
public interface IEmployeesAPIServices
{
public Task<Result<ICollection<Employee>>> GetAll();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment