begin new and last rework

This commit is contained in:
2024-07-18 14:44:29 +02:00
parent abfc997ac1
commit 1a976fc9ef
26 changed files with 444 additions and 101 deletions

View File

@@ -0,0 +1,25 @@
namespace OwnChar.ServerNew.Api.Endpoint;
internal class ApiBuilder(WebApplication app) : IApiBuilder
{
public void Map(string method, string pattern, Delegate action)
{
if (method == ApiRequestMethods.Get)
app.MapGet(pattern, action);
else if (method == ApiRequestMethods.Post)
app.MapPost(pattern, action);
else if (method == ApiRequestMethods.Put)
app.MapPut(pattern, action);
else if (method == ApiRequestMethods.Patch)
app.MapPatch(pattern, action);
else if (method == ApiRequestMethods.Delete)
app.MapDelete(pattern, action);
else
throw new NotSupportedException();
}
public void Map(string pattern, Delegate action)
{
app.Map(pattern, action);
}
}

View File

@@ -0,0 +1,12 @@
using System.Net;
namespace OwnChar.ServerNew.Api.Endpoint;
public static class ApiRequestMethods
{
public static string Get => WebRequestMethods.Http.Get;
public static string Put => WebRequestMethods.Http.Put;
public static string Post => WebRequestMethods.Http.Post;
public static string Patch => "PATCH";
public static string Delete => "DELETE";
}

View File

@@ -0,0 +1,6 @@
namespace OwnChar.ServerNew.Api.Endpoint;
public interface IApiBuilder
{
void Map(string path, Delegate action);
}

View File

@@ -0,0 +1,6 @@
namespace OwnChar.ServerNew.Api.Endpoint;
internal interface IApiEndpoint
{
void Initialize(IApiBuilder builder);
}

View File

@@ -0,0 +1,34 @@
namespace OwnChar.ServerNew.Api.Endpoint.Implementations;
internal class CharactersApi(IServer server) : IApiEndpoint
{
public void Initialize(IApiBuilder builder)
{
throw new NotImplementedException();
}
private IResult GetCharacter(long characterId)
{
}
private IResult CreateGroupCharacter(string name, long groupId)
{
}
private IResult CreateUserCharacter(string name, long userId)
{
}
private IResult UpdateCharacter(long characterId)
{
}
private IResult DeleteCharacter(long characterId)
{
}
}

View File

@@ -0,0 +1,48 @@
namespace OwnChar.ServerNew.Api.Endpoint.Implementations;
internal class GroupsApi(IServer server) : IApiEndpoint
{
public void Initialize(IApiBuilder builder)
{
}
private IResult GetGroups()
{
}
private IResult GetGroups(long characterId)
{
}
private IResult GetGroup(long groupId)
{
}
private IResult CreateGroup(string name)
{
}
private IResult UpdateGroup(int groupId, string name)
{
}
private IResult DeleteGroup(int groupId)
{
}
private IResult AddMember(int groupId, long memberId)
{
}
private IResult RemoveMember(int groupId, long memberId)
{
}
}

View File

@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using OwnChar.Api.Responses;
using OwnChar.Model;
namespace OwnChar.ServerNew.Api.Endpoint.Implementations;
internal class LoginApi(ServerContext server) : IApiEndpoint
{
public void Initialize(IApiBuilder builder)
{
builder.Map("/auth/login/{username}", Login);
builder.Map("/auth/logout/{secret}", Logout);
}
private IResult Login(string username, [FromHeader(Name = "X-USER-PASSWORD")] string password)
{
if (server.Data != null && server.Data.GetAll<UserAccount>()?.FirstOrDefault(n => n.Username == username && n.Password == password) is UserAccount account)
return TypedResults.Ok(new LoginResponse
{
Secret = server.Login(account),
UserAccount = account,
});
return TypedResults.Unauthorized();
}
private IResult Logout([FromHeader(Name = "X-AUTH-SECRET")] string secret)
{
server.Logout(secret);
return TypedResults.Ok();
}
}

View File

@@ -0,0 +1,44 @@
namespace OwnChar.ServerNew.Api.Endpoint.Implementations;
internal class UsersApi(IServer server) : IApiEndpoint
{
public void Initialize(IApiBuilder builder)
{
throw new NotImplementedException();
}
private IResult GetUsers()
{
}
private IResult GetUser(long userId)
{
}
private IResult GetUserProfile(long userId)
{
}
private IResult CreateUser(string username, string password)
{
}
private IResult DeleteUser(long userId)
{
}
private IResult UpdateUserPassword(long userId, string username, string password)
{
}
private IResult UpdateUserProfile(long profileId, string displayName)
{
}
}