Compare commits

...

3 Commits

Author SHA1 Message Date
7b779463bb feat: add converter from Dto.PairPeriodTime toEnpoint.PairPeriodTime and vice versa
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m51s
2024-05-28 06:59:28 +03:00
baedc667b7 feat: add PairPeriod for ApiDto 2024-05-28 06:57:54 +03:00
0e9bb04b96 feat: add setup token 2024-05-28 06:56:25 +03:00
5 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,22 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Mirea.Api.Dto.Common;
/// <summary>
/// Represents a pair of time periods.
/// </summary>
public class PairPeriodTime
{
/// <summary>
/// Gets or sets the start time of the period.
/// </summary>
[Required]
public TimeOnly Start { get; set; }
/// <summary>
/// Gets or sets the end time of the period.
/// </summary>
[Required]
public TimeOnly End { get; set; }
}

View File

@ -0,0 +1,9 @@
using System;
namespace Mirea.Api.Endpoint.Common.Interfaces;
public interface ISetupToken
{
bool MatchToken(ReadOnlySpan<byte> token);
void SetToken(ReadOnlySpan<byte> token);
}

View File

@ -0,0 +1,13 @@
using Mirea.Api.Endpoint.Configuration.General.Settings;
using System.Collections.Generic;
using System.Linq;
namespace Mirea.Api.Endpoint.Common.Services;
public static class PairPeriodTimeConverter
{
public static Dictionary<int, Dto.Common.PairPeriodTime> ConvertToDto(this IDictionary<int, ScheduleSettings.PairPeriodTime> pairPeriod) =>
pairPeriod.ToDictionary(kvp => kvp.Key, kvp => new Dto.Common.PairPeriodTime { Start = kvp.Value.Start, End = kvp.Value.End });
public static Dictionary<int, ScheduleSettings.PairPeriodTime> ConvertFromDto(this IDictionary<int, Dto.Common.PairPeriodTime> pairPeriod) => pairPeriod.ToDictionary(kvp => kvp.Key, kvp => new ScheduleSettings.PairPeriodTime(kvp.Value.Start, kvp.Value.End));
}

View File

@ -0,0 +1,28 @@
using Mirea.Api.Endpoint.Common.Interfaces;
using System;
namespace Mirea.Api.Endpoint.Configuration.General;
public class SetupTokenService : ISetupToken
{
public ReadOnlyMemory<byte>? Token { get; private set; }
public bool MatchToken(ReadOnlySpan<byte> token)
{
if (Token == null || token.Length != Token.Value.Length)
return false;
var token2 = Token.Value.Span;
int result = 0;
for (int i = 0; i < Token.Value.Length; i++)
result |= token2[i] ^ token[i];
return result == 0;
}
public void SetToken(ReadOnlySpan<byte> token)
{
Token = token.ToArray();
}
}

View File

@ -8,6 +8,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Mirea.Api.DataAccess.Application;
using Mirea.Api.DataAccess.Persistence;
using Mirea.Api.Endpoint.Common.Interfaces;
using Mirea.Api.Endpoint.Common.Services;
using Mirea.Api.Endpoint.Configuration;
using Mirea.Api.Endpoint.Configuration.General;
@ -50,6 +51,7 @@ public class Program
builder.Services.AddPersistence(builder.Configuration.Get<GeneralConfig>()?.DbSettings?.ConnectionStringSql ?? string.Empty);
builder.Services.AddControllers();
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>