public OperationResultVo <Guid> Save(Guid currentUserId, LocalizationViewModel viewModel)
        {
            int pointsEarned = 0;

            try
            {
                Localization model;

                Localization existing = translationDomainService.GetById(viewModel.Id);
                if (existing != null)
                {
                    model = mapper.Map(viewModel, existing);
                }
                else
                {
                    model = mapper.Map <Localization>(viewModel);
                }

                foreach (LocalizationTerm term in model.Terms)
                {
                    if (term.UserId == Guid.Empty)
                    {
                        term.UserId = currentUserId;
                    }
                }

                if (viewModel.Id == Guid.Empty)
                {
                    translationDomainService.Add(model);
                    viewModel.Id = model.Id;

                    pointsEarned += gamificationDomainService.ProcessAction(currentUserId, PlatformAction.LocalizationRequest);
                }
                else
                {
                    translationDomainService.Update(model);
                }

                unitOfWork.Commit();

                viewModel.Id = model.Id;

                return(new OperationResultVo <Guid>(model.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
Esempio n. 2
0
        public async Task <OperationResultVo <Guid> > SaveCourse(Guid currentUserId, CourseViewModel vm)
        {
            int pointsEarned = 0;

            try
            {
                StudyCourse model;

                StudyCourse existing = await mediator.Query <GetCourseByIdQuery, StudyCourse>(new GetCourseByIdQuery(vm.Id));

                if (existing != null)
                {
                    model = mapper.Map(vm, existing);
                }
                else
                {
                    model = mapper.Map <StudyCourse>(vm);
                }

                CommandResult result = await mediator.SendCommand(new SaveCourseCommand(model));

                if (model.Id == Guid.Empty && result.Validation.IsValid)
                {
                    pointsEarned += gamificationDomainService.ProcessAction(currentUserId, PlatformAction.CourseAdd);
                }

                return(new OperationResultVo <Guid>(model.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
Esempio n. 3
0
        public OperationResultVo <Guid> Save(Guid currentUserId, TeamViewModel viewModel)
        {
            int pointsEarned = 0;

            try
            {
                Team model;

                Team existing = teamDomainService.GetById(viewModel.Id);
                if (existing != null)
                {
                    model = mapper.Map(viewModel, existing);
                }
                else
                {
                    model = mapper.Map <Team>(viewModel);
                }

                if (viewModel.Id == Guid.Empty)
                {
                    teamDomainService.Add(model);
                    viewModel.Id = model.Id;

                    pointsEarned += gamificationDomainService.ProcessAction(viewModel.UserId, PlatformAction.TeamAdd);
                }
                else
                {
                    teamDomainService.Update(model);
                }

                unitOfWork.Commit();

                viewModel.Id = model.Id;

                return(new OperationResultVo <Guid>(model.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
Esempio n. 4
0
        public OperationResultVo PollVote(Guid currentUserId, Guid pollOptionId)
        {
            try
            {
                int  pointsEarned = 0;
                Poll poll         = pollDomainService.GetPollByOptionId(pollOptionId);

                if (poll == null)
                {
                    return(new OperationResultVo("Unable to identify the poll you are voting for."));
                }

                bool alreadyVoted = poll.Votes.Any(x => x.PollOptionId == pollOptionId && x.UserId == currentUserId);

                if (alreadyVoted)
                {
                    return(new OperationResultVo("You already voted on this option."));
                }

                IEnumerable <PollVote> userVotesOnThisPoll = poll.Votes.Where(x => x.UserId == currentUserId);

                if (poll.MultipleChoice || !userVotesOnThisPoll.Any())
                {
                    pollDomainService.AddVote(currentUserId, poll.Id, pollOptionId);
                }
                else
                {
                    PollVote oldVote = userVotesOnThisPoll.FirstOrDefault();
                    if (oldVote != null)
                    {
                        pollDomainService.ReplaceVote(currentUserId, poll.Id, oldVote.PollOptionId, pollOptionId);
                    }
                }

                if (!userVotesOnThisPoll.Any())
                {
                    pointsEarned = gamificationDomainService.ProcessAction(currentUserId, PlatformAction.PollVote);
                }

                unitOfWork.Commit();

                PollResultsViewModel resultVm = CalculateNewResult(poll);

                return(new OperationResultVo <PollResultsViewModel>(resultVm, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo(ex.Message));
            }
        }
Esempio n. 5
0
        public OperationResultVo <Guid> SaveGiveaway(Guid currentUserId, GiveawayViewModel vm)
        {
            int pointsEarned = 0;

            try
            {
                Giveaway model;

                Giveaway existing = giveawayDomainService.GetById(vm.Id);
                if (existing != null)
                {
                    model = mapper.Map(vm, existing);
                }
                else
                {
                    model = mapper.Map <Giveaway>(vm);
                }

                FormatImagesToSave(model);

                if (vm.Id == Guid.Empty)
                {
                    giveawayDomainService.Add(model);
                    vm.Id = model.Id;

                    pointsEarned += gamificationDomainService.ProcessAction(currentUserId, PlatformAction.GiveawayAdd);
                }
                else
                {
                    giveawayDomainService.Update(model);
                }

                unitOfWork.Commit();

                vm.Id = model.Id;

                return(new OperationResultVo <Guid>(model.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
Esempio n. 6
0
        public OperationResultVo <Guid> SaveCourse(Guid currentUserId, CourseViewModel vm)
        {
            int pointsEarned = 0;

            try
            {
                StudyCourse model;

                StudyCourse existing = studyDomainService.GetCourseById(vm.Id);
                if (existing != null)
                {
                    model = mapper.Map(vm, existing);
                }
                else
                {
                    model = mapper.Map <StudyCourse>(vm);
                }

                if (vm.Id == Guid.Empty)
                {
                    studyDomainService.AddCourse(model);
                    vm.Id = model.Id;

                    pointsEarned += gamificationDomainService.ProcessAction(currentUserId, PlatformAction.CourseAddition);
                }
                else
                {
                    studyDomainService.UpdateCourse(model);
                }

                unitOfWork.Commit();

                vm.Id = model.Id;

                return(new OperationResultVo <Guid>(model.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
Esempio n. 7
0
        public OperationResultVo <Guid> Save(Guid currentUserId, BrainstormIdeaViewModel viewModel)
        {
            try
            {
                BrainstormSession session = brainstormDomainService.GetById(viewModel.SessionId);

                BrainstormIdea model;

                BrainstormIdea existing = brainstormDomainService.GetIdea(viewModel.Id);
                if (existing != null)
                {
                    model = mapper.Map(viewModel, existing);
                }
                else
                {
                    model = mapper.Map <BrainstormIdea>(viewModel);
                }

                model.SessionId = session.Id;

                if (model.Id == Guid.Empty)
                {
                    brainstormDomainService.AddIdea(model);
                }
                else
                {
                    brainstormDomainService.UpdateIdea(model);
                }

                gamificationDomainService.ProcessAction(viewModel.UserId, PlatformAction.IdeaSuggested);

                unitOfWork.Commit();

                return(new OperationResultVo <Guid>(model.Id));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
Esempio n. 8
0
        public OperationResultVo <Guid> Save(Guid currentUserId, GameViewModel viewModel)
        {
            int pointsEarned = 0;

            try
            {
                Game game;
                Team newTeam    = null;
                bool createTeam = viewModel.TeamId == Guid.Empty;

                ISpecification <GameViewModel> spec = new UserMustBeAuthenticatedSpecification <GameViewModel>(currentUserId)
                                                      .And(new UserIsOwnerSpecification <GameViewModel>(currentUserId));

                if (!spec.IsSatisfiedBy(viewModel))
                {
                    return(new OperationResultVo <Guid>(spec.ErrorMessage));
                }

                if (createTeam)
                {
                    newTeam      = teamDomainService.GenerateNewTeam(currentUserId);
                    newTeam.Name = String.Format("Team {0}", viewModel.Title);
                    teamDomainService.Add(newTeam);
                }

                viewModel.ExternalLinks.RemoveAll(x => String.IsNullOrWhiteSpace(x.Value));

                Game existing = gameDomainService.GetById(viewModel.Id);
                if (existing != null)
                {
                    game = mapper.Map(viewModel, existing);
                }
                else
                {
                    game = mapper.Map <Game>(viewModel);
                }

                if (viewModel.Id == Guid.Empty)
                {
                    gameDomainService.Add(game);

                    pointsEarned += gamificationDomainService.ProcessAction(viewModel.UserId, PlatformAction.GameAdd);
                }
                else
                {
                    gameDomainService.Update(game);
                }

                unitOfWork.Commit();
                viewModel.Id = game.Id;

                if (createTeam && newTeam != null)
                {
                    game.TeamId = newTeam.Id;
                    gameDomainService.Update(game);
                    unitOfWork.Commit();
                }

                return(new OperationResultVo <Guid>(game.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }
        public OperationResultVo <Guid> Save(Guid currentUserId, UserContentViewModel viewModel)
        {
            try
            {
                int pointsEarned = 0;

                UserContent model;

                bool isSpam = CheckSpam(viewModel.Id, viewModel.Content);

                bool isNew = viewModel.Id == Guid.Empty;

                if (isSpam)
                {
                    return(new OperationResultVo <Guid>("Calm down! You cannot post the same content twice in a row."));
                }

                string youtubePattern = @"(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+";

                viewModel.Content = Regex.Replace(viewModel.Content, youtubePattern, delegate(Match match)
                {
                    string v = match.ToString();
                    if (match.Index == 0 && String.IsNullOrWhiteSpace(viewModel.FeaturedImage))
                    {
                        viewModel.FeaturedImage = v;
                    }
                    return(v);
                });

                UserContent existing = userContentDomainService.GetById(viewModel.Id);
                if (existing != null)
                {
                    model = mapper.Map(viewModel, existing);
                }
                else
                {
                    model = mapper.Map <UserContent>(viewModel);
                }

                if (model.PublishDate == DateTime.MinValue)
                {
                    model.PublishDate = model.CreateDate;
                }

                if (isNew)
                {
                    userContentDomainService.Add(model);

                    PlatformAction action = viewModel.IsComplex ? PlatformAction.ComplexPost : PlatformAction.SimplePost;
                    pointsEarned += gamificationDomainService.ProcessAction(viewModel.UserId, action);

                    unitOfWork.Commit().Wait();
                    viewModel.Id = model.Id;

                    if (viewModel.Poll != null && viewModel.Poll.PollOptions != null && viewModel.Poll.PollOptions.Any())
                    {
                        CreatePoll(viewModel);

                        pointsEarned += gamificationDomainService.ProcessAction(viewModel.UserId, PlatformAction.PollPost);
                    }
                }
                else
                {
                    userContentDomainService.Update(model);
                }

                unitOfWork.Commit().Wait();

                return(new OperationResultVo <Guid>(model.Id, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo <Guid>(ex.Message));
            }
        }