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 CreateOraNaplozas(int teacherId, int intezmenyId, string intezmenyAzonosito, int tanevId, List request, IFileServiceHelper fileServiceHelper, ICoreApiClient coreApiClient) { var response = new List(); 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 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 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)); } } } } }