Ejemplo n.º 1
0
        public async Task <ActionResult> Login([FromBody] LoginModel user)
        {
            var userData = await _repository.GetRegisteredUserAsync(user.Email);

            if (userData == null)
            {
                return(NotFound("Failed to find the user with this email and password"));
            }

            var decryptedPassword = CommonMethods.DecryptPassword(userData.Password);

            if (user.Email == userData.Email && user.Password == decryptedPassword)
            {
                var secretKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["ApplicationSettings:JWT_Secret"].ToString()));
                var signingCreditials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);

                var tokenOptions = new JwtSecurityToken(
                    issuer: _configuration["ApplicationSettings:Server_URL"].ToString(),
                    audience: _configuration["ApplicationSettings:Server_URL"].ToString(),
                    claims: new List <Claim>(),
                    expires: DateTime.Now.AddMinutes(30),
                    signingCredentials: signingCreditials
                    );

                var tokenString = new JwtSecurityTokenHandler().WriteToken(tokenOptions);
                return(Ok(new { Token = tokenString }));
            }

            return(Unauthorized());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Order[]> > Post([FromRoute] string userData, [FromRoute] int numberOfInstruments, [FromBody] dynamic data)
        {
            try
            {
                List <Order>      orders      = new List <Order>();
                List <Instrument> instruments = new List <Instrument>();

                var splittedUserData = userData.Split(";");

                string email     = splittedUserData[0];
                string firstName = splittedUserData[1];
                string lastName  = splittedUserData[2];

                UserData user = await _repository.GetRegisteredUserAsync(email);

                if (user == null)
                {
                    user           = new UserData();
                    user.Email     = email;
                    user.FirstName = firstName;
                    user.LastName  = lastName;
                }


                for (int i = 0; i < numberOfInstruments; i++)
                {
                    instruments.Add(JsonConvert.DeserializeObject <Instrument>(data[i].ToString()));
                }

                Order lastOrder = await _repository.GetLastOrderAsync();

                int orderNumber;

                if (lastOrder == null)
                {
                    orderNumber = 1;
                }
                else
                {
                    orderNumber = lastOrder.OrderNumber + 1;
                }

                string addressData = data[numberOfInstruments].ToString();

                var splittedData = addressData.Split(";");

                for (int i = 0; i < instruments.Count; i++)
                {
                    Order order = new Order();
                    order.FirstName         = user.FirstName;
                    order.LastName          = user.LastName;
                    order.Email             = user.Email;
                    order.InstrumentName    = instruments[i].Name;
                    order.Code              = instruments[i].Code;
                    order.Price             = instruments[i].Price;
                    order.Quantity          = instruments[i].Quantity;
                    order.BillingCity       = splittedData[0];
                    order.BillingState      = splittedData[1];
                    order.BillingPostalCode = splittedData[2];
                    order.BillingAddress    = splittedData[3];
                    order.Date              = DateTime.Now;
                    order.OrderNumber       = orderNumber;
                    order.Status            = "Requested";

                    orders.Add(order);
                }

                foreach (var item in orders)
                {
                    _repository.Add(item);
                }

                if (await _repository.SaveChangesAsync())
                {
                    foreach (var item in orders)
                    {
                        var instrument = await _repository.GetInstrumentAsync(item.Code);

                        instrument.Quantity -= item.Quantity;
                        await _repository.SaveChangesAsync();
                    }

                    CommonMethods.SendEmail(orders.ToArray());
                    return(Created("", _mapper.Map <OrderModel[]>(orders)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to save the orders"));
            }

            return(BadRequest());
        }