Commit 6cd1771b authored by Almouhannad's avatar Almouhannad

(B) Add seed medicines from API

parent 48046c6a
......@@ -27,6 +27,7 @@
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Domain\Domain.csproj" />
<ProjectReference Include="..\Infrastructure\NotificationsService.csproj" />
<ProjectReference Include="..\MedicinesAPI\MedicinesAPI.csproj" />
<ProjectReference Include="..\Persistence\Persistence.csproj" />
<ProjectReference Include="..\Presentation\Presentation.csproj" />
</ItemGroup>
......
......@@ -48,6 +48,10 @@ builder.Services.AddSignalR();
builder.Services.AddCors();
#endregion
#region Add HTTP client
builder.Services.AddHttpClient();
#endregion
#region Link interfaces implemented in infrastructre
// Using Scrutor library
builder
......@@ -56,7 +60,8 @@ builder
selector => selector
.FromAssemblies(
Persistence.AssemblyReference.Assembly,
NotificationsService.AssemblyReference.Assembly
NotificationsService.AssemblyReference.Assembly,
MedicinesAPI.AssemblyReference.Assembly
)
.AddClasses(false)
.AsImplementedInterfaces()
......
using Persistence.SeedDatabase.AdminUser;
using Persistence.SeedDatabase.Medicines;
using Domain.Entities.Medicals.Medicines;
using MedicinesAPI.Services;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace API.SeedDatabaseHelper;
......@@ -9,8 +11,32 @@ public class SeedMedicinesHelper
{
using (var serviceScope = applicationBuilder.ApplicationServices.CreateScope())
{
var seedMedicines = serviceScope.ServiceProvider.GetRequiredService<ISeedMedicines>();
await seedMedicines.Seed();
var medicinesAPIServices = serviceScope.ServiceProvider.GetService<IMedicinesAPIServices>();
var context = serviceScope.ServiceProvider.GetService<ClinicsDbContext>();
if (medicinesAPIServices is not null && context is not null)
{
var Medicines = context.Set<Medicine>();
var currentCount = (await Medicines.ToListAsync()).Count;
if (currentCount == 0)
{
var medicinesResult = await medicinesAPIServices.GetAll();
if (medicinesResult.IsSuccess)
{
var medicines = medicinesResult.Value;
foreach (var medicine in medicines)
{
context.Entry(medicine.MedicineForm).State = EntityState.Unchanged;
Medicines.Add(medicine);
}
await context.SaveChangesAsync();
}
else
{
Console.WriteLine($"Error seeding medicines: {medicinesResult.Error}");
}
}
}
}
}
}
\ No newline at end of file
......@@ -27,6 +27,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{05E390C6
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchitectureTests", "ArchitectureTests\ArchitectureTests.csproj", "{07C6D0DB-7181-4E6F-9BC1-863FCDAD9490}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ExternalAPIs", "ExternalAPIs", "{74A202AC-94B8-40C0-B308-CCCEAED898CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MedicinesAPI", "MedicinesAPI\MedicinesAPI.csproj", "{D0573185-52B8-4091-B3B2-BD8D354C715C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -61,6 +65,10 @@ Global
{07C6D0DB-7181-4E6F-9BC1-863FCDAD9490}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07C6D0DB-7181-4E6F-9BC1-863FCDAD9490}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07C6D0DB-7181-4E6F-9BC1-863FCDAD9490}.Release|Any CPU.Build.0 = Release|Any CPU
{D0573185-52B8-4091-B3B2-BD8D354C715C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -76,6 +84,8 @@ Global
{15B2AA13-EBBD-408B-A4B3-4BAB43D74267} = {26CA1441-8533-428E-9DD2-26F2FB428488}
{4EB41743-695F-4822-87F6-E6F9B48A8E6B} = {FDA56BCD-A53D-4BA1-A59D-3F44FA32DDD7}
{07C6D0DB-7181-4E6F-9BC1-863FCDAD9490} = {05E390C6-AFE5-42FB-A3E2-CEEE1E6A75EE}
{74A202AC-94B8-40C0-B308-CCCEAED898CF} = {FDA56BCD-A53D-4BA1-A59D-3F44FA32DDD7}
{D0573185-52B8-4091-B3B2-BD8D354C715C} = {74A202AC-94B8-40C0-B308-CCCEAED898CF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E9FC9252-1283-485F-8F84-3574CFA12633}
......
......@@ -12,7 +12,7 @@ public sealed class Medicine : Entity
private Medicine(int id) : base(id) { }
public Medicine(int id, MedicineForm medicineForm, int amount, string name, decimal dosage) : base(id)
private Medicine(int id, MedicineForm medicineForm, int amount, string name, decimal dosage) : base(id)
{
MedicineForm = medicineForm;
Amount = amount;
......
using Domain.Exceptions.InvalidValue;
namespace Domain.Entities.Medicals.Medicines.MedicineFormValues;
namespace Domain.Entities.Medicals.Medicines.MedicineFormValues;
public static class MedicineForms
{
#region Constant values
public static int Count => 2;
public static MedicineForm Tablet
{
get
{
var result = MedicineForm.Create("حبوب", 1);
if (result.IsFailure)
throw new InvalidValuesDomainException<MedicineForm>();
return result.Value;
}
}
public static MedicineForm Syrup
{
get
{
var result = MedicineForm.Create("شراب", 2);
if (result.IsFailure)
throw new InvalidValuesDomainException<MedicineForm>();
return result.Value;
}
}
private static readonly MedicineForm _tablet = MedicineForm.Create("حبوب", 1).Value;
public static MedicineForm Tablet => _tablet;
private static readonly MedicineForm _syrup = MedicineForm.Create("شراب", 2).Value;
public static MedicineForm Syrup => _syrup;
#endregion
}
using Domain.Shared;
namespace Domain.Errors;
public class APIErrors
{
public static Error NoData =>
new("APIError.NoData", "API gave no data");
public static Error UnableToConnect =>
new("APIError.UnableToConnect", "Unable to connect to API");
}
using System.Reflection;
namespace MedicinesAPI;
public class AssemblyReference
{
public static readonly Assembly Assembly = typeof(AssemblyReference).Assembly;
}
namespace MedicinesAPI.Configuration;
public static class APILink
{
public static readonly string Link = "http://localhost:9001/medicines";
}
using Domain.Entities.Medicals.Medicines;
using Domain.Shared;
namespace MedicinesAPI.Contrants;
public class MedicineResponse
{
public string Form { get; set; } = null!;
public int Amount { get; set; }
public string Name { get; set; } = null!;
public decimal Dosage { get; set; }
public string Id { get; set; } = null!;
public Result<Medicine> GetMedicine()
{
var medicineCreateResult = Medicine.Create(Form, Amount, Name, Dosage);
if (medicineCreateResult.IsFailure)
return Result.Failure<Medicine>(medicineCreateResult.Error);
return medicineCreateResult.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.Medicals.Medicines;
using Domain.Shared;
namespace MedicinesAPI.Services;
public interface IMedicinesAPIServices
{
public Task<Result<ICollection<Medicine>>> GetAll();
}
using Domain.Entities.Medicals.Medicines;
using Domain.Errors;
using Domain.Shared;
using MedicinesAPI.Configuration;
using MedicinesAPI.Contrants;
using System.Text.Json;
namespace MedicinesAPI.Services;
public class MedicinesAPIServices : IMedicinesAPIServices
{
#region Http client DI
private readonly HttpClient _httpClient;
public MedicinesAPIServices(HttpClient httpClient)
{
_httpClient = httpClient;
}
#endregion
public async Task<Result<ICollection<Medicine>>> GetAll()
{
try
{
// Get response
var response = await _httpClient.GetAsync(APILink.Link);
response.EnsureSuccessStatusCode();
// Parse response
var responseBody = await response.Content.ReadAsStringAsync();
var medicineResponses = JsonSerializer.Deserialize<MedicineResponse[]>(
responseBody,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true } // id in Json, Id in contract class
);
List<Medicine> medicines = new();
if (medicineResponses is not null)
{
foreach (var medicineResponse in medicineResponses)
{
Result<Medicine> medicineResult = medicineResponse.GetMedicine();
if (medicineResult.IsFailure)
return Result.Failure<ICollection<Medicine>>(medicineResult.Error);
medicines.Add(medicineResult.Value);
}
return medicines;
}
else
{
return Result.Failure<ICollection<Medicine>>(APIErrors.NoData);
}
}
catch (Exception)
{
return Result.Failure<ICollection<Medicine>>(APIErrors.UnableToConnect);
}
}
}
namespace Persistence.SeedDatabase.Medicines;
public interface ISeedMedicines
{
public Task Seed();
}
using Domain.Entities.Medicals.Medicines;
using Domain.Entities.Medicals.Medicines.MedicineFormValues;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace Persistence.SeedDatabase.Medicines;
public class SeedMedicines : ISeedMedicines
{
#region Ctor DI
private readonly ClinicsDbContext _clinicsContext;
public SeedMedicines(ClinicsDbContext clinicsContext)
{
_clinicsContext = clinicsContext;
}
#endregion
private readonly MedicineForm Tablet = Domain.Entities.Medicals.Medicines.MedicineFormValues.MedicineForms.Tablet;
private readonly MedicineForm Syrup = Domain.Entities.Medicals.Medicines.MedicineFormValues.MedicineForms.Syrup;
public async Task Seed()
{
var currentSize = (await (_clinicsContext.Set<Medicine>().ToListAsync())).Count;
if (currentSize == 0)
{
List<Medicine> medicines = new();
medicines.Add(new Medicine(0, Tablet, 10, "Paracetamol", 325));
medicines.Add(new Medicine(0, Tablet, 10, "Cetamol", 325));
medicines.Add(new Medicine(0, Tablet, 10, "Ceatcodaen", 325));
medicines.Add(new Medicine(0, Tablet, 10, "Cetaprofin", 325));
medicines.Add(new Medicine(0, Tablet, 10, "Cetacold", 325));
medicines.Add(new Medicine(0, Tablet, 10, "Cetagrape", 325));
medicines.Add(new Medicine(0, Syrup, 100, "Cough", 50));
medicines.Add(new Medicine(0, Tablet, 12, "Aspirin", 500));
medicines.Add(new Medicine(0, Syrup, 120, "Pediatric", 60));
medicines.Add(new Medicine(0, Tablet, 8, "Amoxicillin", 250));
medicines.Add(new Medicine(0, Syrup, 150, "Acetaminophen", 160));
medicines.Add(new Medicine(0, Tablet, 15, "Ibuprofen", 400));
medicines.Add(new Medicine(0, Tablet, 20, "Loratadine", 10));
medicines.Add(new Medicine(0, Syrup, 200, "Vitamin D", 400));
medicines.Add(new Medicine(0, Tablet, 30, "Nurofen", 200));
medicines.Add(new Medicine(0, Syrup, 120, "Antihistamine", 5));
medicines.Add(new Medicine(0, Tablet, 14, "Claritin", 10));
medicines.Add(new Medicine(0, Syrup, 150, "Flu", 100));
medicines.Add(new Medicine(0, Tablet, 10, "Panadol", 500));
medicines.Add(new Medicine(0, Syrup, 100, "Ibuprofen", 100));
medicines.Add(new Medicine(0, Tablet, 16, "Spirin", 100));
medicines.Add(new Medicine(0, Syrup, 180, "Cough Suppressant", 60));
medicines.Add(new Medicine(0, Tablet, 18, "Cetal", 500));
medicines.Add(new Medicine(0, Syrup, 120, "Tuskan", 120));
medicines.Add(new Medicine(0, Tablet, 12, "Naproxen", 250));
medicines.Add(new Medicine(0, Syrup, 100, "Bronchicum", 150));
medicines.Add(new Medicine(0, Tablet, 14, "Fenistil", 5));
medicines.Add(new Medicine(0, Syrup, 100, "Fenistil", 30));
medicines.Add(new Medicine(0, Tablet, 10, "Diclofenac", 50));
medicines.Add(new Medicine(0, Syrup, 125, "Amoclan", 250));
medicines.Add(new Medicine(0, Tablet, 12, "Zinnat", 250));
medicines.Add(new Medicine(0, Syrup, 200, "Novalgin", 500));
medicines.Add(new Medicine(0, Tablet, 15, "Milga", 250));
medicines.Add(new Medicine(0, Syrup, 150, "Multivitamin", 50));
medicines.Add(new Medicine(0, Tablet, 18, "Trifluor", 250));
medicines.Add(new Medicine(0, Syrup, 100, "Zithromax", 200));
medicines.Add(new Medicine(0, Tablet, 10, "Ferrotron", 50));
medicines.Add(new Medicine(0, Syrup, 100, "Feroglobin", 100));
medicines.Add(new Medicine(0, Tablet, 16, "Farcolin", 200));
medicines.Add(new Medicine(0, Syrup, 120, "Clindamycin", 300));
medicines.Add(new Medicine(0, Tablet, 12, "Trimed Flu", 325));
medicines.Add(new Medicine(0, Syrup, 150, "Prospan", 100));
medicines.Add(new Medicine(0, Tablet, 20, "Omeprazole", 40));
medicines.Add(new Medicine(0, Syrup, 200, "Lactulose", 15));
medicines.Add(new Medicine(0, Tablet, 18, "Tavanic", 500));
medicines.Add(new Medicine(0, Syrup, 100, "Ventolin", 2));
medicines.Add(new Medicine(0, Tablet, 10, "Losartan", 50));
medicines.Add(new Medicine(0, Syrup, 120, "Promax", 100));
medicines.Add(new Medicine(0, Tablet, 12, "Nurofen Cold & Flu", 200));
medicines.Add(new Medicine(0, Syrup, 150, "Guava", 60));
medicines.Add(new Medicine(0, Tablet, 8, "Profenid", 150));
medicines.Add(new Medicine(0, Syrup, 100, "Depakine", 200));
foreach (var medicine in medicines)
{
_clinicsContext.Entry(medicine.MedicineForm).State = EntityState.Unchanged;
_clinicsContext.Set<Medicine>().Add(medicine);
}
await _clinicsContext.SaveChangesAsync();
}
}
}
\ No newline at end of file
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