コード例 #1
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()
        {
            //PlatformOfGame
            if (newGamePlatform != null && newGamePlatform.PlatformId != 0)
            {
                newGamePlatform.GameId = Game.GameId;
                gamePlatformdb.Add(newGamePlatform);
                await gamePlatformdb.CommitAsync();

                return(RedirectToPage("./EditGame", new { id = Game.GameId }));
            }
            else if (delGamePlatform != null && delGamePlatform.PlatformId != 0)
            {
                delGamePlatform = gamePlatformdb.GetById(delGamePlatform.GameId, delGamePlatform.PlatformId);
                if (delGamePlatform == null)
                {
                    return(Page());
                }
                gamePlatformdb.Delete(delGamePlatform);
                await gamePlatformdb.CommitAsync();

                return(RedirectToPage("./EditGame", new { id = delGamePlatform.GameId }));
            }

            //Game
            Game.HtmlText = HttpUtility.HtmlEncode(Game.HtmlText);
            Game.Price    = Math.Round(Game.Price, 2, MidpointRounding.ToEven);

            db.Update(Game);
            await db.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);
                //Update database
                GamePicInfo.Path      = uniqueFileName;
                GamePicInfo.imageType = ImageType.Icon;
                GamePicInfo.GameId    = Game.GameId;
                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);
                        }
                    });
                }
            }
            TempData["CompanyStatus"] = "Your game has been updated!";

            return(RedirectToPage("./Index"));
        }
コード例 #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"));
        }