Пример #1
0
            public async Task <CommandResponse <Note> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

                if (user == null)
                {
                    return(CommandResponse <Note> .Failed(
                               "Unable to find user account for notes operation"));
                }

                if (user.State.Verified == null)
                {
                    return(CommandResponse <Note> .Failed(
                               "Please verify your email first before you can start creating notes"));
                }

                var note = new Note(
                    cmd.UserId,
                    cmd.Note,
                    cmd.Ticker,
                    cmd.Created ?? DateTimeOffset.UtcNow);

                await _storage.Save(note, cmd.UserId);

                return(CommandResponse <Note> .Success(note));
            }
Пример #2
0
            public async Task <CommandResponse <User> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var r = await _storage.GetUserAssociation(cmd.Id.Value);

                if (r == null)
                {
                    return(CommandResponse <User> .Failed(
                               "Invalid password reset token. Check the link in the email or request a new password reset"));
                }

                if (r.IsOlderThan(15))
                {
                    return(CommandResponse <User> .Failed(
                               "Password reset link has expired. Please request a new password reset"));
                }

                var u = await _storage.GetUser(r.UserId);

                if (u == null)
                {
                    return(CommandResponse <User> .Failed(
                               "User account is no longer valid"));
                }

                var hash = _hash.Generate(cmd.Password, 32);

                u.SetPassword(hash.Hash, hash.Salt);

                await _storage.Save(u);

                return(CommandResponse <User> .Success(u));
            }
Пример #3
0
            public async Task <CommandResponse <User> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var r = await _storage.GetUserAssociation(cmd.Id);

                if (r == null)
                {
                    return(CommandResponse <User> .Failed(
                               "Invalid confirmation identifier."));
                }

                if (r.IsOlderThan(60 * 24 * 30)) // 30 day expiration?
                {
                    return(CommandResponse <User> .Failed(
                               "Account confirmation link is expired. Please request a new one."));
                }

                var u = await _storage.GetUser(r.UserId);

                if (u == null)
                {
                    return(CommandResponse <User> .Failed(
                               "User account is no longer valid"));
                }

                u.Confirm();

                await _storage.Save(u);

                return(CommandResponse <User> .Success(u));
            }
Пример #4
0
            public async Task <CommandResponse <User> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _storage.GetUserByEmail(request.Email);

                if (user == null)
                {
                    return(CommandResponse <User> .Failed(GENERIC_MSG));
                }

                // oauth path where password was not set....
                if (!user.State.IsPasswordAvailable)
                {
                    return(CommandResponse <User> .Failed(GENERIC_MSG));
                }

                var computed = _hash.Generate(request.Password, user.State.GetSalt());

                var matches = user.PasswordHashMatches(computed);

                if (matches)
                {
                    user.LoggedIn(request.IPAddress, DateTimeOffset.UtcNow);

                    await _storage.Save(user);

                    return(CommandResponse <User> .Success(user));
                }

                return(CommandResponse <User> .Failed(GENERIC_MSG));
            }
Пример #5
0
        private CommandResponse ExecuteCreateComment(CreateCommentCommand command)
        {
            var comment = new Comment
            {
                Id        = Guid.NewGuid(),
                Timestamp = DateTime.Now,
                Author    = command.IssuedBy,
                Text      = command.Text
            };

            foreach (var q in Questions)
            {
                if (q.Id == command.QuestionOrAnswerId)
                {
                    q.Comments.Add(comment);
                    return(CommandResponse.Success(comment));
                }

                foreach (var a in q.Answers)
                {
                    if (a.Id == command.QuestionOrAnswerId)
                    {
                        a.Comments.Add(comment);
                        return(CommandResponse.Success(comment));
                    }
                }
            }
            return(CommandResponse.Failure($"Question or answer with id {command.QuestionOrAnswerId} does not exist."));
        }
Пример #6
0
        private CommandResponse ExecuteDeleteQuestion(DeleteQuestionCommand command)
        {
            var existingQuestion = Questions.FirstOrDefault(q => q.Id == command.QuestionId);

            if (existingQuestion == null)
            {
                return(CommandResponse.Failure($"Question {command.QuestionId} doesn't exist."));
            }
            Questions.Remove(existingQuestion);
            return(CommandResponse.Success());
        }
Пример #7
0
            public async Task <CommandResponse <User> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var exists = await _storage.GetUserByEmail(cmd.Email);

                if (exists != null)
                {
                    return(CommandResponse <User> .Failed($"Account with {cmd.Email} already exists"));
                }

                return(CommandResponse <User> .Success(null));
            }
Пример #8
0
        private CommandResponse ExecuteEditComment(EditCommentCommand command)
        {
            var existingComment = Questions.SelectMany(q => q.Comments).Concat(Questions.SelectMany(q => q.Answers.SelectMany(a => a.Comments)))
                                  .SingleOrDefault(c => c.Id == command.CommentId);

            if (existingComment == null)
            {
                return(CommandResponse.Failure($"Comment with id {command.CommentId} does not exist."));
            }

            existingComment.Text = command.Text;
            return(CommandResponse.Success(existingComment));
        }
Пример #9
0
 private CommandResponse ExecuteAcceptAnswerCommand(AcceptAnswerCommand command)
 {
     foreach (var q in Questions)
     {
         var answer = q.Answers.SingleOrDefault(a => a.Id == command.AnswerId);
         if (answer != null)
         {
             q.AcceptedAnswerId = answer.Id;
             return(CommandResponse.Success(answer));
         }
     }
     return(CommandResponse.Failure($"Answer {command.AnswerId} doesn't exist."));
 }
Пример #10
0
            public async Task <CommandResponse <OwnedOption> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var option = await _storage.GetOwnedOption(cmd.Id, cmd.UserId);

                if (option == null)
                {
                    return(CommandResponse <OwnedOption> .Failed("Trying to delete not owned option"));
                }

                option.Delete();

                await this._storage.Save(option, cmd.UserId);

                return(CommandResponse <OwnedOption> .Success(option));
            }
Пример #11
0
            public async Task <CommandResponse <OwnedOption> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var option = await _storage.GetOwnedOption(cmd.Id.Value, cmd.UserId);

                if (option == null)
                {
                    return(CommandResponse <OwnedOption> .Failed("Trying to expire not owned option"));
                }

                option.Expire(cmd.Assigned.Value);

                await _storage.Save(option, cmd.UserId);

                return(CommandResponse <OwnedOption> .Success(option));
            }
Пример #12
0
            public async Task <CommandResponse <OwnedStock> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var stock = await _storage.GetStock(cmd.Id, cmd.UserId);

                if (stock == null)
                {
                    return(CommandResponse <OwnedStock> .Failed("Trying to delete not owned stock"));
                }

                stock.Delete();

                await _storage.Save(stock, cmd.UserId);

                return(CommandResponse <OwnedStock> .Success(stock));
            }
Пример #13
0
        private CommandResponse ExecuteVoteQuestionCommand(VoteQuestionCommand command)
        {
            var existingQuestion = Questions.FirstOrDefault(q => q.Id == command.QuestionId);

            if (existingQuestion == null)
            {
                return(CommandResponse.Failure($"Question {command.QuestionId} doesn't exist."));
            }

            existingQuestion.Votes.Add(new Vote
            {
                Author    = command.IssuedBy,
                Id        = Guid.NewGuid(),
                Timestamp = DateTime.Now,
                Direction = command.VoteDirection
            });
            return(CommandResponse.Success(existingQuestion));
        }
Пример #14
0
        private CommandResponse ExecuteCreateQuestion(CreateQuestionCommand command)
        {
            var question = new Question
            {
                Id        = Guid.NewGuid(),
                Author    = command.IssuedBy,
                Tags      = command.Tags?.ToList() ?? new List <Tag>(),
                Text      = command.Text,
                Title     = command.Title,
                Votes     = new List <Vote>(),
                Timestamp = DateTime.Now,
                Comments  = new List <Comment>(),
                Answers   = new List <Answer>()
            };

            Questions.Add(question);
            return(CommandResponse.Success(question));
        }
Пример #15
0
        private CommandResponse ExecuteEditAnswer(EditAnswerCommand command)
        {
            var existingAnswer = Questions.SelectMany(q => q.Answers).SingleOrDefault(a => a.Id == command.AnswerId);

            if (existingAnswer == null)
            {
                return(CommandResponse.Failure($"Answer {command.AnswerId} doesn't exist."));
            }

            existingAnswer.History.Add(new PostHistoryItem
            {
                Author = existingAnswer.Author,
                Text   = existingAnswer.Text
            });

            existingAnswer.Text = command.Text;
            return(CommandResponse.Success(existingAnswer));
        }
Пример #16
0
        private CommandResponse ExecuteEditQuestion(EditQuestionCommand command)
        {
            var existingQuestion = Questions.FirstOrDefault(q => q.Id == command.QuestionId);

            if (existingQuestion == null)
            {
                return(CommandResponse.Failure($"Question {command.QuestionId} doesn't exist."));
            }

            existingQuestion.History.Add(new PostHistoryItem
            {
                Author = existingQuestion.Author,
                Text   = existingQuestion.Text
            });

            existingQuestion.Title = command.Title;
            existingQuestion.Text  = command.Text;
            return(CommandResponse.Success(existingQuestion));
        }
Пример #17
0
        private CommandResponse ExecuteVoteAnswer(VoteAnswerCommand command)
        {
            var existingAnswer = Questions.SelectMany(q => q.Answers).SingleOrDefault(a => a.Id == command.AnswerId);

            if (existingAnswer == null)
            {
                return(CommandResponse.Failure($"Answer {command.AnswerId} doesn't exist."));
            }

            existingAnswer.Votes.Add(new Vote
            {
                Author    = command.IssuedBy,
                Id        = Guid.NewGuid(),
                Timestamp = DateTime.Now,
                Direction = command.VoteDirection
            });

            return(CommandResponse.Success(existingAnswer));
        }
Пример #18
0
        private CommandResponse ExecuteCreateAnswer(CreateAnswerCommand command)
        {
            var existingQuestion = Questions.FirstOrDefault(q => q.Id == command.QuestionId);

            if (existingQuestion == null)
            {
                return(CommandResponse.Failure($"Question {command.QuestionId} doesn't exist."));
            }

            var answer = new Answer
            {
                Author    = command.IssuedBy,
                Id        = Guid.NewGuid(),
                Timestamp = DateTime.Now,
                Text      = command.Text
            };

            existingQuestion.Answers.Add(answer);

            return(CommandResponse.Success(answer));
        }
Пример #19
0
            public override async Task <CommandResponse <OwnedOption> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

                if (user == null)
                {
                    return(CommandResponse <OwnedOption> .Failed(
                               "Unable to find user account for options operation"));
                }

                if (user.State.Verified == null)
                {
                    return(CommandResponse <OwnedOption> .Failed(
                               "Please verify your email first before you can record option transaction"));
                }

                var options = await _storage.GetOwnedOptions(cmd.UserId);

                var type = (OptionType)Enum.Parse(typeof(OptionType), cmd.OptionType);

                var option = options.SingleOrDefault(o => o.IsMatch(cmd.Ticker, cmd.StrikePrice, type, cmd.ExpirationDate.Value));

                if (option == null)
                {
                    option = new OwnedOption(
                        cmd.Ticker,
                        cmd.StrikePrice,
                        type,
                        cmd.ExpirationDate.Value,
                        cmd.UserId
                        );
                }

                option.Sell(cmd.NumberOfContracts, cmd.Premium, cmd.Filled.Value, cmd.Notes);

                await _storage.Save(option, cmd.UserId);

                return(CommandResponse <OwnedOption> .Success(option));
            }
Пример #20
0
            public async Task <CommandResponse <User> > Handle(Command cmd, CancellationToken cancellationToken)
            {
                var exists = await _storage.GetUserByEmail(cmd.UserInfo.Email);

                if (exists != null)
                {
                    return(CommandResponse <User> .Failed($"Account with {cmd.UserInfo} already exists"));
                }

                var u = new User(cmd.UserInfo.Email, cmd.UserInfo.Firstname, cmd.UserInfo.Lastname);

                var(hash, salt) = _hash.Generate(cmd.UserInfo.Password, 32);

                u.SetPassword(hash, salt);

                if (cmd.PaymentInfo != null)
                {
                    var result = _subscriptions.Create(
                        u,
                        planId: cmd.PaymentInfo.PlanId,
                        paymentToken: cmd.PaymentInfo.Token.Id,
                        email: cmd.PaymentInfo.Token.Email);

                    if (result.CustomerId != null)
                    {
                        u.SubscribeToPlan(cmd.PaymentInfo.PlanId, result.CustomerId, result.SubscriptionId);
                    }
                    else
                    {
                        return(CommandResponse <User> .Failed(
                                   $"Failed to process the payment, please try again or use a different payment form"
                                   ));
                    }
                }

                await _storage.Save(u);

                return(CommandResponse <User> .Success(u));
            }
Пример #21
0
        private CommandResponse ExecuteDeleteComment(DeleteCommentCommand command)
        {
            foreach (var q in Questions)
            {
                var existingComment = q.Comments.SingleOrDefault(c => c.Id == command.CommentId);
                if (existingComment != null)
                {
                    q.Comments.Remove(existingComment);
                    return(CommandResponse.Success());
                }

                foreach (var a in q.Answers)
                {
                    existingComment = a.Comments.SingleOrDefault(c => c.Id == command.CommentId);
                    if (existingComment != null)
                    {
                        a.Comments.Remove(existingComment);
                        return(CommandResponse.Success());
                    }
                }
            }
            return(CommandResponse.Failure($"Comment with id {command.CommentId} does not exist."));
        }