Esempio n. 1
0
        private void sendNotifyLikeLessons(DateTime now)
        {
            var requiredNotifyLikeLesson = _likeLessonRepo.GetRequireNotifyLikeLessons().ToList();

            if (!requiredNotifyLikeLesson.Any())
            {
                return;
            }

            var          lessonLikerUserProfileIds = requiredNotifyLikeLesson.Select(it => it.LikedByUserProfileId).Distinct();
            var          friendsRelated            = _friendRequestRepo.GetFriendRequestByUserProfileId(lessonLikerUserProfileIds).ToList(); // HACK: จะต้องส่งให้เฉพาะเพื่อนที่กำลังอยู่ใน course นี้เท่านั้น
            const string Message           = "Like a lesson";
            var          notifyLikeLessons = requiredNotifyLikeLesson.Select(it =>
            {
                var friends = friendsRelated
                              .Where(f => !f.DeletedDate.HasValue)
                              .Where(f => f.FromUserProfileId == it.LikedByUserProfileId && f.Status == FriendRequest.RelationStatus.Friend)
                              .Select(f => new Notification
                {
                    id = Guid.NewGuid().ToString(),
                    ByUserProfileId = new List <string> {
                        it.LikedByUserProfileId
                    },
                    ClassRoomId     = it.ClassRoomId,
                    CreatedDate     = now,
                    LastUpdateDate  = now,
                    LessonId        = it.LessonId,
                    Message         = Message,
                    Tag             = Notification.NotificationTag.FriendLikesALesson,
                    TotalLikes      = 1,
                    ToUserProfileId = f.ToUserProfileId
                });
                return(friends);
            }).Where(it => it.Any()).SelectMany(it => it).ToList();

            _notificationRepo.Insert(notifyLikeLessons);
            requiredNotifyLikeLesson.ForEach(it =>
            {
                it.LastNotifyComplete = now;
                _likeLessonRepo.UpsertLikeLesson(it);
            });
        }
        public void Post(LikeLessonRequest body)
        {
            var isArgumentValid = body != null &&
                                  !string.IsNullOrEmpty(body.ClassRoomId) &&
                                  !string.IsNullOrEmpty(body.LessonId) &&
                                  !string.IsNullOrEmpty(body.UserProfileId);

            if (!isArgumentValid)
            {
                return;
            }

            var canAccessToTheClassRoom = _userprofileRepo.CheckAccessPermissionToSelectedClassRoom(body.UserProfileId, body.ClassRoomId);

            if (!canAccessToTheClassRoom)
            {
                return;
            }

            var now = _dateTime.GetCurrentTime();
            var canAccessToTheClassLesson = _classCalendarRepo.CheckAccessPermissionToSelectedClassLesson(body.ClassRoomId, body.LessonId, now);

            if (!canAccessToTheClassLesson)
            {
                return;
            }

            var selectedClassRoom    = _classRoomRepo.GetClassRoomById(body.ClassRoomId);
            var isLikeConditionValid = selectedClassRoom != null && selectedClassRoom.Lessons.Any(it => it.id == body.LessonId);

            if (!isLikeConditionValid)
            {
                return;
            }

            var likeLessons = _likeLessonRepo.GetLikeLessonsByLessonId(body.LessonId)
                              .Where(it => !it.DeletedDate.HasValue)
                              .ToList();

            if (likeLessons == null)
            {
                return;
            }

            var likedLessonsByThisUser = likeLessons
                                         .Where(it => it.LikedByUserProfileId.Equals(body.UserProfileId, StringComparison.CurrentCultureIgnoreCase));

            var isUnlike = likedLessonsByThisUser.Any();

            if (isUnlike)
            {
                foreach (var item in likedLessonsByThisUser)
                {
                    item.DeletedDate = now;
                    _likeLessonRepo.UpsertLikeLesson(item);
                }
            }
            else
            {
                var selectedUserActivity = _userActivityRepo.GetUserActivityByUserProfileIdAndClassRoomId(body.UserProfileId, body.ClassRoomId);
                if (selectedUserActivity == null)
                {
                    return;
                }
                var selectedLessonActivity = selectedUserActivity.LessonActivities.FirstOrDefault(it => it.LessonId.Equals(body.LessonId));
                if (selectedLessonActivity == null)
                {
                    return;
                }
                selectedLessonActivity.ParticipationAmount++;
                _userActivityRepo.UpsertUserActivity(selectedUserActivity);

                var newLike = new LikeLesson
                {
                    id                   = Guid.NewGuid().ToString(),
                    ClassRoomId          = body.ClassRoomId,
                    LessonId             = body.LessonId,
                    LikedByUserProfileId = body.UserProfileId,
                    CreatedDate          = now
                };
                likeLessons.Add(newLike);
                _likeLessonRepo.UpsertLikeLesson(newLike);
            }

            var selectedLesson = selectedClassRoom.Lessons.First(it => it.id == body.LessonId);

            selectedLesson.TotalLikes = likeLessons.Where(it => !it.DeletedDate.HasValue).Count();
            _classRoomRepo.UpsertClassRoom(selectedClassRoom);
            _notificationCtrl.SendNotification();
        }