public void putProduct(int id, Product product)
        {
            Product updatedProduct = getProductById(id);

            updatedProduct.Name             = product.Name;
            updatedProduct.Price            = product.Price;
            updatedProduct.InventoryCount   = product.InventoryCount;
            updatedProduct.CreatedDate      = product.CreatedDate;
            updatedProduct.LastModifiedDate = product.LastModifiedDate;
            updatedProduct.CategoryId       = product.CategoryId;
            updatedProduct.ManufacturerId   = product.ManufacturerId;
            db.Entry(updatedProduct).State  = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
Beispiel #2
0
        public HttpResponseMessage UpdateProducts([FromUri] string email, List <Product> products)
        {
            ProductPurchased purchased = new ProductPurchased();
            Product          product;
            decimal          count = 0;

            if (products == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
            }
            Sale sale = new Sale();

            sale.SaleDate = DateTime.Today;
            sale.Email    = email;
            db.Sales.Add(sale);
            foreach (var items in products)
            {
                sale.TotalAmount += items.InventoryCount * items.Price;
            }
            foreach (var item in products)
            {
                product = db.Products.Find(item.ProductId);
                product.InventoryCount = product.InventoryCount - item.InventoryCount;

                purchased.ProductId = item.ProductId;
                purchased.Quantity  = item.InventoryCount;
                purchased.SaleId    = sale.SaleId;
                db.ProductPurchased.Add(purchased);

                //db.Entry(product).State = EntityState.Modified;
                //db.Entry(purchased).State = EntityState.Modified;
                //db.Entry(sale).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(item.ProductId))
                    {
                        return(Request.CreateResponse(HttpStatusCode.NoContent));
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Beispiel #3
0
        public HttpResponseMessage GetApiKey(string email, string password)
        {
            var getUser = repo.getApiKey(email, password);
            var user    = db.Users.First(x => x.Email == email);

            if (user != null)
            {
                if (Crypto.VerifyHashedPassword(user.Password, password))
                {
                    FormsAuthentication.SetAuthCookie(user.Email, true);
                }
            }

            if (getUser == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid Email or Password"));
            }
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (getUser.ApiKey == null)
            {
                try
                {
                    getUser.ApiKey          = GetApiKey();
                    db.Entry(getUser).State = EntityState.Modified;
                    //db.Entry(getUser).CurrentValues.SetValues(getUser);
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Failed to save API key to database"));
                }
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Headers.Add("xcmps383authenticationid", getUser.UserId.ToString());
            response.Headers.Add("xcmps383authenticationkey", getUser.ApiKey);



            return(Request.CreateResponse(HttpStatusCode.OK, TheDTOFactory.Create(getUser.ApiKey, getUser.UserId)));


            //  return response;
        }