public int AddTruck(DataService dataService, User user) { // Create the truck, driver, and route // Truck truck = new Truck { Name = TruckName, PlateNumber = TruckPlate, Type = TruckType, IsPrivate = IsPrivate, User = user }; IEnumerable<string> brokenRules; dataService.Trucks.Add(truck, out brokenRules); // Add the intial position of the truck // Location startLocation = new Location { Latitude = LatValue, Longitude = LngValue, Timestamp = DateTime.Now, Truck = truck }; dataService.Locations.Add(startLocation, out brokenRules); dataService.Commit(); return truck.Id; }
protected bool FetchUser(out User user) { user = FetchUser(); if (user == null) { return false; } return true; }
protected bool Authorize(Truck truck, User user) { if (truck.User.Id != user.Id) { return false; } return true; }
public bool Add(User entity, out IEnumerable<string> brokenRules) { if(!_validation.IsValid(entity)) { brokenRules = _validation.BrokenRules(entity); return false; } brokenRules = null; return _userRepo.Add(entity); }
public void Add_User_Fails_If_UserName_Not_Unique() { string userName = Guid.NewGuid().ToString(); User user1 = new User { FirstName = "First", LastName = "User1", UserName = userName, Email = "*****@*****.**" }; User user2 = new User { FirstName = "Second", LastName = "User2", UserName = userName, Email = "*****@*****.**" }; NHibernateHelper helper = new NHibernateHelper(); // create using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); Assert.IsTrue(userRepo.Add(user1)); unitOfWork.Commit(); } try { using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); Assert.IsTrue(userRepo.Add(user2)); unitOfWork.Commit(); } } catch (NHibernate.Exceptions.GenericADOException ex) { // expected this } finally { // delete using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); Assert.IsTrue(userRepo.Delete(user1)); unitOfWork.Commit(); } } }
protected bool Authorize(int truckId, out User user, out Truck truck) { truck = null; if (!FetchUser(out user)) { return false; } if (!FetchTruck(truckId, out truck)) { _logger.Debug("No truck with id=" + truckId + " (" + user.UserName + ")"); return false; } return Authorize(truck, user); }
public ActionResult Create(User user) { // Validate the information. if(_dataService.Users.FindBy(x=>x.UserName == user.UserName)==null) { IEnumerable<string> brokenRules; _dataService.Users.Add(user, out brokenRules); _unitOfWork.Commit(); } Session["UserName"] = user.UserName; string fullName = user.FirstName + " " + user.LastName; Session["FullName"] = fullName; _formsAuth.SignIn(fullName, false); Growl("Welcome", "Thanks for registering and welcome to Truck Tracker."); return RedirectToAction("Index", "Truck"); }
public void Add_User_Fails_If_UserName_Is_Null() { User user = new User { FirstName = "Bob", LastName = "Test", UserName = null, Email = "*****@*****.**" }; NHibernateHelper helper = new NHibernateHelper(); using(UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); userRepo.Add(user); unitOfWork.Commit(); } }
public void SetUp() { using (var db = new TrackerDataContext()) { var user = new User { EmailAddress = "*****@*****.**", FirstName = "Testie", LastName = "McTester", PasswordHash = "aM/Vndh7cYd3Mxq7msArjl9YU8zoR6fF+sVTSUCcsJi2bx+cwOI0/Bkr5hfq9vYfTe3/rlgPpSMg108acpw+qA", PasswordSalt = "=Unc%", IsApproved = true, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, LastActivityDate = DateTime.Now, LastLoginDate = DateTime.Now, LastPasswordChangeDate = DateTime.Now, RoleList = new List<Role>() }; db.User.InsertOnSubmit(user); var role = new Role { Name = "Tester", Description = "Does testing.", CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now }; db.Role.InsertOnSubmit(role); user.RoleList.Add(role); db.SubmitChanges(); UserId = user.Id; RoleId = role.Id; } }
partial void OnUserChanged(User value);
partial void OnUserChanging(User newValue, User oldValue);
private static bool AreEqual(User user1, User user2) { if((user1==null && user2!=null) || (user1!=null && user2==null)) { return false; } if (user1.Id != user2.Id) return false; if (user1.Email != user2.Email) return false; if (user1.FirstName != user2.FirstName) return false; if (user1.LastName != user2.LastName) return false; return true; }
partial void OnUserAssignedChanged(User value);
partial void OnUserAssignedChanging(User newValue, User oldValue);
private ActionResult CreateUser(string userName, string firstName, string lastName, string email) { User user = _dataService.Users.FindBy(x => x.UserName == userName); if(user==null) { user = new User { UserName = userName, FirstName = firstName, LastName = lastName, Email = email }; ViewData["HideLogin"] = true; return View("CreateUser", user); } Session["UserName"] = userName; string fullName = user.FirstName + " " + user.LastName; Session["FullName"] = fullName; _formsAuth.SignIn(fullName, false); return RedirectToAction("Index", "Truck"); }
public void Crud_User_Succeeds() { string userName = Guid.NewGuid().ToString(); User user = new User { FirstName = "Bob", LastName = "Test", UserName = userName, Email = "*****@*****.**" }; NHibernateHelper helper = new NHibernateHelper(); // create using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); Assert.IsTrue(userRepo.Add(user)); unitOfWork.Commit(); } // read using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); User user1 = userRepo.FindBy(user.Id); Assert.IsTrue(AreEqual(user, user1)); User user2 = userRepo.FindBy(c => c.UserName == userName); Assert.IsTrue(AreEqual(user, user2)); unitOfWork.Commit(); } // update const string newFirstName = "This is new"; using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); User user1 = userRepo.FindBy(user.Id); user1.FirstName = newFirstName; Assert.IsTrue(userRepo.Update(user1)); unitOfWork.Commit(); } using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); User user1 = userRepo.FindBy(user.Id); Assert.IsTrue(user1.FirstName == newFirstName); } // delete using (UnitOfWork unitOfWork = new UnitOfWork(helper.SessionFactory)) { Repository<User> userRepo = new Repository<User>(unitOfWork.Session); Assert.IsTrue(userRepo.Delete(user)); unitOfWork.Commit(); } }
public bool Delete(User entity) { return _userRepo.Delete(entity); }
partial void OnUserCreatedChanged(User value);