From 578c8faa8657173125ba4e3ab9229695be7622e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20D=C3=A1quina?= Date: Fri, 26 Jul 2024 09:46:16 +0200 Subject: [PATCH 1/3] Default setup for DB Creation and Medication Table --- SM.Medication.Api.sln | 23 +++++++++- src/SM.Medication.Api/Program.cs | 11 +++++ .../SM.Medication.Api.csproj | 2 + src/SM.Medication.Api/appsettings.json | 3 ++ .../SM.Medication.Application.csproj | 13 ++++++ .../Common/AuditableEntity.cs | 10 ++++ src/SM.Medication.Domain/Common/BaseEntity.cs | 6 +++ .../Entities/Medication.cs | 24 ++++++++++ .../SM.Medication.Domain.csproj | 12 +++++ .../Mapping/MedicationMapping.cs | 43 +++++++++++++++++ .../SmartMedMedicationDbContext.cs | 46 +++++++++++++++++++ .../SM.Medication.Infrastructure.csproj | 25 ++++++++++ 12 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 src/SM.Medication.Application/SM.Medication.Application.csproj create mode 100644 src/SM.Medication.Domain/Common/AuditableEntity.cs create mode 100644 src/SM.Medication.Domain/Common/BaseEntity.cs create mode 100644 src/SM.Medication.Domain/Entities/Medication.cs create mode 100644 src/SM.Medication.Domain/SM.Medication.Domain.csproj create mode 100644 src/SM.Medication.Infrastructure/Mapping/MedicationMapping.cs create mode 100644 src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs create mode 100644 src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj diff --git a/SM.Medication.Api.sln b/SM.Medication.Api.sln index e4f04c7..2ae59bd 100644 --- a/SM.Medication.Api.sln +++ b/SM.Medication.Api.sln @@ -15,7 +15,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SM.Medication.Auth", "src\S EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SM.Medication.Shared", "src\SM.Medication.Shared\SM.Medication.Shared.csproj", "{3C8A55C6-1F4B-4E37-90BA-5AC27E59ECAB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM.Medication.Auth.Tests", "tests\SM.Medication.Auth.Tests\SM.Medication.Auth.Tests.csproj", "{A50EB5E4-B7DE-4F2C-823C-E09B94B9A666}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SM.Medication.Auth.Tests", "tests\SM.Medication.Auth.Tests\SM.Medication.Auth.Tests.csproj", "{A50EB5E4-B7DE-4F2C-823C-E09B94B9A666}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM.Medication.Infrastructure", "src\SM.Medication.Infrastructure\SM.Medication.Infrastructure.csproj", "{9817D66D-4A56-4E4E-A283-9F07068A4E4D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM.Medication.Domain", "src\SM.Medication.Domain\SM.Medication.Domain.csproj", "{EC398AFF-49C8-4510-9BE8-D7B4F0D5810E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM.Medication.Application", "src\SM.Medication.Application\SM.Medication.Application.csproj", "{12AA402A-D97A-471E-898E-9EB3FA4D39D9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,6 +49,18 @@ Global {A50EB5E4-B7DE-4F2C-823C-E09B94B9A666}.Debug|Any CPU.Build.0 = Debug|Any CPU {A50EB5E4-B7DE-4F2C-823C-E09B94B9A666}.Release|Any CPU.ActiveCfg = Release|Any CPU {A50EB5E4-B7DE-4F2C-823C-E09B94B9A666}.Release|Any CPU.Build.0 = Release|Any CPU + {9817D66D-4A56-4E4E-A283-9F07068A4E4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9817D66D-4A56-4E4E-A283-9F07068A4E4D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9817D66D-4A56-4E4E-A283-9F07068A4E4D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9817D66D-4A56-4E4E-A283-9F07068A4E4D}.Release|Any CPU.Build.0 = Release|Any CPU + {EC398AFF-49C8-4510-9BE8-D7B4F0D5810E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC398AFF-49C8-4510-9BE8-D7B4F0D5810E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC398AFF-49C8-4510-9BE8-D7B4F0D5810E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC398AFF-49C8-4510-9BE8-D7B4F0D5810E}.Release|Any CPU.Build.0 = Release|Any CPU + {12AA402A-D97A-471E-898E-9EB3FA4D39D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12AA402A-D97A-471E-898E-9EB3FA4D39D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12AA402A-D97A-471E-898E-9EB3FA4D39D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12AA402A-D97A-471E-898E-9EB3FA4D39D9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -53,6 +71,9 @@ Global {D3A3FF06-ED0D-47A8-8D23-8F55C1A7F634} = {F33F386A-8F3F-42F1-B2A3-B73095B6354C} {3C8A55C6-1F4B-4E37-90BA-5AC27E59ECAB} = {F33F386A-8F3F-42F1-B2A3-B73095B6354C} {A50EB5E4-B7DE-4F2C-823C-E09B94B9A666} = {0D40AD36-75CA-4281-9AF0-CF4DF8CBF74A} + {9817D66D-4A56-4E4E-A283-9F07068A4E4D} = {F33F386A-8F3F-42F1-B2A3-B73095B6354C} + {EC398AFF-49C8-4510-9BE8-D7B4F0D5810E} = {F33F386A-8F3F-42F1-B2A3-B73095B6354C} + {12AA402A-D97A-471E-898E-9EB3FA4D39D9} = {F33F386A-8F3F-42F1-B2A3-B73095B6354C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A3C23F82-CBC0-482F-8E01-FDFE5DC1D812} diff --git a/src/SM.Medication.Api/Program.cs b/src/SM.Medication.Api/Program.cs index 8255177..d06749f 100644 --- a/src/SM.Medication.Api/Program.cs +++ b/src/SM.Medication.Api/Program.cs @@ -1,7 +1,9 @@ +using Microsoft.EntityFrameworkCore; using SM.Medication.Api.Extensions; using SM.Medication.Auth; using SM.Medication.Auth.Extensions; using SM.Medication.Auth.Options; +using SM.Medication.Infrastructure.Persistence; using SmartMed.Medication.Auth.Constants; using Swashbuckle.AspNetCore.Annotations; @@ -12,6 +14,9 @@ builder.Services.SetupOpenApi(); +builder.Services.AddDbContext(options => + options.UseSqlServer(builder.Configuration.GetConnectionString("MedicationDb"))); + builder.Services.AddAuthentication(options => options.DefaultScheme = AuthSchemeConstants.SmartMedAuthScheme) .AddScheme(AuthSchemeConstants.SmartMedAuthScheme, options => { }); @@ -28,6 +33,12 @@ .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status500InternalServerError, "Failed!")) .RequireAuthorization(); +app.MapGet("/medications", async (SmartMedMedicationDbContext dbContext) => +{ + var medications = await dbContext.Medications.ToListAsync(); + return medications; +}); + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/src/SM.Medication.Api/SM.Medication.Api.csproj b/src/SM.Medication.Api/SM.Medication.Api.csproj index df63162..4025ca6 100644 --- a/src/SM.Medication.Api/SM.Medication.Api.csproj +++ b/src/SM.Medication.Api/SM.Medication.Api.csproj @@ -19,7 +19,9 @@ + + diff --git a/src/SM.Medication.Api/appsettings.json b/src/SM.Medication.Api/appsettings.json index fe475af..6e16ee4 100644 --- a/src/SM.Medication.Api/appsettings.json +++ b/src/SM.Medication.Api/appsettings.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "MedicationDb": "Server=MyServer; Database=MyOnlineShopDb; Trusted_Connection=True; MultipleActiveResultSets=true" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/SM.Medication.Application/SM.Medication.Application.csproj b/src/SM.Medication.Application/SM.Medication.Application.csproj new file mode 100644 index 0000000..08065c7 --- /dev/null +++ b/src/SM.Medication.Application/SM.Medication.Application.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/src/SM.Medication.Domain/Common/AuditableEntity.cs b/src/SM.Medication.Domain/Common/AuditableEntity.cs new file mode 100644 index 0000000..c1d6e10 --- /dev/null +++ b/src/SM.Medication.Domain/Common/AuditableEntity.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Http; + +namespace SM.Medication.Domain.Common; +public class AuditableEntity +{ + public string? CreatedBy { get; set; } + public string? ModifiedBy { get; set; } + public DateTime CreatedAt { get; set; } + public DateTime ModifiedAt { get; set; } +} diff --git a/src/SM.Medication.Domain/Common/BaseEntity.cs b/src/SM.Medication.Domain/Common/BaseEntity.cs new file mode 100644 index 0000000..f4a7d20 --- /dev/null +++ b/src/SM.Medication.Domain/Common/BaseEntity.cs @@ -0,0 +1,6 @@ +namespace SM.Medication.Domain.Common; + +public class BaseEntity : AuditableEntity +{ + public int Id { get; private set; } +} diff --git a/src/SM.Medication.Domain/Entities/Medication.cs b/src/SM.Medication.Domain/Entities/Medication.cs new file mode 100644 index 0000000..0596bc0 --- /dev/null +++ b/src/SM.Medication.Domain/Entities/Medication.cs @@ -0,0 +1,24 @@ +using System.ComponentModel.DataAnnotations; +using SM.Medication.Domain.Common; + +namespace SM.Medication.Domain.Entities; + +public class Medication : BaseEntity +{ + public string? Name { get; set; } + + private int _quantity; + [Range(1, int.MaxValue)] + public int Quantity + { + get { return _quantity; } + set + { + if (value < 1) + { + throw new ArgumentOutOfRangeException(nameof(value), "Quantity must be greater than 0"); + } + _quantity = value; + } + } +} diff --git a/src/SM.Medication.Domain/SM.Medication.Domain.csproj b/src/SM.Medication.Domain/SM.Medication.Domain.csproj new file mode 100644 index 0000000..53f22f4 --- /dev/null +++ b/src/SM.Medication.Domain/SM.Medication.Domain.csproj @@ -0,0 +1,12 @@ + + + + net8.0 + enable + enable + + + + + + diff --git a/src/SM.Medication.Infrastructure/Mapping/MedicationMapping.cs b/src/SM.Medication.Infrastructure/Mapping/MedicationMapping.cs new file mode 100644 index 0000000..f1efa32 --- /dev/null +++ b/src/SM.Medication.Infrastructure/Mapping/MedicationMapping.cs @@ -0,0 +1,43 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace SM.Medication.Infrastructure.Mapping; +public class MedicationMapping : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("Medication"); + builder.HasKey(x => x.Id); + + builder.Property(p => p.Id) + .HasColumnName("MedicationId") + .ValueGeneratedOnAdd(); + + builder.Property(p => p.Name) + .HasColumnName("Name") + .HasMaxLength(100) + .IsRequired(); + + builder.Property(p => p.Quantity) + .HasColumnName("Quantity") + .IsRequired(); + + builder.Property(p => p.CreatedAt) + .HasColumnName("CreatedAt") + .IsRequired(); + + builder.Property(p => p.CreatedBy) + .HasColumnName("CreatedBy") + .HasMaxLength(100) + .IsRequired(); + + builder.Property(p => p.ModifiedAt) + .HasColumnName("ModifiedAt") + .IsRequired(); + + builder.Property(p => p.ModifiedBy) + .HasColumnName("ModifiedBy") + .HasMaxLength(100) + .IsRequired(); + } +} diff --git a/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs b/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs new file mode 100644 index 0000000..01e0bf8 --- /dev/null +++ b/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; + +namespace SM.Medication.Infrastructure.Persistence; + +public class SmartMedMedicationDbContext : DbContext +{ + public SmartMedMedicationDbContext(DbContextOptions options) : base(options) + { + var dbCreater = Database.GetService() as RelationalDatabaseCreator; + + if (dbCreater != null) + { + if (!dbCreater.CanConnect()) + { + dbCreater.Create(); + } + + if (!dbCreater.HasTables()) + { + dbCreater.CreateTables(); + + } + } + } + + public DbSet Medications { get; set; } = null!; + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.HasDefaultSchema("dbo"); + modelBuilder.ApplyConfigurationsFromAssembly(typeof(SmartMedMedicationDbContext).Assembly); + + modelBuilder.Entity().HasData( + new SM.Medication.Domain.Entities.Medication { Name = "Paracetamol", Quantity = 100, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }, + new SM.Medication.Domain.Entities.Medication { Name = "Ibuprofen", Quantity = 50, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }, + new SM.Medication.Domain.Entities.Medication { Name = "Aspirin", Quantity = 75, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }); + + } +} diff --git a/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj b/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj new file mode 100644 index 0000000..0641d77 --- /dev/null +++ b/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + From afabeb621296c243d26c0bfc6637f4e21ef579c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20D=C3=A1quina?= Date: Fri, 26 Jul 2024 12:26:09 +0200 Subject: [PATCH 2/3] Adding Migration --- src/SM.Medication.Api/SM.Medication.Api.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SM.Medication.Api/SM.Medication.Api.csproj b/src/SM.Medication.Api/SM.Medication.Api.csproj index 4025ca6..bec7e13 100644 --- a/src/SM.Medication.Api/SM.Medication.Api.csproj +++ b/src/SM.Medication.Api/SM.Medication.Api.csproj @@ -7,6 +7,7 @@ db35fc35-788e-4166-b5f9-b68fb7c2d7fe Linux ..\.. + True From e87ff617a8d50e83007edba3b819ef2ca2b7a904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20D=C3=A1quina?= Date: Fri, 26 Jul 2024 12:26:42 +0200 Subject: [PATCH 3/3] Base setup for db access with medication table --- src/SM.Medication.Api/Program.cs | 32 ++++-- .../SM.Medication.Api.csproj | 9 +- src/SM.Medication.Api/appsettings.json | 2 +- src/SM.Medication.Domain/Common/BaseEntity.cs | 2 +- ...20240726082831_InitialDataBase.Designer.cs | 107 ++++++++++++++++++ .../20240726082831_InitialDataBase.cs | 58 ++++++++++ ...martMedMedicationDbContextModelSnapshot.cs | 104 +++++++++++++++++ .../SmartMedMedicationDbContext.cs | 11 +- .../SM.Medication.Infrastructure.csproj | 1 + .../SM.Medication.Api.Tests.csproj | 14 ++- .../SM.Medication.Auth.Tests.csproj | 14 ++- 11 files changed, 327 insertions(+), 27 deletions(-) create mode 100644 src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.Designer.cs create mode 100644 src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.cs create mode 100644 src/SM.Medication.Infrastructure/Migrations/SmartMedMedicationDbContextModelSnapshot.cs diff --git a/src/SM.Medication.Api/Program.cs b/src/SM.Medication.Api/Program.cs index d06749f..3c650a6 100644 --- a/src/SM.Medication.Api/Program.cs +++ b/src/SM.Medication.Api/Program.cs @@ -1,3 +1,5 @@ +using Microsoft.AspNetCore.Localization; +using System.Globalization; using Microsoft.EntityFrameworkCore; using SM.Medication.Api.Extensions; using SM.Medication.Auth; @@ -25,19 +27,33 @@ var app = builder.Build(); -app.MapGet("/", () => "Hello World!") - .WithTags("Home") - .WithMetadata(new SwaggerOperationAttribute("Home", "Base Get Endpoint")) - .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status200OK, "Success!")) - .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status401Unauthorized, "You're not Authorized!")) - .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status500InternalServerError, "Failed!")) - .RequireAuthorization(); +//var supportedCultures = new[] +//{ +// new CultureInfo("en-US"), +// new CultureInfo("fr"), +//}; +//app.UseRequestLocalization(new RequestLocalizationOptions +//{ +// DefaultRequestCulture = new RequestCulture("en-US"), +// // Formatting numbers, dates, etc. +// SupportedCultures = supportedCultures, +// // UI strings that we have localized. +// SupportedUICultures = supportedCultures +//}); + +app.MapGet("/", () => "Hello World!"); + app.MapGet("/medications", async (SmartMedMedicationDbContext dbContext) => { var medications = await dbContext.Medications.ToListAsync(); return medications; -}); +}).WithTags("Home") + .WithMetadata(new SwaggerOperationAttribute("Home", "Base Get Endpoint")) + .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status200OK, "Success!")) + .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status401Unauthorized, "You're not Authorized!")) + .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status500InternalServerError, "Failed!")) + .RequireAuthorization(); ; if (app.Environment.IsDevelopment()) { diff --git a/src/SM.Medication.Api/SM.Medication.Api.csproj b/src/SM.Medication.Api/SM.Medication.Api.csproj index bec7e13..d5fd3c9 100644 --- a/src/SM.Medication.Api/SM.Medication.Api.csproj +++ b/src/SM.Medication.Api/SM.Medication.Api.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -11,6 +11,13 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/src/SM.Medication.Api/appsettings.json b/src/SM.Medication.Api/appsettings.json index 6e16ee4..d7e3fdd 100644 --- a/src/SM.Medication.Api/appsettings.json +++ b/src/SM.Medication.Api/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "MedicationDb": "Server=MyServer; Database=MyOnlineShopDb; Trusted_Connection=True; MultipleActiveResultSets=true" + "MedicationDb": "Server=localhost;Database=master;User Id=sa;Password=Password123456;TrustServerCertificate=true" }, "Logging": { "LogLevel": { diff --git a/src/SM.Medication.Domain/Common/BaseEntity.cs b/src/SM.Medication.Domain/Common/BaseEntity.cs index f4a7d20..490769f 100644 --- a/src/SM.Medication.Domain/Common/BaseEntity.cs +++ b/src/SM.Medication.Domain/Common/BaseEntity.cs @@ -2,5 +2,5 @@ namespace SM.Medication.Domain.Common; public class BaseEntity : AuditableEntity { - public int Id { get; private set; } + public int Id { get; set; } } diff --git a/src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.Designer.cs b/src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.Designer.cs new file mode 100644 index 0000000..99f24f4 --- /dev/null +++ b/src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.Designer.cs @@ -0,0 +1,107 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SM.Medication.Infrastructure.Persistence; + +#nullable disable + +namespace SM.Medication.Infrastructure.Migrations +{ + [DbContext(typeof(SmartMedMedicationDbContext))] + [Migration("20240726082831_InitialDataBase")] + partial class InitialDataBase + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("dbo") + .HasAnnotation("ProductVersion", "8.0.7") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SM.Medication.Domain.Entities.Medication", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("MedicationId"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreatedAt") + .HasColumnType("datetime2") + .HasColumnName("CreatedAt"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)") + .HasColumnName("CreatedBy"); + + b.Property("ModifiedAt") + .HasColumnType("datetime2") + .HasColumnName("ModifiedAt"); + + b.Property("ModifiedBy") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)") + .HasColumnName("ModifiedBy"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)") + .HasColumnName("Name"); + + b.Property("Quantity") + .HasColumnType("int") + .HasColumnName("Quantity"); + + b.HasKey("Id"); + + b.ToTable("Medication", "dbo"); + + b.HasData( + new + { + Id = 1, + CreatedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2807), + CreatedBy = "DbContext", + ModifiedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2809), + ModifiedBy = "DbContex", + Name = "Paracetamol", + Quantity = 100 + }, + new + { + Id = 2, + CreatedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2811), + CreatedBy = "DbContext", + ModifiedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2811), + ModifiedBy = "DbContex", + Name = "Ibuprofen", + Quantity = 50 + }, + new + { + Id = 3, + CreatedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2813), + CreatedBy = "DbContext", + ModifiedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2813), + ModifiedBy = "DbContex", + Name = "Aspirin", + Quantity = 75 + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.cs b/src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.cs new file mode 100644 index 0000000..3141624 --- /dev/null +++ b/src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.cs @@ -0,0 +1,58 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace SM.Medication.Infrastructure.Migrations +{ + /// + public partial class InitialDataBase : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.EnsureSchema( + name: "dbo"); + + migrationBuilder.CreateTable( + name: "Medication", + schema: "dbo", + columns: table => new + { + MedicationId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: false), + Quantity = table.Column(type: "int", nullable: false), + CreatedBy = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: false), + ModifiedBy = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: false), + CreatedAt = table.Column(type: "datetime2", nullable: false), + ModifiedAt = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Medication", x => x.MedicationId); + }); + + migrationBuilder.InsertData( + schema: "dbo", + table: "Medication", + columns: new[] { "MedicationId", "CreatedAt", "CreatedBy", "ModifiedAt", "ModifiedBy", "Name", "Quantity" }, + values: new object[,] + { + { 1, new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2807), "DbContext", new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2809), "DbContex", "Paracetamol", 100 }, + { 2, new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2811), "DbContext", new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2811), "DbContex", "Ibuprofen", 50 }, + { 3, new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2813), "DbContext", new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2813), "DbContex", "Aspirin", 75 } + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Medication", + schema: "dbo"); + } + } +} diff --git a/src/SM.Medication.Infrastructure/Migrations/SmartMedMedicationDbContextModelSnapshot.cs b/src/SM.Medication.Infrastructure/Migrations/SmartMedMedicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..4d7206d --- /dev/null +++ b/src/SM.Medication.Infrastructure/Migrations/SmartMedMedicationDbContextModelSnapshot.cs @@ -0,0 +1,104 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SM.Medication.Infrastructure.Persistence; + +#nullable disable + +namespace SM.Medication.Infrastructure.Migrations +{ + [DbContext(typeof(SmartMedMedicationDbContext))] + partial class SmartMedMedicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("dbo") + .HasAnnotation("ProductVersion", "8.0.7") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SM.Medication.Domain.Entities.Medication", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("MedicationId"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreatedAt") + .HasColumnType("datetime2") + .HasColumnName("CreatedAt"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)") + .HasColumnName("CreatedBy"); + + b.Property("ModifiedAt") + .HasColumnType("datetime2") + .HasColumnName("ModifiedAt"); + + b.Property("ModifiedBy") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)") + .HasColumnName("ModifiedBy"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)") + .HasColumnName("Name"); + + b.Property("Quantity") + .HasColumnType("int") + .HasColumnName("Quantity"); + + b.HasKey("Id"); + + b.ToTable("Medication", "dbo"); + + b.HasData( + new + { + Id = 1, + CreatedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2807), + CreatedBy = "DbContext", + ModifiedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2809), + ModifiedBy = "DbContex", + Name = "Paracetamol", + Quantity = 100 + }, + new + { + Id = 2, + CreatedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2811), + CreatedBy = "DbContext", + ModifiedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2811), + ModifiedBy = "DbContex", + Name = "Ibuprofen", + Quantity = 50 + }, + new + { + Id = 3, + CreatedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2813), + CreatedBy = "DbContext", + ModifiedAt = new DateTime(2024, 7, 26, 8, 28, 30, 203, DateTimeKind.Utc).AddTicks(2813), + ModifiedBy = "DbContex", + Name = "Aspirin", + Quantity = 75 + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs b/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs index 01e0bf8..dad6143 100644 --- a/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs +++ b/src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; @@ -38,9 +33,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.ApplyConfigurationsFromAssembly(typeof(SmartMedMedicationDbContext).Assembly); modelBuilder.Entity().HasData( - new SM.Medication.Domain.Entities.Medication { Name = "Paracetamol", Quantity = 100, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }, - new SM.Medication.Domain.Entities.Medication { Name = "Ibuprofen", Quantity = 50, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }, - new SM.Medication.Domain.Entities.Medication { Name = "Aspirin", Quantity = 75, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }); + new SM.Medication.Domain.Entities.Medication { Id = 1, Name = "Paracetamol", Quantity = 100, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }, + new SM.Medication.Domain.Entities.Medication { Id = 2, Name = "Ibuprofen", Quantity = 50, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }, + new SM.Medication.Domain.Entities.Medication { Id = 3, Name = "Aspirin", Quantity = 75, CreatedAt = DateTime.UtcNow, CreatedBy = "DbContext", ModifiedAt = DateTime.UtcNow, ModifiedBy = "DbContex" }); } } diff --git a/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj b/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj index 0641d77..3f32827 100644 --- a/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj +++ b/src/SM.Medication.Infrastructure/SM.Medication.Infrastructure.csproj @@ -7,6 +7,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/SM.Medication.Api.Tests/SM.Medication.Api.Tests.csproj b/tests/SM.Medication.Api.Tests/SM.Medication.Api.Tests.csproj index 3aa9860..54470b6 100644 --- a/tests/SM.Medication.Api.Tests/SM.Medication.Api.Tests.csproj +++ b/tests/SM.Medication.Api.Tests/SM.Medication.Api.Tests.csproj @@ -10,10 +10,16 @@ - - - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/tests/SM.Medication.Auth.Tests/SM.Medication.Auth.Tests.csproj b/tests/SM.Medication.Auth.Tests/SM.Medication.Auth.Tests.csproj index 96192b6..fbf731f 100644 --- a/tests/SM.Medication.Auth.Tests/SM.Medication.Auth.Tests.csproj +++ b/tests/SM.Medication.Auth.Tests/SM.Medication.Auth.Tests.csproj @@ -10,11 +10,17 @@ - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +