Add Ping backend
.NET 10 solution for Ping, a Discord style chat app: Domain, Application and Infrastructure projects plus the Api host. SignalR for chat and presence, LiveKit for call tokens, EF Core on Postgres, S3 compatible storage for attachments, and OIDC auth shared with the Musify Zitadel instance. Includes the Compose stack for local development.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using Mediator;
|
||||
using Ping.Application.Contracts;
|
||||
using Ping.Application.Servers.Responses;
|
||||
using Ping.Domain.Entities;
|
||||
using Ping.Domain.ValueObjects;
|
||||
|
||||
namespace Ping.Application.Servers
|
||||
{
|
||||
public record CreateServerCommand(long OwnerId, string Name) : ICommand<ServerResponse>;
|
||||
|
||||
public class CreateServerCommandHandler(IDatabase database)
|
||||
: ICommandHandler<CreateServerCommand, ServerResponse>
|
||||
{
|
||||
public async ValueTask<ServerResponse> Handle(CreateServerCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var server = new Server
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = request.Name,
|
||||
OwnerId = request.OwnerId,
|
||||
};
|
||||
await database.Servers.AddAsync(server, cancellationToken);
|
||||
|
||||
await database.ServerMembers.AddAsync(new ServerMember
|
||||
{
|
||||
ServerId = server.Id,
|
||||
UserId = request.OwnerId,
|
||||
Role = ServerMemberRole.Owner,
|
||||
}, cancellationToken);
|
||||
|
||||
await database.Channels.AddAsync(new Channel
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ServerId = server.Id,
|
||||
Name = "general",
|
||||
Type = ChannelType.Text,
|
||||
Position = 0,
|
||||
}, cancellationToken);
|
||||
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
return ServerResponse.FromEntity(server);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Mediator;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Ping.Application.Contracts;
|
||||
using Ping.Application.Servers.Responses;
|
||||
|
||||
namespace Ping.Application.Servers
|
||||
{
|
||||
public record GetUserServersQuery(long UserId) : IQuery<IReadOnlyList<ServerResponse>>;
|
||||
|
||||
public class GetUserServersQueryHandler(IDatabase database)
|
||||
: IQueryHandler<GetUserServersQuery, IReadOnlyList<ServerResponse>>
|
||||
{
|
||||
public async ValueTask<IReadOnlyList<ServerResponse>> Handle(GetUserServersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var servers = await database.ServerMembers.AsNoTracking()
|
||||
.Where(m => m.UserId == request.UserId)
|
||||
.Select(m => m.Server!)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return servers.Select(ServerResponse.FromEntity).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Ping.Application.Serialization;
|
||||
using Ping.Domain.Entities;
|
||||
|
||||
namespace Ping.Application.Servers.Responses
|
||||
{
|
||||
public record ServerResponse(Guid Id, string Name, string? IconUrl, [property: JsonConverter(typeof(LongAsStringConverter))] long OwnerId)
|
||||
{
|
||||
public static ServerResponse FromEntity(Server server) =>
|
||||
new(server.Id, server.Name, server.IconUrl, server.OwnerId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user