Esempio n. 1
0
        // GET: api/Manufacturers
        public HttpResponseMessage GetManufacturer()
        {
            List <ManufacturerDTO> ListOfManufacturers = new List <ManufacturerDTO>();

            foreach (var item in repo.GetManufacturers())
            {
                ListOfManufacturers.Add(TheDTOFactory.Create(item));
            }
            return(Request.CreateResponse(HttpStatusCode.OK, ListOfManufacturers));
        }
Esempio n. 2
0
        // GET: api/Categories
        public HttpResponseMessage GetCategory()
        {
            List <CategoryDTO> ListOfCategory = new List <CategoryDTO>();

            foreach (var item in repo.GetCategory())
            {
                ListOfCategory.Add(TheDTOFactory.Create(item));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, ListOfCategory));
        }
Esempio n. 3
0
        public HttpResponseMessage GetManufacturer(string name)
        {
            var manufacturer = repo.GetManufacturer(name);

            if (manufacturer == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "the category is not found"));
            }
            var factoryManufacturer = TheDTOFactory.Create(manufacturer);

            return(Request.CreateResponse(HttpStatusCode.OK, factoryManufacturer));
        }
Esempio n. 4
0
        public HttpResponseMessage GetCategory(int id)
        {
            var category = repo.GetCategory(id);

            if (category == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Category not found"));
            }
            var factoryCategory = TheDTOFactory.Create(category);

            return(Request.CreateResponse(HttpStatusCode.OK, factoryCategory));
        }
Esempio n. 5
0
        public HttpResponseMessage GetProduct(int id)
        {
            // find the product from the repo + database
            Product productFound = repo.getProductById(id);

            if (productFound == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Product not found"));
            }

            ProductDTO productfactoried = TheDTOFactory.Create(productFound);

            return(Request.CreateResponse(HttpStatusCode.OK, productfactoried));
        }
Esempio n. 6
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;
        }
Esempio n. 7
0
        public HttpResponseMessage GetProducts()
        {
            //  if (IsAuthorized(Request, new List<Role> { Role.Admin }))
            //{
            List <ProductDTO> ProductList = new List <ProductDTO>();

            foreach (var item in repo.GetAllProducts())
            {
                ProductList.Add(TheDTOFactory.Create(item));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, ProductList));


            // }
            // return Request.CreateResponse(HttpStatusCode.Unauthorized);
        }
Esempio n. 8
0
        public HttpResponseMessage PutProduct([FromBody] Product product)
        {
            if (ModelState.IsValid)
            {
                repo.createProduct(product);

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.ProductId }));


                ProductDTO productfactoried = TheDTOFactory.Create(product);

                return(Request.CreateResponse(HttpStatusCode.OK, productfactoried));
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }