示例#1
0
 public HttpResponseMessage Put(products res, int?ID = null)
 {
     using (SqlConnection con = DbUtil.GetConnection()) {
         SqlCommand com = new SqlCommand("API_products_put", con);
         com.CommandType = CommandType.StoredProcedure;
         SqlParameter RetVal = com.Parameters.Add("RetVal", SqlDbType.Int);
         RetVal.Direction = ParameterDirection.ReturnValue;
         com.Parameters.Add("ID", SqlDbType.Int).Value = ID;
         com.Parameters.Add("ProductName", SqlDbType.VarChar, 100).Value = res.ProductName;
         con.Open();
         com.ExecuteNonQuery();
         logger.Info("products_put:@ID={0}, @ProductName={1}, return={2}", ID, res.ProductName, RetVal.Value);
         if (0 == (int)RetVal.Value)
         {
             RetVal.Value = 200;
         }
         if (200 == (int)RetVal.Value || 201 == (int)RetVal.Value)
         {
             var response = Request.CreateResponse((HttpStatusCode)RetVal.Value, "null");
             return(response);
         }
         if (DbUtil.message.Length > 0)
         {
             return(Request.CreateErrorResponse((HttpStatusCode)RetVal.Value, DbUtil.message));
         }
         else
         {
             return(Request.CreateResponse((HttpStatusCode)RetVal.Value));
         }
     }
 }
示例#2
0
/// <summary>
/// Add a new product to the database
/// </summary>
/// <remarks> A link to the new product is added  </remarks>
/// <response code="201">OK</response>
/// <response code="521">Bad productname</response>
/// <response code="522">Product with name exists</response>
        public HttpResponseMessage  Post(products res)
        {
            if (res == null)
            {
                logger.Fatal("products_post: Cannot parse resource. Check parameters");
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
            int?NewId = null;

            using (SqlConnection con = DbUtil.GetConnection()) {
                SqlCommand com = new SqlCommand("API_products_post", con);
                com.CommandType = CommandType.StoredProcedure;
                SqlParameter RetVal = com.Parameters.Add("RetVal", SqlDbType.Int);
                RetVal.Direction = ParameterDirection.ReturnValue;
                com.Parameters.Add("ProductName", SqlDbType.VarChar, 100).Value = res.ProductName;
                com.Parameters.Add("NewId", SqlDbType.Int).Value = NewId;
                com.Parameters["NewId"].Direction = ParameterDirection.Output;
                try {
                    con.Open();
                    com.ExecuteNonQuery();
                } catch (Exception ex) {
                    logger.Info("products_post:@ProductName={0}, @NewId={1}, return={2}", res.ProductName, NewId, RetVal.Value);
                    logger.Fatal("products_post: SqlException:" + ex.Message);
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
                }
                logger.Info("products_post:@ProductName={0}, @NewId={1}, return={2}", res.ProductName, NewId, RetVal.Value);
                if (0 == (int)RetVal.Value)
                {
                    RetVal.Value = 200;
                }
                if (200 == (int)RetVal.Value || 201 == (int)RetVal.Value)
                {
                    var    response = Request.CreateResponse((HttpStatusCode)RetVal.Value, "null");
                    string uri      = Url.Link("DefaultApi", new { id = com.Parameters["NewID"].Value.ToString() });
                    response.Headers.Location = new Uri(uri);
                    return(response);
                }
                if (DbUtil.message.Length > 0)
                {
                    return(Request.CreateErrorResponse((HttpStatusCode)RetVal.Value, DbUtil.message));
                }
                else
                {
                    return(Request.CreateResponse((HttpStatusCode)RetVal.Value));
                }
            }
        }