public IActionResult Details(int id) { var game = gameRepository.Get(id); if (game == null) { throw new InvalidOperationException("Entity does not exist."); } UpdateStatistics(game); var model = new BoardGameDetailsViewModel() { GameId = game.Id, Name = game.Name, MinPlayerAge = game.MinPlayerAge, MaxPlayersNumber = game.MaxPlayersNumber, MinPlayersNumber = game.MinPlayersNumber, Statistics = gameStatisticsService.GetStatisticsForGame(game.Id) .Select(s => new GameStatisticViewModel() { Source = s.Source.GetAttribute <DisplayAttribute>().Name, ViewedDate = s.ViewedDate }) .ToList() }; return(View(model)); }
public async Task <IActionResult> Create() { BoardGame boardGame = new BoardGame() { ReleaseDate = DateTime.UtcNow.Date }; IEnumerable <Brand> brands = await _db.Brands .OrderBy(b => b.BrandName) .ToListAsync(); BoardGameDetailsViewModel boardGameDetailsViewModel = new BoardGameDetailsViewModel() { BoardGame = boardGame, Brands = brands }; return(View(boardGameDetailsViewModel)); }
public async Task <IActionResult> Edit(int?id) { BoardGame boardGame = await _db.BoardGames.FindAsync(id); if (boardGame == null) { return(NotFound()); } IEnumerable <Brand> brands = await _db.Brands .OrderBy(b => b.BrandName) .ToListAsync(); BoardGameDetailsViewModel boardGameDetailsViewModel = new BoardGameDetailsViewModel() { BoardGame = boardGame, Brands = brands }; return(View(boardGameDetailsViewModel)); }
public async Task <IActionResult> Edit(int id, [Bind("UPC", "Title", "Description", "PlayerNumber", "ReleaseDate", "Price", "ImageFilePath", "FormFileUpload", "BrandID")] BoardGame boardGame) { var boardGameToUpdate = await _db.BoardGames.FindAsync(id); if (boardGameToUpdate == null) { return(NotFound()); } var filepath = boardGame.ImageFilePath; var oldFilePath = boardGame.ImageFilePath; if (ModelState.IsValid) { if (boardGame.FormFileUpload != null) { var rand = new Random(); var randNum = rand.Next(100000, 1000000); var fileName = randNum.ToString() + "_" + boardGame.FormFileUpload.FileName; filepath = $"/images/boardgames/{fileName}"; var file = Path.Combine(_environment.ContentRootPath, "wwwroot/images/boardgames/", fileName); using (var fileStream = new FileStream(file, FileMode.Create)) { await boardGame.FormFileUpload.CopyToAsync(fileStream); } // Will only delete if the path exists and if the path is not pointed to the default avatar image var deleteFile = Path.Combine(_environment.ContentRootPath, $"wwwroot{boardGameToUpdate.ImageFilePath}"); if (System.IO.File.Exists(deleteFile) && (oldFilePath != "/images/boardgames/defaultBoardGame/Lead-Gray.jpg")) { System.IO.File.Delete(deleteFile); } } if (await TryUpdateModelAsync <BoardGame>( boardGameToUpdate, "boardgame", b => b.UPC, b => b.Title, b => b.Description, b => b.PlayerNumber, b => b.ReleaseDate, b => b.Price, b => b.ImageFilePath, b => b.BrandID)) { if (boardGame.FormFileUpload != null) { boardGameToUpdate.ImageFilePath = filepath; } await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } } IEnumerable <Brand> brands = await _db.Brands.ToListAsync(); BoardGameDetailsViewModel boardGameDetailsViewModel = new BoardGameDetailsViewModel() { Brands = brands, BoardGame = boardGame }; return(View(boardGameDetailsViewModel)); }