kreta/Kreta.BusinessLogic/Classes/MobileApi/Naplo/V2/SubqueriesRepo/Post/OrarendPostSubqueries.cs
2024-03-13 00:33:46 +01:00

96 lines
4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Kreta.BusinessLogic.Classes.MobileApi.Naplo.V2.Co.Post.Naplozas;
using Kreta.BusinessLogic.Interfaces;
using Kreta.BusinessLogic.Logic.Naplozas;
using Kreta.BusinessLogic.Logic.Naplozas.Validacio;
using Kreta.BusinessLogic.Validation;
using Kreta.Client.CoreApi;
using Kreta.Core.ConnectionType;
using Kreta.Core.Validation.Exceptions;
using Kreta.Core.Validation.Exceptions.Enum;
using Kreta.Framework.Entities;
using Kreta.Resources;
namespace Kreta.BusinessLogic.Classes.MobileApi.Naplo.V2.SubqueriesRepo.Post
{
public static class OrarendPostSubqueries
{
public static List<OraNaplozasResponseCo> CreateOraNaplozas(int teacherId, int intezmenyId, string intezmenyAzonosito, int tanevId, List<OraNaplozasRequestCo> request, IFileServiceHelper fileServiceHelper, ICoreApiClient coreApiClient)
{
var response = new List<OraNaplozasResponseCo>();
if (request.Count < 1)
{
throw new ValidationException(ValidationErrorType.Undefined, ErrorResource.NincsNaplozandoOra);
}
var naploValidacioParameters = new NaploValidacioParameters(from: request.Min(x => x.Datum), to: request.Max(x => x.Datum).AddDays(1), new MobileConnectionType(teacherId, intezmenyId, intezmenyAzonosito, tanevId), felhasznaloSzerepkor: null);
var naploValidator = new NaploValidacio(naploValidacioParameters);
CheckAzonosElem(request);
return OraNaplozas();
List<OraNaplozasResponseCo> OraNaplozas()
{
foreach (var naplozandoOra in request)
{
try
{
var blValidator = new BlValidator(naplozandoOra);
if (!blValidator.IsValid)
{
throw blValidator.ConvertToValidationException();
}
var logicCo = new NaplozasMobilCo();
naplozandoOra.ConvertTo(logicCo);
var naplozasLogic = new NaplozasLogic(naploValidator, logicCo);
naplozasLogic.SaveNaplozas(fileServiceHelper, coreApiClient);
response.Add(new OraNaplozasResponseCo()
{
Exception = null,
MobilId = naplozandoOra.MobilId
});
}
catch (ValidationException execption)
{
response.Add(new OraNaplozasResponseCo()
{
Exception = execption,
MobilId = naplozandoOra.MobilId
});
}
catch (EntityNotFoundException)
{
response.Add(new OraNaplozasResponseCo()
{
Exception = new ValidationException(ValidationErrorType.Undefined, ErrorResource.NaplozasSikertelenAzOraMarNemLetezikAKivalasztottNapon),
MobilId = naplozandoOra.MobilId
});
}
}
return response;
}
}
private static void CheckAzonosElem(List<OraNaplozasRequestCo> request)
{
var tupleList = new List<(int OraRendiOraId, DateTime Date)>();
foreach (var naplozandoOra in request)
{
if (tupleList.Any(x => x.OraRendiOraId == naplozandoOra.OrarendiOraId && x.Date == naplozandoOra.Datum))
{
throw new ValidationException(ValidationErrorType.Undefined, ErrorResource.TobbszorosNaplozas);
}
if (naplozandoOra.OrarendiOraId.HasValue)
{
tupleList.Add((OraRendiOraId: naplozandoOra.OrarendiOraId.Value, Date: naplozandoOra.Datum));
}
}
}
}
}