public int Insert(EchoAddRequest model) { int result = 0; using (SqlConnection conn = new SqlConnection(connectionString)) { string cmdText = "Echo_Insert"; using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { cmd.CommandType = CommandType.StoredProcedure; SqlParameter param = new SqlParameter(); param.ParameterName = "@Id"; param.SqlDbType = SqlDbType.Int; param.Direction = ParameterDirection.Output; cmd.Parameters.Add(param); cmd.Parameters.AddWithValue("@Echo", model.Echo); conn.Open(); cmd.ExecuteNonQuery(); result = (int)cmd.Parameters["@Id"].Value; conn.Close(); } } return(result); }
public void Insert_Test() { EchoAddRequest model = new EchoAddRequest { Echo = "ECHO Echo echo" }; EchoService svc = new EchoService(); int result = svc.Insert(model); Assert.IsTrue(result > 0, "The insert failed!"); }
public HttpResponseMessage Post([FromBody] EchoAddRequest model) { try { if (ModelState.IsValid) { EchoService svc = new EchoService(); ItemResponse <int> response = new ItemResponse <int>(); response.Item = svc.Insert(model); return(Request.CreateResponse(HttpStatusCode.OK, response)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState)); } } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } }