-
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.
- Loading branch information
Showing
19 changed files
with
295 additions
and
260 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
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 was deleted.
Oops, something went wrong.
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,6 @@ | ||
@Echo.OpenAPI_HostAddress = http://localhost:5000 | ||
|
||
GET {{Echo.OpenAPI_HostAddress}}/message/ | ||
Accept: application/json | ||
|
||
### |
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,25 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>25e57428-8ef1-40a9-b9e3-41d95a73b652</UserSecretsId> | ||
<RootNamespace>EchoApi</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.6" /> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" /> | ||
<PackageReference Include="Microsoft.AspNetCore.RateLimiting" Version="7.0.0-rc.2.22476.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="NSwag.AspNetCore" Version="14.0.7" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,106 @@ | ||
using EchoApi.Auth; | ||
using EchoApi.DAL; | ||
using EchoApi.Model; | ||
using EchoApi.Services; | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace EchoApi; | ||
|
||
public static class EndpointMappings | ||
{ | ||
public static void MapEchoApiV1(this IEndpointRouteBuilder group) | ||
{ | ||
group.MapGet("/healthz", () => Results.Ok()); | ||
group.MapPost("/token", (TokenService tokenService, [FromBody] UserCredentials credentials) => | ||
{ | ||
bool isValidUser = AuthenticateUser(credentials); | ||
|
||
if (isValidUser) | ||
{ | ||
var token = tokenService.GenerateToken(credentials.Username); | ||
return Results.Ok(new { token }); | ||
} | ||
else | ||
{ | ||
return Results.Unauthorized(); | ||
} | ||
}); | ||
|
||
group.MapGet("/api/messages", GetAllMessages); | ||
group.MapGet("/api/message/{id:int}", GetMessageById).WithOpenApi(); | ||
group.MapPost("/", CreateMessage).RequireAuthorization().WithOpenApi(); | ||
group.MapPut("/api/message/{id}", UpdateMessage).RequireAuthorization().WithOpenApi(); | ||
group.MapDelete("/api/message/{id}", DeleteMessage).RequireAuthorization().WithOpenApi(); | ||
} | ||
|
||
private static IResult GetAllMessages(IMessageRepository msgRepository) | ||
{ | ||
int MAX_MESSAGE_ITEMS = 10; | ||
return TypedResults.Ok(msgRepository.GetItems().Take(MAX_MESSAGE_ITEMS)); | ||
} | ||
|
||
private static IResult GetMessageById(int id, IMessageRepository msgRepository) | ||
{ | ||
var item = msgRepository.GetItem(id); | ||
return item != null ? TypedResults.Ok(item) : TypedResults.NotFound(); | ||
} | ||
|
||
private static IResult CreateMessage(Messages item, IMessageRepository msgRepository) | ||
{ | ||
msgRepository.AddItem(item); | ||
msgRepository.SaveChanges(); | ||
return Results.Created($"/api/message/{item.Id}", item); | ||
} | ||
|
||
private static IResult UpdateMessage(int id, Messages msgItem, IMessageRepository msgRepository) | ||
{ | ||
var existingItem = msgRepository.GetItem(id); | ||
|
||
if (existingItem is null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
existingItem.Message = msgItem.Message; | ||
|
||
msgRepository.UpdateItem(existingItem); | ||
msgRepository.SaveChanges(); | ||
return TypedResults.NoContent(); | ||
} | ||
|
||
private static IResult DeleteMessage(int id, IMessageRepository msgRepository) | ||
{ | ||
var existingItem = msgRepository.GetItem(id); | ||
|
||
if (existingItem is null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
msgRepository.RemoveItem(existingItem); | ||
msgRepository.SaveChanges(); | ||
return TypedResults.NoContent(); | ||
} | ||
|
||
/// <summary> | ||
/// Authenticates the user. | ||
/// </summary> | ||
/// <param name="credentials">The user credentials to authenticate.</param> | ||
/// <returns>True if the user is authenticated, otherwise false.</returns> | ||
private static bool AuthenticateUser(UserCredentials credentials) | ||
{ | ||
var USERNAME = Environment.GetEnvironmentVariable("USERNAME") ?? "admin"; //builder.Configuration["AppSettings:Authentication:Username"]; | ||
var PASSWORD = Environment.GetEnvironmentVariable("PASSWORD") ?? "admin123"; //builder.Configuration["AppSettings:Authentication:Password"]; | ||
|
||
if (credentials.Username != USERNAME || credentials.Password != PASSWORD) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
namespace EchoApi.Model | ||
{ | ||
public class Messages | ||
{ | ||
public int Id { get; private set; } | ||
public long UpdatedAt { get; private set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | ||
public required string Message { get; set; } | ||
} | ||
} |
Oops, something went wrong.