diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d2fcd2b --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..5c314f5 --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..acd9a77 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,10 @@ + + + + net10.0 + enable + enable + true + + + diff --git a/MedicationsApi.slnx b/Medications.slnx similarity index 62% rename from MedicationsApi.slnx rename to Medications.slnx index 4fa1980..f4c7203 100644 --- a/MedicationsApi.slnx +++ b/Medications.slnx @@ -1,6 +1,6 @@ - - + + diff --git a/src/Medications.Api/Contracts/CreateMedicationRequest.cs b/src/Medications.Api/Contracts/CreateMedicationRequest.cs new file mode 100644 index 0000000..7d28a9f --- /dev/null +++ b/src/Medications.Api/Contracts/CreateMedicationRequest.cs @@ -0,0 +1,5 @@ +namespace Medications.Api.Contracts; + +public class CreateMedicationRequest +{ +} diff --git a/src/Medications.Api/Contracts/CreateMedicationResponse.cs b/src/Medications.Api/Contracts/CreateMedicationResponse.cs new file mode 100644 index 0000000..b39badb --- /dev/null +++ b/src/Medications.Api/Contracts/CreateMedicationResponse.cs @@ -0,0 +1,6 @@ +namespace Medications.Api.Contracts; + +public class CreateMedicationResponse +{ + public int Id { get; set; } +} diff --git a/src/Medications.Api/Data/MedicationsDbContext.cs b/src/Medications.Api/Data/MedicationsDbContext.cs new file mode 100644 index 0000000..b6103eb --- /dev/null +++ b/src/Medications.Api/Data/MedicationsDbContext.cs @@ -0,0 +1,9 @@ +using Medications.Api.Entities; +using Microsoft.EntityFrameworkCore; + +namespace Medications.Api.Data; + +public class MedicationsDbContext(DbContextOptions options) : DbContext(options) +{ + public DbSet Medications => Set(); +} diff --git a/src/Medications.Api/Endpoints/MedicationEndpoints.cs b/src/Medications.Api/Endpoints/MedicationEndpoints.cs new file mode 100644 index 0000000..1d043c9 --- /dev/null +++ b/src/Medications.Api/Endpoints/MedicationEndpoints.cs @@ -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>> GetAllMedications(MedicationsDbContext dbContext) + { + var medications = await dbContext.Medications.ToListAsync(); + return TypedResults.Ok(medications); + } + + private static async Task, NotFound>> GetMedication(int id, MedicationsDbContext dbContext) + { + var medication = await dbContext.Medications.FindAsync(id); + + if (medication is null) return TypedResults.NotFound(); + + return TypedResults.Ok(medication); + } +} diff --git a/src/Medications.Api/Entities/Medication.cs b/src/Medications.Api/Entities/Medication.cs new file mode 100644 index 0000000..26b1b86 --- /dev/null +++ b/src/Medications.Api/Entities/Medication.cs @@ -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; } +} diff --git a/src/Medications.Api/MedicationModel.cs b/src/Medications.Api/MedicationModel.cs deleted file mode 100644 index 1126b53..0000000 --- a/src/Medications.Api/MedicationModel.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace MedicationsApi; - -public class MedicationModel -{ - /// - /// Gets or sets the ID of the medication. - /// - [Key] - public int MedicationId { get; set; } - - /// - /// Gets or sets the name of the medication. - /// - public required string Name { get; set; } - - /// - /// Gets or sets the quantity of the medication. - /// - public int Quantity { get; set; } - - /// - /// Gets or sets the creation date of the medication. - /// - public DateTime CreationDate { get; set; } -} \ No newline at end of file diff --git a/src/Medications.Api/Medications.Api.csproj b/src/Medications.Api/Medications.Api.csproj new file mode 100644 index 0000000..08df76d --- /dev/null +++ b/src/Medications.Api/Medications.Api.csproj @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/Medications.Api/MedicationsApi.csproj b/src/Medications.Api/MedicationsApi.csproj deleted file mode 100644 index b446a8e..0000000 --- a/src/Medications.Api/MedicationsApi.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - net10.0 - enable - enable - - - - - - - - - diff --git a/src/Medications.Api/MedicationsApi.http b/src/Medications.Api/MedicationsApi.http deleted file mode 100644 index 02beb66..0000000 --- a/src/Medications.Api/MedicationsApi.http +++ /dev/null @@ -1,6 +0,0 @@ -@MedicationsApi_HostAddress = http://localhost:5122 - -GET {{MedicationsApi_HostAddress}}/weatherforecast/ -Accept: application/json - -### diff --git a/src/Medications.Api/MedicationsDb.cs b/src/Medications.Api/MedicationsDb.cs deleted file mode 100644 index a7cb3b8..0000000 --- a/src/Medications.Api/MedicationsDb.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.EntityFrameworkCore; - -namespace MedicationsApi; - -public class MedicationsDb : DbContext -{ - public MedicationsDb(DbContextOptions options) - : base(options) { } - - public DbSet Medications => Set(); -} \ No newline at end of file diff --git a/src/Medications.Api/Program.cs b/src/Medications.Api/Program.cs index 9f94c63..e143637 100644 --- a/src/Medications.Api/Program.cs +++ b/src/Medications.Api/Program.cs @@ -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(opt => opt.UseInMemoryDatabase("Medications")); +builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("Medications")); builder.Services.AddOpenApi(); var app = builder.Build(); @@ -13,10 +14,6 @@ app.UseHttpsRedirection(); -app.MapGet("/medications", async (MedicationsDb db) => -{ - await db.Medications.ToListAsync(); -}) -.WithName("GetMedications"); +app.MapMedicationEndpoints(); await app.RunAsync(); diff --git a/src/Medications.AppHost/AppHost.cs b/src/Medications.AppHost/AppHost.cs index 1864027..6f0f455 100644 --- a/src/Medications.AppHost/AppHost.cs +++ b/src/Medications.AppHost/AppHost.cs @@ -1,8 +1,9 @@ +using Projects; using Scalar.Aspire; var builder = DistributedApplication.CreateBuilder(args); -var apiService = builder.AddProject("medicationsapi"); +var apiService = builder.AddProject("medications-api"); var scalar = builder.AddScalarApiReference("scalar", options => { @@ -15,4 +16,4 @@ scalar .WithApiReference(apiService); -await builder.Build().RunAsync(); \ No newline at end of file +await builder.Build().RunAsync(); diff --git a/src/Medications.AppHost/Medications.AppHost.csproj b/src/Medications.AppHost/Medications.AppHost.csproj index 58e14ab..706261d 100644 --- a/src/Medications.AppHost/Medications.AppHost.csproj +++ b/src/Medications.AppHost/Medications.AppHost.csproj @@ -1,19 +1,16 @@ - - Exe - net10.0 - enable - enable - dc8941e3-0794-4122-a9d7-f3c18746f746 - + + Exe + dc8941e3-0794-4122-a9d7-f3c18746f746 + - - - + + + - - - + + +