public async Task <HttpResponseMessage> DeleteCustomer(int id) { log.Debug(string.Format("DeleteCustomer({0})", JsonConvert.SerializeObject(id, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); try { using (EncodeContext db = new EncodeContext()) { Customer customer = db.Customers.Find(id); if (customer == null) { return(this.Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Id")); } db.Customers.Remove(customer); await db.SaveChangesAsync(); #region ' Faulted MySQL Provider ' //MySql - Entity Framework update all the rows of the table so I use T-SQL Commands for the updating //string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["EncodeContext"].ConnectionString; //using (MySqlConnection cn = new MySqlConnection(connectionString)) //{ // MySqlCommand cmd = new MySqlCommand(); // cmd.Connection = cn; // string query = string.Format("DELETE FROM {0} WHERE Id = {1}", "customers.customer", id); // cmd.CommandText = query; // try // { // cn.Open(); // int numRowsUpdated = cmd.ExecuteNonQuery(); // cmd.Dispose(); // } // catch (MySql.Data.MySqlClient.MySqlException ex) // { // log.Debug(string.Format("DeleteCustomer()= Exception{0}", JsonConvert.SerializeObject(ex, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); // return InternalServerError(ex); // } //} #endregion log.Debug(string.Format("DeleteCustomer({0})=", JsonConvert.SerializeObject(customer, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(Request.CreateResponse <Customer>(HttpStatusCode.OK, customer)); } } catch (Exception ex) { log.Debug(string.Format("DeleteCustomer()= Exception{0}", JsonConvert.SerializeObject(ex, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public async Task <HttpResponseMessage> PostCustomer(Customer customer) { log.Debug(string.Format("PostCustomer({0})", JsonConvert.SerializeObject(customer, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); if (customer == null) { return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Customer Cannot be null")); } if (!ModelState.IsValid) { log.Debug(string.Format("PostCustomer()= BadRequest{0}", JsonConvert.SerializeObject(ModelState, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } try { using (EncodeContext db = new EncodeContext()) { if (Settings.Default.DumpSQL) { db.Database.Log = m => log.Info(m); } db.Customers.Add(customer); await db.SaveChangesAsync(); if (customer != null) { return(this.Request.CreateResponse <Customer>(HttpStatusCode.Created, customer)); } } } catch (Exception ex) { log.Debug(string.Format("PostCustomer()= Exception{0}", JsonConvert.SerializeObject(ex, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid Data")); }
public async Task <HttpResponseMessage> PutCustomer(int id, Customer customer) { log.Debug(string.Format("PutCustomer({0})", JsonConvert.SerializeObject(customer, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); if (customer == null) { return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Customer Cannot be null")); } if (!ModelState.IsValid) { log.Debug(string.Format("PutCustomer()= BadRequest{0}", JsonConvert.SerializeObject(ModelState, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (!CustomerExists(id)) { log.Debug(string.Format("PutCustomer()= NotFound{0}", JsonConvert.SerializeObject(id, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Id")); } try { using (EncodeContext db = new EncodeContext()) { Customer item = await db.Customers.FindAsync(id); if (item != null) { item.NumberOfEmployees = customer.NumberOfEmployees; item.Title = customer.Title; await db.SaveChangesAsync(); log.Debug(string.Format("PutCustomer({0})", JsonConvert.SerializeObject("Modified", Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateResponse(HttpStatusCode.NoContent)); } return(this.Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Id")); } #region ' Faulted MySQL Provider ' //MySql - Entity Framework update all the rows of the table so I use T-SQL Commands for the updating //string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["EncodeContext"].ConnectionString; //using (MySqlConnection cn = new MySqlConnection(connectionString)) //{ // MySqlCommand cmd = new MySqlCommand(); // cmd.Connection = cn; // string query = string.Format("UPDATE {0} SET Title = '{1}', NumberOfEmployees={2} WHERE Id = {3}", "customers.customer", customer.Title, customer.NumberOfEmployees, id); // cmd.CommandText = query; // try // { // cn.Open(); // int numRowsUpdated = cmd.ExecuteNonQuery(); // cmd.Dispose(); // } // catch (MySql.Data.MySqlClient.MySqlException ex) // { // log.Debug(string.Format("PutCustomer()= Exception{0}", JsonConvert.SerializeObject(ex, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); // return InternalServerError(ex); // } // log.Debug(string.Format("PutCustomer({0})", JsonConvert.SerializeObject("Modified", Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); // return StatusCode(HttpStatusCode.NoContent); //} #endregion } catch (Exception ex) { log.Debug(string.Format("PutCustomer()= Exception{0}", JsonConvert.SerializeObject(ex, Encode.API.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None))); return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }