mirror of
https://github.com/Sonarr/Sonarr
synced 2026-05-09 05:40:53 +02:00
Convert QualityDefinitionLimits to static
This commit is contained in:
parent
2f193ac58a
commit
9549038121
6 changed files with 31 additions and 43 deletions
|
|
@ -1,4 +1,4 @@
|
|||
using FluentValidation.TestHelper;
|
||||
using FluentValidation.TestHelper;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Qualities;
|
||||
using Sonarr.Api.V3.Qualities;
|
||||
|
|
@ -8,19 +8,12 @@ namespace NzbDrone.Api.Test.v3.Qualities;
|
|||
[Parallelizable(ParallelScope.All)]
|
||||
public class QualityDefinitionResourceValidatorTests
|
||||
{
|
||||
private readonly QualityDefinitionResourceValidator _validator;
|
||||
private readonly QualityDefinitionLimits _limits;
|
||||
|
||||
public QualityDefinitionResourceValidatorTests()
|
||||
{
|
||||
_limits = new QualityDefinitionLimits();
|
||||
_validator = new QualityDefinitionResourceValidator(_limits);
|
||||
}
|
||||
private readonly QualityDefinitionResourceValidator _validator = new ();
|
||||
|
||||
[Test]
|
||||
public void Validate_fails_when_min_size_is_below_min_limit()
|
||||
{
|
||||
var resource = new QualityDefinitionResource { MinSize = _limits.MinLimit - 1 };
|
||||
var resource = new QualityDefinitionResource { MinSize = QualityDefinitionLimits.Min - 1 };
|
||||
|
||||
var result = _validator.TestValidate(resource);
|
||||
|
||||
|
|
@ -48,8 +41,8 @@ public void Validate_passes_when_min_size_is_within_limits()
|
|||
{
|
||||
var resource = new QualityDefinitionResource
|
||||
{
|
||||
MinSize = _limits.MinLimit,
|
||||
PreferredSize = _limits.MaxLimit
|
||||
MinSize = QualityDefinitionLimits.Min,
|
||||
PreferredSize = QualityDefinitionLimits.Max
|
||||
};
|
||||
|
||||
var result = _validator.TestValidate(resource);
|
||||
|
|
@ -75,7 +68,7 @@ public void Validate_fails_when_max_size_is_below_preferred_size()
|
|||
[Test]
|
||||
public void Validate_fails_when_max_size_exceeds_max_limit()
|
||||
{
|
||||
var resource = new QualityDefinitionResource { MaxSize = _limits.MaxLimit + 1 };
|
||||
var resource = new QualityDefinitionResource { MaxSize = QualityDefinitionLimits.Max + 1 };
|
||||
|
||||
var result = _validator.TestValidate(resource);
|
||||
|
||||
|
|
@ -88,8 +81,8 @@ public void Validate_passes_when_max_size_is_within_limits()
|
|||
{
|
||||
var resource = new QualityDefinitionResource
|
||||
{
|
||||
MaxSize = _limits.MaxLimit,
|
||||
PreferredSize = _limits.MinLimit
|
||||
MaxSize = QualityDefinitionLimits.Max,
|
||||
PreferredSize = QualityDefinitionLimits.Min
|
||||
};
|
||||
|
||||
var result = _validator.TestValidate(resource);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace NzbDrone.Core.Qualities;
|
||||
|
||||
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification =
|
||||
"Serializable properties of a DTO")]
|
||||
public record QualityDefinitionLimits
|
||||
public static class QualityDefinitionLimits
|
||||
{
|
||||
public int MinLimit => 0;
|
||||
public int MaxLimit => 1000;
|
||||
public const int Min = 0;
|
||||
public const int Max = 1000;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ public interface IQualityDefinitionService
|
|||
List<QualityDefinition> All();
|
||||
QualityDefinition GetById(int id);
|
||||
QualityDefinition Get(Quality quality);
|
||||
QualityDefinitionLimits GetLimits();
|
||||
}
|
||||
|
||||
public class QualityDefinitionService : IQualityDefinitionService, IExecute<ResetQualityDefinitionsCommand>, IHandle<ApplicationStartedEvent>
|
||||
|
|
@ -65,11 +64,6 @@ public QualityDefinition Get(Quality quality)
|
|||
return GetAll()[quality];
|
||||
}
|
||||
|
||||
public QualityDefinitionLimits GetLimits()
|
||||
{
|
||||
return new QualityDefinitionLimits();
|
||||
}
|
||||
|
||||
private void InsertMissingDefinitions()
|
||||
{
|
||||
var insertList = new List<QualityDefinition>();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Api.V3.Qualities;
|
||||
using NzbDrone.Core.Datastore.Events;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Qualities;
|
||||
|
|
@ -21,14 +22,8 @@ public QualityDefinitionController(IQualityDefinitionService qualityDefinitionSe
|
|||
{
|
||||
_qualityDefinitionService = qualityDefinitionService;
|
||||
|
||||
SetupValidation(qualityDefinitionService);
|
||||
}
|
||||
|
||||
private void SetupValidation(IQualityDefinitionService qualityDefinitionService)
|
||||
{
|
||||
var limits = qualityDefinitionService.GetLimits();
|
||||
SharedValidator.RuleFor(c => c)
|
||||
.SetValidator(new QualityDefinitionResourceValidator(limits));
|
||||
.SetValidator(new QualityDefinitionResourceValidator());
|
||||
}
|
||||
|
||||
[RestPutById]
|
||||
|
|
@ -65,9 +60,9 @@ public object UpdateMany([FromBody] List<QualityDefinitionResource> resource)
|
|||
}
|
||||
|
||||
[HttpGet("limits")]
|
||||
public ActionResult<QualityDefinitionLimits> GetLimits()
|
||||
public ActionResult<QualityDefinitionLimitsResource> GetLimits()
|
||||
{
|
||||
return Ok(_qualityDefinitionService.GetLimits());
|
||||
return Ok(new QualityDefinitionLimitsResource());
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
using NzbDrone.Core.Qualities;
|
||||
|
||||
namespace NzbDrone.Api.V3.Qualities
|
||||
{
|
||||
public class QualityDefinitionLimitsResource
|
||||
{
|
||||
public int Min { get; set; } = QualityDefinitionLimits.Min;
|
||||
public int Max { get; set; } = QualityDefinitionLimits.Max;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +1,27 @@
|
|||
using FluentValidation;
|
||||
using FluentValidation;
|
||||
using NzbDrone.Core.Qualities;
|
||||
|
||||
namespace Sonarr.Api.V3.Qualities;
|
||||
|
||||
public class QualityDefinitionResourceValidator : AbstractValidator<QualityDefinitionResource>
|
||||
{
|
||||
public QualityDefinitionResourceValidator(QualityDefinitionLimits limits)
|
||||
public QualityDefinitionResourceValidator()
|
||||
{
|
||||
When(c => c.MinSize is not null, () =>
|
||||
{
|
||||
RuleFor(c => c.MinSize)
|
||||
.GreaterThanOrEqualTo(limits.MinLimit)
|
||||
.GreaterThanOrEqualTo(QualityDefinitionLimits.Min)
|
||||
.WithErrorCode("GreaterThanOrEqualTo")
|
||||
.LessThanOrEqualTo(c => c.PreferredSize ?? limits.MaxLimit)
|
||||
.LessThanOrEqualTo(c => c.PreferredSize ?? QualityDefinitionLimits.Max)
|
||||
.WithErrorCode("LessThanOrEqualTo");
|
||||
});
|
||||
|
||||
When(c => c.MaxSize is not null, () =>
|
||||
{
|
||||
RuleFor(c => c.MaxSize)
|
||||
.GreaterThanOrEqualTo(c => c.PreferredSize ?? limits.MinLimit)
|
||||
.GreaterThanOrEqualTo(c => c.PreferredSize ?? QualityDefinitionLimits.Min)
|
||||
.WithErrorCode("GreaterThanOrEqualTo")
|
||||
.LessThanOrEqualTo(limits.MaxLimit)
|
||||
.LessThanOrEqualTo(QualityDefinitionLimits.Max)
|
||||
.WithErrorCode("LessThanOrEqualTo");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue