public async Task <IHttpActionResult> InvalidateBuyIntent(BuyIntentApiRequest request)
        {
            BuyIntentRequest buyIntentRequest = Mapper.Map <BuyIntentRequest>(request);

            await AppBusiness.Purchase.DenyBuyIntent(buyIntentRequest);

            return(Ok(new BaseApiResult()));
        }
        public async Task <BuyIntentResult> RegisterBuyIntent(BuyIntentRequest request)
        {
            request.DateIntent = DateTime.UtcNow;

            BuyIntentResult response = await AppRepository.Purchase.InsertBuyIntent(request);

            return(response);
        }
        public async Task <BuyIntentResult> InsertBuyIntent(BuyIntentRequest request)
        {
            BuyIntent intent = Parse(request);

            AppRepository.EntitiesContext.BuyIntents.Add(intent);

            await AppRepository.EntitiesContext.SaveChangesAsync();

            return(Parse(intent));
        }
        public async Task DenyBuyIntent(BuyIntentRequest request)
        {
            BuyIntent buyIntent = await AppRepository.EntitiesContext.BuyIntents.FindAsync(new Guid(request.Id));

            buyIntent.DateResult = DateTime.UtcNow;
            buyIntent.Processed  = true;
            buyIntent.Realized   = false;

            await AppRepository.EntitiesContext.SaveChangesAsync();
        }
        public async Task <IHttpActionResult> RegisterPurchase(BuyIntentApiRequest request)
        {
            BuyIntentRequest buyIntentRequest = Mapper.Map <BuyIntentRequest>(request);

            UserResult userResult = await AppBusiness.Purchase.ConfirmBuyIntent(buyIntentRequest);

            UserApiResult response = Mapper.Map <UserApiResult>(userResult);

            return(Ok(response));
        }
        public async Task <IHttpActionResult> InsertBuyIntent(BuyIntentApiRequest request)
        {
            request.UserId = (await GetUser()).Id;

            BuyIntentRequest buyIntentRequest = Mapper.Map <BuyIntentRequest>(request);

            BuyIntentResult buyIntentResult = await AppBusiness.Purchase.RegisterBuyIntent(buyIntentRequest);

            BuyIntentApiResult response = Mapper.Map <BuyIntentApiResult>(buyIntentResult);

            return(Ok(response));
        }
        private BuyIntent Parse(BuyIntentRequest request)
        {
            if (request != null)
            {
                BuyIntent response = new BuyIntent();

                response.AvailableTrap = AppRepository.EntitiesContext.AvailableTraps.Find(new Guid(request.AvailableTrapId));
                response.DateIntent    = request.DateIntent;
                response.DateResult    = request.DateResult;
                response.Processed     = request.Processed;
                response.Realized      = request.Realized;
                response.StoreKey      = request.StoreKey;
                response.User          = AppRepository.EntitiesContext.Users.Find(new Guid(request.UserId));

                return(response);
            }
            else
            {
                return(null);
            }
        }
        public async Task <UserResult> ConfirmBuyIntent(BuyIntentRequest request)
        {
            UserResult response = null;

            BuyIntentResult actualIntent = await AppRepository.Purchase.GetById(request.Id);

            if (actualIntent != null)
            {
                request = Mapper.Map <BuyIntentRequest>(actualIntent);

                await AppRepository.Purchase.ConfirmBuyIntent(request);

                response = await AppBusiness.User.GetById(actualIntent.UserId);

                if (response != null)
                {
                    AvailableTrapResult availableTrap = await AppBusiness.Purchase.GetAvailableTrapById(actualIntent.AvailableTrapId);

                    if (availableTrap != null)
                    {
                        TrapResult trap = await AppBusiness.Trap.GetByNameKey(availableTrap.NameKey);

                        UserTrapRequest userTrap = new UserTrapRequest();

                        userTrap.TrapId = trap.Id.ToString();
                        userTrap.Amount = availableTrap.Amount;

                        UserRequest userRequest = Mapper.Map <UserRequest>(response);

                        AppBusiness.User.AddTrap(userRequest, userTrap);

                        await AppRepository.User.UpdateTraps(userRequest.Id, userRequest.Traps);

                        response = await AppBusiness.User.GetById(userRequest.Id);
                    }
                }
            }

            return(response);
        }
 public async Task DenyBuyIntent(BuyIntentRequest request)
 {
     await AppRepository.Purchase.DenyBuyIntent(request);
 }