Example #1
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            var user = await userManager.GetUserAsync(User);

            int companyid = (int)user.CompanyID;

            if (id == null)
            {
                return(RedirectToPage("../NotFound"));
            }

            Game = await db.GetByIdAuthorizeAsync(id, companyid);

            if (Game == null)
            {
                return(RedirectToPage("../NotFound"));
            }
            await Task.Run(() =>
            {
                var GamePicInfo         = picdb.GetIconById(Game.GameId);
                Game.HtmlText           = HttpUtility.HtmlDecode(Game.HtmlText);
                imgDisplay              = GamePicInfo == null ? "~/img/notfound.jpg" : @"~/img/" + GamePicInfo.Path;
                ListOfSupportedPlatform = gamePlatformdb.GetAllSupportedPlatform(Game.GameId);
                var platformlist        = platformdb.GetAllByName("");
                if (ListOfSupportedPlatform.Any())
                {
                    foreach (var r1 in ListOfSupportedPlatform)
                    {
                        platformlist = platformlist.Where(s => s.PlatformId != r1.PlatformId);
                    }
                }
                PlatformList    = new SelectList(platformlist, "PlatformId", "Platform_name");
                delGamePlatform = new PlatformOfGame();
                newGamePlatform = new PlatformOfGame();
                //Load info images
                oldInfo = picdb.GetById(Game.GameId, ImageType.Info);
            });

            return(Page());
        }
Example #2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                PlatformList = new SelectList(platformdb.GetAllByName(""), "PlatformId", "Platform_name");
                return(Page());
            }

            var user = await userManager.GetUserAsync(User);

            await Task.Run(() =>
            {
                var companyId    = user.CompanyID;
                Game.CompanyID   = (int)companyId;
                Game.HtmlText    = HttpUtility.HtmlEncode(Game.HtmlText);
                Game.Price       = Math.Round(Game.Price, 2, MidpointRounding.ToEven);
                Game.ReleaseDate = DateTime.Now;
            });

            db.Add(Game);
            await db.CommitAsync();

            //PlatformOfGame
            newGamePlatform.GameId = Game.GameId;
            gamePlatformdb.Add(newGamePlatform);
            await gamePlatformdb.CommitAsync();

            //Image uploading
            if (Imgfile != null)
            {
                GamePicInfo = GamePicInfo == null ? new GamePicture() : GamePicInfo;
                //Upload to file system
                string uploadsFolder  = Path.Combine(env.WebRootPath, "img");
                string uniqueFileName = Path.Combine("g", Guid.NewGuid().ToString() + "_" + Imgfile.FileName);
                string filePath       = Path.Combine(uploadsFolder, uniqueFileName);
                await Task.Run(() =>
                {
                    GamePicInfo.Path      = uniqueFileName;
                    GamePicInfo.imageType = ImageType.Icon;
                    GamePicInfo.GameId    = Game.GameId;
                });

                //Update database
                picdb.AddIcon(GamePicInfo);
                await picdb.CommitAsync();

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await Imgfile.CopyToAsync(fileStream);
                }
            }
            //Info images update
            if (infoImg != null)
            {
                //Remove old info image
                var oldinfo = picdb.GetById(Game.GameId, ImageType.Info);
                foreach (var r in oldinfo)
                {
                    picdb.Delete(r);
                    await picdb.CommitAsync();
                }
                foreach (var r in infoImg)
                {
                    await Task.Run(async() =>
                    {
                        string uploadsFolder  = Path.Combine(env.WebRootPath, "img");
                        string uniqueFileName = Path.Combine("g", Guid.NewGuid().ToString() + "_" + r.FileName);
                        string filePath       = Path.Combine(uploadsFolder, uniqueFileName);
                        var infoImage         = new GamePicture
                        {
                            GameId    = Game.GameId,
                            imageType = ImageType.Info,
                            Path      = uniqueFileName
                        };
                        //update database
                        picdb.Add(infoImage);
                        await picdb.CommitAsync();
                        using (var fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            await r.CopyToAsync(fileStream);
                        }
                    });
                }
            }
            return(RedirectToPage("../g/Index"));
        }