Beispiel #1
0
        public async Task <ActionResult> Post(IEnumerable <OrderItemInputModel> orderItemModels)
        {
            var user = await _repositories.Users.GetByIdAsync(CurrentUserId);

            if (user.Address == null)
            {
                return(BadRequest(new ErrorModel(
                                      $"There is not an address registered yet. " +
                                      $"Request {Url.Action(nameof(AddressController.Post), nameof(AddressController))} using POST HTTP method in order to register it.")));
            }

            var products = await _repositories.Products
                           .GetAsync(orderItemModels.Select(d => d.ProductId).ToArray());

            var orderItems = _mapper.Map <ICollection <OrderItem> >(orderItemModels);

            foreach (var item in orderItems)
            {
                var product = products.FirstOrDefault(d => d.Id == item.ProductId);

                if (product == null)
                {
                    return(BadRequest(new ErrorModel(
                                          $"There is not a product with id {item.ProductId}.")));
                }

                if (product.Quantity < item.Quantity)
                {
                    return(BadRequest(new ErrorModel(
                                          $"The requested quantity of product '{product.Title}' is more than the available.")));
                }

                item.UnityPoints  = product.Points;
                product.Quantity -= item.Quantity;
            }

            var order = await _repositories.Orders.NewAsync(user);

            order.SetItems(orderItems);

            if (user.Account.PointBalance < order.TotalPoints)
            {
                return(BadRequest(new ErrorModel(
                                      $"Your point balance is insuficient for getting the requested products.")));
            }

            user.Account.SetTransaction(
                TransactionType.Debit,
                "Purchase",
                order.TotalPoints);

            await _repositories.CommitChangesAsync();

            var orderModel = _mapper.Map <OrderModel>(order);

            return(Created(
                       Url.Action(nameof(GetById), new { id = order.Id }),
                       orderModel));
        }
Beispiel #2
0
        public async Task <ActionResult> Post(AddressModel addressModel)
        {
            var user = await _repositories.Users.GetByIdAsync(CurrentUserId);

            if (user.Address != null)
            {
                return(BadRequest(new ErrorModel(
                                      $"An address is already registered. " +
                                      $"Request {Url.Action(nameof(Put))} using PUT HTTP method in order to update it.")));
            }

            var address = await _repositories.Addresses.NewAsync(user);

            _mapper.Map(addressModel, address);

            await _repositories.CommitChangesAsync();

            addressModel.Id = address.Id;

            return(Created(
                       Url.Action(nameof(Get)),
                       addressModel));
        }
Beispiel #3
0
        public async Task <ActionResult> Register(RegisterModel registerModel)
        {
            var registerInfo = await _securityProvider.Register(
                registerModel.Name,
                registerModel.Email,
                registerModel.Password);

            if (!registerInfo.Result.Succeeded)
            {
                return(BadRequest(registerInfo.Result.Errors.Where(d => !d.Code.Equals("DuplicateUserName"))));
            }

            await _repositories.Accounts.NewAsync(registerInfo.User.Id);

            await _repositories.CommitChangesAsync();

            return(Created(
                       Url.Action(nameof(Me)),
                       new AuthResultModel
            {
                User = _mapper.Map <UserModel>(registerInfo.User),
                Token = registerInfo.Token
            }));
        }