Ejemplo n.º 1
0
 public async Task <HistoricalEntity> Update(HistoricalEntity historical)
 {
     var requestErrors = new string[]
     {
         Guid.TryParse(historical.Id, out var _) ? null : GlobalMessages.InvalidId(historical.Id),
         historical.Occurrence == DateTime.MinValue ? HistoricalsMessages.InvalidOccurrence(historical.Occurrence) : null,
     }.Where(e => e != null);
Ejemplo n.º 2
0
        public async Task <HistoricalEntity> Insert(HistoricalInputEntity historicalInput)
        {
            if (historicalInput.Occurrence == DateTime.MinValue)
            {
                throw new ApiException(HttpStatusCode.BadRequest, HistoricalsMessages.InvalidOccurrence(historicalInput.Occurrence));
            }

            var(leader, led) = await employeesService.ObtainPair(historicalInput?.LeaderId, historicalInput?.LedId);

            var historicalList = await historicalsRepository.ObtainByPair(historicalInput.LeaderId, historicalInput.LedId);

            var occurrenceObtained = historicalList?.FirstOrDefault(historical => historical.Occurrence.Date == historicalInput.Occurrence.Date);

            if (occurrenceObtained != null)
            {
                throw new ApiException(HttpStatusCode.Conflict, HistoricalsMessages.Conflict(leader.Email, led.Email, historicalInput.Occurrence));
            }

            _ = await oneononesService.ObtainByPair(leader.Id, led.Id);

            var historical = new HistoricalEntity(leader, led, historicalInput.Occurrence, historicalInput.Commentary);
            var inserted   = await historicalsRepository.Insert(historical);

            if (!inserted)
            {
                throw new ApiException(HttpStatusCode.InternalServerError, HistoricalsMessages.Insert(historical.Leader.Email, historical.Led.Email, historical.Occurrence));
            }

            return(historical);
        }
Ejemplo n.º 3
0
        public async Task <HistoricalEntity> ObtainByOccurrence(string leaderId, string ledId, DateTime occurrence)
        {
            var(leader, led) = await employeesService.ObtainPair(leaderId, ledId);

            if (occurrence == DateTime.MinValue)
            {
                throw new ApiException(HttpStatusCode.BadRequest, HistoricalsMessages.InvalidOccurrence(occurrence));
            }

            var historicalList = await historicalsRepository.ObtainByPair(leaderId, ledId);

            if (historicalList == null || !historicalList.Any())
            {
                throw new ApiException(HttpStatusCode.NotFound, HistoricalsMessages.Empty(leader.Email, led.Email));
            }

            var occurrenceObtained = historicalList.FirstOrDefault(historical => historical.Occurrence.Date == occurrence.Date);

            if (occurrenceObtained == null)
            {
                throw new ApiException(HttpStatusCode.NotFound, HistoricalsMessages.NotFound(leaderId, ledId, occurrence));
            }

            FillEmployees(occurrenceObtained, leader, led);
            return(occurrenceObtained);
        }
Ejemplo n.º 4
0
        public async Task <IList <HistoricalEntity> > ObtainByPair(string leaderId, string ledId)
        {
            var(leader, led) = await employeesService.ObtainPair(leaderId, ledId);

            var historicalList = await historicalsRepository.ObtainByPair(leaderId, ledId);

            if (historicalList == null || !historicalList.Any())
            {
                throw new ApiException(HttpStatusCode.NotFound, HistoricalsMessages.Empty(leader.Email, led.Email));
            }

            await FillEmployees(historicalList);

            return(historicalList.OrderByDescending(h => h.Occurrence).ToList());
        }
Ejemplo n.º 5
0
        public async Task <HistoricalEntity> ObtainByPairLast(string leaderId, string ledId)
        {
            var(leader, led) = await employeesService.ObtainPair(leaderId, ledId);

            var historicalList = await historicalsRepository.ObtainByPair(leaderId, ledId);

            if (historicalList == null || !historicalList.Any())
            {
                throw new ApiException(HttpStatusCode.NotFound, HistoricalsMessages.Empty(leader.Email, led.Email));
            }

            var occurrenceLast = historicalList.Max(historical => historical.Occurrence);
            var historicalLast = historicalList.FirstOrDefault(historical => historical.Occurrence.Date == occurrenceLast.Date);

            FillEmployees(historicalLast, leader, led);
            return(historicalLast);
        }
Ejemplo n.º 6
0
        public async Task <HistoricalEntity> Obtain(string id)
        {
            if (!Guid.TryParse(id, out var _))
            {
                throw new ApiException(HttpStatusCode.BadRequest, GlobalMessages.InvalidId(id));
            }

            var historical = await historicalsRepository.Obtain(id);

            if (historical == null)
            {
                throw new ApiException(HttpStatusCode.NotFound, HistoricalsMessages.NotFound(id));
            }

            await FillEmployees(historical);

            return(historical);
        }