Ejemplo n.º 1
0
        public IHttpActionResult GetAllProducts()
        {
            IList <Product> products = null;

            var ctx = new TrialCoreDBEntities();

            var queryToList = (from product in ctx.Products
                               select new
            {
                ProductId = product.ProductId,
                ProductName = product.ProductName,
                ProductDescr = product.ProductDescr,
                ProductRating = product.ProductRating,
                ProductReview = product.ProductReview
            }).ToList();

            products = queryToList.Select(s => new Product
            {
                ProductId     = s.ProductId,
                ProductName   = s.ProductName,
                ProductDescr  = s.ProductDescr,
                ProductRating = s.ProductRating,
                ProductReview = s.ProductReview
            }).ToList();



            if (products.Count == 0)
            {
                return(NotFound());
            }

            return(Ok(products));
        }
Ejemplo n.º 2
0
        public IHttpActionResult Put(UserViewModel user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new TrialCoreDBEntities())
            {
                var existingUser = ctx.Users.Where(s => s.UserId == user.UserId)
                                   .FirstOrDefault <User>();

                if (existingUser != null)
                {
                    existingUser.UserName = user.UserName;


                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
Ejemplo n.º 3
0
        public IHttpActionResult Put(Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new TrialCoreDBEntities())
            {
                var existingProduct = ctx.Products.Where(s => s.ProductId == product.ProductId)
                                      .FirstOrDefault <Product>();

                if (existingProduct != null)
                {
                    existingProduct.ProductName   = product.ProductName;
                    existingProduct.ProductDescr  = product.ProductDescr;
                    existingProduct.ProductRating = product.ProductRating;
                    existingProduct.ProductReview = product.ProductReview;


                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
Ejemplo n.º 4
0
        public IHttpActionResult Delete(int id)
        {
            if (id <= 0)
            {
                return(BadRequest("Not a valid user id"));
            }

            using (var ctx = new TrialCoreDBEntities())
            {
                var user = ctx.Users
                           .Where(s => s.UserId == id)
                           .FirstOrDefault();

                ctx.Entry(user).State = System.Data.Entity.EntityState.Deleted;
                ctx.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 5
0
        public IHttpActionResult PostNewStudent(UserViewModel user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new TrialCoreDBEntities())
            {
                ctx.Users.Add(new User()
                {
                    UserId   = user.UserId,
                    UserName = user.UserName
                });

                ctx.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 6
0
        public IHttpActionResult GetAllUsers()
        {
            IList <UserViewModel> users = null;

            using (var ctx = new TrialCoreDBEntities())
            {
                users = ctx.Users.Include("Users")
                        .Select(s => new UserViewModel()
                {
                    UserId   = s.UserId,
                    UserName = s.UserName
                }).ToList <UserViewModel>();
            }

            if (users.Count == 0)
            {
                return(NotFound());
            }

            return(Ok(users));
        }
Ejemplo n.º 7
0
        //public IHttpActionResult GetAllProducts(int productid)
        //{
        //    IList<Product> products = null;

        //    using (var ctx = new TrialCoreDBEntities())
        //    {
        //        products = ctx.Products
        //            .Where(s => s.ProductId == productid)
        //            .Select(s => new Product()
        //            {
        //                ProductId = s.ProductId,
        //                ProductName = s.ProductName,
        //                ProductDescr = s.ProductDescr,
        //                ProductRating = s.ProductRating,
        //                ProductReview = s.ProductReview

        //            }).ToList<Product>();
        //    }

        //    if (products.Count == 0)
        //    {
        //        return NotFound();
        //    }

        //    return Ok(products);
        //}


        public IHttpActionResult Post(Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new TrialCoreDBEntities())
            {
                ctx.Products.Add(new Product()
                {
                    ProductId     = product.ProductId,
                    ProductName   = product.ProductName,
                    ProductDescr  = product.ProductDescr,
                    ProductRating = product.ProductRating,
                    ProductReview = product.ProductReview
                });

                ctx.SaveChanges();
            }

            return(Ok());
        }