public IHttpActionResult Patchcompany(CompanyPatch companyPatch) { //CompanyPatch companyPatch string error_message = "patch錯誤,請至伺服器log查詢錯誤訊息"; //拿已登入的流水 string token = Request.Headers.Authorization.Parameter; JwtAuthUtil jwtAuthUtil = new JwtAuthUtil(); string userseq = jwtAuthUtil.Getuserseq(token); if (ModelState.IsValid) { using (var transaction1 = db.Database.BeginTransaction()) { try { Company company = db.Company.Find(userseq); company.introduce = companyPatch.introduce; company.morning = Convert.ToBoolean(companyPatch.morning); company.afternoon = Convert.ToBoolean(companyPatch.afternoon); company.night = Convert.ToBoolean(companyPatch.night); company.midnight = Convert.ToBoolean(companyPatch.midnight); company.bannerimg = companyPatch.bannerimg; db.Entry(company).State = EntityState.Modified; db.SaveChanges(); transaction1.Commit(); } catch (Exception ex)//失敗 { transaction1.Rollback(); Utility.log("Patchcompany", ex.ToString()); return(Ok(new { result = error_message//修改失敗 })); } return(Ok(new { result = "修改成功" })); } } else { return(Ok(new { result = ModelState })); } }
/// <summary> /// Updates a single Company with one or more values /// </summary> /// <param name="updatedCompany">The new data for the Company you wish to update</param> /// <returns>Returns a result indicating if the operation succeeded</returns> public async Task <Result> UpdateCompany(CompanyPatch updatedCompany) { try { using (var con = new Npgsql.NpgsqlConnection(settings.Connection.DatabaseConnectionString)) { var sqlPatchOperations = new StringBuilder(); var obj = updatedCompany; var operationCount = 0; if (obj.CompanyName != null) { sqlPatchOperations.AppendLine(obj.CompanyName.Operation == OperationKind.Remove ? "companyName = NULL," : "companyName = @CompanyName," ); operationCount++; } if (obj.CompanyAddress != null) { sqlPatchOperations.AppendLine(obj.CompanyAddress.Operation == OperationKind.Remove ? "companyAddress = NULL," : "companyAddress = @CompanyAddress," ); operationCount++; } if (obj.CompanyPostcode != null) { sqlPatchOperations.AppendLine(obj.CompanyPostcode.Operation == OperationKind.Remove ? "companyPostcode = NULL," : "companyPostcode = @CompanyPostcode," ); operationCount++; } if (obj.CompanyContact != null) { sqlPatchOperations.AppendLine(obj.CompanyContact.Operation == OperationKind.Remove ? "companyContact = NULL," : "companyContact = @CompanyContact," ); operationCount++; } if (obj.CompanyPhone != null) { sqlPatchOperations.AppendLine(obj.CompanyPhone.Operation == OperationKind.Remove ? "companyPhone = NULL," : "companyPhone = @CompanyPhone," ); operationCount++; } if (obj.CompanyEmail != null) { sqlPatchOperations.AppendLine(obj.CompanyEmail.Operation == OperationKind.Remove ? "companyEmail = NULL," : "companyEmail = @CompanyEmail," ); operationCount++; } if (obj.ExternalAccountId != null) { sqlPatchOperations.AppendLine(obj.ExternalAccountId.Operation == OperationKind.Remove ? "externalAccountId = NULL," : "externalAccountId = @ExternalAccountId," ); operationCount++; } if (obj.ExternalCustomerId != null) { sqlPatchOperations.AppendLine(obj.ExternalCustomerId.Operation == OperationKind.Remove ? "externalCustomerId = NULL," : "externalCustomerId = @ExternalCustomerId," ); operationCount++; } if (obj.ExternalAccessToken != null) { sqlPatchOperations.AppendLine(obj.ExternalAccessToken.Operation == OperationKind.Remove ? "externalAccessToken = NULL," : "externalAccessToken = @ExternalAccessToken," ); operationCount++; } if (obj.ExternalRefreshToken != null) { sqlPatchOperations.AppendLine(obj.ExternalRefreshToken.Operation == OperationKind.Remove ? "externalRefreshToken = NULL," : "externalRefreshToken = @ExternalRefreshToken," ); operationCount++; } if (obj.ExternalTokenExpiry != null) { sqlPatchOperations.AppendLine(obj.ExternalTokenExpiry.Operation == OperationKind.Remove ? "externalTokenExpiry = NULL," : "externalTokenExpiry = @ExternalTokenExpiry," ); operationCount++; } if (obj.ReferenceCode != null) { sqlPatchOperations.AppendLine(obj.ReferenceCode.Operation == OperationKind.Remove ? "referenceCode = NULL," : "referenceCode = @ReferenceCode," ); operationCount++; } var patchOperations = sqlPatchOperations.ToString(); if (operationCount > 0) { // Remove final ", " from StringBuilder to ensure query is valid patchOperations = patchOperations.TrimEnd(System.Environment.NewLine.ToCharArray()); patchOperations = patchOperations.TrimEnd(','); } await con.ExecuteAsync($"UPDATE \"Company\" SET {patchOperations} WHERE companyId = @ResourceId", new { ResourceId = obj.ResourceId, CompanyName = (string)(obj.CompanyName == default ? default : obj.CompanyName.Value), CompanyAddress = (string)(obj.CompanyAddress == default ? default : obj.CompanyAddress.Value), CompanyPostcode = (string)(obj.CompanyPostcode == default ? default : obj.CompanyPostcode.Value), CompanyContact = (string)(obj.CompanyContact == default ? default : obj.CompanyContact.Value), CompanyPhone = (string)(obj.CompanyPhone == default ? default : obj.CompanyPhone.Value), CompanyEmail = (string)(obj.CompanyEmail == default ? default : obj.CompanyEmail.Value), ExternalAccountId = (string)(obj.ExternalAccountId == default ? default : obj.ExternalAccountId.Value), ExternalCustomerId = (string)(obj.ExternalCustomerId == default ? default : obj.ExternalCustomerId.Value), ExternalAccessToken = (string)(obj.ExternalAccessToken == default ? default : obj.ExternalAccessToken.Value), ExternalRefreshToken = (string)(obj.ExternalRefreshToken == default ? default : obj.ExternalRefreshToken.Value), ExternalTokenExpiry = (DateTime)(obj.ExternalTokenExpiry == default ? default : obj.ExternalTokenExpiry.Value), ReferenceCode = (string)(obj.ReferenceCode == default ? default : obj.ReferenceCode.Value) }).ConfigureAwait(false); return(Result.Ok()); }
/// <summary> /// update the Company update the Company /// </summary> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="id">the id of the Company</param> /// <param name="company">the data to update</param> /// <returns>Task of ApiResponse (PatchResponse)</returns> public async System.Threading.Tasks.Task <ApiResponse <PatchResponse> > UpdateCompanyAsyncWithHttpInfo(string id, CompanyPatch company) { // verify the required parameter 'id' is set if (id == null) { throw new ApiException(400, "Missing required parameter 'id' when calling CompanyApi->UpdateCompany"); } // verify the required parameter 'company' is set if (company == null) { throw new ApiException(400, "Missing required parameter 'company' when calling CompanyApi->UpdateCompany"); } var localVarPath = "/companies/{id}"; var localVarPathParams = new Dictionary <String, String>(); var localVarQueryParams = new Dictionary <String, String>(); var localVarHeaderParams = new Dictionary <String, String>(Configuration.DefaultHeader); var localVarFormParams = new Dictionary <String, String>(); var localVarFileParams = new Dictionary <String, FileParameter>(); Object localVarPostBody = null; // to determine the Content-Type header String[] localVarHttpContentTypes = new String[] { }; String localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); // to determine the Accept header String[] localVarHttpHeaderAccepts = new String[] { "application/json" }; String localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); if (localVarHttpHeaderAccept != null) { localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); } // set "format" to json by default // e.g. /pet/{petId}.{format} becomes /pet/{petId}.json localVarPathParams.Add("format", "json"); if (id != null) { localVarPathParams.Add("id", Configuration.ApiClient.ParameterToString(id)); // path parameter } if (company != null && company.GetType() != typeof(byte[])) { localVarPostBody = Configuration.ApiClient.Serialize(company); // http body (model) parameter } else { localVarPostBody = company; // byte array } // authentication (OAuth) required // oauth required if (!String.IsNullOrEmpty(Configuration.AccessToken)) { localVarHeaderParams["Authorization"] = "Bearer " + Configuration.AccessToken; } // make the HTTP request IRestResponse localVarResponse = (IRestResponse)await Configuration.ApiClient.CallApiAsync(localVarPath, Method.PATCH, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarPathParams, localVarHttpContentType); int localVarStatusCode = (int)localVarResponse.StatusCode; if (localVarStatusCode >= 400) { throw new ApiException(localVarStatusCode, "Error calling UpdateCompany: " + localVarResponse.Content, localVarResponse.Content); } else if (localVarStatusCode == 0) { throw new ApiException(localVarStatusCode, "Error calling UpdateCompany: " + localVarResponse.ErrorMessage, localVarResponse.ErrorMessage); } return(new ApiResponse <PatchResponse>(localVarStatusCode, localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), (PatchResponse)Configuration.ApiClient.Deserialize(localVarResponse, typeof(PatchResponse)))); }
/// <summary> /// update the Company update the Company /// </summary> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="id">the id of the Company</param> /// <param name="company">the data to update</param> /// <returns>Task of PatchResponse</returns> public async System.Threading.Tasks.Task <PatchResponse> UpdateCompanyAsync(string id, CompanyPatch company) { ApiResponse <PatchResponse> localVarResponse = await UpdateCompanyAsyncWithHttpInfo(id, company); return(localVarResponse.Data); }
/// <summary> /// update the Company update the Company /// </summary> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="id">the id of the Company</param> /// <param name="company">the data to update</param> /// <returns>PatchResponse</returns> public PatchResponse UpdateCompany(string id, CompanyPatch company) { ApiResponse <PatchResponse> localVarResponse = UpdateCompanyWithHttpInfo(id, company); return(localVarResponse.Data); }