public static OrderParams GetOrderParamsByID(int orderID) { using (var db = new GameStoreDBContext()) { var currentOrder = (from order in db.Order where order.ID == orderID select order).First(); var connections = (from connection in db.Orders_Games where connection.Order_ID == orderID select connection); var orderParams = new OrderParams(); orderParams.Date = currentOrder.Date; orderParams.ID = currentOrder.ID; orderParams.games = new List <GameParams>(); foreach (var connection in connections) { var currentGame = ExtractGameParams((from game in db.Game where game.ID == connection.Game_ID select game).First()); currentGame.Quantity = connection.Games_number; orderParams.games.Add(currentGame); } return(orderParams); } }
public static List <OrderParams> GetOrdersParams(DateTime dateFrom, DateTime dateTo) { var orders = new List <OrderParams>(); var actualDateTo = dateTo.AddDays(1); Console.WriteLine(actualDateTo); using (var db = new GameStoreDBContext()) { var currentOrders = from order in db.Order where order.Date >= dateFrom && order.Date <= actualDateTo select order; foreach (var order in currentOrders) { var currentOrder = new OrderParams() { Date = order.Date, ID = order.ID, games = ExtractGamesFromOrder(order) }; orders.Add(currentOrder); } } return(orders); }
public HttpResponseMessage Post([FromBody] ProductViewModel value) { if (ModelState.IsValid) { if (value == null || String.IsNullOrEmpty(value.ProductName)) { return(Request.CreateResponse(HttpStatusCode.OK, "Product Name can't be empty!")); } if (value.Discount < 0 || value.Discount > 100) { return(Request.CreateResponse(HttpStatusCode.OK, "Discount must between 0 ~ 100.")); } using (GameStoreDBContext context = new GameStoreDBContext()) { bool exist = context.Products.Any(c => c.ProductId == value.ProductId); if (!exist) { return(Request.CreateResponse(HttpStatusCode.OK, "Product [" + value.ProductId + "] does not exist!")); } exist = context.Products.Where(c => c.ProductId != value.ProductId).Any(c => c.ProductName.Equals(value.ProductName, StringComparison.OrdinalIgnoreCase)); if (exist) { return(Request.CreateResponse(HttpStatusCode.OK, "Product [" + value.ProductName + "] is already existed, please try another name!")); } var product = context.Products.Find(value.ProductId); if (product == null) { return(Request.CreateResponse(HttpStatusCode.OK, "No such product!")); } bool isAdvanced = HttpContext.Current.User.IsInRole("Advanced"); if (isAdvanced && product.UserId != HttpContext.Current.User.Identity.GetUserId()) { return(Request.CreateResponse(HttpStatusCode.OK, "You have no authorization to update this product!")); } HttpContext.Current.Cache.Remove("ProductList" + product.CategoryId); product.ProductName = value.ProductName; product.CategoryId = value.CategoryId; product.Price = value.Price; product.Image = value.Image; product.Condition = value.Condition; product.Discount = value.Discount; context.SaveChanges(); //context.Entry(product).CurrentValues.SetValues(value); //context.SaveChanges(); HttpContext.Current.Cache.Remove("ProductList"); HttpContext.Current.Cache.Remove("ProductList" + value.CategoryId); HttpContext.Current.Cache.Remove("Product" + product.ProductId); return(Request.CreateResponse(HttpStatusCode.OK, "Okay")); } } else { return(Request.CreateResponse(HttpStatusCode.OK, "ModelState.IsValid=false")); } }
//PUT REMOVED // Ajax.htmlForm does not support put and delete, only supports get and post. public HttpResponseMessage Post([FromBody] CategoryViewModel value) { if (value == null || String.IsNullOrEmpty(value.CategoryName)) { return(Request.CreateResponse(HttpStatusCode.OK, "Category Name can't be empty!")); } using (GameStoreDBContext context = new GameStoreDBContext()) { bool exist = context.Categories.Any(c => c.CategoryId == value.CategoryId); if (!exist) { return(Request.CreateResponse(HttpStatusCode.OK, "Category [" + value.CategoryId + "] does not exist!")); } exist = context.Categories.Where(c => c.CategoryId != value.CategoryId).Any(c => c.CategoryName.Equals(value.CategoryName, StringComparison.OrdinalIgnoreCase)); if (exist) { return(Request.CreateResponse(HttpStatusCode.OK, "Category [" + value.CategoryName + "] is already existed, please try another name!")); } var category = context.Categories.Find(value.CategoryId); category.CategoryName = value.CategoryName; context.SaveChanges(); HttpContext.Current.Cache.Remove("CategoryList"); HttpContext.Current.Cache.Remove("Category" + value.CategoryId); return(Request.CreateResponse(HttpStatusCode.OK, "Okay")); } }
public ActionResult CreateOrUpdate(CartViewModel value) { ShoppingCart cart = (ShoppingCart)Session["ShoppingCart"]; if (cart == null) { cart = new ShoppingCart(); Session["ShoppingCart"] = cart; } using (GameStoreDBContext context = new GameStoreDBContext()) { Product product = context.Products.Find(value.Id); if (product != null) { if (value.Quantity == 0) { cart.AddItem(value.Id, product); } else { cart.SetItemQuantity(value.Id, value.Quantity, product); } } } Session["CartCount"] = cart.GetItems().Count(); return(View("Index", cart)); }
public List <ProductDTO> GetByUserId([FromUri] CategoryViewModel value) { String userid = User.Identity.GetUserId(); using (GameStoreDBContext context = new GameStoreDBContext()) { if (value.CategoryId == 0) { var query = from product in context.Products where product.UserId == userid join category in context.Categories on product.CategoryId equals category.CategoryId select new ProductDTO { ProductId = product.ProductId, ProductName = product.ProductName, CategoryId = product.CategoryId, CategoryName = category.CategoryName, Price = product.Price, Image = product.Image, Condition = product.Condition, Discount = product.Discount, UserId = product.UserId }; List <ProductDTO> products = query.ToList(); return(products); } else { var query = from product in context.Products where product.CategoryId == value.CategoryId && product.UserId == userid join category in context.Categories on product.CategoryId equals category.CategoryId select new ProductDTO { ProductId = product.ProductId, ProductName = product.ProductName, CategoryId = product.CategoryId, CategoryName = category.CategoryName, Price = product.Price, Image = product.Image, Condition = product.Condition, Discount = product.Discount, UserId = product.UserId }; List <ProductDTO> products = query.ToList(); return(products); } } }
public int GetCount() { using (GameStoreDBContext context = new GameStoreDBContext()) { return(context.Orders.Count()); } }
public ActionResult Search(string productname) { List <ProductDTO> list = new List <ProductDTO>(); using (GameStoreDBContext context = new GameStoreDBContext()) { if (String.IsNullOrEmpty(productname)) { var query = from product in context.Products join category in context.Categories on product.CategoryId equals category.CategoryId select new ProductDTO { ProductId = product.ProductId, ProductName = product.ProductName, CategoryId = product.CategoryId, CategoryName = category.CategoryName, Price = product.Price, Image = product.Image, Condition = product.Condition, Discount = product.Discount, UserId = product.UserId }; list = query.ToList(); } else { var query = from product in context.Products where product.ProductName.ToLower().Contains(productname.ToLower()) join category in context.Categories on product.CategoryId equals category.CategoryId select new ProductDTO { ProductId = product.ProductId, ProductName = product.ProductName, CategoryId = product.CategoryId, CategoryName = category.CategoryName, Price = product.Price, Image = product.Image, Condition = product.Condition, Discount = product.Discount, UserId = product.UserId }; list = query.ToList(); } } ViewBag.Title = "Search Result"; return(View("List", list)); }
private List <ProductDTO> GetProductsByCategory(int categoryid) { List <ProductDTO> list = new List <ProductDTO>(); if (System.Web.HttpContext.Current.Cache["ProductList" + categoryid] != null) { list = (List <ProductDTO>)System.Web.HttpContext.Current.Cache["ProductList" + categoryid]; } else { using (GameStoreDBContext context = new GameStoreDBContext()) { var query = from product in context.Products where product.CategoryId == categoryid join category in context.Categories on product.CategoryId equals category.CategoryId select new ProductDTO { ProductId = product.ProductId, ProductName = product.ProductName, CategoryId = product.CategoryId, CategoryName = category.CategoryName, Price = product.Price, Image = product.Image, Condition = product.Condition, Discount = product.Discount, UserId = product.UserId }; list = query.ToList(); System.Web.HttpContext.Current.Cache["ProductList" + categoryid] = list; } } return(list); }
public static bool IsTypeInDB(string typeName) { using (var db = new GameStoreDBContext()) { return((from type in db.Type where type.Name == typeName select type).Count() > 0); } }
public static bool IsGenreInDB(string genreName) { using (var db = new GameStoreDBContext()) { return((from genre in db.Genre where genre.Name == genreName select genre).Count() > 0); } }
public static string GetPropertyValue(int gameID, string propertyName) { using (var db = new GameStoreDBContext()) { var currentGame = (from game in db.Game where game.ID == gameID select game).First(); switch (propertyName) { case "Name": return(currentGame.Name); case "Description": return(currentGame.Description); case "Price": return((Math.Round(currentGame.Price, 2)).ToString()); case "Quantity": return(currentGame.Quantity.ToString()); case "Author": return((from author in db.Author where author.ID == currentGame.Author_ID select author).First().Manufacturer); case "minDuration": return(currentGame.Min_Duration.ToString()); case "maxDuration": return(currentGame.Max_Duration.ToString()); case "Difficulty": return(currentGame.Difficulty.ToString()); case "minPlayers": return(currentGame.Min_Players.ToString()); case "maxPlayers": return(currentGame.Max_Players.ToString()); case "Genre": return((from genre in db.Genre where genre.ID == currentGame.Genre_ID select genre).First().Name); case "Type": return((from type in db.Type where type.ID == currentGame.Type_ID select type).First().Name); default: return("Невозможно получить данные"); } } }
public static GameParams GetGameByID(int id) { using (var db = new GameStoreDBContext()) { var currentGame = (from game in db.Game where game.ID == id select game).First(); return(ExtractGameParams(currentGame)); } }
public static int GetGameIDByName(string name) { using (var db = new GameStoreDBContext()) { var id = (from game in db.Game where game.Name == name select game.ID).First(); return(id); } }
public static GameParams GetGameByName(string name) { using (var db = new GameStoreDBContext()) { var currentGame = (from game in db.Game where game.Name == name select game).First(); return(ExtractGameParams(currentGame)); } }
public static void AddImageToGameByID(int id, byte[] image) { using (var db = new GameStoreDBContext()) { var selectedGame = (from game in db.Game where game.ID == id select game).First(); selectedGame.Image = image; db.SaveChanges(); } }
public static Client GetClientByOrderID(int orderID) { using (var db = new GameStoreDBContext()) { return((from client in db.Client where client.ID == (from order in db.Order where order.ID == orderID select order.Client_ID).FirstOrDefault() select client).First()); } }
private void GetOrderCount(string id) { int count = 0; using (GameStoreDBContext context = new GameStoreDBContext()) { count = context.Orders.Where(o => o.UserId == id).Count(); } Session["OrderCount"] = count; Session["CartCount"] = 0; }
public List <String> GetAutoComplete([FromUri] string name) { using (GameStoreDBContext context = new GameStoreDBContext()) { var query = from product in context.Products where product.ProductName.ToLower().Contains(name.ToLower()) select product.ProductName; List <String> names = query.ToList(); return(names); } }
public static string GetGenreDescription(int gameID) { using (var db = new GameStoreDBContext()) { var currentGenre = (from genre in db.Genre where genre.ID == (from game in db.Game where game.ID == gameID select game).FirstOrDefault().Genre_ID select genre).First(); return(currentGenre.Description); } }
public ActionResult MemberProfile() { UserDTO user = new UserDTO(); using (GameStoreDBContext context = new GameStoreDBContext()) { AppUser u = context.Users.Find(User.Identity.GetUserId()); user = new UserDTO { Id = u.Id, Email = u.Email, UserName = u.UserName, Membership = u.Membership }; } return(View(user)); }
public static void ChangeGameAuthor(int gameID, string newAuthorName) { using (var db = new GameStoreDBContext()) { var currentAuthor = (from author in db.Author where author.ID == (from game in db.Game where game.ID == gameID select game).FirstOrDefault().Author_ID select author).First(); currentAuthor.Manufacturer = newAuthorName; db.SaveChanges(); } }
public static int AddAuthorToDB(string author) { var newAuthor = new Author { Manufacturer = author }; using (var db = new GameStoreDBContext()) { db.Author.Add(newAuthor); db.SaveChanges(); return(newAuthor.ID); } }
public HttpResponseMessage Create([FromBody] ProductViewModel value) { if (ModelState.IsValid) { if (value.Discount < 0 || value.Discount > 100) { return(Request.CreateResponse(HttpStatusCode.OK, "Discount must between 0 ~ 100.")); } using (GameStoreDBContext context = new GameStoreDBContext()) { bool exist = context.Products.Any(c => c.ProductName.Equals(value.ProductName, StringComparison.OrdinalIgnoreCase)); if (exist) { return(Request.CreateResponse(HttpStatusCode.OK, "Product [" + value.ProductName + "] is already existed, please try another name!")); } //if (file != null) //{ // string[] formats = new string[] { ".jpg", ".png", ".gif", ".jpeg" }; // add more if u like... // // linq from Henrik Stenbæk // bool isimage = formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase)); // if (!isimage) // { // return Request.CreateResponse(HttpStatusCode.OK, "The image format is not valid, must be .jpg, .png, .gif, or .jpeg"); // } //} Product newProduct = context.Products.Create(); newProduct.ProductName = value.ProductName; newProduct.CategoryId = value.CategoryId; newProduct.Price = value.Price; newProduct.Image = value.Image; newProduct.Condition = value.Condition; newProduct.Discount = value.Discount; newProduct.UserId = User.Identity.GetUserId(); //string root = System.Web.Hosting.HostingEnvironment.MapPath("~/images/"); //string filename = string.Format(@"{0}.{1}", DateTime.Now.Ticks, System.IO.Path.GetExtension(file.FileName)); //file.SaveAs(System.IO.Path.Combine(root, filename)); //newProduct.Image = filename; context.Products.Add(newProduct); context.SaveChanges(); HttpContext.Current.Cache.Remove("ProductList"); HttpContext.Current.Cache.Remove("ProductList" + newProduct.CategoryId); return(Request.CreateResponse(HttpStatusCode.OK, "Okay")); } } else { return(Request.CreateResponse(HttpStatusCode.OK, "ModelState.IsValid=false")); } }
public HttpResponseMessage Delete(int id) { using (GameStoreDBContext context = new GameStoreDBContext()) { var order = context.Orders.Find(id); if (order == null) { return(Request.CreateResponse(HttpStatusCode.OK, "No such order [" + id + "].")); } context.Orders.Remove(order); context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "Okay")); } }
public static List <GameParams> GetGamesParams() { var games = new List <GameParams>(); using (var db = new GameStoreDBContext()) { foreach (var game in db.Game) { var currentGame = ExtractGameParams(game); games.Add(currentGame); } } return(games); }
public static void ChangeGameGenre(int gameID, string genreName, string description) { using (var db = new GameStoreDBContext()) { var currentGameGenre = (from genre in db.Genre where genre.ID == (from game in db.Game where game.ID == gameID select game).FirstOrDefault().Genre_ID select genre).First(); currentGameGenre.Name = genreName; currentGameGenre.Description = description; db.SaveChanges(); } }
public static void ChangeGameType(int gameID, string gameType, string description) { using (var db = new GameStoreDBContext()) { var currentGameType = (from type in db.Type where type.ID == (from game in db.Game where game.ID == gameID select game).FirstOrDefault().Type_ID select type).First(); currentGameType.Name = gameType; currentGameType.Description = description; db.SaveChanges(); } }
public static bool AddGameToDB(GameParams game) { using (var db = new GameStoreDBContext()) { var currentGenreIds = (from genre in db.Genre where genre.Name == game.Genre select genre.ID); if (currentGenreIds.Count() < 1) { return(false); } var currentGenreID = currentGenreIds.First(); var currentTypeIds = (from type in db.Type where type.Name == game.Type select type.ID); if (currentTypeIds.Count() < 1) { return(false); } var currentTypeID = currentTypeIds.First(); var currentAuthorIds = (from author in db.Author where author.Manufacturer == game.Author select author.ID); if (currentAuthorIds.Count() < 1) { return(false); } var currentAuthorID = currentAuthorIds.First(); var currentGame = new Game { Name = game.Name, Max_Duration = game.maxDuration, Min_Duration = game.minDuration, Max_Players = game.maxPlayers, Min_Players = game.minPlayers, Genre_ID = currentGenreID, Type_ID = currentTypeID, Price = game.Price, Description = game.Description, Quantity = game.Quantity, Author_ID = currentAuthorID, Difficulty = game.Difficulty }; db.Game.Add(currentGame); db.SaveChanges(); return(true); } }
// GET api/<controller> public List <CategoryDTO> Get() { if (HttpContext.Current.Cache["CategoryList"] != null) { return((List <CategoryDTO>)HttpContext.Current.Cache["CategoryList"]); } using (GameStoreDBContext context = new GameStoreDBContext()) { List <CategoryDTO> categories = context.Categories.Select(c => new CategoryDTO { CategoryId = c.CategoryId, CategoryName = c.CategoryName }).ToList(); HttpContext.Current.Cache["CategoryList"] = categories; return(categories); } }