Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.{json,yml,yaml}]
indent_size = 2

[*.{csproj,props,targets,slnx}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.cs]
csharp_prefer_braces = when_multiline
dotnet_diagnostic.IDE0011.severity = suggestion

dotnet_style_namespace_match_folder = true
dotnet_diagnostic.IDE0130.severity = warning
13 changes: 13 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Normalize line endings to LF everywhere, regardless of core.autocrlf.
* text=auto eol=lf

# Formats that require a specific terminator.
*.sh text eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

# Never convert.
*.pdf binary
*.png binary
*.jpg binary
*.ico binary
10 changes: 10 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions MedicationsApi.slnx → Medications.slnx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Solution>
<Folder Name="/src/">
<Project Path="src\Medications.AppHost\Medications.AppHost.csproj" />
<Project Path="src\Medications.Api\MedicationsApi.csproj" />
<Project Path="src\Medications.Api\Medications.Api.csproj"/>
<Project Path="src\Medications.AppHost\Medications.AppHost.csproj"/>
</Folder>
</Solution>
5 changes: 5 additions & 0 deletions src/Medications.Api/Contracts/CreateMedicationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Medications.Api.Contracts;

public class CreateMedicationRequest

Check warning on line 3 in src/Medications.Api/Contracts/CreateMedicationRequest.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this empty class, write its code or make it an "interface".

See more on https://sonarcloud.io/project/issues?id=fernandotonacoder_medications-rest-api&issues=AZ-1BThErS4DKItMqxKv&open=AZ-1BThErS4DKItMqxKv&pullRequest=3
{
}
6 changes: 6 additions & 0 deletions src/Medications.Api/Contracts/CreateMedicationResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Medications.Api.Contracts;

public class CreateMedicationResponse
{
public int Id { get; set; }
}
9 changes: 9 additions & 0 deletions src/Medications.Api/Data/MedicationsDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Medications.Api.Entities;
using Microsoft.EntityFrameworkCore;

namespace Medications.Api.Data;

public class MedicationsDbContext(DbContextOptions<MedicationsDbContext> options) : DbContext(options)
{
public DbSet<Medication> Medications => Set<Medication>();
}
38 changes: 38 additions & 0 deletions src/Medications.Api/Endpoints/MedicationEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Medications.Api.Data;
using Medications.Api.Entities;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;

namespace Medications.Api.Endpoints;

public static class MedicationEndpoints
{
public static void MapMedicationEndpoints(this WebApplication app)
{
var group = app.MapGroup("/api/medications")
.WithTags("Medications");

group.MapGet("/", GetAllMedications)
.WithName("GetMedications")
.WithDescription("Retrieve all medications.");

group.MapGet("/{id}", GetMedication)
.WithName("GetMedication")
.WithDescription("Retrieve a specific medication by its ID.");
}

private static async Task<Ok<List<Medication>>> GetAllMedications(MedicationsDbContext dbContext)
{
var medications = await dbContext.Medications.ToListAsync();
return TypedResults.Ok(medications);
}

private static async Task<Results<Ok<Medication>, NotFound>> GetMedication(int id, MedicationsDbContext dbContext)
{
var medication = await dbContext.Medications.FindAsync(id);

if (medication is null) return TypedResults.NotFound();

return TypedResults.Ok(medication);
}
}
12 changes: 12 additions & 0 deletions src/Medications.Api/Entities/Medication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Medications.Api.Entities;

public class Medication
{
public int Id { get; set; }

public required string Name { get; set; }

public int Quantity { get; set; }

public required DateTimeOffset CreationDate { get; set; }
}
27 changes: 0 additions & 27 deletions src/Medications.Api/MedicationModel.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/Medications.Api/Medications.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory"/>
<PackageReference Include="Microsoft.OpenApi"/>
</ItemGroup>

</Project>
15 changes: 0 additions & 15 deletions src/Medications.Api/MedicationsApi.csproj

This file was deleted.

6 changes: 0 additions & 6 deletions src/Medications.Api/MedicationsApi.http

This file was deleted.

11 changes: 0 additions & 11 deletions src/Medications.Api/MedicationsDb.cs

This file was deleted.

11 changes: 4 additions & 7 deletions src/Medications.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using MedicationsApi;
using Medications.Api.Data;
using Medications.Api.Endpoints;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MedicationsDb>(opt => opt.UseInMemoryDatabase("Medications"));
builder.Services.AddDbContext<MedicationsDbContext>(opt => opt.UseInMemoryDatabase("Medications"));
builder.Services.AddOpenApi();
var app = builder.Build();

Expand All @@ -13,10 +14,6 @@

app.UseHttpsRedirection();

app.MapGet("/medications", async (MedicationsDb db) =>
{
await db.Medications.ToListAsync();
})
.WithName("GetMedications");
app.MapMedicationEndpoints();

await app.RunAsync();
5 changes: 3 additions & 2 deletions src/Medications.AppHost/AppHost.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Projects;
using Scalar.Aspire;

var builder = DistributedApplication.CreateBuilder(args);

var apiService = builder.AddProject<Projects.MedicationsApi>("medicationsapi");
var apiService = builder.AddProject<Medications_Api>("medications-api");

var scalar = builder.AddScalarApiReference("scalar", options =>
{
Expand All @@ -15,4 +16,4 @@
scalar
.WithApiReference(apiService);

await builder.Build().RunAsync();
await builder.Build().RunAsync();
23 changes: 10 additions & 13 deletions src/Medications.AppHost/Medications.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<Project Sdk="Aspire.AppHost.Sdk/13.4.6">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>dc8941e3-0794-4122-a9d7-f3c18746f746</UserSecretsId>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<UserSecretsId>dc8941e3-0794-4122-a9d7-f3c18746f746</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Medications.Api\MedicationsApi.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Medications.Api\Medications.Api.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Scalar.Aspire" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Scalar.Aspire"/>
</ItemGroup>

</Project>