public async Task <IActionResult> Create([FromBody] MunicipalityTaxBody tax) { if (tax == null) { return(BadRequest("No tax data provided")); } try { var entity = RemapEntity(tax); await _repository.AddAsync(entity); return(Ok(tax)); } catch (Exception e) { return(BadRequest($"Invalid Request Body Model. Error: {e.Message}")); } }
private MunicipalityTax RemapEntity(MunicipalityTaxBody body) { var entity = new MunicipalityTax { Id = Guid.NewGuid(), Name = body.Name }; if (body.Weekly != null) { entity.Weekly = new PeriodicTax { Value = body.Weekly.Value, StartDate = DateTime.Parse(body.Weekly.StartDate), EndDate = DateTime.Parse(body.Weekly.EndDate) }; } if (body.Monthly != null) { entity.Monthly = new PeriodicTax { Value = body.Monthly.Value, StartDate = DateTime.Parse(body.Monthly.StartDate), EndDate = DateTime.Parse(body.Monthly.EndDate) }; } if (body.Yearly != null) { entity.Yearly = new PeriodicTax { Value = body.Yearly.Value, StartDate = DateTime.Parse(body.Yearly.StartDate), EndDate = DateTime.Parse(body.Yearly.EndDate) }; } if (body.Daily != null) { var listOfDates = body.Daily.Dates.ToList().Select(d => DateTime.Parse(d)); entity.Daily = new DailyTax { Value = body.Daily.Value, Dates = listOfDates }; } return(entity); }