public GetCommentRespond Get(string targetUserId, string requestByUserId, string classRoomId) { var noDataRespond = new GetCommentRespond { Comments = Enumerable.Empty <CommentInformation>() }; var areArgumentsValid = !string.IsNullOrEmpty(targetUserId) && !string.IsNullOrEmpty(requestByUserId); if (!areArgumentsValid) { return(noDataRespond); } var userprofile = _userprofileRepo.GetUserProfileById(requestByUserId); if (userprofile == null) { return(noDataRespond); } var isTeacher = (bool)userprofile.Subscriptions?.Any(it => !it.DeletedDate.HasValue && it.ClassRoomId == classRoomId && it.Role == UserProfile.AccountRole.Teacher); var isSelftRequest = requestByUserId == targetUserId; var isFriend = false; if (!isSelftRequest && !isTeacher) { var friends = _friendRequestRepo.GetFriendRequestByUserProfileId(requestByUserId); if (friends == null || !friends.Any()) { return(noDataRespond); } isFriend = friends .Where(it => !it.DeletedDate.HasValue) .Where(it => it.Status == FriendRequest.RelationStatus.Friend) .Any(it => it.ToUserProfileId.Equals(targetUserId)); if (!isFriend && !isSelftRequest) { var targetUserProfile = _userprofileRepo.GetUserProfileById(targetUserId); if (requestByUserId == null) { return(noDataRespond); } if (targetUserProfile.IsPrivateAccount) { return new GetCommentRespond { IsPrivateAccount = true } } ; } } var userComments = _commentRepo.GetCommentsByUserProfileId(targetUserId, classRoomId).ToList(); if (userComments == null || !userComments.Any()) { return(noDataRespond); } var selectedClassRoom = _classRoomRepo.GetClassRoomById(classRoomId); if (selectedClassRoom == null) { return(noDataRespond); } var lessonIds = userComments.Select(it => it.LessonId).Distinct(); var lessonQry = from it in selectedClassRoom.Lessons where it != null where lessonIds.Contains(it.id) select new { LessonId = it.id, LessonCatalogId = it.LessonCatalogId, LessonCatalog = _lessonCatalogRepo.GetLessonCatalogById(it.LessonCatalogId) }; var lessons = lessonQry.Where(it => it.LessonCatalog != null).ToList(); var comments = userComments .Where(it => lessons.Any(lesson => lesson.LessonId == it.LessonId)) .Select(it => { var selectedLessonCatalog = lessons.FirstOrDefault(l => l.LessonId == it.LessonId); if (selectedLessonCatalog == null) { return(null); } var order = 1; return(new CommentInformation { ClassRoomId = it.ClassRoomId, CreatedByUserProfileId = it.CreatedByUserProfileId, CreatedDate = it.CreatedDate, CreatorDisplayName = it.CreatorDisplayName, CreatorImageUrl = it.CreatorImageUrl, Description = it.Description, id = it.id, Order = order++, LessonId = it.LessonId, LessonWeek = selectedLessonCatalog.LessonCatalog.Order, TotalDiscussions = it.Discussions.Where(dc => !dc.DeletedDate.HasValue).Count(), TotalLikes = it.TotalLikes }); }) .Where(it => it != null) .OrderByDescending(it => it.LessonWeek) .ThenByDescending(it => it.CreatedDate) .ToList(); var canCreateADiscussion = isFriend || isSelftRequest || isTeacher; return(new GetCommentRespond { IsDiscussionAvailable = canCreateADiscussion, Comments = comments }); } #endregion Methods //// GET: api/journal //public IEnumerable<string> Get() //{ // return new string[] { "value1", "value2" }; //} //// GET: api/journal/5 //public string Get(int id) //{ // return "value"; //} //// POST: api/journal //public void Post([FromBody]string value) //{ //} //// PUT: api/journal/5 //public void Put(int id, [FromBody]string value) //{ //} //// DELETE: api/journal/5 //public void Delete(int id) //{ //} }
public GetCommentRespond Comments(string id, string classRoomId, string userId) { var areArgumentsValid = !string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(classRoomId) && !string.IsNullOrEmpty(userId); if (!areArgumentsValid) { return(null); } UserProfile userprofile; var canAccessToTheClassRoom = _userprofileRepo.CheckAccessPermissionToSelectedClassRoom(userId, classRoomId, out userprofile); if (!canAccessToTheClassRoom) { return(null); } var userSubscription = userprofile.Subscriptions .Where(it => !it.DeletedDate.HasValue) .FirstOrDefault(it => it.ClassRoomId == classRoomId); if (userSubscription == null) { return(null); } var isTeacher = userSubscription.Role == UserProfile.AccountRole.Teacher; var now = _dateTime.GetCurrentTime(); var canAccessToTheClassLesson = isTeacher || _classCalendarRepo.CheckAccessPermissionToSelectedClassLesson(classRoomId, id, now); if (!canAccessToTheClassLesson) { return(null); } var allUsersInCourse = _userprofileRepo.GetUserProfilesByClassRoomId(classRoomId) .Where(it => !it.DeletedDate.HasValue) .ToList(); var filterByCreatorNames = Enumerable.Empty <string>(); if (isTeacher) { filterByCreatorNames = allUsersInCourse.Select(it => it.id).ToList(); } else { var friendRequests = _friendRequestRepo.GetFriendRequestByUserProfileId(userId); if (friendRequests == null) { return(null); } var teacherIds = allUsersInCourse .Where(it => it.Subscriptions.Any(s => s.ClassRoomId == classRoomId && s.Role == UserProfile.AccountRole.Teacher)) .Select(it => it.id); var friendIds = friendRequests .Where(it => !it.DeletedDate.HasValue) .Where(it => it.Status == FriendRequest.RelationStatus.Friend) .Select(it => it.ToUserProfileId); filterByCreatorNames = friendIds.Union(new string[] { userId }).Union(teacherIds).Distinct(); } var order = 1; var comments = _commentRepo.GetCommentsByLessonId(id, filterByCreatorNames) .Where(it => !it.DeletedDate.HasValue) .OrderByDescending(it => it.CreatedDate) .Select(it => new CommentInformation { id = it.id, Order = order++, Description = it.Description, TotalLikes = it.TotalLikes, CreatorImageUrl = it.CreatorImageUrl, CreatorDisplayName = it.CreatorDisplayName, ClassRoomId = it.ClassRoomId, LessonId = it.LessonId, CreatedByUserProfileId = it.CreatedByUserProfileId, TotalDiscussions = (it.Discussions ?? Enumerable.Empty <Comment.Discussion>()) .Where(dit => !dit.DeletedDate.HasValue).Count(), CreatedDate = it.CreatedDate }).ToList(); var result = new GetCommentRespond { Comments = comments, IsDiscussionAvailable = true, }; return(result); }