Skip to content

Commit

Permalink
Merge pull request #5 from lucasdaquina/feat/db-setup
Browse files Browse the repository at this point in the history
Feat/db setup
  • Loading branch information
lucasdaquina authored Jul 26, 2024
2 parents 60209b1 + e87ff61 commit b173e5c
Show file tree
Hide file tree
Showing 17 changed files with 530 additions and 13 deletions.
23 changes: 22 additions & 1 deletion SM.Medication.Api.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
33 changes: 30 additions & 3 deletions src/SM.Medication.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Microsoft.AspNetCore.Localization;
using System.Globalization;
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;

Expand All @@ -12,6 +16,9 @@

builder.Services.SetupOpenApi();

builder.Services.AddDbContext<SmartMedMedicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MedicationDb")));

builder.Services.AddAuthentication(options =>
options.DefaultScheme = AuthSchemeConstants.SmartMedAuthScheme)
.AddScheme<SMAuthSchemeOptions, SmartMedAuthenticationHandler>(AuthSchemeConstants.SmartMedAuthScheme, options => { });
Expand All @@ -20,13 +27,33 @@

var app = builder.Build();

app.MapGet("/", () => "Hello World!")
.WithTags("Home")
//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();
.RequireAuthorization(); ;

if (app.Environment.IsDevelopment())
{
Expand Down
12 changes: 11 additions & 1 deletion src/SM.Medication.Api/SM.Medication.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -7,9 +7,17 @@
<UserSecretsId>db35fc35-788e-4166-b5f9-b68fb7c2d7fe</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.15" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.6.2" />
Expand All @@ -19,7 +27,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SM.Medication.Application\SM.Medication.Application.csproj" />
<ProjectReference Include="..\SM.Medication.Auth\SM.Medication.Auth.csproj" />
<ProjectReference Include="..\SM.Medication.Infrastructure\SM.Medication.Infrastructure.csproj" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/SM.Medication.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"MedicationDb": "Server=localhost;Database=master;User Id=sa;Password=Password123456;TrustServerCertificate=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
13 changes: 13 additions & 0 deletions src/SM.Medication.Application/SM.Medication.Application.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SM.Medication.Domain\SM.Medication.Domain.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/SM.Medication.Domain/Common/AuditableEntity.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
6 changes: 6 additions & 0 deletions src/SM.Medication.Domain/Common/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SM.Medication.Domain.Common;

public class BaseEntity : AuditableEntity
{
public int Id { get; set; }
}
24 changes: 24 additions & 0 deletions src/SM.Medication.Domain/Entities/Medication.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
12 changes: 12 additions & 0 deletions src/SM.Medication.Domain/SM.Medication.Domain.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
43 changes: 43 additions & 0 deletions src/SM.Medication.Infrastructure/Mapping/MedicationMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace SM.Medication.Infrastructure.Mapping;
public class MedicationMapping : IEntityTypeConfiguration<SM.Medication.Domain.Entities.Medication>
{
public void Configure(EntityTypeBuilder<SM.Medication.Domain.Entities.Medication> 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();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b173e5c

Please sign in to comment.