Exemplo n.º 1
0
        public async Task <IActionResult> CreatePeriod([FromRoute] int budgetId, [FromBody] PeriodRequest request)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "id").Value;

            // check if user exists
            var userExists = await _identityService.CheckIfUserExists(userId);

            if (!userExists)
            {
                return(NotFound(new ErrorResponse(new ErrorModel {
                    Message = $"There is no user with id: {userId}"
                })));
            }

            // check if new period budgetId is correct
            var budget = await _budgetService.GetBudgetAsync(budgetId);

            if (budget == null)
            {
                return(NotFound(new ErrorResponse(new ErrorModel {
                    Message = $"There is no budget with id: {budgetId}"
                })));
            }

            if (budget.UserId != userId)
            {
                return(Forbid());
            }


            // Check if new period dates are between any other period dates
            var periodList = await _periodService.GetBudgetPeriodsAsync(budgetId);

            if (periodList.Any(x => x.FirstDay <= request.FirstDay && x.LastDay >= request.FirstDay))
            {
                return(BadRequest(new ErrorResponse(new ErrorModel {
                    FieldName = nameof(request.FirstDay), Message = "First day of new period cannot be between other period dates"
                })));
            }
            if (periodList.Any(x => x.FirstDay <= request.LastDay && x.LastDay >= request.LastDay))
            {
                return(BadRequest(new ErrorResponse(new ErrorModel {
                    FieldName = nameof(request.LastDay), Message = "Last day of new period cannot be between other period dates"
                })));
            }

            // create new period
            var period = await _periodService.NewPeriodAsync(request, userId);

            if (period == null)
            {
                return(BadRequest(new ErrorResponse(new ErrorModel {
                    Message = "Could not create new period"
                })));
            }

            var locationUri = _uriService.GetPeriodUri(budgetId, period.Id);

            return(Created(locationUri, new Response <PeriodResponse>(_mapper.Map <PeriodResponse>(period))));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> List([FromQuery] PeriodRequest request)
        {
            var list = await _mediator.Send(new PeriodListQuery(
                                                request.SubMonths,
                                                request.NextMonths
                                                ));

            return(Ok(list));
        }
Exemplo n.º 3
0
        public async Task <Period> NewPeriodAsync(PeriodRequest periodRequest, string userId)
        {
            var periodDto = new PeriodDto
            {
                UserId      = userId,
                CreatedAt   = DateTime.UtcNow,
                DisplayName = periodRequest.DisplayName,
                FirstDay    = periodRequest.FirstDay,
                LastDay     = periodRequest.LastDay,
                Notes       = periodRequest.Notes,
                BudgetId    = periodRequest.BudgetId
            };

            var period = _mapper.Map <Period>(periodDto);

            var created = await _baseRepository.CreateEntityAsync(period);

            return(created);
        }
Exemplo n.º 4
0
        public override async Task GetAllStationsStatisticForChartAsync(PeriodRequest request, IServerStreamWriter <InvertorProducingStatistic> responseStream, ServerCallContext context)
        {
            try
            {
                List <InvertorProducingStatistic> statistics = db.InvertorProducingStatistics.Where(x => x.Date >= request.FromDate && x.Date <= request.ToDate).ToList();

                foreach (InvertorProducingStatistic item in statistics)
                {
                    if (powerInMW)
                    {
                        item.ProducedEnergy     /= 1000;
                        item.PredictedProducing /= 1000;
                    }
                    await responseStream.WriteAsync(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                await responseStream.WriteAsync(new InvertorProducingStatistic());
            }
        }
Exemplo n.º 5
0
        public async Task <CustomersStatisticListResponse> GetCustomerStatisticsAsync(PeriodRequest periodRequest)
        {
            var result = await _statisticsService.GetCustomersStatisticsByDayAsync(periodRequest.FromDate, periodRequest.ToDate);

            return(_mapper.Map <CustomersStatisticListResponse>(result));
        }
Exemplo n.º 6
0
        public async Task <TokensStatisticListResponse> GetByDaysAsync([FromQuery] PeriodRequest request)
        {
            var result = await _tokensStatisticsService.GetTokensStatsForPeriod(request.FromDate, request.ToDate);

            return(_mapper.Map <TokensStatisticListResponse>(result));
        }