Ejemplo n.º 1
0
        public ActionResult Edit(string Id)
        {
            var model = _db.Games.Single(x => x.Id == Id);

            var result = new GameCreateOrUpdateVM
            {
                Title       = model.Title,
                Count       = model.Count,
                Description = model.Description,
                GenreId     = model.GenreId,
                PegiRating  = model.PegiRating,
                Price       = model.Price,
                ReleaseDate = model.ReleaseDate,
                Id          = model.Id,
                ImagePath   = model.ImagePath,
                Genres      = _db.Genres.Select(x => new SelectListItem
                {
                    Text  = x.Title,
                    Value = x.Id
                }),
            };

            result.Genres = _db.Genres.Select(x => new SelectListItem
            {
                Text  = x.Title,
                Value = x.Id
            });
            return(View(result));
        }
Ejemplo n.º 2
0
        public ActionResult Create(GameCreateOrUpdateVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                var newGame = new Game
                {
                    Title             = model.Title,
                    Count             = model.Count,
                    Description       = model.Description,
                    GenreId           = model.GenreId,
                    PegiRating        = model.PegiRating,
                    Price             = model.Price,
                    ReleaseDate       = model.ReleaseDate,
                    ApplicationUserId = _db.Users.Single(x => x.UserName.Equals(User.Identity.Name)).Id
                };

                if (Request.Files != null && Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    var file = Request.Files[0];

                    var fileName = Path.GetFileName(file.FileName);
                    var path     = Path.Combine(Server.MapPath(IMAGE_FILES_PATH), fileName);
                    file.SaveAs(path);
                    newGame.ImagePath = fileName;
                }
                _db.Games.Add(newGame);
                _db.SaveChanges();

                ViewBag.Alert = new AlertInformation
                {
                    Title     = "Succeed",
                    Message   = string.Format("The {0} game created successfully.", newGame.Title),
                    AlertType = AlertType.success
                };
                return(RedirectToAction("Index", "Game"));
            }
            catch (Exception ex)
            {
                ViewBag.Alert = new AlertInformation
                {
                    Title     = "Error",
                    Message   = string.Format("There was an error while creating {0} game .", model.Title),
                    AlertType = AlertType.danger
                };
                return(View(model));
            }
            finally
            {
                model.Genres = _db.Genres.Select(x => new SelectListItem
                {
                    Text  = x.Title,
                    Value = x.Id
                });
            }
        }
Ejemplo n.º 3
0
        public ActionResult Create()
        {
            var model = new GameCreateOrUpdateVM();

            model.Genres = _db.Genres.Select(x => new SelectListItem
            {
                Text  = x.Title,
                Value = x.Id
            });
            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult Edit(GameCreateOrUpdateVM model)
        {
            model.Genres = _db.Genres.Select(x => new SelectListItem
            {
                Text  = x.Title,
                Value = x.Id
            });
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                var currentModel = _db.Games.Find(model.Id);
                var updated      = new Game
                {
                    Title             = model.Title,
                    Count             = model.Count,
                    Description       = model.Description,
                    GenreId           = model.GenreId,
                    PegiRating        = model.PegiRating,
                    Price             = model.Price,
                    ReleaseDate       = model.ReleaseDate,
                    ApplicationUserId = _db.Users.Single(x => x.UserName.Equals(User.Identity.Name)).Id,
                    UpdatedAt         = DateTime.Now,
                    Id        = model.Id,
                    ImagePath = currentModel.ImagePath
                };

                if (Request.Files != null && Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    var file = Request.Files[0];
                    if (!string.IsNullOrEmpty(updated.ImagePath))
                    {
                        var deleted = Path.Combine(Server.MapPath(IMAGE_FILES_PATH), updated.ImagePath);
                        System.IO.File.Delete(deleted);
                    }

                    if (!string.IsNullOrEmpty(file.FileName))
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path     = Path.Combine(Server.MapPath(IMAGE_FILES_PATH), fileName);
                        file.SaveAs(path);
                        updated.ImagePath = fileName;
                    }
                }

                _db.Entry(currentModel).CurrentValues.SetValues(updated);
                _db.SaveChanges();
                ViewBag.Alert = new AlertInformation
                {
                    Title     = "Succeed",
                    Message   = string.Format("The {0} game updated successfully.", model.Title),
                    AlertType = AlertType.success
                };
                return(View("Index", _db.Games.ToList()));
            }
            catch (Exception ex)
            {
                ViewBag.Alert = new AlertInformation
                {
                    Title     = "Error",
                    Message   = string.Format("There was an error while updating {0} game .", model.Title),
                    AlertType = AlertType.success
                };
                return(View(model));
            }
        }