public static bool PublishMessage(int SubjId, int SubjType, int ObjId, int ObjType, int MsgType)
        {
            // SubjId is Publisher
            List <UserProfile> subscribers = Follow_Logic.GetFollowingUsersOfType(2, SubjId, -1);

            // Send message to shop owner
            if (MsgType == Constant.MESSAGE_TYPE_COMMENT)
            {
                Product pro = db.Products.Find(ObjId);
                if (pro != null && subscribers.Contains(pro.Store.Owner) == false)
                {
                    subscribers.Add(pro.Store.Owner);
                }
            }
            // Send message to follow destination user
            if (MsgType == Constant.MESSAGE_TYPE_FOLLOW)
            {
                if (ObjType == Constant.PRONOUN_TYPE_USER)
                {
                    UserProfile user = db.UserProfiles.Find(ObjId);
                    if (user != null && subscribers.Contains(user) == false)
                    {
                        subscribers.Add(user);
                    }
                }
                else if (ObjType == Constant.PRONOUN_TYPE_STORE)
                {
                    Store store = db.Stores.Find(ObjId);
                    if (store != null && subscribers.Contains(store.Owner) == false)
                    {
                        subscribers.Add(store.Owner);
                    }
                }
            }
            List <MessageRecipient> recipients = new List <MessageRecipient>();
            MessageRecipient        r          = null;

            foreach (var s in subscribers)
            {
                if (s != null)
                {
                    r = new MessageRecipient {
                        RecipientId = s.UserId, CreateDate = DateTime.Now, IsRead = false
                    };
                    recipients.Add(r);
                }
            }
            Message msg = new Message
            {
                SubjectId     = SubjId,
                SubjectTypeId = SubjType,
                ObjectId      = ObjId,
                ObjectTypeId  = ObjType,
                MessageTypeId = MsgType,
                Recipients    = recipients
            };

            try
            {
                db.Messages.Add(msg);
                db.SaveChanges();
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        ///  Get List User Like,Buy Product
        /// </summary>
        /// <param name="type"> 1_Like , 2_Buy</param>
        /// <param name="ID">ID Product</param>
        /// <param name="number">Amount row select</param>
        /// <returns>List <UserProfile></returns>
        public static List <UserProfile> GetListUserProfileRandom(int type, int productId, int number, int userId = 0)
        {
            List <UserProfile> listuser      = new List <UserProfile>();
            List <UserProfile> followedUsers = Follow_Logic.GetFollowedUsers(userId);

            switch (type)
            {
            case 1:
                if (userId <= 0)
                {
                    // Not login user
                    listuser = (from ProductLike pro in db.ProductLikes
                                where pro.ProductId == productId orderby pro.ProductLikeId descending
                                select pro.User).Distinct().Take(number).ToList();
                }
                else
                {
                    // Login user

                    List <UserProfile> likeClickedUsers = (from ProductLike pro in db.ProductLikes
                                                           where pro.ProductId == productId orderby pro.ProductLikeId descending
                                                           select pro.User).Distinct().ToList();
                    listuser = (from f in followedUsers
                                join l in likeClickedUsers on f.UserId equals l.UserId
                                select l).ToList();
                    foreach (var followedUser in followedUsers)
                    {
                        likeClickedUsers.RemoveAll(u => u.UserId == followedUser.UserId);
                    }
                    listuser.AddRange(likeClickedUsers);
                }

                break;

            case 2:
                if (userId <= 0)
                {
                    listuser = (from OrderDetail order in db.OrderDetails
                                where order.ProductId == productId
                                orderby order.OrderId descending
                                select order.Order.Users).Distinct().Take(number).ToList();
                }
                else
                {
                    // Login user
                    List <UserProfile> orderedUsers = (from OrderDetail order in db.OrderDetails
                                                       where order.ProductId == productId
                                                       orderby order.OrderId descending
                                                       select order.Order.Users).Distinct().ToList();
                    listuser = (from f in followedUsers
                                join o in orderedUsers on f.UserId equals o.UserId
                                select o).ToList();
                    foreach (var followedUser in followedUsers)
                    {
                        orderedUsers.RemoveAll(u => u.UserId == followedUser.UserId);
                    }
                    listuser.AddRange(orderedUsers);
                }

                break;

            default:
                break;
            }

            // int count = listuser.Count(); // get count here
            // int index = new Random(number).Next(count);

            // var result = listuser.Skip(index).ToList(); // pick on here

            return(listuser);
        }