public Class CreateClass(User aCreatedByUser, string aUniversityId, string aSubject, string aClassCode, string aClassTitle)
 {
     Class myClass = Class.CreateClass(0, aUniversityId, aCreatedByUser.Id, aSubject, aClassCode, aClassTitle, DateTime.UtcNow);
     theEntities.AddToClasses(myClass);
     theEntities.SaveChanges();
     return myClass;
 }
 public IEnumerable<SendItem> GetSendItemsForUser(User aUser)
 {
     return (from s in theEntities.SendItems
             where s.ToUserId == aUser.Id
             && !s.Seen
             select s).OrderByDescending(s => s.DateTimeStamp).ToList<SendItem>();
 }
 private SendItem GetSendItem(User aUser, int anItemId)
 {
     return (from i in theEntities.SendItems
             where i.Id == anItemId
             && i.ToUserId == aUser.Id
             select i).FirstOrDefault<SendItem>();
 }
 public void DeleteUser(User userToDelete)
 {
     theEntities = new UniversityOfMeEntities();
     User originalUser = GetUser(userToDelete.Id);
     theEntities.DeleteObject(originalUser);
     theEntities.SaveChanges();
 }
 public void MarkItemAsSeen(User aUser, int anItemId)
 {
     SendItem mySendItem = GetSendItem(aUser, anItemId);
     mySendItem.Seen = true;
     theEntities.ApplyCurrentValues(mySendItem.EntityKey.EntitySetName, mySendItem);
     theEntities.SaveChanges();
 }
 public UserBadge GetLatestBadgeEarnedAndNotSeen(User aUser)
 {
     return (from ub in theEntities.UserBadges
             where ub.UserId == aUser.Id
             && !ub.Seen
             select ub).FirstOrDefault<UserBadge>();
 }
 public IEnumerable<Badge> GetBadgesForUser(User aUser)
 {
     return (from ub in theEntities.UserBadges
             where ub.UserId == aUser.Id
             && ub.Received
             select ub.Badge);
 }
 public int GetUnviewedBoardCount(User aRequestingUser)
 {
     return (from v in theEntities.BoardViewedStates
             where v.UserId == aRequestingUser.Id
             && v.Viewed == false
             select v).Count<BoardViewedState>();
 }
 public int GetPendingFriendRequestCount(User aRequestingUser)
 {
     return (from f in theEntities.Friends
             where f.SourceUserId == aRequestingUser.Id
             && f.Approved == false
             select f).Count<Friend>();
 }
        public static string GetMainUniversityId(User aUser)
        {
            foreach (UserUniversity myUserUniversity in aUser.UserUniversities) {
                return myUserUniversity.UniversityEmail.UniversityId;
            }

            return null;
        }
        public UserRole AddUserToRole(User aUser, Role aRole)
        {
            UserRole myRole = UserRole.CreateUserRole(0, aUser.Id, aRole.Id);
            theEntities.AddToUserRoles(myRole);
            theEntities.SaveChanges();

            return myRole;
        }
 public bool AddCommentToPhoto(User aCommentingUser, int aPhotoId, string aComment)
 {
     if (!IsValidComment(aComment)) {
         return false;
     }
     thePhotoRepo.AddPhotoComment(aCommentingUser, aPhotoId, aComment);
     return true;
 }
 public int GetUnreadMessagesReceivedCount(User aRequestingUser)
 {
     return (from m in theEntities.Messages
             where m.ToUserId == aRequestingUser.Id
             && m.ToViewed == false
             && !m.ToDeleted
             select m).Count<Message>();
 }
        public static IEnumerable<string> GetUniversityAffiliations(User aUser)
        {
            List<string> myUniversityAffiliations = new List<string>();
            foreach (UserUniversity myUserUniversity in aUser.UserUniversities) {
                myUniversityAffiliations.Add(myUserUniversity.UniversityEmail.UniversityId);
            }

            return myUniversityAffiliations;
        }
 public int GetUnreadMessagesSentCount(User aRequestingUser)
 {
     return (from m in theEntities.Messages
             where m.FromUserId == aRequestingUser.Id
             && m.FromViewed == false
             && m.FromDeleted == false
             && m.RepliedTo == true
             select m).Count<Message>();
 }
 public IEnumerable<BoardViewedState> GetBoardViewedStates(User aUser)
 {
     return (from bvs in theEntities.BoardViewedStates
             join b in theEntities.Boards on bvs.BoardId equals b.Id
             where bvs.UserId == aUser.Id
             && !bvs.Viewed
             && !b.Deleted
             select bvs);
 }
        public void SendItemToUser(int aToUserId, User aFromUser, int anItemEnumeration)
        {
            SendItem mySendItem = SendItem.CreateSendItem(0, aToUserId, aFromUser.Id, anItemEnumeration, DateTime.UtcNow, false);
            theEntities.AddToSendItems(mySendItem);
            theEntities.SaveChanges();

            BadgeHelper.AddNecessaryBadgesAndPoints(theEntities, aFromUser.Id, BadgeAction.BEER_SENT, BadgeSection.ITEM, mySendItem.Id);
            theEntities.SaveChanges();
        }
 public MarketplaceItem AddItemToMarketplace(string aUniversityId, User aUser, string anItemType, string aTitle, string aDescription, double aPrice, string anImageName, DateTime anExpireListing)
 {
     MarketplaceItem myItemSelling = MarketplaceItem.CreateMarketplaceItem(0, aUniversityId, aUser.Id, anItemType, aTitle, aDescription, aPrice, DateTime.UtcNow, anExpireListing, false, false);
     if (!string.IsNullOrEmpty(anImageName)) {
         myItemSelling.ImageName = anImageName;
     }
     theEntities.AddToMarketplaceItems(myItemSelling);
     theEntities.SaveChanges();
     return myItemSelling;
 }
 public UserBadge GetLatestUnseenBadgeForUser(User aUser)
 {
     return (from ub in theEntities.UserBadges
             where ub.UserId == aUser.Id
             && ub.Received
             && !ub.Seen
             select ub)
             .OrderByDescending(ub => ub.DateTimeStamp)
             .FirstOrDefault<UserBadge>();
 }
        public static bool PrivacyAllows(User aUser, SocialPrivacySetting aPrivacySetting)
        {
            foreach (UserPrivacySetting myUserPrivacySetting in aUser.UserPrivacySettings) {
                if (myUserPrivacySetting.PrivacySettingName.Equals(aPrivacySetting.ToString())) {
                    return true;
                }

            }

            return false;
        }
        public static string NewMessageBody(User aUserSending)
        {
            var myLink = new TagBuilder("a");
            myLink.MergeAttribute("href", "http://www.universityof.me");
            myLink.InnerHtml = "UniversityOf.Me";

            string myBody = "Hey! <br /><br /> " + NameHelper.FullName(aUserSending) + " has sent you a private message. Login to "
                + myLink.ToString() + " to read it. <br /><br />-UniversityOf.Me team";

            return myBody;
        }
        public static string NewBoardBody(User aPostedByUser, User aSourceUser)
        {
            var myLink = new TagBuilder("a");
            myLink.MergeAttribute("href", LinkIt(URLHelper.ProfileUrl(aSourceUser)));
            myLink.InnerHtml = "Click here to go to your profile page";

            string myBody = "Hey! <br /><br /> " + NameHelper.FullName(aPostedByUser) + " has posted to your board. "
                + myLink.ToString() + ". Or copy and paste this URL into your browser: " + LinkIt(URLHelper.ProfileUrl(aSourceUser))
                + "<br /><br />-UniversityOf.Me team";

            return myBody;
        }
        public void MarkUserBadgeAsSeen(User aUser, int aUserBadgeId)
        {
            UserBadge myUserBadge = (from ub in theEntities.UserBadges
                         where ub.Id == aUserBadgeId &&
                         ub.UserId == aUser.Id
                         select ub).FirstOrDefault<UserBadge>();

            if(myUserBadge != null) {
                myUserBadge.Seen = true;
                theEntities.ApplyCurrentValues(myUserBadge.EntityKey.EntitySetName, myUserBadge);
                theEntities.SaveChanges();
            }
        }
        public static string FriendRequestBody(User aUserAdding)
        {
            var myLink = new TagBuilder("a");
            myLink.MergeAttribute("href", "http://www.universityof.me");
            myLink.InnerHtml = "universityof.me";

            string mySubject = EmailContent.FriendRequestSubject();

            string myBody = "Hey! <br /><br /> " + NameHelper.FullName(aUserAdding) + " has sent you a friend request. Login to "
                + myLink.ToString() + " to accept them. <br /><br />-UniversityOf.Me team";

            return myBody;
        }
        public AbstractUserModel<User> CreateUser(User userToCreate, string aNotConfirmedRoleName)
        {
            theEntities.AddToUsers(userToCreate);
            theEntities.SaveChanges();

            try {
                AddUserToNotConfirmedUserRole(userToCreate, aNotConfirmedRoleName);
            } catch (NullRoleException exception) {
                DeleteUser(userToCreate);
                throw new NullRoleException("Deleted user because there was no \"Not confirmed\" role assigned properly.", exception);
            }

            return SocialUserModel.Create(userToCreate);
        }
        public static University GetMainUniversity(User aUser)
        {
            foreach (UserUniversity myUserUniversity in aUser.UserUniversities) {
                try {
                    return myUserUniversity.UniversityEmail.University;
                } catch (Exception) {
                    string myUniversityId = myUserUniversity.UniversityEmail.UniversityId;
                    IUniversityService myUniService = new UniversityService(null);
                    return myUniService.GetUniversityById(myUniversityId);
                }
            }

            return null;
        }
        public static bool IsFriend(User aSourceUser, User aTargetUserCheckingToSeeIfFriend)
        {
            bool myIsFriend = true;

            if (aSourceUser.Id != aTargetUserCheckingToSeeIfFriend.Id) {
                IEnumerable<int> myFriendIds =
                    (from f in aSourceUser.Friends.ToList<Friend>()
                     select f.SourceUserId).ToList<int>(); ;

                myIsFriend = myFriendIds.Contains(aTargetUserCheckingToSeeIfFriend.Id);
            }

            return myIsFriend;
        }
        public static bool IsFeatureEnabled(User aUser, Features aFeature)
        {
            bool myIsEnabled = true;

            foreach (FeaturesEnabled myFeaturesEnabled in aUser.FeaturesEnableds) {
                if(myFeaturesEnabled.FeatureName.Equals(aFeature.ToString())) {
                    if(!myFeaturesEnabled.Enabled) {
                        myIsEnabled = false;
                        break;
                    }
                }
            }

            return myIsEnabled;
        }
        public static bool IsFromUniversity(User aFrom, string aUniversityId)
        {
            if (aUniversityId == null) {
                return false;
            }

            bool myIsFromUniversity = false;
            foreach (UserUniversity myUserUniversity in aFrom.UserUniversities) {
                if (myUserUniversity.UniversityEmail.UniversityId.ToUpper().Equals(aUniversityId.ToUpper())) {
                    myIsFromUniversity = true;
                    break;
                }
            }
            return myIsFromUniversity;
        }
 public void CreateTextbook(User aCreatingUser, string aUniversityId, string aTextBookCondition, string aBookTitle, string aBookAuthor, 
                            string aBookImage, string aClassSubject, string aClassCourse, int anEdition, double anAskingPrice, string aDetails, string anISBN)
 {
     TextBook myTextBook = TextBook.CreateTextBook(0, aCreatingUser.Id, aUniversityId, aTextBookCondition, aBookTitle, aBookAuthor,
         anEdition, anAskingPrice, DateTime.UtcNow, true);
     myTextBook.BookPicture = aBookImage;
     myTextBook.Details = aDetails;
     myTextBook.ISBN = anISBN;
     if (!string.IsNullOrEmpty(aClassSubject)) {
         myTextBook.ClassSubject = aClassSubject;
     }
     if (!string.IsNullOrEmpty(aClassCourse)) {
         myTextBook.ClassCourse = aClassCourse;
     }
     theEntities.AddToTextBooks(myTextBook);
     theEntities.SaveChanges();
 }