Exemplo n.º 1
0
        public async Task <ActionResult <InstrumentModel> > Post(InstrumentModel model)
        {
            try
            {
                var existing = await _repository.GetInstrumentAsync(model.Code);

                if (existing != null)
                {
                    return(BadRequest("Code in use"));
                }

                var location = _linkGenerator.GetPathByAction("Get",
                                                              "Instruments",
                                                              new { code = model.Code });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use current name"));
                }

                var instrument = _mapper.Map <Instrument>(model);
                _repository.Add(instrument);
                if (await _repository.SaveChangesAsync())
                {
                    return(Created("", _mapper.Map <InstrumentModel>(instrument)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to get the Instrument"));
            }

            return(BadRequest());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Post(UserDataModel model)
        {
            try
            {
                var existing = await _repository.GetRegisteredUserAsync(model.Email);

                if (existing != null)
                {
                    return(BadRequest("Email in use"));
                }

                var location = _linkGenerator.GetPathByAction("GetByEmail",
                                                              "Customers",
                                                              new { email = model.Email });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use current email"));
                }

                var userData = _mapper.Map <UserData>(model);

                var plainText = userData.Password;

                var encryptedPassword = CommonMethods.EncryptPassword(plainText);

                userData.Password = encryptedPassword;

                _repository.Add(userData);
                if (await _repository.SaveChangesAsync())
                {
                    return(Created("", _mapper.Map <UserDataModel>(userData)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to get the user"));
            }

            return(BadRequest());
        }
Exemplo n.º 3
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());
        }