public LockModel CreateLock(string lockName, IList <int> allowedUsers) { using (var smartLock = new SmartLockEntities()) { int changes = 0; var lockInfo = new LockInfo { Name = lockName, State = "Locked" }; smartLock.LockInfoes.Add(lockInfo); changes += smartLock.SaveChanges(); if (allowedUsers.Count > 0) { smartLock.LockAccesses.AddRange(this.PopulateLockAccessList(lockInfo.LockId, allowedUsers)); changes += smartLock.SaveChanges(); } if (changes == allowedUsers.Count + 1) { return(new LockModel { LockId = lockInfo.LockId, Name = lockName, State = lockInfo.State, AllowedUsers = allowedUsers }); } return(null); } }
public UserModel GetUser(string userEmail, string userPassword) { var hashedPassword = this.GetSHA256Password(userPassword); using (var smartLock = new SmartLockEntities()) { var query = from users in smartLock.UserInfoes where (users.Email == userEmail && users.Password == hashedPassword) select users; UserInfo user = query.FirstOrDefault(); if (user == null) { throw new UserNotFoundException("Not found."); } return(new UserModel { UserId = user.UserId, UserName = user.UserName, Email = user.Email, IsAdmin = user.IsAdmin }); } }
public bool ModifyLockState(int lockId, int userId, LockState state) { using (var smartLock = new SmartLockEntities()) { LockInfo lockInfo = smartLock.LockInfoes.FirstOrDefault(l => l.LockId == lockId); if (lockInfo == null) { // lock not found. throw new LockNotFoundException("Lock not found."); } LockAccess lockAccess = smartLock.LockAccesses.FirstOrDefault(l => l.UserId == userId && l.LockId == lockId); // TODO: Expand check to limit access based on time if (lockAccess == null) { // user does not have access to the lock. Throw exception. throw new UnauthorizedUserException("User unauthorized."); } // modify lock state. lockInfo.State = String.Concat(state.ToString(), "ed"); int changes = smartLock.SaveChanges(); return(changes == 1 ? true : false); } }
public bool CreateUserAccess(int lockId, IList <int> allowedUsers) { using (var smartLock = new SmartLockEntities()) { smartLock.LockAccesses.AddRange(this.PopulateLockAccessList(lockId, allowedUsers)); int changes = smartLock.SaveChanges(); return(changes == allowedUsers.Count ? true : false); } }
public string GetLockState(int lockId) { using (var smartLock = new SmartLockEntities()) { LockInfo lockInfo = smartLock.LockInfoes.FirstOrDefault(l => l.LockId == lockId); if (lockInfo == null) { throw new LockNotFoundException("lockId"); } return(lockInfo.State); } }
public bool CreateEvent(int lockId, int userId, string lockState) { using (var smartLock = new SmartLockEntities()) { smartLock.UserEvents.Add( new UserEvent { LockId = lockId, UserId = userId, State = lockState, Timestamp = DateTime.UtcNow }); int change = smartLock.SaveChanges(); return(change == 1 ? true : false); } }
public int CreateUser(string userName, string userEmail, string userPassword, bool isAdmin) { var hashedPassword = this.GetSHA256Password(userPassword); using (var smartLock = new SmartLockEntities()) { var userInfo = new UserInfo { UserName = userName, Email = userEmail, Password = hashedPassword, IsAdmin = isAdmin }; smartLock.UserInfoes.Add(userInfo); smartLock.SaveChanges(); return(userInfo.UserId); } }
public IList <EventModel> GetUserEvents(int userId) { using (var smartLock = new SmartLockEntities()) { var query = from events in smartLock.UserEvents where events.UserId == userId select events; var eventsList = new List <EventModel>(); foreach (var element in query) { eventsList.Add( new EventModel { LockId = element.LockId, Timestamp = element.Timestamp, State = element.State }); } return(eventsList); } }
public IList <LockModel> GetLocks() { using (var smartLock = new SmartLockEntities()) { var query = from locks in smartLock.LockInfoes select locks; var locksList = new List <LockModel>(); foreach (LockInfo element in query) { locksList.Add( new LockModel { LockId = element.LockId, Name = element.Name, State = element.State }); } return(locksList); } }
public UserModel GetUser(int userId) { using (var smartLock = new SmartLockEntities()) { var query = from users in smartLock.UserInfoes where users.UserId == userId select users; UserInfo user = query.FirstOrDefault(); if (user == null) { throw new UserNotFoundException("Not found."); } return(new UserModel { UserId = user.UserId, UserName = user.UserName, IsAdmin = user.IsAdmin }); } }