public IActionResult Create(VideoGame obj) { _db.VideoGame.Add(obj); _db.SaveChanges(); //we go back to index so we can go back to see all videogames return(RedirectToAction("Index")); }
public bool Create(string email, string name, string password) { using (var db = new GameStoreDbContext()) { bool emailAlreadyExists = db.Users.Any(x => x.Email == email); if (emailAlreadyExists) { return(false); } bool isThisDudeAdmin = !db.Users.Any(); string passwordHash = PasswordUtilities.GenerateHash(password); User user = new User { Email = email, FullName = name, PasswordHash = passwordHash, IsAdmin = isThisDudeAdmin, }; db.Users.Add(user); db.SaveChanges(); return(true); } }
public bool Create(string email, string name, string password) { using (GameStoreDbContext db = new GameStoreDbContext()) { if (db.Users.Any(u => u.Email == email)) { // email must be unique return(false); } // the first registered becomes admin bool isAdmin = !db.Users.Any(); User user = new User { Email = email, Name = name, Password = password, IsAdmin = isAdmin }; db.Add(user); db.SaveChanges(); } return(true); }
public bool Update(int id, string title, string description, string image, decimal price, double size, string videoId, DateTime releaseDate) { Game gameToEdit = default(Game); using (var db = new GameStoreDbContext()) { gameToEdit = db.Games.FirstOrDefault(x => x.Id == id); if (gameToEdit == default(Game)) { return(false); } gameToEdit.Id = id; gameToEdit.Title = title; gameToEdit.Description = description; gameToEdit.ImageURL = image; gameToEdit.Price = price; gameToEdit.Size = size; gameToEdit.Trailer = videoId; gameToEdit.ReleaseDate = releaseDate; db.SaveChanges(); } return(true); }
public bool CreateUser(RegisterViewModel model) { using (var db = new GameStoreDbContext()) { if (db.Users.Any(x => x.Email == model.Email)) { return(false); } var isAdmin = !db.Users.Any(); var user = new User() { Name = model.FullName, Email = model.Email, Password = model.Password, IsAdmin = isAdmin }; db.Users.Add(user); db.SaveChanges(); return(true); } }
public bool AddGame(GameViewModel model) { using (var db = new GameStoreDbContext()) { if (db.Games.Any(g => g.Title == model.Title)) { return(false); } var game = new Game { Title = model.Title, Desctription = model.Desctription, Price = model.Price, ReleaseDate = model.ReleaseDate, Size = model.Size, ThumbnailURL = model.ThumbnailURL, TrailerId = model.TrailerId }; db.Add(game); db.SaveChanges(); return(true); } }
public IHttpResponse SaveChanges(IHttpRequest req) { this.LoginCheck(req); var formData = req.FormData; int gameId = int.Parse(formData["gameId"]); CultureInfo provider = CultureInfo.InvariantCulture; using (var db = new GameStoreDbContext()) { Game game = db.Games.Find(gameId); string title = formData["title"]; double size = double.Parse(formData["size"]); double price = double.Parse(formData["price"]); string videoUrl = formData["videoUrl"]; string imageUrl = formData["imageUrl"]; string description = formData["description"]; string relDate = formData["releaseDate"]; DateTime releaseDate = DateTime.ParseExact(relDate, "yyyy-MM-dd", provider); game.Title = title; game.Size = size; game.Price = price; game.ImageUrl = imageUrl; game.TrailerUrl = videoUrl; game.ReleaseDate = releaseDate; game.Description = description; db.SaveChanges(); } return(ListAllGames(req)); }
public bool Create(string email, string name, string password) { using (GameStoreDbContext database = new GameStoreDbContext()) { if (database.Users.Any(u => u.Email == email)) { return(false); } bool isAdmin = !database.Users.Any(); User user = new User { Email = email, Name = name, Password = password, IsAdmin = isAdmin }; database.Users.Add(user); database.SaveChanges(); } return(true); }
public bool Update(int id, string title, string description, string image, decimal price, double size, string videoId, DateTime releaseDate) { using (var context = new GameStoreDbContext()) { var game = context.Games.FirstOrDefault(g => g.Id == id); if (game == null) { return(false); } game.Title = title; game.Description = description; game.ImageUrl = image; game.Price = price; game.Size = size; game.VideoId = videoId; game.ReleaseDate = releaseDate; context.Update(game); context.SaveChanges(); return(true); } }
public bool Create(RegisterUserViewModel model) { using (var db = new GameStoreDbContext()) { if (db.Users.Any(u => u.Email == model.Email)) { return(false); } var isFirstUser = !db.Users.Any(); var user = new User { Name = model.FullName, Email = model.Email, Password = model.Password }; if (isFirstUser) { user.IsAdmin = true; } db.Users.Add(user); db.SaveChanges(); return(true); } }
public void Create( string title, string description, string image, decimal price, double size, string trailerId, DateTime releaseDate) { using (var db = new GameStoreDbContext()) { var game = new Game { Title = title, Description = description, ImageUrl = image, Price = price, SizeGigabytes = size, TrailerId = trailerId, ReleaseDate = releaseDate }; db.Games.Add(game); db.SaveChanges(); } }
public bool Create(string email, string name, string password) { using (var db = new GameStoreDbContext()) { if (db.Users.Any(u => u.Email == email)) { return(false); } var isAdmin = !db.Users.Any(); var user = new User { Email = email, Name = name, Password = password, IsAdmin = isAdmin }; db.Users.Add(user); db.SaveChanges(); return(true); } }
public void Purchase(int userId, IEnumerable <int> gameIds) { var alreadyOwnedIds = this.db .Orders .Where(o => o.UserId == userId && gameIds.Contains(o.GameId)) .Select(o => o.GameId) .ToList(); var newGamesIds = new List <int>(gameIds); foreach (var gameId in alreadyOwnedIds) { newGamesIds.Remove(gameId); } foreach (var newGameId in newGamesIds) { var order = new Order { GameId = newGameId, UserId = userId }; db.Orders.Add(order); } db.SaveChanges(); }
public bool Create(string email, string name, string password) { using (var context = new GameStoreDbContext()) { // Ensure Unique Email var emailExists = context.Users.Any(u => u.Email == email); if (emailExists) { return(false); } // The first registered user is admin var isAdmin = !context.Users.Any(); // Create User var user = new User { Email = email, Name = name, Password = password, IsAdmin = isAdmin }; context.Add(user); context.SaveChanges(); return(true); } }
public bool Save(string email, string password, string fullName) { using (var db = new GameStoreDbContext()) { var users = db.Users; if (users.Any(u => u.Email.ToLower() == email.ToLower())) { return(false); } User user = new User() { Email = email, FullName = fullName, Password = password, IsAdmin = !users.Any() }; db.Add(user); db.SaveChanges(); return(true); } }
public void Create( string title, string description, string image, decimal price, double size, string videoId, DateTime releaseDate) { using (var db = new GameStoreDbContext()) { var game = new Game { Title = title, Description = description, Image = image, Price = price, Size = size, VideoId = videoId, ReleaseDate = releaseDate }; db.Add(game); db.SaveChanges(); } }
public bool CreateOrder(int userId, ShoppingCart shoppingCart) { using (GameStoreDbContext database = new GameStoreDbContext()) { User user = database.Users.Include(u => u.Games).FirstOrDefault(u => u.Id == userId); if (user == null) { return(false); } foreach (int gameId in shoppingCart.GameIds) { UserGame order = new UserGame { UserId = user.Id, GameId = gameId }; user.Games.Add(order); } database.SaveChanges(); return(true); } }
public void EditGame(GameToAddOrEditViewModel gameModel) { if (!Validation.TryValidate(gameModel)) { return; } using (GameStoreDbContext db = new GameStoreDbContext()) { Game gameToEdit = db .Games .Single(g => g.Id == gameModel.Id); gameToEdit.Price = gameModel.Price; gameToEdit.Description = gameModel.Description; gameToEdit.ThumbnailUrl = gameModel.ThumbnailUrl; gameToEdit.Size = gameModel.Size; gameToEdit.Trailer = gameModel.Trailer; gameToEdit.ReleaseDate = gameModel.ReleaseDate; gameToEdit.Title = gameModel.Title; db.Games.Update(gameToEdit); db.SaveChanges(); } }
public bool CreateUser(RegisterViewModel registerModel) { bool isUserValid = Validation.TryValidate(registerModel); if (!isUserValid) { return(false); } using (GameStoreDbContext db = new GameStoreDbContext()) { if (db.Users.Any(u => u.Email == registerModel.Email)) { return(false); } User user = this.mapper.Map <User>(registerModel); if (!db.Users.Any()) { user.IsAdmin = true; } db.Users.Add(user); db.SaveChanges(); return(true); } }
public IHttpResponse Edit(AdminGameViewModel model) { if (!this.Authenticate.IsAdmin) { return(this.RedirectResponse(HomePath)); } bool isDataValid = ValidateGameInput(model.Title, model.Description, model.ImageUrl, model.Price, model.Size, model.ReleaseDate, model.Trailer); if (!isDataValid) { return(this.FileViewResponse(EditGame)); } using (var db = new GameStoreDbContext()) { Game gameToDelete = db.Games.FirstOrDefault(x => x.Id == model.Id); gameToDelete.Title = model.Title; gameToDelete.Description = model.Description; gameToDelete.ImageURL = model.ImageUrl; gameToDelete.Price = model.Price; gameToDelete.Size = model.Size; gameToDelete.Trailer = model.Trailer; gameToDelete.ReleaseDate = model.ReleaseDate; db.SaveChanges(); } return(this.RedirectResponse(HomePath)); }
public void Edit( int id, string title, decimal price, double size, string trailer, string thumbnailUrl, string description, DateTime releaseDate) { using (GameStoreDbContext dbContext = new GameStoreDbContext()) { Game game = dbContext .Games .FirstOrDefault(g => g.Id == id); game.Title = title; game.Price = price; game.Size = size; game.Trailer = trailer; game.ImageThumbnail = thumbnailUrl; game.Description = description; game.ReleaseDate = releaseDate; dbContext.SaveChanges(); } }
public IHttpResponse FinishOrder(IHttpRequest req) { var shoppingCart = req.Session.Get <ShoppingCart>(ShoppingCart.SessionKey); int userId = req.Session.Get <int>(SessionStore.CurrentUserKey); using (var db = new GameStoreDbContext()) { User user = db.Users.Find(userId); foreach (var game in shoppingCart.Games) { var userGame = new UserGame(userId, game.Id); db.UserGames.Add(userGame); user.Games.Add(userGame); game.Users.Add(userGame); } db.SaveChanges(); } return(new RedirectResponse(@"/")); }
public void Create( string title, decimal price, double size, string trailer, string thumbnailUrl, string description, DateTime releaseDate) { using (GameStoreDbContext dbContext = new GameStoreDbContext()) { dbContext .Games .Add(new Game { Title = title, Price = price, Size = size, Trailer = trailer, ImageThumbnail = thumbnailUrl, Description = description, ReleaseDate = releaseDate }); dbContext.SaveChanges(); } }
public void Update( int id, string title, string description, string thumbnailUrl, decimal price, double size, string videoId, DateTime releaseDate) { using (var db = new GameStoreDbContext()) { var game = db.Games.Find(id); game.Title = title; game.Description = description; game.ThumbnailUrl = thumbnailUrl; game.Price = price; game.Size = size; game.VideoId = videoId; game.ReleaseDate = releaseDate; db.SaveChanges(); } }
public int Order(HashSet <IndexViewGame> orderGames, string username) { using (var db = new GameStoreDbContext()) { int userId = this.Find(username).Id; var user = db.Users .First(u => u.Id == userId); var userGames = db.UserGames.Where(ug => ug.UserId == userId); foreach (var orderGame in orderGames) { var game = db.Games .First(g => g.Id == orderGame.Id); var order = new UserGame() { UserId = user.Id, User = user, GameId = game.Id, Game = game }; if (!userGames.Any(ug => ug.UserId == userId && ug.GameId == game.Id)) { db.Add(order); } } return(db.SaveChanges()); } }
public void CreateOrder(int userId, IEnumerable <int> gameIds) { using (var db = new GameStoreDbContext()) { var order = new Order { UserId = userId, Games = gameIds.Select(id => new OrderGame() { GameId = id }).ToList() }; var user = db.Users.First(u => u.Id == userId); foreach (var id in gameIds) { user.Games.Add(new UserGame { GameId = id }); } order.TotalPrice = db.Games.Where(g => gameIds.Contains(g.Id)).Sum(g => g.Price); db.Add(order); db.SaveChanges(); } }
public void AddRange(IEnumerable <UserGame> userGames) { using (var db = new GameStoreDbContext()) { db.AddRange(userGames); db.SaveChanges(); } }
public IActionResult Delete(Game game) { using (var db = new GameStoreDbContext()) { db.Remove(game); db.SaveChanges(); } return(RedirectToAction("Index")); }
public void Delete(int id) { using (var db = new GameStoreDbContext()) { var game = db.Games.Find(id); db.Games.Remove(game); db.SaveChanges(); } }
public void Delete(int id) { using (var db = new GameStoreDbContext()) { var game = db.Games.FirstOrDefault(g => g.Id == id); db.Games.Remove(game); db.SaveChanges(); } }