Ejemplo n.º 1
0
        public async Task <IActionResult> GetByNameAndDate([FromBody] TaxRequestBody body)
        {
            if (body == null)
            {
                return(BadRequest("Request body cannot be empty"));
            }

            if (string.IsNullOrEmpty(body.Municipality) || body.Date == null)
            {
                return(BadRequest("Invalid request data"));
            }

            var tax = await _repository.FirstOrDefault(body.Municipality);

            if (tax == null)
            {
                return(NotFound($"Tax entry for {body.Municipality} not found"));
            }

            try
            {
                if (DateTime.TryParse(body.Date, out DateTime reqDate))
                {
                    var dateFound = false;

                    if (tax.Daily?.Dates?.ToList().Count > 0)
                    {
                        dateFound = tax.Daily.Dates.ToList().Any(d => d == reqDate);

                        if (dateFound)
                        {
                            return(Ok(tax.Daily.Value));
                        }
                    }

                    if (tax.Weekly != null)
                    {
                        dateFound = ValidateTimePeriod(reqDate, tax.Weekly.StartDate, tax.Weekly.EndDate);
                        if (dateFound)
                        {
                            return(Ok(tax.Weekly.Value));
                        }
                    }

                    if (tax.Monthly != null)
                    {
                        dateFound = ValidateTimePeriod(reqDate, tax.Monthly.StartDate, tax.Monthly.EndDate);
                        if (dateFound)
                        {
                            return(Ok(tax.Monthly.Value));
                        }
                    }

                    if (tax.Yearly != null)
                    {
                        dateFound = ValidateTimePeriod(reqDate, tax.Yearly.StartDate, tax.Yearly.EndDate);
                        if (dateFound)
                        {
                            return(Ok(tax.Yearly.Value));
                        }
                    }

                    return(NotFound($"Could not find a tax value for Municipality {body.Municipality} and date: {body.Date}"));
                }
                else
                {
                    return(BadRequest($"Invalid date: {body.Date}"));
                }
            }
            catch (Exception e)
            {
                return(NotFound($"Could not find an entry for Municipality {body.Municipality} and date: {body.Date}"));
            }
        }