예제 #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);
        }
예제 #2
0
        public async Task <IResponse> AddPoints([FromBody] AddPointsModel addPointsModel)
        {
            AddPointsResponse addPointsResponse = new AddPointsResponse();
            PointsModel       points            = await _context.Points.SingleOrDefaultAsync(x => x.Account.Id == addPointsModel.UserId);

            Account account = await _userManager.FindByIdAsync(addPointsModel.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 == addPointsModel.UserId);
                }

                var result = await _pointManager.AddToUserAsync(account, addPointsModel.Value);

                double test = result.pointTransaction.Amount;
                addPointsResponse.Succeeded = result.succeeded;
            }


            return(addPointsResponse);
        }
예제 #3
0
        public async Task <ActionResult> GivePoints(ManagePointsModel managePointsModel)
        {
            string userId = (string)TempData["UserId"];

            AddPointsModel addPointsModel = new AddPointsModel();

            addPointsModel.UserId = userId;
            addPointsModel.Value  = managePointsModel.Points;

            string jsonString = JsonConvert.SerializeObject(addPointsModel);

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

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

            if (addPointsResponse.Succeeded)
            {
                string[] message = { "Points added to account" };

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

            return(RedirectToAction("Index", new { id = userId }));
        }
예제 #4
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 }));
        }
예제 #5
0
        public async Task <IResponse> RemovePoints([FromBody] AddPointsModel addPointsModel)
        {
            AddPointsResponse addPointsResponse = new AddPointsResponse();
            PointsModel       points            = await _context.Points.SingleOrDefaultAsync(x => x.Account.Id == addPointsModel.UserId);

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

            List <string> errors = new List <string>();

            if (account == null)
            {
                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 == addPointsModel.UserId);
                }

                if (points.Value >= addPointsModel.Value && !points.Value.Equals(0.0))
                {
                    var result = await _pointManager.RemoveFromUserAsync(account, addPointsModel.Value);

                    double test = result.pointTransaction.Amount;
                    if (result.succeeded)
                    {
                        addPointsResponse.Succeeded = true;
                    }
                    else
                    {
                        errors.Add("Action failed.");
                    }
                }
                else
                {
                    errors.Add("User doesn't have enough points");
                }
            }
            if (errors.Count > 0)
            {
                addPointsResponse.Errors = errors;
            }


            return(addPointsResponse);
        }