Exemple #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));
            }
Exemple #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));
            }
Exemple #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));
            }
            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));
            }
Exemple #5
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));
            }
Exemple #6
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));
            }
Exemple #7
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));
            }
Exemple #8
0
 public IActionResult Scalar([FromBody] Command command)
 {
     return(this.Intercept(() =>
     {
         try
         {
             var results = dbConnection.ExecuteScalars <long>(command.Commands);
             return Ok(CommandResponse.Ok(results));
         }
         catch (Exception err)
         {
             return Ok(CommandResponse.Failed(err.Message));
         }
     }));
 }
Exemple #9
0
 public IActionResult Command([FromBody] Command command)
 {
     return(this.Intercept(() =>
     {
         try
         {
             dbConnection.ExecuteCommands(command.Commands);
             return Ok(CommandResponse.Ok());
         }
         catch (Exception err)
         {
             return Ok(CommandResponse.Failed(err.Message));
         }
     }));
 }
Exemple #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));
            }
Exemple #11
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));
            }
Exemple #12
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));
            }
Exemple #13
0
        public static CommandResponse PostCommandRequest(CommandRequest request, string url = "http://localhost:8033/command")
        {
            bool isRetry = false;

            //Ok!
retry:
            try
            {
                {
                    HttpClient client = new HttpClient();
                    client.Timeout = TimeSpan.FromSeconds(15);
                    StringWriter wr = new StringWriter();
                    JsonSerializer.Create().Serialize(wr, request);
                    HttpContent content = new StringContent(wr.ToString());
                    content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                    var    task       = client.PostAsync(url, content).GetAwaiter().GetResult();
                    string jsonResult = task.Content.ReadAsStringAsync().GetAwaiter().GetResult();

                    return(JsonConvert.DeserializeObject <CommandResponse>(jsonResult, new JsonSerializerSettings()
                    {
                        StringEscapeHandling = StringEscapeHandling.EscapeNonAscii,
                        Formatting = Formatting.None
                    }));
                }
                isRetry = false;
            }
            catch (HttpRequestException httpRequestException)
            {
                if (httpRequestException.Message.Contains("target machine actively refused"))
                {
                }
                else if (httpRequestException.Message.Contains("An error occurred while sending the request"))
                {
                    Console.WriteLine("Failed to send request to " + url);
                    //Damn it.
                    if (!isRetry)
                    {
                        isRetry = true;
                        goto retry;
                    }
                    else
                    {
                        isRetry = false;
                    }
                }
                else if (httpRequestException.Message.Contains("established connection was aborted by the software in your host machine"))
                {
                    //Damn it.
                    if (!isRetry)
                    {
                        isRetry = true;
                        goto retry;
                    }
                    else
                    {
                        isRetry = false;
                    }
                }
                else
                {
                    Console.WriteLine(httpRequestException);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            return(CommandResponse.Failed());
        }