Beispiel #1
0
        public int AddToOrders(int reservedID, string adress, string phone, string e_mail)
        {
            using (var context = new eshopContext())
            {
                var reserved = context.Reserved.FirstOrDefault(res => res.ReservedId == reservedID);

                if (reserved != null)
                {
                    reserved.ReservedOrdered = true;
                    context.SaveChanges();

                    Orders order = new Orders();

                    order.CartId           = reserved.CartId;
                    order.CategoryId       = reserved.CategoryId;
                    order.ProductId        = reserved.ProductId;
                    order.UserId           = reserved.UserId;
                    order.ReservedId       = reserved.ReservedId;
                    order.OrderDate        = DateTime.Now;
                    order.OrderAdress      = adress;
                    order.OrderPhoneNumber = phone;
                    order.OrderEmail       = e_mail;

                    context.Orders.Add(order);
                }

                return(context.SaveChanges());
            }
        }
Beispiel #2
0
        public int JsonStringBody([FromBody] RegistrationModel content)
        {
            using (var context = new eshopContext())
            {
                if (!context.Users.ToList().Any(mail => mail.EMail == content.EMail))
                {
                    Users newUser = new Users();
                    {
                        newUser.FirstName = content.FirstName;
                        newUser.LastName  = content.LastName;
                        newUser.BirthDate = content.BirthDate;
                        newUser.EMail     = content.EMail;
                        newUser.Password  = CreateMD5(content.Password);

                        context.Users.Add(newUser);
                        context.SaveChanges();

                        RoleList roleList = new RoleList();

                        roleList.UserId = newUser.UserId;
                        roleList.RoleId = context.Roles.FirstOrDefault(r => r.RoleName.Equals("User")).RoleId;

                        Cart cart = new Cart();
                        cart.UserId      = newUser.UserId;
                        cart.CartAddDate = DateTime.Now;

                        context.Cart.Add(cart);
                        context.RoleList.Add(roleList);
                    }
                }

                return(context.SaveChanges());
            }
        }
Beispiel #3
0
        public int AddToCart(int ID, int prodID, int amount)
        {
            using (var context = new eshopContext())
            {
                var product = context.Products.FirstOrDefault(prod => prod.ProductId == prodID);

                if (product != null && amount > 0 && amount <= product.ProductAmount)
                {
                    product.ProductAmount -= amount;
                    context.SaveChanges();

                    Reserved reserved = new Reserved();

                    reserved.ProductId       = prodID;
                    reserved.CategoryId      = product.CategoryId;
                    reserved.UserId          = ID;
                    reserved.CartId          = context.Cart.FirstOrDefault(cart => cart.UserId == ID).CartId;
                    reserved.ReservedAmount  = amount;
                    reserved.ReservedOrdered = false;

                    context.Reserved.Add(reserved);
                }


                return(context.SaveChanges());
            }
        }
Beispiel #4
0
        public string JsonStringBody([FromBody] EditProductModel content)
        {
            using (var context = new eshopContext())
            {
                var productDB = context.Products.FirstOrDefault(item => item.ProductId == content.productId);

                if (productDB != null && productDB.CategoryId != content.category)
                {
                    var reserved = context.Reserved.Where(item => item.ProductId == content.productId);
                    var ordered  = context.Orders.Where(i => i.ProductId == content.productId);
                    context.Reserved.RemoveRange(reserved);
                    context.Orders.RemoveRange(ordered);
                    context.Products.Remove(productDB);
                    context.SaveChanges();

                    Products product = new Products();
                    product.ProductName        = content.tittle;
                    product.ProductPrice       = content.price;
                    product.ProductAmount      = content.amount;
                    product.ProductDescription = content.description;
                    product.ProductPhoto       = content.photo;
                    product.CategoryId         = content.category;

                    context.Products.Add(product);
                    context.SaveChanges();

                    return(product.ProductId.ToString());
                }
                else
                {
                    productDB.ProductName        = content.tittle;
                    productDB.ProductPrice       = content.price;
                    productDB.ProductAmount      = content.amount;
                    productDB.ProductDescription = content.description;
                    productDB.ProductPhoto       = content.photo;

                    context.SaveChanges();

                    return(productDB.ProductId.ToString());
                }
            }
            return(null);
        }
Beispiel #5
0
 public int JsonStringBody([FromBody] CategoryModel content)
 {
     using (var context = new eshopContext())
     {
         Category category = new Category();
         category.CategoryName = content.categoryName;
         context.Category.Add(category);
         return(context.SaveChanges());
     }
 }
Beispiel #6
0
        public int BlockUser([FromBody] UserManagmentModel content)
        {
            using (var context = new eshopContext())
            {
                var itemToRemove = context.RoleList.Where(user => user.UserId == content.ID);

                if (itemToRemove != null)
                {
                    context.RoleList.RemoveRange(itemToRemove);
                }

                return(context.SaveChanges());
            }
        }
Beispiel #7
0
        public int GetVisits()
        {
            using (var context = new eshopContext())
            {
                if (context.Logs.ToList().Count == 0)
                {
                    Logs log = new Logs();
                    log.LogCounter = 1;
                    context.Logs.Add(log);
                    context.SaveChanges();

                    return(1);
                }
                else
                {
                    var log = context.Logs.FirstOrDefault();
                    log.LogCounter++;
                    context.SaveChanges();

                    return((int)log.LogCounter);
                }
            }
        }
Beispiel #8
0
        public int EditUser([FromBody] EditUserModel content)
        {
            using (var context = new eshopContext())
            {
                var user = context.Users.FirstOrDefault(u => u.UserId == content.ID);
                if (user != null)
                {
                    user.FirstName = content.FirstName;
                    user.LastName  = content.LastName;
                    user.BirthDate = content.BirthDate;
                }

                return(context.SaveChanges());
            }
        }
Beispiel #9
0
        public int AddComment(int userID, int productID, string text)
        {
            using (var context = new eshopContext())
            {
                Comment comment = new Comment();

                comment.UserId      = userID;
                comment.ProductId   = productID;
                comment.CategoryId  = context.Products.FirstOrDefault(prod => prod.ProductId == productID).CategoryId;
                comment.CommentText = text;

                context.Comment.Add(comment);

                return(context.SaveChanges());
            }
        }
Beispiel #10
0
        public int DeleteReservation([FromBody] ReservedProduct content)
        {
            using (var context = new eshopContext())
            {
                var reserved = context.Reserved.FirstOrDefault(res => res.ReservedId == content.ID);

                if (reserved != null)
                {
                    var prod = context.Products.FirstOrDefault(p => p.ProductId == reserved.ProductId);
                    prod.ProductAmount += reserved.ReservedAmount;
                    context.Reserved.Remove(reserved);
                }

                return(context.SaveChanges());
            }
        }
Beispiel #11
0
        public int DeleteProduct(int ID)
        {
            using (var context = new eshopContext())
            {
                var itemToRemove       = context.Products.SingleOrDefault(product => product.ProductId == ID); //returns a single item.
                var removeFromReserved = context.Reserved.Where(product => product.ProductId == ID);

                if (removeFromReserved != null)
                {
                    context.Reserved.RemoveRange(removeFromReserved);
                }

                if (itemToRemove != null)
                {
                    context.Products.Remove(itemToRemove);
                }

                return(context.SaveChanges());
            }
        }
Beispiel #12
0
        public ActionResult <IEnumerable <News> > AddNews([FromBody] NewsModel content)
        {
            using (var context = new eshopContext())
            {
                if (!context.News.ToList().Any(newsses => newsses.NewsTittle == content.NewsTittle))
                {
                    News newNews = new News();

                    newNews.NewsTittle      = content.NewsTittle;
                    newNews.NewsDescription = content.NewsDescription;
                    newNews.NewsBody        = content.NewsBody;
                    newNews.NewsDateTime    = DateTime.Now;
                    newNews.UserId          = content.UserId;


                    context.News.Add(newNews);
                    context.SaveChanges();
                }
            }
            return(new List <News>());
        }
Beispiel #13
0
        public ActionResult <IEnumerable <Products> > JsonStringBody([FromBody] AddProductModel content)
        {
            using (var context = new eshopContext())
            {
                if (!context.Products.ToList().Any(prod => prod.ProductName == content.tittle))
                {
                    Products newProduct = new Products();

                    newProduct.ProductName        = content.tittle;
                    newProduct.ProductPrice       = content.price;
                    newProduct.ProductAmount      = content.amount;
                    newProduct.ProductDescription = content.description;
                    newProduct.ProductPhoto       = content.photo;
                    newProduct.CategoryId         = content.category;


                    context.Products.Add(newProduct);
                    context.SaveChanges();
                }
            }
            return(new List <Products>());
        }
Beispiel #14
0
        public int DeleteUser([FromBody] UserManagmentModel content)
        {
            using (var context = new eshopContext())
            {
                var deleteRoleList = context.RoleList.Where(user => user.UserId == content.ID);

                if (deleteRoleList != null)
                {
                    context.RoleList.RemoveRange(deleteRoleList);
                    context.SaveChanges();
                }

                var itemToRemove = context.Users.SingleOrDefault(user => user.UserId == content.ID); //returns a single item.

                var deleteOrdered = context.Orders.Where(user => user.UserId == content.ID);

                if (deleteOrdered != null)
                {
                    context.Orders.RemoveRange(deleteOrdered);
                    context.SaveChanges();
                }
                var deleteComments = context.Comment.Where(user => user.UserId == content.ID);

                if (deleteComments != null)
                {
                    context.Comment.RemoveRange(deleteComments);
                    context.SaveChanges();
                }

                var deleteReserved = context.Reserved.Where(user => user.UserId == content.ID);

                if (deleteReserved != null)
                {
                    context.Reserved.RemoveRange(deleteReserved);
                    context.SaveChanges();
                }

                var deleteCart = context.Cart.Where(user => user.UserId == content.ID);

                if (deleteCart != null)
                {
                    context.Cart.RemoveRange(deleteCart);
                    context.SaveChanges();
                }

                var deleteNews = context.News.Where(user => user.UserId == content.ID);

                if (deleteNews != null)
                {
                    context.News.RemoveRange(deleteNews);
                    context.SaveChanges();
                }

                if (itemToRemove != null)
                {
                    context.Users.Remove(itemToRemove);
                }

                return(context.SaveChanges());
            }
        }