コード例 #1
0
ファイル: Users.cs プロジェクト: laimis/stock-analysis
            public async Task <object> Handle(Users.Query cmd, CancellationToken cancellationToken)
            {
                var users = await _storage.GetUserEmailIdPairs();

                var result = new List <UserView>();

                foreach (var(email, userId) in users)
                {
                    var guid = new System.Guid(userId);
                    var user = await _storage.GetUser(guid);

                    var options = await _portfolio.GetOwnedOptions(guid);

                    var notes = await _portfolio.GetNotes(guid);

                    var stocks = await _portfolio.GetStocks(guid);

                    var alerts = await _alerts.GetAlerts(guid);

                    var u = new UserView(user, stocks, options, notes, alerts);

                    result.Add(u);
                }

                return(result.OrderByDescending(u => u.LastLogin));
            }
コード例 #2
0
        public async Task <ActionResult> LoginAs(Guid userId)
        {
            var u = await _storage.GetUser(userId);

            await AccountController.EstablishSignedInIdentity(HttpContext, u);

            return(this.Redirect("~/"));
        }
コード例 #3
0
            public override async Task <CommandResponse> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

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

                var crypto = await _storage.GetCrypto(cmd.Token, cmd.UserId);

                if (crypto == null)
                {
                    return(CommandResponse.Failed(
                               $"You don't have shares of {cmd.Token.ToString()} to sell, record buy transaction first"));
                }

                crypto.Sell(
                    quantity: cmd.Quantity,
                    dollarAmountReceived: cmd.DollarAmount,
                    date: cmd.Date.Value,
                    notes: cmd.Notes);

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

                return(CommandResponse.Success());
            }
コード例 #4
0
            public override async Task <CommandResponse> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

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

                if (!user.Verified)
                {
                    return(CommandResponse.Failed(
                               "Please verify your email first before you can record buy transaction"));
                }

                var stock = await this._storage.GetStock(cmd.Ticker, cmd.UserId);

                if (stock == null)
                {
                    stock = new OwnedStock(cmd.Ticker, cmd.UserId);
                }

                stock.Purchase(cmd.NumberOfShares, cmd.Price, cmd.Date.Value, cmd.Notes);

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

                return(CommandResponse.Success());
            }
コード例 #5
0
ファイル: Confirm.cs プロジェクト: laimis/stock-analysis
            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));
            }
コード例 #6
0
            public override async Task <CommandResponse> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

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

                var crypto = await _storage.GetCrypto(cmd.Token, cmd.UserId);

                if (crypto == null)
                {
                    crypto = new OwnedCrypto(cmd.Token, cmd.UserId);
                }

                crypto.Purchase(
                    quantity: cmd.Quantity, dollarAmountSpent: cmd.DollarAmount, date: cmd.Date.Value, notes: cmd.Notes
                    );

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

                return(CommandResponse.Success());
            }
コード例 #7
0
        public async Task Handle(UserPasswordResetRequested e, CancellationToken cancellationToken)
        {
            // generate random guid that maps back to aggregate id
            // store it in the storage
            // send email

            Console.WriteLine("Issuing password reset");

            var u = await _storage.GetUser(e.AggregateId);

            if (u == null)
            {
                return;
            }

            var association = new ProcessIdToUserAssociation(e.AggregateId, e.When);

            await _storage.SaveUserAssociation(association);

            var reseturl = $"{EmailSettings.PasswordResetUrl}/{association.Id}";

            await _email.Send(
                u.State.Email,
                Sender.NoReply,
                EmailTemplate.PasswordReset,
                new { reseturl }
                );
        }
コード例 #8
0
ファイル: Sell.cs プロジェクト: laimis/stock-analysis
            public override async Task <CommandResponse> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

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

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

                var stock = await _storage.GetStock(cmd.Ticker, cmd.UserId);

                if (stock == null)
                {
                    return(CommandResponse.Failed(
                               $"You don't have shares of {cmd.Ticker.ToString()} to sell, record buy transaction first"));
                }

                stock.Sell(cmd.NumberOfShares, cmd.Price, cmd.Date.Value, cmd.Notes);

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

                return(CommandResponse.Success());
            }
コード例 #9
0
            public override async Task <CommandResponse> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _accounts.GetUser(cmd.UserId);

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

                var stock = await _storage.GetStock(cmd.Ticker, cmd.UserId);

                if (stock == null)
                {
                    return(CommandResponse.Failed(
                               "Unable to find stock for settings change"
                               ));
                }

                stock.UpdateSettings(cmd.Category);

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

                return(CommandResponse.Success());
            }
コード例 #10
0
ファイル: Add.cs プロジェクト: laimis/stock-analysis
            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));
            }
コード例 #11
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));
            }
コード例 #12
0
ファイル: Status.cs プロジェクト: laimis/stock-analysis
            private async Task <AccountStatusView> GetFromDatabase(Guid userId)
            {
                var user = await _storage.GetUser(userId);

                if (user == null)
                {
                    return(new AccountStatusView
                    {
                        LoggedIn = false,
                    });
                }

                return(new AccountStatusView
                {
                    LoggedIn = true,
                    Username = user.Id,
                    Verified = user.State.Verified != null,
                    Created = user.State.Created,
                    Email = user.State.Email,
                    Firstname = user.State.Firstname,
                    Lastname = user.State.Lastname,
                    IsAdmin = user.State.Email == EmailSettings.Admin.Email,
                    SubscriptionLevel = user.State.SubscriptionLevel
                });
            }
コード例 #13
0
ファイル: Get.cs プロジェクト: DT021/stock-analysis
            public async Task <object> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _storage.GetUser(request.UserId);

                if (user == null)
                {
                    return(new
                    {
                        loggedIn = false,
                    });
                }

                return(new
                {
                    username = user.Id,
                    loggedIn = true,
                    verified = user.Verified,
                    created = user.Created,
                    email = user.Email,
                    firstname = user.Firstname,
                    lastname = user.Lastname,
                    isAdmin = user.Email == EmailSettings.Admin,
                    subscriptionLevel = user.SubscriptionLevel
                });
            }
コード例 #14
0
ファイル: Delete.cs プロジェクト: DT021/stock-analysis
            public async Task <Unit> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _storage.GetUser(cmd.UserId);

                if (user == null)
                {
                    return(new Unit());
                }

                user.Delete(cmd.Feedback);

                await _storage.Save(user);

                await _emails.Send(
                    EmailSettings.Admin,
                    Sender.NoReply,
                    EmailTemplate.AdminUserDeleted,
                    new { feedback = cmd.Feedback, email = user.State.Email });

                await _storage.Delete(user);

                await _portfolio.Delete(user.Id);

                return(new Unit());
            }
コード例 #15
0
        public async Task Handle(UserCreated e, CancellationToken cancellationToken)
        {
            var u = await _storage.GetUser(e.AggregateId);

            if (u == null)
            {
                return;
            }

            await SendConfirmAccountEmail(e, u);

            await SendNewUserSignedUpEmail(u);
        }
コード例 #16
0
            public async Task <Unit> Handle(Command cmd, CancellationToken cancellationToken)
            {
                var user = await _storage.GetUser(cmd.UserId);

                if (user == null)
                {
                    return(new Unit());
                }

                await _portfolio.Delete(user.Id);

                return(new Unit());
            }
コード例 #17
0
            public async Task <CommandResponse> Handle(Command request, CancellationToken cancellationToken)
            {
                var(records, err) = _parser.Parse <TransactionRecord>(request.Content);
                if (err != null)
                {
                    return(CommandResponse.Failed(err));
                }

                var user = await _storage.GetUser(request.UserId);

                if (user == null)
                {
                    return(CommandResponse.Failed("User not found"));
                }

                await SendEmail(user, subject : "Started importing transactions", body : "");

                var errors = new List <string>();

                try
                {
                    foreach (var cmd in GetCommands(records))
                    {
                        cmd.WithUserId(request.UserId);
                        var response = await _mediator.Send(cmd);

                        if (response is CommandResponse r && r.Error != null)
                        {
                            errors.Add(r.Error);
                        }
                    }
                }
                catch (Exception ex)
                {
                    await SendEmail(
                        user : user,
                        subject : "Failed to import transactions",
                        body : ex.ToString()
                        );

                    return(CommandResponse.Failed($"Entry Failed: {ex}"));
                }

                var onFinished = errors.Count switch
                {
                    0 => AfterSuccess(user),
                    _ => AfterFailure(errors, user)
                };

                return(await onFinished);
            }
コード例 #18
0
ファイル: Login.cs プロジェクト: laimis/stock-analysis
            public async Task <string> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _storage.GetUser(request.UserId);

                if (user == null)
                {
                    return($"Unable to load user {request.UserId}");
                }

                user.LoggedIn(request.IPAddress, request.Timestamp);

                await _storage.Save(user);

                return("");
            }
コード例 #19
0
        public async Task Handle(UserConfirmed e, CancellationToken cancellationToken)
        {
            var u = await _storage.GetUser(e.AggregateId);

            if (u == null)
            {
                return;
            }

            await _email.Send(
                new Recipient(email : u.State.Email, name : u.State.Name),
                Sender.Support,
                EmailTemplate.NewUserWelcome,
                new {}
                );
        }
コード例 #20
0
ファイル: Sell.cs プロジェクト: laimis/stock-analysis
            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));
            }