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()); } }
public ActionResult <IEnumerable <Category> > GetCategorys() { using (var context = new eshopContext()) { return(context.Category.ToList()); } }
public ActionResult <IEnumerable <News> > GetNews() { using (var context = new eshopContext()) { return(context.News.ToList()); } }
public ActionResult <IEnumerable <Users> > JsonStringBody([FromBody] LoginModel content) { using (var context = new eshopContext()) { var result = from user in context.Users where (user.EMail.Equals(content.eMail) && user.Password.Equals(CreateMD5(content.password))) select new { id = user.UserId, eMail = user.EMail, firstName = user.FirstName, lastName = user.LastName, birthDate = user.BirthDate, token = TokenManager.GenerateToken(user.EMail), roles = context.Roles.Select(role => new { role.RoleId, role.RoleName, permisions = context.Permissions.Select(perm => new { perm.PermissionId, perm.PermissionName }).Where( p => role.PermissionList.Select(pl => pl.PermissionId).Contains(p.PermissionId) ).ToList() } ).Where( r => user.RoleList.Select(d => d.RoleId).Contains(r.RoleId) ).ToList() }; return(Ok(result.ToList())); } }
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()); } }
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()); } }
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()); } }
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()); } }
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()); } }
public ActionResult <IEnumerable <Comment> > GetComments(int productID) { using (var context = new eshopContext()) { var result = from Comment in context.Comment where (Comment.ProductId == productID) select new { user = context.Users.FirstOrDefault(u => u.UserId == Comment.UserId).FirstName + " " + context.Users.FirstOrDefault(u => u.UserId == Comment.UserId).LastName, userID = Comment.UserId, text = Comment.CommentText }; return(Ok(result.ToList())); } }
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()); } }
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()); } }
public ActionResult <IEnumerable <Reserved> > GetAllReserved() { using (var context = new eshopContext()) { var result = from Reserved in context.Reserved select new { reservedID = Reserved.ReservedId, tittle = context.Products.FirstOrDefault(prod => prod.ProductId == Reserved.ProductId).ProductName, price = (context.Products.FirstOrDefault(prod => prod.ProductId == Reserved.ProductId).ProductPrice *Reserved.ReservedAmount), user = (context.Users.FirstOrDefault(u => u.UserId == Reserved.UserId).FirstName) + " " + (context.Users.FirstOrDefault(u => u.UserId == Reserved.UserId).LastName), userEmail = (context.Users.FirstOrDefault(u => u.UserId == Reserved.UserId).EMail) }; return(Ok(result.ToList())); } }
public ActionResult <IEnumerable <Users> > Validate(string token, string email) { using (var context = new eshopContext()) { var tempUser = context.Users.FirstOrDefault(u => u.EMail == email); if (tempUser != null) { int UserId = tempUser.UserId; if (UserId == 0) { return(null); } string tokenUsername = TokenManager.ValidateToken(token); if (email.Equals(tokenUsername)) { var result = from user in context.Users where (user.EMail.Equals(email)) select new { id = user.UserId, eMail = user.EMail, firstName = user.FirstName, lastName = user.LastName, birthDate = user.BirthDate, roles = context.Roles.Select(role => new { role.RoleId, role.RoleName, permisions = context.Permissions.Select(perm => new { perm.PermissionId, perm.PermissionName }).Where( p => role.PermissionList.Select(pl => pl.PermissionId).Contains(p.PermissionId)).ToList() }).Where( r => user.RoleList.Select(d => d.RoleId).Contains(r.RoleId)).ToList() }; return(Ok(result.ToList())); } } return(null); } }
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); }
public ActionResult <IEnumerable <Reserved> > GetAllOrders() { using (var context = new eshopContext()) { var result = from Orders in context.Orders select new { orderID = Orders.OrderId, adress = Orders.OrderAdress, phone = Orders.OrderPhoneNumber, email = Orders.OrderEmail, orderDate = Orders.OrderDate, tittle = context.Products.FirstOrDefault(prod => prod.ProductId == Orders.ProductId).ProductName, photo = context.Products.FirstOrDefault(prod => prod.ProductId == Orders.ProductId).ProductPhoto, price = (context.Products.FirstOrDefault(prod => prod.ProductId == Orders.ProductId).ProductPrice *(context.Products.FirstOrDefault(prod => prod.ProductId == Orders.ProductId).ProductAmount)) }; return(Ok(result.ToList())); } }
public ActionResult <IEnumerable <Reserved> > GetMyProducts([FromBody] ReservedProduct content) { using (var context = new eshopContext()) { var result = from reserved in context.Reserved where (reserved.UserId == content.ID) select new { reservedID = reserved.ReservedId, productID = reserved.ProductId, amount = reserved.ReservedAmount, ordered = reserved.ReservedOrdered, tittle = context.Products.FirstOrDefault(prod => prod.ProductId == reserved.ProductId).ProductName, photo = context.Products.FirstOrDefault(prod => prod.ProductId == reserved.ProductId).ProductPhoto, price = (context.Products.FirstOrDefault(prod => prod.ProductId == reserved.ProductId).ProductPrice *reserved.ReservedAmount) }; return(Ok(result.ToList())); } }
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()); } }
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>()); }
public ActionResult <IEnumerable <Products> > GetProduct(int ID) { using (var context = new eshopContext()) { var result = from products in context.Products where (products.ProductId == ID) select new { productId = products.ProductId, tittle = products.ProductName, description = products.ProductDescription, price = products.ProductPrice, amount = products.ProductAmount, expireDate = products.ProductExpireDate, categoryId = products.CategoryId, categoryName = context.Category.Where(cat => cat.CategoryId == products.CategoryId).Select(c => c.CategoryName).ToList(), photo = products.ProductPhoto }; return(Ok(result.ToList())); } }
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>()); }
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); } } }
public ActionResult <IEnumerable <Users> > GetUser() { using (var context = new eshopContext()) { var result = from user in context.Users select new { userId = user.UserId, eMail = user.EMail, firstName = user.FirstName, lastName = user.LastName, birthDate = user.BirthDate, roles = context.Roles.Select(role => new { role.RoleId, role.RoleName } ).Where( r => user.RoleList.Select(d => d.RoleId).Contains(r.RoleId) ).ToList() }; return(Ok(result.ToList())); } }
public ActionResult <IEnumerable <Products> > GetProductWithName(string name) { using (var context = new eshopContext()) { if (name != null) { var result = from product in context.Products where (product.ProductName.Contains(name)) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else { var result = from product in context.Products select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } } }
public SystemUsersController(eshopContext context, IHostingEnvironment env) { _context = context; _env = env; }
public ActionResult <IEnumerable <Products> > GetAllProducts(int categoryId, decimal priceFrom, decimal priceTo, string name, int amount) { using (var context = new eshopContext()) { if (categoryId != -1 && priceFrom != -1 && priceTo != -1 && name != null && amount != -1) { var result = from product in context.Products where (product.CategoryId == categoryId & product.ProductPrice >= priceFrom && product.ProductPrice <= priceTo & product.ProductName.Equals(name) & product.ProductAmount == amount ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId != -1 && priceFrom != -1 && priceTo != -1 && name == null && amount != -1) { var result = from product in context.Products where (product.CategoryId == categoryId & product.ProductPrice >= priceFrom && product.ProductPrice <= priceTo & product.ProductAmount == amount ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId == -1 && priceFrom != -1 && priceTo != -1 && name == null && amount != -1) { var result = from product in context.Products where ( product.ProductPrice >= priceFrom && product.ProductPrice <= priceTo & product.ProductAmount == amount ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId != -1 && priceFrom != -1 && priceTo != -1 && name == null && amount == -1) { var result = from product in context.Products where ( product.CategoryId == categoryId & product.ProductPrice >= priceFrom && product.ProductPrice <= priceTo ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId == -1 && priceFrom != -1 && priceTo != -1 && name == null && amount == -1) { var result = from product in context.Products where ( product.ProductPrice >= priceFrom && product.ProductPrice <= priceTo ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId == -1 && priceFrom == -1 && priceTo == -1 && name == null && amount != -1) { var result = from product in context.Products where ( product.ProductAmount == amount ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId == -1 && priceFrom != -1 && priceTo == -1 && name == null && amount == -1) { var result = from product in context.Products where ( product.ProductPrice >= priceFrom ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId == -1 && priceFrom == -1 && priceTo != -1 && name == null && amount == -1) { var result = from product in context.Products where ( product.ProductPrice <= priceTo ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else if (categoryId != -1 && priceFrom == -1 && priceTo == -1 && name == null && amount == -1) { var result = from product in context.Products where (product.CategoryId == categoryId ) select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } else { var result = from product in context.Products select new { ID = product.ProductId, categoryID = product.CategoryId, tittle = product.ProductName, price = product.ProductPrice, amount = product.ProductAmount, photo = product.ProductPhoto }; return(Ok(result.ToList())); } } }
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()); } }
public SystemItemsController(eshopContext context) { _context = context; }