Beispiel #1
0
        public IActionResult Create(Hero item)
        {
            _context.Hero.Add(item);
            _context.SaveChanges();

            return(CreatedAtRoute("GetHero", new { id = item.Id }, item));
        }
Beispiel #2
0
 public IActionResult Post([FromBody] Hero person)
 {
     try
     {
         _context.heroes.Add(person);
         _context.SaveChanges();
     }
     catch (System.ArgumentException) { }
     return(CreatedAtRoute("GetById", new { id = person.id }, person));
 }
Beispiel #3
0
        public ActionResult GetId(int id)
        {
            var heroi = new Hero {
                Name = "Thor"
            };

            _context.Heroes.Add(heroi); // defino de forma explicita quem estou adicionando
            //contexto.Add(heroi);
            //definindo o id estou fazendo um update, caso não coloque id ele considera um insert
            _context.SaveChanges();
            return(Ok()); //retorna 200
        }
Beispiel #4
0
        public IActionResult Post([FromBody] Hero hero)
        {
            if (hero == null)
            {
                return(BadRequest());
            }

            dbContext.Heroes.Add(hero);
            dbContext.SaveChanges();

            return(new ObjectResult(hero));
        }
Beispiel #5
0
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
            //var user = new ApplicationUser() { UserName = email.Text, Email = email.Text };

            var user = new ApplicationUser
            {
                UserName = EmailTextBox.Text,
                Email    = EmailTextBox.Text
            };


            IdentityResult result = manager.Create(user);

            if (result.Succeeded)
            {
                var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (loginInfo == null)
                {
                    RedirectOnFail();
                    return;
                }
                result = manager.AddLogin(user.Id, loginInfo.Login);
                if (result.Succeeded)
                {
                    using (var heroContext = new HeroContext())
                    {
                        var hero = new Hero
                        {
                            UserId         = user.Id,
                            Name           = HeroNameTextbox.Text,
                            MovesRemaining = 25,
                            Credits        = 10,
                            Health         = 50,
                            TrainingLevel  = 0,
                            ArmorBonus     = 0,
                            WeaponBonus    = 0,
                            Wins           = 0,
                            Losses         = 0
                        };
                        heroContext.Heroes.Add(hero);
                        heroContext.SaveChanges();
                    }

                    IdentityHelper.SignIn(manager, user, isPersistent: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // var code = manager.GenerateEmailConfirmationToken(user.Id);
                    // Send this link via email: IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id)

                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    return;
                }
            }
            AddErrors(result);
        }
Beispiel #6
0
 public Task <Hero> Handle(HeroCommands.Create request, CancellationToken cancellationToken)
 {
     request.Hero.Id = context.Heroes.Max(r => r.Id) + 1;
     context.Heroes.Add(request.Hero);
     context.SaveChanges();
     return(Task.FromResult(request.Hero));
 }
Beispiel #7
0
        public Task <Unit> Handle(HeroCommands.Delete request, CancellationToken cancellationToken)
        {
            var hero = context.Heroes.Find(request.Id);

            context.Heroes.Remove(hero);
            context.SaveChanges();
            return(Task.FromResult(Unit.Value));
        }
Beispiel #8
0
        public static void Initialize(HeroContext context)
        {
            context.Database.EnsureCreated();

            if (context.HeroItems.Any())
            {
                return;
            }

            var HeroItems = new HeroItem[]
            {
                new HeroItem {
                    Id = 1, Name = "Szymon Piotr"
                },
                new HeroItem {
                    Id = 2, Name = "Andrzej"
                },
                new HeroItem {
                    Id = 3, Name = "Jakub"
                },
                new HeroItem {
                    Id = 4, Name = "Jan"
                },
                new HeroItem {
                    Id = 5, Name = "Filip"
                },
                new HeroItem {
                    Id = 6, Name = "Bartłomiej"
                },
                new HeroItem {
                    Id = 7, Name = "Tomasz"
                },
                new HeroItem {
                    Id = 8, Name = "Mateusz"
                },
                new HeroItem {
                    Id = 9, Name = "Jakub"
                },
                new HeroItem {
                    Id = 10, Name = "Tadeusz"
                },
                new HeroItem {
                    Id = 11, Name = "Szymon Gorliwy"
                },
                new HeroItem {
                    Id = 12, Name = "Judasz"
                }
            };

            foreach (HeroItem hero in HeroItems)
            {
                context.HeroItems.Add(hero);
            }

            context.SaveChanges();
        }
 public bool InitDataBase()
 {
     using (HeroContext context = MySqlDataBaseConfig.CreateContext())
     {
         context.Heroes.Add(new Hero {
             Name = "Aaron"
         });
         return(context.SaveChanges() > 0);
     }
 }
 public static void PopulateTestData(HeroContext context)
 {
     context.Heroes.Add(new Hero {
         Id = 1, Name = "Iron Man", Identity = "Tony Stark", Hometown = "L.A.", Age = 40
     });
     context.Heroes.Add(new Hero {
         Id = 2, Name = "Spiderman", Identity = "Peter Parker", Hometown = "N.Y.", Age = 17
     });
     context.SaveChanges();
 }
Beispiel #11
0
        public Task <Hero> Handle(HeroCommands.Update request, CancellationToken cancellationToken)
        {
            var existingHero = context.Heroes.Find(request.Hero.Id);

            existingHero.Name     = request.Hero.Name;
            existingHero.Universe = request.Hero.Universe;
            context.SaveChanges();

            return(Task.FromResult(request.Hero));
        }
Beispiel #12
0
        public Task <List <Hero> > Handle(HeroQueries.GetAll request, CancellationToken cancellationToken)
        {
            if (!context.Heroes.Any())
            {
                context.Heroes.AddRange(GetHeroes());
                context.SaveChanges();
            }
            var heroes = context.Heroes.ToList();

            return(Task.FromResult(heroes));
        }
Beispiel #13
0
        public HeroController(HeroContext context)
        {
            _context = context;

            if (_context.Hero.Count() == 0)
            {
                _context.Hero.Add(new Hero {
                    Name = "DefaultHero"
                });
                _context.SaveChanges();
            }
        }
Beispiel #14
0
        /// <summary>
        /// ヒーローを追加する
        /// </summary>
        /// <param name="heroName">追加するヒーローの名前</param>
        /// <remarks>
        /// Fiddler2などでのテストの仕方
        /// httpヘッダに追加
        ///   Accept: text/html
        ///   Content-Type: text/json; charset=utf-8
        /// ボディに追加するヒーローの名前をutf-8でダブルクォート(")で囲う。
        ///   例:"仮面ライダー1号"
        /// </remarks>
        /// <returns>追加に成功した場合201を返して、追加したヒーローを返す。</returns>
        public HttpResponseMessage Post([FromBody] string heroName)
        {
            Hero _hero;

            using (var context = new HeroContext()) {
                _hero      = new Hero();
                _hero.Name = heroName;
                context.Heroes.Add(_hero);
                context.SaveChanges();
                //201でレスポンスメッセージを作成する。
                return(Request.CreateResponse <Hero>(HttpStatusCode.Created, _hero));
            }
        }
Beispiel #15
0
        public HeroController(HeroContext context)
        {
            _context = context;

            if (_context.Heroes.Count() == 0)
            {
                // Create a new Hero if collection is empty,
                // which means you can't delete all Heroes.
                _context.Heroes.Add(new Hero {
                    Name = "Iron Man", Identity = "Tony Stark", Hometown = "L.A.", Age = 40
                });
                _context.SaveChanges();
            }
        }
Beispiel #16
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();

            //var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };

            var user = new ApplicationUser
            {
                UserName = EmailTextBox.Text,
                Email    = EmailTextBox.Text
            };

            IdentityResult result = manager.Create(user, Password.Text);

            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                //string code = manager.GenerateEmailConfirmationToken(user.Id);
                //string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                //manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

                using (var heroContext = new HeroContext())
                {
                    var hero = new Hero
                    {
                        UserId         = user.Id,
                        Name           = HeroNameTextbox.Text,
                        MovesRemaining = 25,
                        Credits        = 10,
                        Health         = 50,
                        TrainingLevel  = 0,
                        ArmorBonus     = 0,
                        WeaponBonus    = 0,
                        Wins           = 0,
                        Losses         = 0
                    };
                    heroContext.Heroes.Add(hero);
                    heroContext.SaveChanges();
                }
                IdentityHelper.SignIn(manager, user, isPersistent: false);
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
Beispiel #17
0
        //
        //JSON(もしくはxml)で追加を受け付ける場合
        //
        //public HttpResponseMessage Post([FromBody]Hero _hero) {
        //    using (var context = new HeroContext()) {
        //        context.Heroes.Add(_hero);
        //        context.SaveChanges();
        //        //201でレスポンスメッセージを作成する。
        //        return Request.CreateResponse<Hero>(HttpStatusCode.Created, _hero);
        //    }
        //}

        /// <summary>
        /// ヒーローの名前を変更する
        /// </summary>
        /// <param name="id">名前を変更したいヒーローのid</param>
        /// <param name="name">変更する名前</param>
        /// <remarks>
        /// Fiddler2などでのテストの仕方
        /// URL
        ///   http://(hostname):(port)/api/heroes?id=(変更するid)
        /// httpヘッダに追加
        ///   Accept: text/html
        ///   Content-Type: text/json; charset=utf-8
        /// ボディに追加するヒーローの名前をutf-8でダブルクォート(")で囲う。
        ///   例:"仮面ライダー1号"
        /// </remarks>
        /// <returns>
        /// 変更に成功した場合には200を返して、変更結果を返す。
        /// </returns>
        public HttpResponseMessage Put(int id, [FromBody] string name)
        {
            using (var context = new HeroContext()) {
                var _hero = context.Heroes.Find(id);
                if (_hero != null)
                {
                    _hero.Name = name;
                    context.SaveChanges();
                    return(Request.CreateResponse <Hero>(HttpStatusCode.OK, _hero));
                }
                else
                {
                    //idのヒーローが見つからない場合には404を返す。
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
        }
Beispiel #18
0
 private void AddSeedingData(HeroContext context)
 {
     context.Database.EnsureCreated();
     context.Heroes.Add(new Hero()
     {
         Id = 11, Name = "Dr Nice"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 12, Name = "Narco"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 13, Name = "Bombasto"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 14, Name = "Celeritas"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 15, Name = "Magneta"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 16, Name = "RubberMan"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 17, Name = "Dynama"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 18, Name = "Dr IQ"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 19, Name = "Magma"
     });
     context.Heroes.Add(new Hero()
     {
         Id = 20, Name = "Tornado"
     });
     context.SaveChanges();
 }
Beispiel #19
0
        /// <summary>
        /// 引数で与えられたヒーローを削除する。
        /// </summary>
        /// <exception cref="HttpResponseException">
        /// 引数idで与えられたヒーローが存在しない場合には404エラーが発生する。
        /// </exception>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            Hero _hero;

            using (var context = new HeroContext()) {
                _hero = context.Heroes.Find(id);
                if (_hero != null)
                {
                    context.Heroes.Remove(_hero);
                    context.SaveChanges();
                }
                else
                {
                    //idのヒーローが見つからない場合には404を返す。
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
        }
 public async Task <IEnumerable <Hero> > Handle(HeroQueries.HeroesQuery request, CancellationToken cancellationToken)
 {
     if (heroContext.Heroes.Count() == 0)
     {
         heroContext.Heroes.AddRange(new Collection <Hero>()
         {
             new Hero {
                 Id = 11, Name = "Capitan America", Active = true
             },
             new Hero {
                 Id = 12, Name = "Ironman", Active = true
             },
             new Hero {
                 Id = 13, Name = "Black-Widow", Active = false
             },
             new Hero {
                 Id = 14, Name = "Chapulin", Active = true
             },
             new Hero {
                 Id = 15, Name = "Hulk", Active = true
             },
             new Hero {
                 Id = 16, Name = "Ant-man", Active = true
             },
             new Hero {
                 Id = 17, Name = "Spiderman", Active = true
             },
             new Hero {
                 Id = 18, Name = "Thor", Active = true
             },
             new Hero {
                 Id = 19, Name = "Loki", Active = false
             },
             new Hero {
                 Id = 20, Name = "Valkiria", Active = false
             },
             new Hero {
                 Id = 21, Name = "Thanos", Active = false
             },
         });
         heroContext.SaveChanges();
     }
     return(await heroContext.Heroes.ToArrayAsync());
 }
Beispiel #21
0
        public HeroController(HeroContext context)
        {
            this._context = context;

            // inserindo dados na database
            if (_context.Heroes.Count() == 0)
            {
                _context.Heroes.Add(new Hero {
                    Name = "Mr. Nice"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Narco"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Bombasto"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Celeritas"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Magneta"
                });
                _context.Heroes.Add(new Hero {
                    Name = "RubberMan"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Dynama"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Dr IQ"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Magma"
                });
                _context.Heroes.Add(new Hero {
                    Name = "Tornado"
                });
                _context.SaveChanges();
            }
        }
Beispiel #22
0
        public HeroesController(HeroContext heroContext)
        {
            _heroContext = heroContext;

            if (_heroContext.Heroes.Count() == 0)
            {
                _heroContext.Heroes.Add(new Hero {
                    Id = 1, Name = "Storm"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 2, Name = "Magneto"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 3, Name = "Wolverine"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 4, Name = "Psylock"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 5, Name = "Deadpool"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 6, Name = "Cyclops"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 7, Name = "Cable"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 8, Name = "Rogue"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 9, Name = "Juggernaut"
                });
                _heroContext.Heroes.Add(new Hero {
                    Id = 10, Name = "Night Crawler"
                });
                _heroContext.SaveChanges();
            }
        }
Beispiel #23
0
        private static void CreateObjects(string inputHeroName, int inputHp, int inputMana, string q, string w, string e,
                                          string playstyle, string ultimate, Mainattribute mainattribute, int inputMs, int inputArmor, DbContextOptionsBuilder <HeroContext> optionsBuilder)
        {
            using (var db = new HeroContext(optionsBuilder.Options))
            {
                Hero newHero = new Hero()
                {
                    Name          = inputHeroName,
                    Hp            = inputHp,
                    Mana          = inputMana,
                    Q             = q,
                    W             = w,
                    E             = e,
                    Ultimate      = ultimate,
                    Playstyle     = playstyle,
                    Mainattribute = mainattribute,
                    Ms            = inputMs,
                    Armor         = inputArmor
                };

                db.Heroes.Add(newHero);
                db.SaveChanges();
            }
        }
 /// <summary>
 /// SaveChange
 /// </summary>
 /// <returns></returns>
 public bool SaveChanges()
 {
     return((_context.SaveChanges()) > 0);
 }
Beispiel #25
0
 public IActionResult Post([FromBody] SuperPower superPower)
 {
     _context.superPowers.Add(superPower);
     _context.SaveChanges();
     return(CreatedAtRoute("GetSuperPowerById", new { id = superPower.id }, superPower));
 }
Beispiel #26
0
 public IActionResult Post([FromBody] ToDo todo)
 {
     _context.todos.Add(todo);
     _context.SaveChanges();
     return(CreatedAtRoute("GetToDoById", new { id = todo.id }, todo));
 }
 public IActionResult Post([FromBody] CitiesList citiesList)
 {
     _context.citiesLists.Add(citiesList);
     _context.SaveChanges();
     return(CreatedAtRoute("GetCitiesListById", new { id = citiesList.id }, citiesList));
 }
Beispiel #28
0
 public void AddAHeroAsync(SuperHero hero)
 {
     context.Superpeople.AddAsync(mapper.ParseSuperHero(hero));
     context.SaveChanges();
 }
Beispiel #29
0
 //Adds the test data
 private void Seed(HeroContext testContext)
 {
     testContext.Charactertype.AddRange(charactertypes);
     testContext.Superpeople.AddRange(testPeople);
     testContext.SaveChanges();
 }
 public void AddAVillain(SuperVillain superVillain)
 {
     context.Superpeople.Add(mapper.ParseSuperVillain(superVillain));
     context.SaveChanges();
 }