/// <summary> /// เพิ่มหรืออัพเดทข้อมูล comment /// </summary> /// <param name="data">ข้อมูล comment ที่จะดำเนินการ</param> public void UpsertComment(Comment data) { var update = Builders<Comment>.Update .Set(it => it.Description, data.Description) .Set(it => it.TotalLikes, data.TotalLikes) .Set(it => it.CreatorImageUrl, data.CreatorImageUrl) .Set(it => it.CreatorDisplayName, data.CreatorDisplayName) .Set(it => it.ClassRoomId, data.ClassRoomId) .Set(it => it.LessonId, data.LessonId) .Set(it => it.CreatedByUserProfileId, data.CreatedByUserProfileId) .Set(it => it.LastNotifyRequest, data.LastNotifyRequest) .Set(it => it.LastNotifyComplete, data.LastNotifyComplete) .Set(it => it.CreatedDate, data.CreatedDate) .Set(it => it.DeletedDate, data.DeletedDate) .Set(it => it.Discussions, data.Discussions); var updateOption = new UpdateOptions { IsUpsert = true }; _mongoUtil.GetCollection<Comment>(TableName) .UpdateOne(it => it.id == data.id, update, updateOption); }
public PostNewCommentRespond Post(PostNewCommentRequest body) { var areArgumentsValid = body != null && !string.IsNullOrEmpty(body.ClassRoomId) && !string.IsNullOrEmpty(body.Description) && !string.IsNullOrEmpty(body.LessonId) && !string.IsNullOrEmpty(body.UserProfileId); if (!areArgumentsValid) return null; UserProfile userprofile; var canAccessToTheClassRoom = _userprofileRepo.CheckAccessPermissionToSelectedClassRoom(body.UserProfileId, body.ClassRoomId, out userprofile); if (!canAccessToTheClassRoom) return null; var isTeacher = userprofile.Subscriptions.Any(it => !it.DeletedDate.HasValue && it.ClassRoomId == body.ClassRoomId && it.Role == UserProfile.AccountRole.Teacher); var now = _dateTime.GetCurrentTime(); var canAccessToTheClassLesson = _classCalendarRepo.CheckAccessPermissionToSelectedClassLesson(body.ClassRoomId, body.LessonId, now) || isTeacher; if (!canAccessToTheClassLesson) return null; var selectedUserActivity = _userActivityRepo.GetUserActivityByUserProfileIdAndClassRoomId(body.UserProfileId, body.ClassRoomId); if (selectedUserActivity == null) return null; var selectedLesson = selectedUserActivity.LessonActivities.FirstOrDefault(it => it.LessonId == body.LessonId); if (selectedLesson == null) return null; var id = Guid.NewGuid().ToString(); var newComment = new Comment { id = id, ClassRoomId = body.ClassRoomId, CreatedByUserProfileId = body.UserProfileId, Description = body.Description, LessonId = body.LessonId, CreatedDate = now, CreatorDisplayName = userprofile.Name, CreatorImageUrl = userprofile.ImageProfileUrl, Discussions = Enumerable.Empty<Comment.Discussion>(), }; _commentRepo.UpsertComment(newComment); selectedLesson.CreatedCommentAmount++; _userActivityRepo.UpsertUserActivity(selectedUserActivity); _notificationCtrl.SendNotification(); return new PostNewCommentRespond { ActualCommentId = id }; }