Exemple #1
0
        public async Task <HttpResponseMessage> DeleteProduct(int id)
        {
            try
            {
                using (var product = new ProductsEntities())
                {
                    var entity = await product.Products.FindAsync(id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Product with {id} is not found on the database."));
                    }
                    else
                    {
                        product.Products.Remove(entity);
                        await product.SaveChangesAsync();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemple #2
0
        public async Task <HttpResponseMessage> AddProduct([FromBody] Product product)
        {
            try
            {
                using (var products = new ProductsEntities())
                {
                    products.Products.Add(product);
                    await products.SaveChangesAsync();

                    var message = Request.CreateResponse(HttpStatusCode.Created, product);
                    message.Headers.Location = new Uri(Request.RequestUri + product.Id.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemple #3
0
 public async Task AddAsync(TEntity obj)
 {
     table.Add(obj);
     await db.SaveChangesAsync();
 }