Esempio n. 1
0
 public static User LoadById(Guid id)
 {
     try
     {
         using (AmbrosiaEntities dc = new AmbrosiaEntities())
         {
             tblUser row = dc.tblUsers.FirstOrDefault(g => g.Id == id);
             if (row != null)
             {
                 User user = new User
                 {
                     Id        = row.Id,
                     FirstName = row.FirstName,
                     LastName  = row.LastName,
                     Username  = row.Username,
                     Password  = row.Password,
                     Meals     = MealManager.Load(row.Id),
                     Workouts  = WorkoutManager.Load(row.Id)
                 };
                 return(user);
             }
             else
             {
                 throw new Exception("Row was not found!");
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 2
0
        public static List <User> Load()
        {
            //Retrieve all the rows in a list
            try
            {
                List <User> rows = new List <User>();
                using (AmbrosiaEntities dc = new AmbrosiaEntities())
                {
                    dc.tblUsers
                    .ToList()
                    .ForEach(g => rows.Add(new User
                    {
                        Id        = g.Id,
                        FirstName = g.FirstName,
                        LastName  = g.LastName,
                        Username  = g.Username,
                        Password  = g.Password,
                        Meals     = MealManager.Load(g.Id),
                        Workouts  = WorkoutManager.Load(g.Id)
                    }));

                    return(rows);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        public static bool Login(User user)
        {
            try
            {
                if (!string.IsNullOrEmpty(user.Username.ToString()))
                {
                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        using (AmbrosiaEntities dc = new AmbrosiaEntities())
                        {
                            tblUser tblUser = dc.tblUsers.FirstOrDefault(u => u.Username == user.Username);
                            if (tblUser != null)
                            {
                                //Check password
                                if (tblUser.Password == GetHash(user.Password))
                                {
                                    //User could login
                                    user.FirstName = tblUser.FirstName;
                                    user.LastName  = tblUser.LastName;
                                    user.Id        = tblUser.Id;
                                    user.Meals     = MealManager.Load(user.Id);
                                    user.Workouts  = WorkoutManager.Load(user.Id);

                                    return(true);
                                }
                                else
                                {
                                    throw new Exception("Could not login with these credentials");
                                }
                            }
                            else
                            {
                                throw new Exception("UserId could not be found");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Password was not set");
                    }
                }
                else
                {
                    throw new Exception("UserId was not set");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }