-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from lucasdaquina/feat/get-medications
Feat/get-medications
- Loading branch information
Showing
24 changed files
with
199 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#! /bin/bash | ||
|
||
echo 'Assuming you have docker installed on your machine. If not, please install docker first.' | ||
|
||
echo 'Pulling the latest version of SQL Server 2019 on Ubuntu 16.04 from Microsoft Container Registry (MCR)' | ||
docker pull mcr.microsoft.com/mssql/server | ||
|
||
echo 'Running the SQL Server 2019 container' | ||
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Password123456" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest | ||
|
||
echo 'SQL Server 2019 container is running' | ||
|
||
echo 'run the command "update-database" inside VisualStudio click Tools -> NuGet Package Manager -> Package Manager Console' | ||
echo 'Select the "Default project" as "src\Application' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
# sm-medication-api | ||
SmartMed Back End code challenge | ||
|
||
|
||
Steps to run api: | ||
|
||
1. Clone the repository | ||
2. Open the project in your favorite IDE (Visual Studio) | ||
3. Open terminal/cmd and navigate to the solution folder | ||
3. Run Init.sh with the command .\Init.sh to pull docker sql server image and run the container | ||
4. After the sql server's running | ||
- On VisualStudio click Tools -> NuGet Package Manager -> Package Manager Console | ||
- Select the "Default project" as "src\Application" | ||
- run the command dotnet ef database update --project .\src\SM.Medication.Infrastructure\SM.Medication.Infrastructure.csproj --startup-project .\src\SM.Medication.Api\SM.Medication.Api.csproj" | ||
|
||
Now the Solution is ready, with a base SQL Server DB running with 3 medications on the medication table already. | ||
|
||
PS: The token to call the API is "SmartMed eyAiVG9rZW4iOiAiMTIzIiwgIlJvbGUiOiAiQWRtaW4ifQ==" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace SM.Medication.Api.EndPoints; | ||
|
||
public static class MedicationEndPoints | ||
{ | ||
private const string MEDICATION_TAG = "Medication"; | ||
public static void MapMedicationEndPoints(this WebApplication app) | ||
{ | ||
app.MapGet("/medications", | ||
async (IMedicationHandler handler) => | ||
{ | ||
return await handler.Handle(); | ||
}) | ||
.WithTags(MEDICATION_TAG) | ||
.WithMetadata(new SwaggerOperationAttribute(MEDICATION_TAG, "Get List of Medications")) | ||
.WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status200OK, "Success!")) | ||
.WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status401Unauthorized, "You're not Authorized!")) | ||
.WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status500InternalServerError, "Failed!")) | ||
.RequireAuthorization(); ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Reflection; | ||
using Mapster; | ||
using MapsterMapper; | ||
|
||
namespace SM.Medication.Api.Extensions; | ||
|
||
public static class MapsterExtensions | ||
{ | ||
public static void SetupMapster(this WebApplicationBuilder builder) | ||
{ | ||
var typeAdapterConfig = TypeAdapterConfig.GlobalSettings; | ||
|
||
// Scan the current assembly | ||
typeAdapterConfig.Scan(Assembly.GetExecutingAssembly()); | ||
|
||
// Scan assemblies of referenced projects | ||
var referencedAssemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); | ||
foreach (var assemblyName in referencedAssemblies) | ||
{ | ||
var assembly = Assembly.Load(assemblyName); | ||
typeAdapterConfig.Scan(assembly); | ||
} | ||
|
||
var mapperConfig = new Mapper(typeAdapterConfig); | ||
builder.Services.AddSingleton<IMapper>(mapperConfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace SM.Medication.Api.Extensions; | ||
|
||
public static class OpenAiExtensions | ||
{ | ||
public static void SetupOpenApi(this WebApplicationBuilder builder) | ||
{ | ||
var securityScheme = new OpenApiSecurityScheme() | ||
{ | ||
Name = "Authorization", | ||
Type = SecuritySchemeType.ApiKey, | ||
Scheme = AuthSchemeConstants.SmartMedAuthScheme, | ||
BearerFormat = "JWT", | ||
In = ParameterLocation.Header, | ||
Description = "JSON Web Token based security" | ||
}; | ||
|
||
var securityRequirement = new OpenApiSecurityRequirement(); | ||
var secondSecurityDefinition = new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = AuthSchemeConstants.SmartMedAuthScheme | ||
} | ||
}; | ||
securityRequirement.Add(secondSecurityDefinition, []); | ||
|
||
var info = new OpenApiInfo() | ||
{ | ||
Version = "v1", | ||
Title = "SmartMed Medication API", | ||
Description = "Medication API service for any communication regard medicament.", | ||
TermsOfService = new Uri("https://www.smartmed.world"), | ||
}; | ||
|
||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(action => | ||
{ | ||
action.SwaggerDoc("v1", info); | ||
action.AddSecurityDefinition(AuthSchemeConstants.SmartMedAuthScheme, securityScheme); | ||
action.AddSecurityRequirement(securityRequirement); | ||
action.EnableAnnotations(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global using SM.Medication.Application.Interfaces; | ||
global using SmartMed.Medication.Auth.Constants; | ||
global using Swashbuckle.AspNetCore.Annotations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using SM.Medication.Domain.DTO; |
16 changes: 16 additions & 0 deletions
16
src/SM.Medication.Application/Handlers/MedicationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using MapsterMapper; | ||
using SM.Medication.Application.Interfaces; | ||
using SM.Medication.Domain.Interfaces; | ||
|
||
namespace SM.Medication.Application.Handlers; | ||
|
||
public class MedicationHandler( | ||
IMedicationRepository medicationRepository, | ||
IMapper mapper) : IMedicationHandler | ||
{ | ||
public async Task<List<MedicationDTO>> Handle() | ||
{ | ||
var medications = await medicationRepository.GetAll(); | ||
return mapper.Map<List<MedicationDTO>>(medications); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/SM.Medication.Application/Interfaces/IMedicationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace SM.Medication.Application.Interfaces; | ||
public interface IMedicationHandler | ||
{ | ||
Task<List<MedicationDTO>> Handle(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global using Microsoft.AspNetCore.Authentication; | ||
global using Microsoft.AspNetCore.Http; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SM.Medication.Domain.DTO; | ||
|
||
public class MedicationDTO | ||
{ | ||
public string? Name { get; set; } | ||
public int Quantity { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SM.Medication.Domain.Interfaces; | ||
|
||
public interface IMedicationRepository | ||
{ | ||
Task<List<Domain.Entities.Medication>> GetAll(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global using Microsoft.EntityFrameworkCore; | ||
global using Microsoft.EntityFrameworkCore.Infrastructure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/SM.Medication.Infrastructure/Migrations/20240726082831_InitialDataBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
2 changes: 0 additions & 2 deletions
2
src/SM.Medication.Infrastructure/Persistence/SmartMedMedicationDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/SM.Medication.Infrastructure/Services/Repositories/MedicationRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using SM.Medication.Domain.Interfaces; | ||
using SM.Medication.Infrastructure.Persistence; | ||
|
||
namespace SM.Medication.Infrastructure.Services.Repositories; | ||
|
||
public class MedicationRepository(SmartMedMedicationDbContext context) : IMedicationRepository | ||
{ | ||
public async Task<List<Domain.Entities.Medication>> GetAll() | ||
{ | ||
return await context | ||
.Medications | ||
.ToListAsync(); | ||
} | ||
} |