public async Task <bool> Update(int id, PutProperty property) { try { using (IDbConnection dbConnection = db) { var result = 0; var updateFields = UpdateStringConstruction(property); dbConnection.Open(); foreach (var updateField in updateFields) { string updateQuery = $"UPDATE dbo.Properties " + $"SET { updateField }" + $"WHERE PropertyId = { id }"; result = await dbConnection.ExecuteAsync(updateQuery, property); } return(result > 0 ? true : false); } } catch { return(false); } }
public async Task <IActionResult> Put(int id, [FromBody] PutProperty property) { var result = await repository.Update(id, property); if (!result) { return(BadRequest("record not updated")); } return(Ok()); }
private IEnumerable <string> UpdateStringConstruction(PutProperty property) { List <string> fields = new List <string>(); if (!string.IsNullOrWhiteSpace(property.Housenumber)) { fields.Add("Forename = @Housenumber "); } if (!string.IsNullOrWhiteSpace(property.Street)) { fields.Add("Surname = @Street "); } if (!string.IsNullOrWhiteSpace(property.Town)) { fields.Add("Email = @Town "); } if (!string.IsNullOrWhiteSpace(property.PostCode)) { fields.Add("PostCode = @PostCode "); } if (property.AvailableFrom != null) { fields.Add("AvailableFrom = @AvailableFrom "); } if (!string.IsNullOrWhiteSpace(property.Status)) { fields.Add("Status = @Status "); } if (property.LandlordId > 0) { fields.Add("Phone = @LandlordId "); } return(fields.ToArray()); }