public MainMenu(HeroContext context, IMapper mapper) { this.heroMenu = new HeroMenu(new FileRepo(), new MessagingService()); this.villainMenu = new VillainMenu(new FileRepo(), new MessagingService()); this.heroMenu = new HeroMenu(new DBRepo(context, mapper), new MessagingService()); this.villainMenu = new VillainMenu(new DBRepo(context, mapper), new MessagingService()); }
public async Task <ActionResult <Hero> > Put([FromServices] HeroContext context, [FromBody] Hero model) { context.Heroes.Update(model); await context.SaveChangesAsync(); return(model); }
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); }
/// <summary> /// Configures the middleware used by the application. /// </summary> /// <param name="app">Application to configure.</param> /// <param name="heroContext">Hero context to use.</param> public void Configure(IApplicationBuilder app, HeroContext heroContext) { app.UseCors("AllowAll"); app.UseMvc(); heroContext.Database.EnsureCreated(); }
public async Task <ActionResult <Hero> > Delete(int id, [FromServices] HeroContext context) { var hero = await context.Heroes.FirstOrDefaultAsync(x => x.Id == id); context.Heroes.Remove(hero); await context.SaveChangesAsync(); return(hero); }
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); } }
/// <summary> /// ヒーローのリストを取得する。 /// </summary> /// <remarks> /// ODataのインターフェイスをサポートするように戻り値をIQueryable(T)型に変更した。 /// </remarks> /// <returns>ヒーローのリスト(JSON).</returns> public IQueryable <Hero> Get() { List <Hero> heroes; using (var context = new HeroContext()) { heroes = context.Heroes.ToList(); } //List<T>をIQueryable<T>に変換する。 return(heroes.AsQueryable()); }
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(); }
public HeroController(HeroContext context) { _context = context; if (_context.Hero.Count() == 0) { _context.Hero.Add(new Hero { Name = "DefaultHero" }); _context.SaveChanges(); } }
/// <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)); } }
private static void QueryData(DbContextOptionsBuilder <HeroContext> optionsBuilder) { using (var db = new HeroContext(optionsBuilder.Options)) { var heroes = db.Heroes; Console.WriteLine("\n"); foreach (var hero in heroes) { Console.WriteLine("{0}", hero.Name); } } }
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(); } }
/// <summary> /// ヒーローを取得する /// </summary> /// <param name="id">取得したいヒーローのid</param> /// <exception cref="HttpResponseException"> /// 指定されたidのヒーローが存在しない場合は404を返します。 /// </exception> /// <returns>ヒーロー</returns> public Hero Get(int id) { Hero _hero; using (var context = new HeroContext()) { _hero = context.Heroes.Find(id); if (_hero == null) { //idのヒーローが見つからない場合には404を返す。 throw new HttpResponseException(HttpStatusCode.NotFound); } } return(_hero); }
public async Task <ActionResult <Hero> > Post([FromServices] HeroContext context, [FromBody] Hero model) { if (ModelState.IsValid) { context.Heroes.Add(model); await context.SaveChangesAsync(); return(model); } else { return(BadRequest(ModelState)); } }
public ActionResult Index() { HeroContext dbContext = new HeroContext(); IAbilityRepository repository = new HeroEntityFrameworkSample.Models.AbilityRepository(dbContext); IAbilityAuthorizationService service = new AbilityAuthorizationService(null, null, repository); HeroConfig.Initialize(service); var abilitesFromHero = HeroConfig.AuthorizationService.GetAbilities(); var abilities = repository.Get <SecureAbility>().ToList(); return(View()); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); using (var context = new HeroContext(Configuration)) { context.Database.Migrate(); } services.AddDbContext <HeroContext>(); services.AddCors(options => { options.AddPolicy(name: MyAllowSpecificOrigins, builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }); }); services.AddScoped <IHeroRepository, HeroRepository>(); }
public void AddHeroShouldAdd() { //Arranging necessary artifacts var options = new DbContextOptionsBuilder <HeroContext>().UseInMemoryDatabase("AddHeroesShouldAdd").Options; using var testContext = new HeroContext(options); repo = new DBRepo(testContext, mapper); //Act repo.AddAHeroAsync(testHero); //Assert using var assertContext = new HeroContext(options); Assert.NotNull(assertContext.Superpeople.Single(h => h.Workname == testHero.Alias)); }
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(); } }
public void GetHeroByNameShouldGetHeroByName() { //Arrange var options = new DbContextOptionsBuilder <HeroContext>().UseInMemoryDatabase("GetHeroByNameShouldGetHeroByName").Options; using var testContext = new HeroContext(options); Seed(testContext); //Act using var assertContext = new HeroContext(options); repo = new DBRepo(assertContext, mapper); var result = repo.GetHeroByName("Bob the builder"); //Assert Assert.NotNull(result); Assert.Equal("Bob", result.RealName); }
public async System.Threading.Tasks.Task GetHeroesShouldGetAsync() { //Arranging necessary artifacts var options = new DbContextOptionsBuilder <HeroContext>().UseInMemoryDatabase("GetHeroesShouldGet").Options; using var testContext = new HeroContext(options); Seed(testContext); //Act using var assertContext = new HeroContext(options); repo = new DBRepo(assertContext, mapper); var result = await repo.GetAllHeroesAsync(); //Assert Assert.NotNull(result); Assert.Equal(1, result.Count); }
public void GetHeroesShouldGet() { //Arrange var options = new DbContextOptionsBuilder <HeroContext>().UseInMemoryDatabase("GetHeroesShouldGet").Options; using var testContext = new HeroContext(options); Seed(testContext); //Act using var assertContext = new HeroContext(options); repo = new DBRepo(assertContext, mapper); var result = repo.GetAllHeroesAsync(); //Assert Assert.NotNull(result); Assert.Equal(1, result.Count); }
// //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); } } }
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(); }
/// <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 UnitOfWorkTest() { var builder = new SqliteConnectionStringBuilder() { DataSource = ":memory:" }; var connectionString = builder.ToString(); _connection = new SqliteConnection(connectionString); _connection.Open(); var options = new DbContextOptionsBuilder <HeroContext>() .UseSqlite(_connection) .Options; Context = new HeroContext(options); Context.Database.EnsureCreated(); Target = new UnitOfWork(Context); }
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(); } }
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(); } }
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(); } }
//Adds the test data private void Seed(HeroContext testContext) { testContext.Charactertype.AddRange(charactertypes); testContext.Superpeople.AddRange(testPeople); testContext.SaveChanges(); }