public IActionResult Update(int id, [FromBody] UpdateClientOptions options) { var result = clientService_.UpdateClient(id, options); if (!result.Success) { return(StatusCode((int)result.ErrorCode, result.ErrorText)); } return(Json(result)); }
public ApiResult <bool> UpdateClient(int?id, UpdateClientOptions options) { if (options == null) { return(ApiResult <bool> .Failed(StatusCode.BadRequest, "Null options")); } if (id == null) { return(ApiResult <bool> .Failed(StatusCode.BadRequest, "Id is empty")); } if (!Client.IsValidEmail(options.Email)) { return(ApiResult <bool> .Failed(StatusCode.BadRequest, "Email is not valid")); } var updateClient = context_ .Set <Client>() .Where(c => c.ClientId == id) .SingleOrDefault(); if (updateClient == null) { return(ApiResult <bool> .Failed( StatusCode.NotFound, $"Could not retrieve Client with this {id}!")); } if (options.Firstname != null) { updateClient.Firstname = options.Firstname; } if (options.Lastname != null) { updateClient.Lastname = options.Lastname; } if (options.Email != null) { updateClient.Email = options.Email; } if (options.Phone != null) { updateClient.Phone = options.Phone; } if (options.IsActive != null) { updateClient.IsActive = options.IsActive.Value; } try { var rows = context_.SaveChanges(); if (rows <= 0) { return(ApiResult <bool> .Failed( StatusCode.InternalServerError, "Client could not be updated")); } } catch (Exception ex) { return(ApiResult <bool> .Failed(StatusCode.InternalServerError, ex.ToString())); } return(ApiResult <bool> .Successful(true)); }