Example #1
0
        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();
            }
        }
Example #2
0
        public GameToAddOrEditViewModel GetById(int id)
        {
            using (GameStoreDbContext db = new GameStoreDbContext())
            {
                Game game = db
                            .Games
                            .FirstOrDefault(g => g.Id == id);

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

                GameToAddOrEditViewModel gameModel = this.mapper.Map <GameToAddOrEditViewModel>(game);

                return(gameModel);
            }
        }
        public IHttpResponse EditGame(Dictionary <string, string> formData, Dictionary <string, string> urlParameters)
        {
            GameToAddOrEditViewModel game = new GameToAddOrEditViewModel()
            {
                Id           = int.Parse(urlParameters["id"]),
                Title        = formData["title"],
                Description  = formData["description"],
                ThumbnailUrl = formData["thumbnail"],
                Trailer      = formData["trailer"],
                Price        = decimal.Parse(formData["price"]),
                Size         = decimal.Parse(formData["size"]),
                ReleaseDate  = DateTime.ParseExact(formData["date"], "yyyy-MM-dd", CultureInfo.InvariantCulture)
            };

            this.gameService.EditGame(game);

            return(new RedirectResponse("/"));
        }
Example #4
0
        public bool CreateGame(GameToAddOrEditViewModel gameView)
        {
            bool success = Validation.TryValidate(gameView);

            if (!success)
            {
                return(false);
            }

            using (GameStoreDbContext db = new GameStoreDbContext())
            {
                Game game = this.mapper.Map <Game>(gameView);

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

                return(true);
            }
        }
        public IHttpResponse DeleteGame(IHttpSession session, string urlParameter)
        {
            if (!session.IsAuthenticated() || !session.Get <LoginViewModel>(SessionStore.CurrentUserKey).IsAdmin)
            {
                return(new RedirectResponse("/"));
            }

            int id = int.Parse(urlParameter);

            GameToAddOrEditViewModel game = this.gameService.GetById(id);

            this.ViewData["id"]          = game.Id.ToString();
            this.ViewData["title"]       = game.Title;
            this.ViewData["description"] = game.Description;
            this.ViewData["thumbnail"]   = game.ThumbnailUrl != null ? game.ThumbnailUrl : string.Empty;
            this.ViewData["trailer"]     = game.Trailer;
            this.ViewData["price"]       = game.Price.ToString("f2");
            this.ViewData["size"]        = game.Size.ToString("f1");
            this.ViewData["date"]        = game.ReleaseDate.ToString("yyyy-MM-dd");

            return(this.FileViewResponse("Game/delete-game"));
        }
        public IHttpResponse AddGame(IHttpRequest req)
        {
            string   title       = req.FormData["title"];
            string   trailer     = req.FormData["trailer"];
            string   thumbnail   = req.FormData.ContainsKey("thumbnail")?req.FormData["thumbnail"]:null;
            string   description = req.FormData["description"];
            decimal  size        = decimal.Parse(req.FormData["size"]);
            DateTime date        = DateTime.ParseExact(req.FormData["date"], "yyyy-MM-dd", CultureInfo.InvariantCulture);
            decimal  price       = decimal.Parse(req.FormData["price"]);

            GameToAddOrEditViewModel gameModel = new GameToAddOrEditViewModel()
            {
                Title        = title,
                Trailer      = trailer,
                ThumbnailUrl = thumbnail,
                Description  = description,
                Size         = size,
                ReleaseDate  = date,
                Price        = price
            };

            bool success = this.gameService.CreateGame(gameModel);

            if (!success)
            {
                this.ViewData["show-error"] = "block";
                this.ViewData["error"]      = AppConstants.IncorrectGameInput;

                return(this.FileViewResponse("Game/add-game"));
            }

            this.ViewData["show-game-added"] = "block";
            this.ViewData["game-added"]      = AppConstants.GameSuccesfullyAdded;

            return(this.FileViewResponse("Game/add-game"));
        }