public IActionResult Put([FromBody] BuildingUpdateModel building) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = unitOfWork.Building.UpdateOne(building, building.ID); return(Ok(new { result })); } catch (Exception ex) { Log.Error(ex, "Exceptions occurred in put building api"); return(StatusCode(500, ex.Message)); } }
public async Task <Response> UpdateById(int id, BuildingUpdateModel building) { building.AddressHashCode = building.Address.GetHashCode(); if (building.WaterMeterId == 0) { building.WaterMeterId = null; } else { BuildingModel overlapWaterMeterId = await _dbContext.Buildings.FirstOrDefaultAsync(b => b.WaterMeterId == building.WaterMeterId); if (overlapWaterMeterId != null && overlapWaterMeterId.Id != building.Id) { return new Response { Message = $"Счетчик с таким s/n уже установлен по адресу {overlapWaterMeterId.Address.ToString()}" } } ; } BuildingModel convertBuilding = Mapper.Map <BuildingUpdateModel, BuildingModel>(building); try { _dbContext.Buildings.AddOrUpdate(convertBuilding); await _dbContext.SaveChangesAsync(); return(new Response { Ok = true }); } catch { return(new Response { Message = "Произошла ошибка при добавлении. Пожалуйсте повторите попытку." }); } }
public async Task <IHttpActionResult> UpdateById(int id, BuildingUpdateModel building) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != building.Id || id == 0) { return(BadRequest()); } Response result = await _buildingsService.UpdateById(id, building); if (result.Ok) { return(Ok()); } else { return(BadRequest(result.Message)); } }
public bool UpdateOne(BuildingUpdateModel model, int id) { try { var building = new Building() { Name = model.Name, SchoolID = model.SchoolID }; Update(building, id); Log.Information("Updated building {0}", model.Name); return(true); } catch (System.Exception ex) { Log.Error(ex, "Exceptions occurred in building update"); return(false); } }