Ejemplo n.º 1
0
        public async Task <IResponse> BuyPoints([FromBody] BuyPointsModel buyPointsModel)
        {
            AddPointsResponse addPointsResponse = new AddPointsResponse();
            PointsModel       points            = await _context.Points.SingleOrDefaultAsync(x => x.Account.Id == buyPointsModel.UserId);

            Account account = await _userManager.FindByIdAsync(buyPointsModel.UserId);

            if (account == null)
            {
                List <string> errors = new List <string>();
                errors.Add("User does not exist.");
                addPointsResponse.Errors = errors;
            }
            else
            {
                if (points == null)
                {
                    _context.Points.Add(new PointsModel {
                        Account = account, Value = 0
                    });
                    await _context.SaveChangesAsync();

                    points = await _context.Points.SingleOrDefaultAsync(x => x.Account.Id == buyPointsModel.UserId);
                }

                var result = await _pointManager.PurchasePoints(account, buyPointsModel.Value, buyPointsModel.Price);

                addPointsResponse.Succeeded = result;
            }
            return(addPointsResponse);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> BuyPoints(ManagePointsModel managePointsModel)
        {
            string userId = (string)TempData["UserId"];

            BuyPointsModel buyPointsModel = new BuyPointsModel
            {
                UserId = userId,
                Value  = managePointsModel.Points,
                Price  = managePointsModel.Price
            };

            string jsonString = JsonConvert.SerializeObject(buyPointsModel);

            HttpResponseMessage responseMessage = await ApiClient.PostAsync("/Point/buyPoints", jsonString);

            string            responseResult    = responseMessage.Content.ReadAsStringAsync().Result;
            AddPointsResponse addPointsResponse = JsonConvert.DeserializeObject <AddPointsResponse>(responseResult);

            if (addPointsResponse.Succeeded)
            {
                string[] message = { "Points purchased successfully." };

                TempData["ManagePointMessage"] = message;
            }
            else
            {
                TempData["ManagePointMessage"] = addPointsResponse.Errors;
            }

            return(RedirectToAction("Index", new { id = userId }));
        }