Esempio n. 1
0
 /// <summary>
 /// Gets all the access types 
 /// </summary>
 /// <returns></returns>
 public static List<AccessType> GetAll()
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.AccessTypes.ToList();
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Saves or updates a checkin
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static Checkin CheckIn(Checkin c)
        {
            using (EntityContext ctx = new EntityContext())
            {
                c.ValidateAndRaise();

                // Validate user has only one active checkin.
                if (ctx.Checkins.Where(ci => !ci.EndTime.HasValue && ci.UserId == c.UserId).Any())
                {
                    throw new CheckinExistsException(i18n.API_ErrorCheckinExists);
                }

                // Validate space is not being used
                if (ctx.Checkins.Where(ci => ci.SpaceId == c.SpaceId && !ci.EndTime.HasValue).Any())
                {
                    throw new CheckinExistsException(i18n.API_ErrorSpaceUsed);
                }

                c.CheckInId = 0;
                c.StartTime = c.StartTime.ToCommonTime();
                c.EndTime = null;

                ctx.Checkins.AddObject(c);

                ctx.SaveChanges();

                // Notify users.
                Notify(NotificationType.Checkin, c);

                return c;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Gets all the endpoints in the system.
 /// </summary>
 /// <returns></returns>
 public static List<Endpoint> GetAll()
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.Endpoints.ToList();
     }
 }
Esempio n. 4
0
 /// <summary>
 /// Gets all the blockings.
 /// </summary>
 /// <returns></returns>
 public static List<SpaceBlocking> GetAll()
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.SpaceBlockings.ToList();
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Gets a specific instance 
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Notification Get(int id)
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.Notifications.Where(n => n.NotificationId.Equals(id)).FirstOrDefault();
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Gets all the blockings for a space id
 /// </summary>
 /// <returns></returns>
 public static List<SpaceBlocking> GetBlockingsForSpaceId(int id)
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.SpaceBlockings.Where(s => s.BaseSpaceId == id).ToList();
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Saves the user extra information
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public static UserInfo Save(UserInfo u)
        {
            using (EntityContext ctx = new EntityContext())
            {
                u.ValidateAndRaise();

                UserInfo existing = ctx.UserInfos.Where(ui => ui.UserId == u.UserId).FirstOrDefault();
                if (existing == null)
                {
                    ctx.UserInfos.AddObject(u);
                }
                else
                {
                    existing.ContactEmail = u.ContactEmail;
                    existing.PhoneCel = u.PhoneCel;
                    existing.PhoneHome = u.PhoneHome;
                    existing.PhoneOffice = u.PhoneOffice;
                    existing.PhoneOfficeExtension = u.PhoneOfficeExtension;
                    existing.FirstName = u.FirstName;
                    existing.LastName = u.LastName;
                    existing.Gender = u.Gender;
                    existing.NotificationsAvailability = u.NotificationsAvailability;
                    existing.Locale = u.Locale;
                }
                ctx.SaveChanges();

                // Notify connected endpoints.
                PubnubFactory.GetInstance().Publish(PubnubFactory.Channels.Users, u);

                return u;
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Gets all the roles in the app
 /// </summary>
 /// <returns></returns>
 public static List<Role> GetAll()
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.Roles.ToList();
     }
 }
Esempio n. 9
0
 /// <summary>
 /// Returns the user information for the specified user id
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public static UserInfo GetByUserId(int userId)
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.UserInfos.Where(u => u.UserId.Equals(userId)).FirstOrDefault();
     }
 }
Esempio n. 10
0
 /// <summary>
 /// Gets a specific place
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Place Get(int id)
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.Places.Where(p => p.PlaceId.Equals(id)).FirstOrDefault();
     }
 }
Esempio n. 11
0
 /// <summary>
 /// Get all the registered applications.
 /// </summary>
 /// <returns></returns>
 public static List<Application> GetAll()
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.Applications.ToList();
     }
 }
Esempio n. 12
0
 /// <summary>
 /// Gets the messages pending to be processed
 /// </summary>
 /// <returns></returns>
 public static List<MessageQueue> GetPending()
 {
     using (EntityContext ctx = new EntityContext())
     {
         return ctx.MessageQueues.Where( mq => mq.Delivered == false || !mq.Delivered.HasValue).ToList();
     }
 }
Esempio n. 13
0
 /// <summary>
 /// Marks the messages as delivered.
 /// </summary>
 /// <param name="ids"></param>
 public static void ClearIds(List<int> ids)
 {
     using (EntityContext ctx = new EntityContext())
     {
         ctx.MessageQueues.Where(mq => ids.Contains(mq.MessageId)).ToList().ForEach(mq => { mq.Delivered = true; });
         ctx.SaveChanges();
     }
 }
Esempio n. 14
0
 public static List<Role> GetRolesForUser(int userId)
 {
     using (EntityContext ctx = new EntityContext())
     {
         List<int> userRoles = ctx.UserRoles.Where(ur => ur.UserId.Equals(userId)).Select(ur => ur.RoleId).ToList();
         return ctx.Roles.Where(r => userRoles.Contains(r.RoleId)).ToList();
     }
 }
Esempio n. 15
0
 /// <summary>
 /// Cleans all the expired sessions.
 /// </summary>
 public static void CleanAll()
 {
     using (EntityContext ctx = new EntityContext())
     {
         ctx.Sessions.Where(s => s.ExpiresAt < DateTime.Now.ToCommonTime() && s.ExpiresAt != DateTime.MaxValue).ToList().ForEach(s => ctx.Sessions.DeleteObject(s));
         ctx.SaveChanges();
     }
 }
Esempio n. 16
0
 public static List<Role> GetRolesForUser(string email)
 {
     using (EntityContext ctx = new EntityContext())
     {
         User u = User.GetByEmail(email);
         List<int> userRoles = ctx.UserRoles.Where(ur => ur.UserId.Equals(u.UserId)).Select(ur => ur.RoleId).ToList();
         return ctx.Roles.Where(r => userRoles.Contains(r.RoleId)).ToList();
     }
 }
Esempio n. 17
0
 /// <summary>
 /// Deletes the role.
 /// </summary>
 /// <param name="u"></param>
 /// <param name="r"></param>
 public static void DeleteRoles(User u, List<Role> r)
 {
     using (EntityContext ctx = new EntityContext())
     {
         ctx.UserRoles.Where(ur => ur.UserId.Equals(u.UserId)
                                     && r.Where(ri => ri.RoleId.Equals(ur.RoleId)).Any()
                                 ).ToList().ForEach(ur => ctx.UserRoles.DeleteObject(ur));
         ctx.SaveChanges();
     }
 }
Esempio n. 18
0
 /// <summary>
 /// Deletes a Place
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Place Delete(int id)
 {
     using (EntityContext ctx = new EntityContext())
     {
         Place p = ctx.Places.Where(px => px.PlaceId == id).FirstOrDefault();
         ctx.Places.DeleteObject(p);
         ctx.SaveChanges();
         return p;
     }
 }
Esempio n. 19
0
        public static List<User> GetUsersInRole(string roleName)
        {
            using (EntityContext ctx = new EntityContext())
            {
                int roleId = ctx.Roles.Where(r => r.RoleName.Equals(roleName)).First().RoleId;

                List<int> userRoles = ctx.UserRoles.Where(ur => ur.RoleId.Equals(roleId)).Select(ur => ur.UserId).ToList();
                return ctx.Users.Where(u => userRoles.Contains(u.UserId)).ToList();
            }
        }
Esempio n. 20
0
 /// <summary>
 /// Deletes a Notification
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Notification Delete(int id)
 {
     using (EntityContext ctx = new EntityContext())
     {
         Notification n = ctx.Notifications.Where(nx => nx.NotificationId == id).FirstOrDefault();
         ctx.Notifications.DeleteObject(n);
         ctx.SaveChanges();
         return n;
     }
 }
Esempio n. 21
0
 /// <summary>
 /// Expires the current session id of a user
 /// </summary>
 /// <param name="sessionId"></param>
 public static void Expire(int userId)
 {
     using (EntityContext ctx = new EntityContext())
     {
         Session session = ctx.Sessions.Where(s => s.UserId.HasValue && s.UserId.Value.Equals(userId)).FirstOrDefault();
         if (session != null)
         {
             ctx.Sessions.DeleteObject(session);
             ctx.SaveChanges();
         }
     }
 }
Esempio n. 22
0
 /// <summary>
 /// Expires a session
 /// </summary>
 /// <param name="sessionId"></param>
 public static void Expire(Guid sessionId)
 {
     using (EntityContext ctx = new EntityContext())
     {
         Session session = ctx.Sessions.Where(s => s.SessionId.Equals(sessionId)).FirstOrDefault();
         if (session != null)
         {
             ctx.Sessions.DeleteObject(session);
             ctx.SaveChanges();
         }
     }
 }
Esempio n. 23
0
        /// <summary>
        /// Gets the notifications for the user id.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="lastAmount"></param>
        /// <returns></returns>
        public static List<Notification> GetLastByUserId(int userId, int lastAmount)
        {
            using (EntityContext ctx = new EntityContext())
            {
                IEnumerable<Notification> nots = ctx.Notifications
                                  .Where(n => n.UserId.Equals(userId))
                                  .OrderByDescending(n => n.CreatedAt);

                if (lastAmount > 0)
                {
                    nots = nots.Take(lastAmount);
                }

                return nots.ToList();
            }
        }
Esempio n. 24
0
        /// <summary>
        /// Saves (insert or updates) a place
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static Place Save(Place p)
        {
            using (EntityContext ctx = new EntityContext())
            {
                p.ValidateAndRaise();

                if (p.PlaceId == 0)
                {
                    ctx.Places.AddObject(p);
                }

                ctx.SaveChanges();

                return p;
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Returns the specified messages as a dictionary
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static Dictionary<string, List<string>> GetPendingAsDictionary(List<int> ids)
        {
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();

            using (EntityContext ctx = new EntityContext())
            {
                ctx.MessageQueues.Where(mq => ids.Contains(mq.MessageId)).ToList()
                    .ForEach(mq => {
                        if (dic.ContainsKey(mq.To))
                        {
                            dic[mq.To].Add(mq.Text);
                        }
                        else
                        {
                            dic.Add(mq.To, new List<string>() { mq.Text });
                        }
                    });
                return dic;
            }
        }
Esempio n. 26
0
 /// <summary>
 /// Adds roles to the user.
 /// </summary>
 /// <param name="u"></param>
 /// <param name="r"></param>
 public static void AddRoles(User u, List<Role> r)
 {
     using (EntityContext ctx = new EntityContext())
     {
         foreach (Role ri in r)
         {
             Models.UserRole ur = new Models.UserRole()
             {
                 UserId = u.UserId,
                 RoleId = ri.RoleId
             };
             if (ctx.UserRoles.Where(uro => uro.UserId.Equals(u.UserId) && uro.RoleId.Equals(ri.RoleId)).Any())
             {
             }
             else
             {
                 ctx.UserRoles.AddObject(ur);
             }
         }
         ctx.SaveChanges();
     }
 }
Esempio n. 27
0
        /// <summary>
        /// Gets the specified session. 
        /// If it has expired it returns none.
        /// </summary>
        /// <param name="sessionId"></param>
        /// <returns></returns>
        public static Session Get(Guid sessionId)
        {
            using (EntityContext ctx = new EntityContext())
            {
                Session sess = ctx.Sessions.Where(s => s.SessionId.Equals(sessionId)).FirstOrDefault();
                if (sess == null)
                {
                    return null;
                }

                if (sess.ExpiresAt < DateTime.Now.ToCommonTime() && sess.ExpiresAt != DateTime.MaxValue)
                {
                    ctx.Sessions.DeleteObject(sess);
                    ctx.SaveChanges();
                    return null;
                }
                sess.LastAccess = DateTime.Now.ToCommonTime();
                ctx.SaveChanges();

                return sess;
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Sets a new session object
        /// </summary>
        /// <param name="s"></param>
        public static Session Set(int userId, TimeSpan timeOut, bool rememberMe)
        {
            using (EntityContext ctx = new EntityContext())
            {
                Session s = new Session()
                {
                    UserId = userId,
                    CreatedAt = DateTime.Now,
                    ExpiresAt = rememberMe ? DateTime.MaxValue : DateTime.Now.ToCommonTime().Add(timeOut),
                    LastAccess = DateTime.Now.ToUniversalTime(),
                    Data = string.Empty
                };

                s.ValidateAndRaise();

                s.SessionId = Guid.NewGuid();
                ctx.Sessions.AddObject(s);
                ctx.SaveChanges();
                return s;
            }
        }
Esempio n. 29
0
        /// <summary>
        /// Sets a new session object
        /// </summary>
        /// <param name="s"></param>
        public static Session Set(Session s)
        {
            using (EntityContext ctx = new EntityContext())
            {
                s.ValidateAndRaise();

                s.SessionId = Guid.NewGuid();
                ctx.Sessions.AddObject(s);
                ctx.SaveChanges();

                return s;
            }
        }
Esempio n. 30
0
        /// <summary>
        /// Saves a new Message
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static MessageQueue Save(MessageQueue s)
        {
            using (EntityContext ctx = new EntityContext())
            {
                s.CreatedAt = DateTime.Now;
                ctx.MessageQueues.AddObject(s);

                ctx.SaveChanges();

                return s;
            }
        }