public void DeleteMethodShouldReturnProperAnswerModel() { var user3 = new ApplicationUser() { Id = "3asdfasdfgahasre", UserName = "******" }; var cat = new Category() { CategoryId = 10, AgeRating=AgeRating.Mature, Name="Category 1" }; List<ApplicationUser> users = new List<ApplicationUser> { new ApplicationUser() { Id = "1asdfasdfgahasre" } }; var quest = new Question() { QuestionId = 10, Creator = user3, Category = cat, Title = "Q Title", Text = "Q Text", IsApproved = true, DateAsked = DateTime.Now }; var list = new List<Answer>(); for (int i = 1; i < 6; i++) { list.Add(new Answer() { AnswerId = i, Comment = "Comment" + i, User = new ApplicationUser() { Id = "1asdfasdfgahasre", UserName = "******" }, DateAnswered = DateTime.Now, Question = quest, IsRead = true, IsReported=true, IsVoted=false, }); } var ansRepoMock = new Mock<IRepository<Answer>>(); ansRepoMock.Setup(ur => ur.All()).Returns(list.AsQueryable()); var uofMock = new Mock<IUowData>(); uofMock.Setup(x => x.Answers).Returns(() => { return ansRepoMock.Object; }); var controller = new AnswersController(uofMock.Object); var viewResult = controller.Delete(2) as ViewResult; var model = viewResult.Model; var modelAsAM = model as AnswerView; Assert.IsNotNull(modelAsAM, "Delete action returns null."); Assert.IsNotNull(model, "The model is null."); }
public ActionResult Edit(ApplicationUser user) { if (ModelState.IsValid) { this.Data.Users.Update(user); this.Data.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Genders = genders; ViewBag.Roles = this.Data.Roles.All().ToList() .Select(x => new SelectListItem { Text = x.Name, Value = x.Id }); return View(user); }
private int GetAge(ApplicationUser user) { var bDay = (DateTime)user.BirthDate; var age = DateTime.Now.Year - bDay.Year; return age; }
public async Task<ActionResult> Register(RegisterViewModel model, int[] categories) { //PopulateGenders(); List<Category> appruvedCategories = new List<Category>(); List<CategoryModel> appruvedCatModels = new List<CategoryModel>(); string errorMsg = ""; model.Gender = (Gender)model.Gender; if (categories != null && categories.Length > 0) { foreach (var catId in categories) { var cat = this.Data.Categories.GetById(catId); if (cat != null) { if (DateTime.Now.AddYears((-1) * (int)cat.AgeRating) >= DateTime.Parse(model.BirthDate)) { appruvedCategories.Add(cat); } else { errorMsg += cat.Name + " ; "; } } } appruvedCatModels = appruvedCategories.AsQueryable().Select(CategoryModel.FromCategory).ToList(); } if (appruvedCategories.Count() >= 3) { if (ModelState.IsValid && appruvedCategories.Count() >= 3) { // Create a local login before signing in the user var user = new ApplicationUser { UserName = model.UserName, BirthDate = DateTime.Parse(model.BirthDate), Gender = model.Gender, }; var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password); if (result.Success) { //add user to role await IdentityManager.Authentication.SignInAsync(AuthenticationManager, user.Id, isPersistent: false); Task<IRole> getRoleTask = IdentityManager.Roles.FindRoleByNameAsync("User"); var userRole = await getRoleTask; await IdentityManager.Roles.AddUserToRoleAsync(user.Id, userRole.Id); string uId = user.Id; var user2 = this.Data.Users.All().FirstOrDefault(u => u.Id == uId); foreach (var cat in appruvedCategories) { if (cat != null) { user2.Categories.Add(cat); } } this.Data.SaveChanges(); return RedirectToAction("Index", "Home"); } else { AddErrors(result); ViewBag.genders = this.PopulateGendersList(); return View(model); } } } else { ViewBag.genders = this.PopulateGendersList(); ViewBag.error = (string.IsNullOrEmpty(errorMsg) ? "Select at least 3 categories" : "Categories NOT suitable for your age are: " + errorMsg); ViewBag.catselected = appruvedCatModels; return View(model); } // If we got this far, something failed, redisplay form return View(model); }