public ActionResult <Municipality> ScheduleTaxMunicipality(long id, ScheduleTaxRequest request) { try { var updatedMunicipality = _repo.ScheduleTaxMunicipality(id, request); return(Ok(updatedMunicipality)); } catch (NotFoundException e) { return(NotFound(e.Message)); } catch (BadRequestException e) { return(BadRequest(e.Message)); } catch (Exception e) { return(StatusCode(500, e.Message)); } }
public Municipality ScheduleTaxMunicipality(long id, ScheduleTaxRequest request) { var municipality = _context.Municipalities .Include(m => m.YearlyTaxes) .Include(m => m.MonthlyTaxes) .Include(m => m.WeeklyTaxes) .Include(m => m.DailyTaxes) .FirstOrDefault(m => m.Id == id); if (municipality == null) { throw new NotFoundException("Municipality with Id: " + id + " has not been found."); } switch (request.Type) { case 'Y': case 'y': municipality.YearlyTaxes = InsertTaxIntoList(municipality.YearlyTaxes, new Tax(request.Value.Value, request.InitialDate.Value, request.FinalDate.Value)); break; case 'M': case 'm': municipality.MonthlyTaxes = InsertTaxIntoList(municipality.MonthlyTaxes, new Tax(request.Value.Value, request.InitialDate.Value, request.FinalDate.Value)); break; case 'W': case 'w': municipality.WeeklyTaxes = InsertTaxIntoList(municipality.WeeklyTaxes, new Tax(request.Value.Value, request.InitialDate.Value, request.FinalDate.Value)); break; case 'D': case 'd': municipality.DailyTaxes = InsertTaxIntoList(municipality.DailyTaxes, new Tax(request.Value.Value, request.InitialDate.Value, request.FinalDate.Value)); break; default: throw new BadRequestException("Invalid tax type provided."); } _context.Entry(municipality).State = EntityState.Modified; try { _context.SaveChanges(); } catch (DbUpdateException) { if (!MunicipalityExists(id)) { throw new NotFoundException("Municipality with Id: " + id + " has not been found"); } throw; } return(municipality); }