public static int GetTotalUnreadMessages(string userId) { using (var context = new BackofficeUnitOfWork()) { List <long> chatIds = context.Chat .Fetch() .Include(i => i.ChatMember) .Where(i => i.ChatMember.Select(j => j.AspNetUsers.Id).ToList().Contains(userId)) .Select(i => i.ID) .ToList(); return(context.ChatMessage .Fetch() .Include(i => i.Chat) .Where(i => null == i.ReadDate && i.ID_User != userId && chatIds.Contains(i.ID_Chat)) .Select(i => i.ID_Chat) .Distinct() .Count()); } }
public static Chat GetChat(BackofficeUnitOfWork context, string userId, string userId2) { //using (var context = new BackofficeUnitOfWork()) //{ Chat chat = context.Chat .Fetch() .Include(i => i.AspNetUsers) .Include(i => i.AspNetUsers.Profile) .Include(i => i.ChatMember) .Include(i => i.ChatMessage) .Include(i => i.ChatNotification) .Include(i => i.ChatNote) //.Include(i => i.ChatMessage.OrderByDescending(j => j.CreateDate).Take(20)) .Where(i => ( (i.AspNetUsers.Id == userId && i.ChatMember.Where(j => j.AspNetUsers.Id == userId2).Count() > 0) || (i.AspNetUsers.Id == userId2 && i.ChatMember.Where(j => j.AspNetUsers.Id == userId).Count() > 0) ) && i.Active) //.Select(i => i) .OrderByDescending(i => i.LastChangeDate) .FirstOrDefault(); if (null != chat && null != chat.ChatMessage) { ChatDelete chatDelete = context.ChatDelete.Fetch().Where(i => i.ID_Chat == chat.ID && i.ID_User == userId).FirstOrDefault(); if (null == chatDelete) { chat.ChatMessage = chat.ChatMessage.OrderByDescending(j => j.CreateDate).Take(20).OrderBy(j => j.CreateDate).ToList(); } else { chat.ChatMessage = chat.ChatMessage.Where(i => i.CreateDate > chatDelete.LastChatDeleteDate).OrderByDescending(j => j.CreateDate).Take(20).OrderBy(j => j.CreateDate).ToList(); } } return(chat); //} }
public static bool AreFriends(string userId, string otherUserId) { using (var context = new BackofficeUnitOfWork()) { long requestAcceptedStatusId = context.FriendStatus.Fetch().FirstOrDefault(j => j.Token == "REQUEST_ACCEPTED").ID; Friend friendItem = context.Friend .Fetch() .FirstOrDefault(i => (i.AspNetUsers.Id == userId && i.AspNetUsers1.Id == otherUserId || i.AspNetUsers1.Id == userId && i.AspNetUsers.Id == otherUserId) && (i.ID_FriendStatus == requestAcceptedStatusId) ); if (null == friendItem) { return(false); } return(true); } }
public static List <Profile> GetUserPendingFriendRequests(string currentUserId) { using (var context = new BackofficeUnitOfWork()) { List <string> friends1Ids = context.Friend.Fetch().Where(j => j.FriendStatus.Token == FriendStatusEnum.REQUEST_SENT.ToString() && j.AspNetUsers1.Id == currentUserId).Select(j => j.AspNetUsers.Id).ToList(); //List<string> friends2Ids = context.Friend.Fetch().Where(j => j.FriendStatus.Token == FriendStatusEnum.REQUEST_ACCEPTED.ToString() && j.AspNetUsers.Id == currentUserId).Select(j => j.AspNetUsers1.Id).ToList(); //friends1Ids = friends1Ids.Concat(friends2Ids).ToList(); List <Profile> friends = context .Profile .Fetch() .Include(i => i.AspNetUsers) .Include(i => i.ProfileSettings) .Include(i => i.Parish) .Include(i => i.County) .Include(i => i.District) .Where(i => friends1Ids.Contains(i.AspNetUsers.Id)) .Select(i => i) .ToList(); return(friends); } }
public static bool LikePost(long postId, string userId) { using (var context = new BackofficeUnitOfWork()) { if (context.PostLike.Fetch().Count(i => i.ID_Post == postId && i.ID_User == userId) > 0) { return(false); } PostLike like = new PostLike() { Active = true, AspNetUsers = context.AspNetUsers.Get(userId), Date = DateTime.Now, Post = context.Post.Get(postId) }; context.PostLike.Create(like); context.Save(); return(like.ID > 0); } }
public static Post GetPost(BackofficeUnitOfWork context, long postId) { Post post = context.Post .Fetch() .Include(i => i.AspNetUsers.Profile) .Include(i => i.PostType) .Include(i => i.PostSubject) .Include(i => i.PostLike) .Include(i => i.PostComment) //.Include(i => i.ChatMessage.OrderByDescending(j => j.CreateDate).Take(20)) .Include(i => i.PostImage) .Where(i => i.ID == postId) .FirstOrDefault(); if (null != post) { post.PostComment = post.PostComment.Where(c => c.Active).Select(c => c).OrderBy(i => i.Date).ToList(); post.PostImage = post.PostImage.Where(c => c.Active).Select(c => c).ToList(); post.PostLike = post.PostLike.Where(c => c.Active).Select(c => c).ToList(); } return(post); }
public static bool MarkNotificationAsRead(long id) { using (var context = new BackofficeUnitOfWork()) { //IQueryable<Notification> query = context.Notification Notification item = context.Notification .Fetch() .FirstOrDefault(i => i.ID == id); if (null == item) { return(false); } item.Read = true; item.ReadDate = DateTime.Now; context.Notification.Update(item); context.Save(); return(true); } }
public static List <ConsultantStatisticsDTO> GetConsultantsStatistics() { using (var context = new BackofficeUnitOfWork()) { var garagesStatistics = context .Payment .Fetch() .Include(i => i.Garage) .Include(i => i.Garage.AspNetUsers) .Include(i => i.Garage.AspNetUsers1) .Include(i => i.Garage.AspNetUsers1.Profile) .Include(i => i.PaymentType) .Include(i => i.PaymentStatus) .Where(i => i.ID_Garage.HasValue) .Select(i => new ConsultantStatisticsDTO() { Consultant_ID_User = i.Garage.AspNetUsers1.Id, Consultant_Username = i.Garage.AspNetUsers1.UserName, Consultant_FirstName = i.Garage.AspNetUsers1.Profile.FirstOrDefault().FirstName, Consultant_LastName = i.Garage.AspNetUsers1.Profile.FirstOrDefault().LastName, Company_ID = i.Garage.ID, Company_ID_User = i.Garage.ID_User, Company_Name = i.Garage.Name, Company_BusinessName = i.Garage.BusinessName, Company_Description = i.Garage.Description, Company_NIF = i.Garage.NIF, Company_ContactEmail = i.Garage.ContactEmail, Company_CompanyType = CompanyTypeEnum.GARAGE.ToString(), Company_CreationDate = i.Garage.CreateDate, Payment_ID = i.ID, Payment_LiquidValue = i.LiquidValue, Payment_TaxValue = i.TaxValue, Payment_TotalValue = i.TotalValue, Payment_Type = i.PaymentType.Token.ToString(), Payment_Status = i.PaymentStatus.Token.ToString(), Payment_CreationDate = i.CreateDate, Payment_LastUpdateDate = i.LastChangeDate, Payment_PaymentDate = i.PaymentDate }) .ToList(); var medicalClinicStatistics = context .Payment .Fetch() .Include(i => i.MedicalClinic) .Include(i => i.MedicalClinic.AspNetUsers) .Include(i => i.MedicalClinic.AspNetUsers1) .Include(i => i.MedicalClinic.AspNetUsers1.Profile) .Include(i => i.PaymentType) .Include(i => i.PaymentStatus) .Where(i => i.ID_MedicalClinic.HasValue) .Select(i => new ConsultantStatisticsDTO() { Consultant_ID_User = i.MedicalClinic.AspNetUsers1.Id, Consultant_Username = i.MedicalClinic.AspNetUsers1.UserName, Consultant_FirstName = i.MedicalClinic.AspNetUsers1.Profile.FirstOrDefault().FirstName, Consultant_LastName = i.MedicalClinic.AspNetUsers1.Profile.FirstOrDefault().LastName, Company_ID = i.MedicalClinic.ID, Company_ID_User = i.MedicalClinic.ID_User, Company_Name = i.MedicalClinic.Name, Company_BusinessName = i.MedicalClinic.BusinessName, Company_Description = i.MedicalClinic.Description, Company_NIF = i.MedicalClinic.NIF, Company_ContactEmail = i.MedicalClinic.ContactEmail, Company_CompanyType = CompanyTypeEnum.MEDICAL_CLINIC.ToString(), Company_CreationDate = i.MedicalClinic.CreateDate, Payment_ID = i.ID, Payment_LiquidValue = i.LiquidValue, Payment_TaxValue = i.TaxValue, Payment_TotalValue = i.TotalValue, Payment_Type = i.PaymentType.Token.ToString(), Payment_Status = i.PaymentStatus.Token.ToString(), Payment_CreationDate = i.CreateDate, Payment_LastUpdateDate = i.LastChangeDate, Payment_PaymentDate = i.PaymentDate }) .ToList(); var constructionCompanyStatistics = context .Payment .Fetch() .Include(i => i.ConstructionCompany) .Include(i => i.ConstructionCompany.AspNetUsers) .Include(i => i.ConstructionCompany.AspNetUsers1) .Include(i => i.ConstructionCompany.AspNetUsers1.Profile) .Include(i => i.PaymentType) .Include(i => i.PaymentStatus) .Where(i => i.ID_ConstructionCompany.HasValue) .Select(i => new ConsultantStatisticsDTO() { Consultant_ID_User = i.ConstructionCompany.AspNetUsers1.Id, Consultant_Username = i.ConstructionCompany.AspNetUsers1.UserName, Consultant_FirstName = i.ConstructionCompany.AspNetUsers1.Profile.FirstOrDefault().FirstName, Consultant_LastName = i.ConstructionCompany.AspNetUsers1.Profile.FirstOrDefault().LastName, Company_ID = i.ConstructionCompany.ID, Company_ID_User = i.ConstructionCompany.ID_User, Company_Name = i.ConstructionCompany.Name, Company_BusinessName = i.ConstructionCompany.BusinessName, Company_Description = i.ConstructionCompany.Description, Company_NIF = i.ConstructionCompany.NIF, Company_ContactEmail = i.ConstructionCompany.ContactEmail, Company_CompanyType = CompanyTypeEnum.CONSTRUCTION_COMPANY.ToString(), Company_CreationDate = i.ConstructionCompany.CreateDate, Payment_ID = i.ID, Payment_LiquidValue = i.LiquidValue, Payment_TaxValue = i.TaxValue, Payment_TotalValue = i.TotalValue, Payment_Type = i.PaymentType.Token.ToString(), Payment_Status = i.PaymentStatus.Token.ToString(), Payment_CreationDate = i.CreateDate, Payment_LastUpdateDate = i.LastChangeDate, Payment_PaymentDate = i.PaymentDate }) .ToList(); var homeApplianceRepairStatistics = context .Payment .Fetch() .Include(i => i.HomeApplianceRepair) .Include(i => i.HomeApplianceRepair.AspNetUsers) .Include(i => i.HomeApplianceRepair.AspNetUsers1) .Include(i => i.HomeApplianceRepair.AspNetUsers1.Profile) .Include(i => i.PaymentType) .Include(i => i.PaymentStatus) .Where(i => i.ID_HomeApplianceRepair.HasValue) .Select(i => new ConsultantStatisticsDTO() { Consultant_ID_User = i.HomeApplianceRepair.AspNetUsers1.Id, Consultant_Username = i.HomeApplianceRepair.AspNetUsers1.UserName, Consultant_FirstName = i.HomeApplianceRepair.AspNetUsers1.Profile.FirstOrDefault().FirstName, Consultant_LastName = i.HomeApplianceRepair.AspNetUsers1.Profile.FirstOrDefault().LastName, Company_ID = i.HomeApplianceRepair.ID, Company_ID_User = i.HomeApplianceRepair.ID_User, Company_Name = i.HomeApplianceRepair.Name, Company_BusinessName = i.HomeApplianceRepair.BusinessName, Company_Description = i.HomeApplianceRepair.Description, Company_NIF = i.HomeApplianceRepair.NIF, Company_ContactEmail = i.HomeApplianceRepair.ContactEmail, Company_CompanyType = CompanyTypeEnum.HOME_APPLIANCES_REPAIR.ToString(), Company_CreationDate = i.HomeApplianceRepair.CreateDate, Payment_ID = i.ID, Payment_LiquidValue = i.LiquidValue, Payment_TaxValue = i.TaxValue, Payment_TotalValue = i.TotalValue, Payment_Type = i.PaymentType.Token.ToString(), Payment_Status = i.PaymentStatus.Token.ToString(), Payment_CreationDate = i.CreateDate, Payment_LastUpdateDate = i.LastChangeDate, Payment_PaymentDate = i.PaymentDate }) .ToList(); var insuranceCompanyContactStatistics = context .Payment .Fetch() .Include(i => i.InsuranceCompanyContact) .Include(i => i.InsuranceCompanyContact.AspNetUsers) .Include(i => i.InsuranceCompanyContact.AspNetUsers1) .Include(i => i.InsuranceCompanyContact.AspNetUsers1.Profile) .Include(i => i.PaymentType) .Include(i => i.PaymentStatus) .Where(i => i.ID_InsuranceCompanyContact.HasValue) .Select(i => new ConsultantStatisticsDTO() { Consultant_ID_User = i.InsuranceCompanyContact.AspNetUsers1.Id, Consultant_Username = i.InsuranceCompanyContact.AspNetUsers1.UserName, Consultant_FirstName = i.InsuranceCompanyContact.AspNetUsers1.Profile.FirstOrDefault().FirstName, Consultant_LastName = i.InsuranceCompanyContact.AspNetUsers1.Profile.FirstOrDefault().LastName, Company_ID = i.InsuranceCompanyContact.ID, Company_ID_User = i.InsuranceCompanyContact.ID_User, Company_Name = i.InsuranceCompanyContact.Name, Company_BusinessName = i.InsuranceCompanyContact.BusinessName, Company_Description = i.InsuranceCompanyContact.Description, Company_NIF = i.InsuranceCompanyContact.NIF, Company_ContactEmail = i.InsuranceCompanyContact.ContactEmail, Company_CompanyType = CompanyTypeEnum.INSURANCE_COMPANY_CONTACT.ToString(), Company_CreationDate = i.InsuranceCompanyContact.CreateDate, Payment_ID = i.ID, Payment_LiquidValue = i.LiquidValue, Payment_TaxValue = i.TaxValue, Payment_TotalValue = i.TotalValue, Payment_Type = i.PaymentType.Token.ToString(), Payment_Status = i.PaymentStatus.Token.ToString(), Payment_CreationDate = i.CreateDate, Payment_LastUpdateDate = i.LastChangeDate, Payment_PaymentDate = i.PaymentDate }) .ToList(); return(garagesStatistics.Concat(medicalClinicStatistics).Concat(constructionCompanyStatistics).Concat(homeApplianceRepairStatistics).Concat(insuranceCompanyContactStatistics).ToList()); } }
//public static List<Post> GetUserPosts(string Id) //{ // using (var context = new BackofficeUnitOfWork()) // { // List<long> hiddenPostIds = context.PostHidden // .Fetch() // .Where(i => i.ID_User == Id && i.Hidden) // .Select(i => i.ID_Post) // .ToList(); // return context.Post // .Fetch() // .Include(i => i.AspNetUsers.Profile) // .Include(i => i.PostType) // .Include(i => i.PostSubject) // .Include(i => i.PostLike) // .Include(i => i.PostComment) // .Include(i => i.PostImage) // .Include(i => i.PostHidden) // .Where(i => i.ID_User == Id // && i.Active // && !hiddenPostIds.Contains(i.ID) // && ( // i.PostSubject.Token.Equals("PERSONAL_POST") // || i.PostSubject.Token.Equals("BUSINESS_POST") // || i.PostSubject.Token.Equals("NEWS_POST") // || i.PostSubject.Token.Equals("PARTNERSHIP_POST") // || i.PostSubject.Token.Equals("WALLET_POST") // || i.PostSubject.Token.Equals("SPONSORED_POST") // || i.PostSubject.Token.Equals("GLOBAL_POST") // )) // .OrderByDescending(i => i.Sticky) // .ThenByDescending(i => i.CreateDate) // .ToList(); // } //} public static List <Post> GetUserRelatedPosts(BackofficeUnitOfWork context, string Id, int skipInterval = 0, int itemsCount = 10) { List <long> hiddenPostIds = context.PostHidden .Fetch() .Where(i => i.ID_User == Id && i.Hidden) .Select(i => i.ID_Post) .ToList(); if (string.IsNullOrEmpty(Id)) { List <Post> anonymousPostList = context.Post .Fetch() .Where(i => ( i.PostSubject.Token.Equals("BUSINESS_POST") || i.PostSubject.Token.Equals("NEWS_POST") || i.PostSubject.Token.Equals("PARTNERSHIP_POST") || i.PostSubject.Token.Equals("WALLET_POST") || i.PostSubject.Token.Equals("SPONSORED_POST") || i.PostSubject.Token.Equals("GLOBAL_POST") ) && i.Active && !hiddenPostIds.Contains(i.ID) ) .Include(i => i.Post1) .Include(i => i.AspNetUsers.Profile) .Include(i => i.PostType) .Include(i => i.PostSubject) .Include(i => i.PostLike) .Include(i => i.PostComment) .Include(i => i.PostHidden) .Include(i => i.PostImage) //.Include(i => i.ChatMessage.OrderByDescending(j => j.CreateDate).Take(20)) //.Include(i => i.PostImage) .OrderByDescending(i => i.Sticky) .ThenByDescending(i => i.CreateDate) .Skip(skipInterval) .Take(itemsCount) .ToList(); return(anonymousPostList); } List <string> friendsUserIds = context .Friend .Fetch() .Include(i => i.FriendStatus) .Where(i => i.ID_User == Id && i.FriendStatus.Token == FriendStatusEnum.REQUEST_ACCEPTED.ToString()) .Select(i => i.ID_User_Friend) .ToList(); List <string> auxFriendsUserIds = context .Friend .Fetch() .Include(i => i.FriendStatus) .Where(i => i.ID_User_Friend == Id && i.FriendStatus.Token == FriendStatusEnum.REQUEST_ACCEPTED.ToString()) .Select(i => i.ID_User) .ToList(); friendsUserIds = friendsUserIds.Concat(auxFriendsUserIds).ToList(); List <Post> postList = context.Post .Fetch() .Where(i => ( (i.ID_User == Id && i.PostSubject.Token.Equals("PERSONAL_POST")) || (friendsUserIds.Contains(i.ID_User) && i.PostSubject.Token.Equals("PERSONAL_POST")) || i.PostSubject.Token.Equals("BUSINESS_POST") || i.PostSubject.Token.Equals("NEWS_POST") || i.PostSubject.Token.Equals("PARTNERSHIP_POST") || i.PostSubject.Token.Equals("WALLET_POST") || i.PostSubject.Token.Equals("SPONSORED_POST") || i.PostSubject.Token.Equals("GLOBAL_POST") ) && i.Active && !hiddenPostIds.Contains(i.ID) ) .Include(i => i.Post1) .Include(i => i.AspNetUsers.Profile) .Include(i => i.PostType) .Include(i => i.PostSubject) .Include(i => i.PostLike) .Include(i => i.PostComment) .Include(i => i.PostHidden) .Include(i => i.PostImage) //.Include(i => i.ChatMessage.OrderByDescending(j => j.CreateDate).Take(20)) //.Include(i => i.PostImage) .OrderByDescending(i => i.Sticky) .ThenByDescending(i => i.CreateDate) .Skip(skipInterval) .Take(itemsCount) .ToList(); //foreach (Post post in postList) //{ // post.PostComment = post.PostComment.Where(c => c.Active).Select(c => c).OrderBy(i => i.Date).ToList(); // post.PostImage = post.PostImage.Where(c => c.Active).Select(c => c).ToList(); //} //var cmd = context.DataContext.Database.Connection.CreateCommand(); //cmd.CommandText = "[dbo].[GetAllPosts]"; //try //{ // context.DataContext.Database.Connection.Open(); // // Run the sproc // var reader = cmd.ExecuteReader(); // // Read Blogs from the first result set // var blogs = ((IObjectContextAdapter)context.DataContext) // .ObjectContext // .Translate<Post>(reader, "Posts", MergeOption.AppendOnly); // // Move to second result set and read Posts // //reader.NextResult(); // //var posts = ((IObjectContextAdapter)db) // // .ObjectContext // // .Translate<Post>(reader, "Posts", MergeOption.AppendOnly); //} //finally //{ // context.DataContext.Database.Connection.Close(); //} return(postList); }
public static AspNetRoles GetRole(BackofficeUnitOfWork context, RoleEnum role) { return(context.AspNetRoles.Fetch().FirstOrDefault(i => i.Name.ToUpper() == role.ToString().ToUpper())); }