private void AddARole2(UserRolePairModel pairing) { using (var context = new ApplicationDbContext()) { var userStore = new UserStore <ApplicationUserModel>(context); var userManager = new UserManager <ApplicationUserModel>(userStore); userManager.AddToRole(pairing.UserId, pairing.RoleName); } }
public async Task <IHttpActionResult> PostGiveReward(int amount) { string id = RequestContext.Principal.Identity.GetUserId(); Test1 test1 = await db.Test1.FindAsync(id); if (test1 == null) { test1 = new Test1(); test1.Id = RequestContext.Principal.Identity.GetUserId(); test1.Punkty = amount; test1.Nickname = RequestContext.Principal.Identity.GetUserName(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Test1.Add(test1); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (Test1Exists(test1.Id)) { return(Conflict()); } else { throw; } } } Test1 test1Get19 = await db.Test1.FindAsync(id); if (test1.Punkty >= 500 - amount && test1.Punkty < 500) { UserRolePairModel userRolePairModel = new UserRolePairModel(); userRolePairModel.UserId = test1.Id; userRolePairModel.RoleName = "Creator"; AddARole2(userRolePairModel); } test1.Punkty = test1Get19.Punkty + amount; test1.Id = RequestContext.Principal.Identity.GetUserId(); db.Entry(test1).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Test1Exists(test1.Id)) { return(NotFound()); } else { throw; } } return(Ok(test1)); }