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 = checkAccessPermissionToSelectedClassRoom(body.UserProfileId, body.ClassRoomId); if (!canAccessToTheClassRoom) return; var now = _dateTime.GetCurrentTime(); var canAccessToTheClassLesson = 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.UpdateClassRoom(selectedClassRoom); }
/// <summary> /// แก้ไขหรือเพิ่มข้อมูล Like lesson /// </summary> /// <param name="update">ข้อมูลที่จะทำการอัพเดทหรือเพิ่ม</param> public void UpsertLikeLesson(LikeLesson update) { throw new NotImplementedException(); }