Beispiel #1
0
        public bool Create(string title, string description, string thumbnail, decimal price, double size, string trailerId,
                           DateTime releaseDate)
        {
            using (var db = new GamestoreAppDbContext())
            {
                if (db.Games.Any(g => g.TrailerId == trailerId))
                {
                    return(false);
                }
                var game = new Game()
                {
                    Title         = title,
                    Description   = description,
                    ImageTumbnail = thumbnail,
                    Price         = price,
                    Size          = size,
                    TrailerId     = trailerId,
                    ReleaseDate   = releaseDate
                };

                db.Games.Add(game);
                db.SaveChanges();

                return(true);
            }
        }
Beispiel #2
0
        public IEnumerable <AllGamesViewModel> List(string user, string filter)
        {
            using (var db = new GamestoreAppDbContext())
            {
                var orderedGames = db.Games
                                   .OrderBy(g => g.Id);

                Game[] games = null;

                if (filter == "Owned")
                {
                    games = orderedGames
                            .Where(g => g.Users.Any(u => u.User.Email == user))
                            .ToArray();
                }
                else
                {
                    games = orderedGames.ToArray();
                }

                return(games
                       .Select(g => new AllGamesViewModel()
                {
                    Id = g.Id.ToString(),
                    ImageTumbnail = g.ImageTumbnail,
                    Title = g.Title,
                    Price = g.Price,
                    Size = g.Size,
                    Description = g.Description
                })
                       .ToArray());
            }
        }
Beispiel #3
0
 public bool Exist(int id)
 {
     using (var db = new GamestoreAppDbContext())
     {
         return(db.Games.Any(g => g.Id == id));
     }
 }
Beispiel #4
0
        public bool Edit(int id, AddGameViewModel viewModel)
        {
            try
            {
                using (var db = new GamestoreAppDbContext())
                {
                    var game = db.Games.Find(id);

                    if (game == null)
                    {
                        return(false);
                    }

                    game.Price         = decimal.Parse(viewModel.Price);
                    game.Description   = viewModel.Description;
                    game.ImageTumbnail = viewModel.ImageTumbnail;
                    game.ReleaseDate   = DateTime.Parse(viewModel.ReleaseDate);
                    game.Size          = double.Parse(viewModel.Size);
                    game.Title         = viewModel.Title;
                    game.TrailerId     = viewModel.TrailerId;

                    db.Update(game);
                    db.SaveChanges();

                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }
Beispiel #5
0
        public bool AddProducts(List <int> ids, string userId)
        {
            try
            {
                using (var db = new GamestoreAppDbContext())
                {
                    var games = db.Games
                                .Where(g => ids.Contains(g.Id))
                                .ToList();

                    var user = db.Users.SingleOrDefault(u => u.Email == userId);
                    foreach (var game in games)
                    {
                        user.Games.Add(new UserGame()
                        {
                            GameId = game.Id
                        });
                    }

                    db.SaveChanges();

                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }
Beispiel #6
0
 public bool IsAdmin(string email)
 {
     using (var db = new GamestoreAppDbContext())
     {
         return(db.Users.Single(u => u.Email == email).IsAdmin);
     }
 }
Beispiel #7
0
 public bool Find(string email, string password)
 {
     using (var db = new GamestoreAppDbContext())
     {
         return(db.Users.Any(u => u.Email == email && u.Password == password));
     }
 }
 public void InitializeDatabase()
 {
     using (var db = new GamestoreAppDbContext())
     {
         db.Database.Migrate();
     }
 }
Beispiel #9
0
 public IEnumerable <AdminListGamesViewModel> All()
 {
     using (var db = new GamestoreAppDbContext())
     {
         return(db.Games
                .OrderBy(g => g.Id)
                .Select(g => new AdminListGamesViewModel()
         {
             Id = g.Id,
             Name = g.Title,
             Price = g.Price,
             Size = g.Size
         })
                .ToArray());
     }
 }
Beispiel #10
0
 public IList <CartViewModel> GetCart(List <int> ids)
 {
     using (var db = new GamestoreAppDbContext())
     {
         return(db.Games
                .Where(g => ids.Contains(g.Id))
                .Select(g => new CartViewModel()
         {
             Description = g.Description,
             GameId = g.Id,
             Price = g.Price,
             Title = g.Title,
             Tumbnail = g.ImageTumbnail
         })
                .ToList());
     }
 }
Beispiel #11
0
        public bool GameExist(int gameId, string userId)
        {
            try
            {
                using (var db = new GamestoreAppDbContext())
                {
                    var user = db.Users.SingleOrDefault(u => u.Email == userId);

                    var exist = db.UserGame.Any(ug => ug.GameId == gameId && ug.UserId == user.Id);
                    return(exist);
                }
            }
            catch
            {
                return(false);
            }
        }
Beispiel #12
0
        public bool Create(string email, string fullName, string password)
        {
            using (var db = new GamestoreAppDbContext())
            {
                if (db.Users.Any(u => u.Email == email))
                {
                    return(false);
                }

                var user = new User()
                {
                    FullName = fullName,
                    Email    = email,
                    Password = password,
                    IsAdmin  = !db.Users.Any()
                };

                db.Users.Add(user);
                db.SaveChanges();

                return(true);
            }
        }
Beispiel #13
0
        public AddGameViewModel Find(int id)
        {
            using (var db = new GamestoreAppDbContext())
            {
                var game = db.Games.Find(id);

                if (game == null)
                {
                    return(null);
                }


                return(new AddGameViewModel()
                {
                    Price = game.Price.ToString("F2"),
                    Description = game.Description,
                    TrailerId = game.TrailerId,
                    ImageTumbnail = game.ImageTumbnail,
                    Size = game.Size.ToString("F1"),
                    Title = game.Title,
                    ReleaseDate = game.ReleaseDate.ToString("yyyy-MM-dd")
                });
            }
        }
Beispiel #14
0
        public bool Delete(int id)
        {
            try
            {
                using (var db = new GamestoreAppDbContext())
                {
                    var game = db.Games.Find(id);

                    if (game == null)
                    {
                        return(false);
                    }

                    db.Remove(game);
                    db.SaveChanges();

                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }