Esempio n. 1
0
      public async Task<ActionResult> Create([Bind(Include = "ID,Name,Visibility")] Game game)
      {
         if (ModelState.IsValid)
         {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.db));

            var appUser = await manager.FindByIdAsync(this.User.Identity.GetUserId());

            db.Games.Add(game);

            await manager.AddClaimAsync(appUser.Id, new Claim("GameMembership", game.ID.ToString()));
            await manager.AddClaimAsync(appUser.Id, new Claim("GameOwnership", game.ID.ToString()));
            await manager.AddClaimAsync(appUser.Id, new Claim("GameMaster", game.ID.ToString()));

            var membership = new GameMembership()
            {
               Game = game,
               ApplicationUser = appUser,
               Roles = "Owner",
            };
            db.GameMemberships.Add(membership);
            await db.SaveChangesAsync();

            return RedirectToAction("Index");
         }

         return View(game);
      }
      public async Task<GameResult> Create([FromBody]GameResult result)
      {
         if (result == null)
         {
            throw new ArgumentNullException("game");
         }

         GameVisibility visibility;
         if (!Enum.TryParse(result.Visibility, out visibility))
         {
            throw new ArgumentException("game.Visibility is not valid");
         }

         var game = new Game()
         {
            Name = result.Name,
            Visibility = visibility,
         };

         var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.db));

         var appUser = await manager.FindByIdAsync(this.User.Identity.GetUserId());

         db.Games.Add(game);

         await manager.AddClaimAsync(appUser.Id, new Claim("GameMembership", game.ID.ToString()));
         await manager.AddClaimAsync(appUser.Id, new Claim("GameOwnership", game.ID.ToString()));
         await manager.AddClaimAsync(appUser.Id, new Claim("GameMaster", game.ID.ToString()));

         var membership = new GameMembership()
         {
            Game = game,
            ApplicationUser = appUser,
            Roles = "Owner",
         };
         db.GameMemberships.Add(membership);
         await db.SaveChangesAsync();


         result.GameId = game.ID;

         return result;
      }