try fix download client #2

This commit is contained in:
Flaminel
2025-06-15 02:45:49 +03:00
parent 3c2e36eb9e
commit 4b5f4dc447
7 changed files with 160 additions and 27 deletions

View File

@@ -12,6 +12,7 @@ using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using Executable.DTOs;
namespace Executable.Controllers;
@@ -92,24 +93,32 @@ public class ConfigurationController : ControllerBase
}
[HttpPost("download_client")]
public async Task<IActionResult> CreateDownloadClientConfig([FromBody] DownloadClientConfig newClient)
public async Task<IActionResult> CreateDownloadClientConfig([FromBody] CreateDownloadClientDto newClient)
{
if (newClient == null)
{
return BadRequest("Invalid download client data");
}
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newClient.Validate();
// Create the full config from the DTO
var clientConfig = new DownloadClientConfig
{
Enabled = newClient.Enabled,
Name = newClient.Name,
TypeName = newClient.TypeName,
Type = newClient.Type,
Host = newClient.Host,
Username = newClient.Username,
Password = newClient.Password,
UrlBase = newClient.UrlBase
};
// Add the new client to the database
_dataContext.DownloadClients.Add(newClient);
_dataContext.DownloadClients.Add(clientConfig);
await _dataContext.SaveChangesAsync();
return CreatedAtAction(nameof(GetDownloadClientConfig), new { id = newClient.Id }, newClient);
return CreatedAtAction(nameof(GetDownloadClientConfig), new { id = clientConfig.Id }, clientConfig);
}
catch (Exception ex)
{

View File

@@ -0,0 +1,66 @@
using Common.Enums;
using Common.Exceptions;
namespace Executable.DTOs;
/// <summary>
/// DTO for creating a new download client (without ID)
/// </summary>
public sealed record CreateDownloadClientDto
{
/// <summary>
/// Whether this client is enabled
/// </summary>
public bool Enabled { get; init; } = false;
/// <summary>
/// Friendly name for this client
/// </summary>
public required string Name { get; init; }
/// <summary>
/// Type name of download client
/// </summary>
public required DownloadClientTypeName TypeName { get; init; }
/// <summary>
/// Type of download client
/// </summary>
public required DownloadClientType Type { get; init; }
/// <summary>
/// Host address for the download client
/// </summary>
public Uri? Host { get; init; }
/// <summary>
/// Username for authentication
/// </summary>
public string? Username { get; init; }
/// <summary>
/// Password for authentication
/// </summary>
public string? Password { get; init; }
/// <summary>
/// The base URL path component, used by clients like Transmission and Deluge
/// </summary>
public string? UrlBase { get; init; }
/// <summary>
/// Validates the configuration
/// </summary>
public void Validate()
{
if (string.IsNullOrWhiteSpace(Name))
{
throw new ValidationException("Client name cannot be empty");
}
if (Host is null && TypeName is not DownloadClientTypeName.Usenet)
{
throw new ValidationException("Host cannot be empty for non-Usenet clients");
}
}
}