Skip to content
Open
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
29 changes: 29 additions & 0 deletions Crypter.API/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2026 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
* Crypter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Crypter source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can be released from the requirements of the aforementioned license
* by purchasing a commercial license. Buying such a license is mandatory
* as soon as you develop commercial activities involving the Crypter source
* code without disclosing the source code of your own applications.
*
* Contact the current copyright holder to discuss commercial license options.
*/

using Immediate.Handlers.Shared;

[assembly: ImmediateAssemblyIdentifier("CrypterApi")]
120 changes: 0 additions & 120 deletions Crypter.API/Controllers/UserContactController.cs

This file was deleted.

2 changes: 2 additions & 0 deletions Crypter.API/Crypter.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Immediate.Apis" Version="6.2.0" />
<PackageReference Include="Immediate.Handlers" Version="3.11.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.10" />
</ItemGroup>
Expand Down
92 changes: 92 additions & 0 deletions Crypter.API/Endpoints/UserContacts/AddUserContactEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2026 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
* Crypter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Crypter source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can be released from the requirements of the aforementioned license
* by purchasing a commercial license. Buying such a license is mandatory
* as soon as you develop commercial activities involving the Crypter source
* code without disclosing the source code of your own applications.
*
* Contact the current copyright holder to discuss commercial license options.
*/

using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Crypter.API.Methods;
using Crypter.Common.Contracts;
using Crypter.Common.Contracts.Features.Contacts;
using Crypter.Core.Features.UserContacts.Commands;
using EasyMonads;
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Crypter.API.Endpoints.UserContacts;

[Handler]
[MapPost("api/user/contact")]
[Authorize]
public static partial class AddUserContactEndpoint
{
public sealed record Request
{
[FromQuery]
public string? Username { get; init; }
}

internal static void CustomizeEndpoint(RouteHandlerBuilder endpoint) =>
endpoint
.WithSummary("Add a user as a contact.")
.Produces<UserContact>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status401Unauthorized)
.Produces<ErrorResponse>(StatusCodes.Status400BadRequest)
.Produces<ErrorResponse>(StatusCodes.Status404NotFound)
.Produces<ErrorResponse>(StatusCodes.Status500InternalServerError);

private static async ValueTask<IResult> HandleAsync(
[AsParameters] Request request,
IHttpContextAccessor httpContextAccessor,
AddUserContactCommand.Handler handler,
CancellationToken cancellationToken)
{
Guid userId = EndpointUser.ParseUserId(httpContextAccessor);
AddUserContactCommand.Command command = new AddUserContactCommand.Command(userId, request.Username);

Either<AddUserContactError, UserContact> result = await handler.HandleAsync(command, cancellationToken);
return result.Match(
MakeErrorResponse,
x => Results.Ok(x),
MakeErrorResponse(AddUserContactError.UnknownError));
}

private static IResult MakeErrorResponse(AddUserContactError error)
{
#pragma warning disable CS8524
return error switch
{
AddUserContactError.UnknownError => EndpointResults.MakeErrorResponse(HttpStatusCode.InternalServerError, error),
AddUserContactError.NotFound => EndpointResults.MakeErrorResponse(HttpStatusCode.NotFound, error),
AddUserContactError.InvalidUser => EndpointResults.MakeErrorResponse(HttpStatusCode.BadRequest, error)
};
#pragma warning restore CS8524
}
}
65 changes: 65 additions & 0 deletions Crypter.API/Endpoints/UserContacts/GetUserContactsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2026 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
* Crypter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Crypter source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can be released from the requirements of the aforementioned license
* by purchasing a commercial license. Buying such a license is mandatory
* as soon as you develop commercial activities involving the Crypter source
* code without disclosing the source code of your own applications.
*
* Contact the current copyright holder to discuss commercial license options.
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Crypter.API.Methods;
using Crypter.Common.Contracts.Features.Contacts;
using Crypter.Core.Features.UserContacts.Queries;
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Crypter.API.Endpoints.UserContacts;

[Handler]
[MapGet("api/user/contact")]
[Authorize]
public static partial class GetUserContactsEndpoint
{
public sealed record Request;

internal static void CustomizeEndpoint(RouteHandlerBuilder endpoint) =>
endpoint
.WithSummary("Get a list of user contacts.")
.Produces<List<UserContact>>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status401Unauthorized);

private static async ValueTask<IResult> HandleAsync(
Request request,
IHttpContextAccessor httpContextAccessor,
GetUserContactsQuery.Handler handler,
CancellationToken cancellationToken)
{
Guid userId = EndpointUser.ParseUserId(httpContextAccessor);
List<UserContact> result = await handler.HandleAsync(new GetUserContactsQuery.Query(userId), cancellationToken);
return Results.Ok(result);
}
}
Loading
Loading