Пример #1
0
        public DialogEntryDTO Post(PostDialogRequest request)
        {
            //Check that sender is logged in
            var currentUserId = (Guid)HttpContext.Current.Items["UserId"];

            if (request.SenderUserId != currentUserId)
            {
                throw new Exception("Cannot send message on behalf of other user");
            }

            //Get user and dialogs
            var userRepo   = new UserRepositoryDocumentDB();
            var dialogRepo = new DialogRepositoryDocumentDb();

            var dialogService = new DialogService(dialogRepo);

            var entry  = dialogService.Post(request.SenderUserId, request.ReceiverUserId, request.Message);
            var sender = userRepo.GetUserById(request.SenderUserId);

            return(new DialogEntryDTO
            {
                SenderName = sender.FirstName + " " + sender.LastName,
                TimeStamp = entry.TimeStamp,
                Text = entry.Text,
                SenderImageUrl = sender.ImageUrl
            });
        }
Пример #2
0
        public GetUserDialogsResponse CurrentUserDialogs()
        {
            var dialogRepo = new DialogRepositoryDocumentDb();
            var userRepo   = new UserRepositoryDocumentDB();

            var userA = userRepo.GetUserById((Guid)HttpContext.Current.Items["UserId"]);

            var dialogs = dialogRepo.GetUserReceivedDialogs((Guid)HttpContext.Current.Items["UserId"]).ToList();

            dialogs = dialogs.OrderByDescending(d => d.Entries.OrderByDescending(e => e.TimeStamp).Last().TimeStamp).ToList();

            var orderedDialogEntries = dialogs.Where(d => d.ReceiverId == userA.Id).SelectMany(d => d.Entries)
                                       .OrderBy(e => e.TimeStamp);
            var unread           = orderedDialogEntries.Where(e => !e.IsRead).ToList();
            var lastUnread       = unread.Any() ? unread.Last() : orderedDialogEntries.Any() ? orderedDialogEntries.Last() : null;
            var lastUnreadDialog = lastUnread != null?dialogs.Single(d => d.Entries.Contains(lastUnread)) : null;

            var ret = new GetUserDialogsResponse
            {
                UserDialogs = dialogs.Select(
                    d =>
                {
                    var userB = userRepo.GetUserById(d.OwnerId);
                    return(UserDialogDTOConverter.ConvertToDTO(d, userA, userB));
                }),
                LatestUnread = lastUnreadDialog != null?UserDialogDTOConverter.ConvertToDTO(
                    lastUnread,
                    userA,
                    userRepo.GetUserById(lastUnreadDialog.OwnerId),
                    lastUnreadDialog) : null
            };

            return(ret);
        }
Пример #3
0
        public UserDialogDto InitiateDialog(InitiateDialogRequest request)
        {
            var userRepo   = new UserRepositoryDocumentDB();
            var dialogRepo = new DialogRepositoryDocumentDb();

            var dialogService = new DialogService(dialogRepo);

            var dialogA = dialogService.InitiateDialog(request.UserA, request.UserB, request.UserADescription, request.UserBDescription);

            var userA = userRepo.GetUserById(request.UserA);
            var userB = userRepo.GetUserById(request.UserB);

            return(UserDialogDTOConverter.ConvertToDTO(dialogA, userA, userB));
        }
        public void PostUserVideoReply(Guid userId, string uuid, PostVideoReplyRequest request)
        {
            var userRepo   = new UserRepositoryDocumentDB();
            var dialogRepo = new DialogRepositoryDocumentDb();
            var usr        = userRepo.GetUserById(userId);

            foreach (var admission in usr.CourseAdmissions)
            {
                var excistingScoreCard = admission.Results.OfType <VideoExerciseScoreCard>().Single(r => r.VideoUuid == uuid);

                if (excistingScoreCard.Phase > 3)
                {
                    throw new UnauthorizedAccessException("Review already has reply");
                }

                excistingScoreCard.VideoReply  = request.Reply;
                excistingScoreCard.Phase       = 4;
                excistingScoreCard.IsCompleted = true;
                userRepo.SaveUser(usr);

                //Create reviewer as user

                var userService = new UserService(
                    userRepo, new DialogRepositoryDocumentDb());

                var reviewerUser = userService.CreateUser(
                    Guid.NewGuid().ToString(),
                    excistingScoreCard.ReviewerFirstName,
                    excistingScoreCard.ReviewerLastName,
                    Guid.NewGuid().ToString(),
                    excistingScoreCard.ReviewerEmail,
                    new List <String> {
                    "Friend"
                }, null);

                //Create dialog
                var dialogService = new DialogService(dialogRepo);
                dialogService.InitiateDialog(usr.Id, reviewerUser.Id, "Din ven", "Din ven");

                var entry1 = dialogService.Post(usr.Id, reviewerUser.Id, excistingScoreCard.MesageToReviewer);
                var entry2 = dialogService.Post(reviewerUser.Id, usr.Id, request.Reply);

                return;
            }

            throw new Exception("Scorecard not found");
        }
Пример #5
0
        public void ResetCourse(User user, UserCourseAdmission courseAdmission, int startingModule)
        {
            user.CourseAdmissions.Remove(courseAdmission);

            var course = _coachingCourseRepository.GetCourseById(courseAdmission.CourseId);
            //Find all exercises and get a fresh scorecard
            var modules   = _moduleRepository.GetModulesWithIds(course.Modules.Select(m => m.ModuleReferenceId)).ToList();
            var exercises = _exerciseRepository.GetExercisesWithIds(modules.SelectMany(m => new[] { m.Exercise.ResourceReferenceId, m.Reflection.ResourceReferenceId })).ToList();

            var freshScoreCards = exercises.SelectMany(er => er.Elements.Select(e =>
                                                                                e.Exercise != null ?
                                                                                e.Exercise.GetEmptyScoreCard() :
                                                                                new BaseScoreCard(
                                                                                    new CoachingModuleReference(modules.Single(m =>
                                                                                                                               m.Exercise.ResourceReferenceId == er.Id).Id), Guid.NewGuid(), "")));

            //Add course admission to user
            if (user.CourseAdmissions == null)
            {
                user.CourseAdmissions = new List <UserCourseAdmission>();
            }

            user.CourseAdmissions.Add(new UserCourseAdmission
            {
                CourseId = course.Id,
                Results  = freshScoreCards.ToList()
            });

            for (int i = 0; i < startingModule; i++)
            {
                var module  = modules[i];
                var results = user.CourseAdmissions.Last().Results.Where(r => r.Module.ModuleReferenceId == module.Id);

                foreach (var result in results)
                {
                    result.IsCompleted = true;
                }
            }

            //Remove dialogs for this user
            var dialogRepo    = new DialogRepositoryDocumentDb();
            var dialogService = new DialogService(dialogRepo);

            var dialogs = dialogService.GetUserDialogs(user.Id);

            foreach (var dialog in dialogs)
            {
                //Get the opposit dialog
                var oppositeDialog = dialogService.GetOpositDialog(dialog);

                if (dialog.OwnerId == user.CoachId)
                {
                    //Delete all except the first
                    dialog.Entries = new List <UserDialogEntry> {
                        dialog.Entries.OrderBy(d => d.TimeStamp).First()
                    };
                    oppositeDialog.Entries = new List <UserDialogEntry> {
                        oppositeDialog.Entries.OrderBy(d => d.TimeStamp).First()
                    };

                    dialogRepo.SaveDialog(dialog);
                    dialogRepo.SaveDialog(oppositeDialog);
                }
                else
                {
                    dialogRepo.DeleteDialog(dialog);
                    dialogRepo.DeleteDialog(oppositeDialog);
                }
            }
            //Save user
            _userRepository.SaveUser(user);
        }