public Municipality UpdateMunicipality(long id, UpdateMunicipalityRequest 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."); } if (!String.IsNullOrEmpty(request.Name)) { municipality.Name = request.Name; } if (request.YearlyTaxes != null) { municipality.YearlyTaxes = request.YearlyTaxes; } if (request.MonthlyTaxes != null) { municipality.MonthlyTaxes = request.MonthlyTaxes; } if (request.WeeklyTaxes != null) { municipality.WeeklyTaxes = request.WeeklyTaxes; } if (request.DailyTaxes != null) { municipality.DailyTaxes = request.DailyTaxes; } _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); }
public ActionResult <Municipality> UpdateMunicipality(long id, UpdateMunicipalityRequest request) { try { var updatedMunicipality = _repo.UpdateMunicipality(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)); } }