Пример #1
0
        public string Post([FromBody] NewOffer value)
        {
            // Users name (it's actually an email) - for this to work in IdentityServer in the ApiClaims must be defined name (and email)
            var user = User.Claims.Where(x => x.Type == "name").FirstOrDefault();

            Console.WriteLine("Authenticated user name is: " + user.Value); //it's in a {key: value} format
            var userName = user.Value;

            DateTime currentTime = DateTime.Now;

            var client         = _context.Users.FirstOrDefault(u => u.Username == userName);
            var job            = _context.Jobs.FirstOrDefault(j => j.Id == value.JobId);
            var currentBalance = _context.Wallet.FirstOrDefault(w => w.UserId == job.ClientId);

            // Insert into the Offers table and modify Job state (only if there is enough balance in the Wallet)
            if (client != null && currentBalance.AvailableAmmount >= 5)
            {
                var newOffer = new Offers
                {
                    JobId          = value.JobId,
                    ClientId       = client.Id,
                    ProviderId     = value.ProviderId,
                    OfferExpiry    = value.OfferExpiry,
                    OfferTimestamp = DateTime.Now
                };
                _context.Offers.Add(newOffer);

                // Changing job state to Offer Sent
                job.JobState = 20;

                // Record change to the WalletHistory table
                var walletHistory = new WalletHistory
                {
                    WalletId = currentBalance.Id,
                    UserId   = currentBalance.UserId,
                    AvailableAmountBefore = currentBalance.AvailableAmmount,
                    BlockedAmountBefore   = currentBalance.BlockedAmmount,
                    AvailableAmountAfter  = currentBalance.AvailableAmmount - 5,
                    BlockedAmountAfter    = currentBalance.BlockedAmmount + 5,
                    TimeStamp             = currentTime
                };
                _context.WalletHistory.Add(walletHistory);

                // Modify Clients' Wallet balance
                currentBalance.AvailableAmmount = currentBalance.AvailableAmmount - 5;
                currentBalance.BlockedAmmount   = currentBalance.BlockedAmmount + 5;

                _context.SaveChanges();

                //return jobid back to Angular (technically there is not much sense returning anything appart from Ok);
                return(newOffer.Id.ToString());
            }

            // Return error
            Console.WriteLine("--Offer for the JobID " + value.JobId + " FAILED");
            this.HttpContext.Response.StatusCode = 404;
            return(null);
        }
Пример #2
0
        public IActionResult KupiIgru(int KupacID, int IgraID)
        {
            LoginInfo Log = HttpContext.Session.GetObjectFromJson <LoginInfo>("LoggedUser");

            Kupac kupac = db.Kupac.Where(k => k.OsobaID == Log.OsobaID).FirstOrDefault();

            KupacID = kupac.KupacID;

            if (ImaIgru(KupacID, IgraID))
            {
                return(RedirectToAction("GameDetailes", new { IgraID }));
            }


            Kupac Kupac = db.Kupac.Where(k => k.KupacID == KupacID)
                          .Include(w => w.Wallet)
                          .FirstOrDefault();
            Igra Igra = db.Igra.Where(i => i.IgraID == IgraID).FirstOrDefault();

            //Popust Popust = db.Popust.Where(p => p.Aktivan == true && p.IgraID == IgraID).FirstOrDefault();

            if (Igra.Cijena < Kupac.Wallet.balance)
            {
                KupacKupuje kupacKupuje = new KupacKupuje()
                {
                    Igra            = Igra,
                    Kupac           = Kupac,
                    Cijena          = Igra.Cijena,
                    VrijemeKupovine = DateTime.Now
                };
                WalletHistory walletHistory = new WalletHistory()
                {
                    WalletID          = Kupac.Wallet.WalletID,
                    IsIgra            = true,
                    TransactionAmount = (double)Igra.Cijena * (-1),
                    CurrentBalance    = Kupac.Wallet.balance - Igra.Cijena,
                    IgraID            = IgraID
                };



                db.WalletHistory.Add(walletHistory);

                RemoveWishList(IgraID, KupacID);
                Kupac.Wallet.balance -= Igra.Cijena;
                db.KupacKupuje.Add(kupacKupuje);
                db.SaveChanges();
                db.Dispose();
            }
            else
            {
                TempData["error-key"] = "<br> No monie boi";
            }
            return(RedirectToAction("GameDetailes", new { IgraID }));
        }
Пример #3
0
        public ActionResult Post([FromBody] PayPalPayment data)
        {
            // Users name (it's actually an email) - for this to work in IdentityServer in the ApiClaims must be defined name (and email)
            var jwtuser = User.Claims.Where(x => x.Type == "name").FirstOrDefault();

            Console.WriteLine("Authenticated user name is: " + jwtuser.Value); //it's in a {key: value} format
            var userName = jwtuser.Value;

            //string userName = "******"; // This value will be taken from the JWT claim

            string paymentDetails = "Ammount: " + data.ammount + ", PayerID: " + data.payerID + ", PaymentID: " + data.paymentID + ", PaymentToken: " + data.paymentToken;

            Console.WriteLine("--AddClientBalance - " + paymentDetails);

            DateTime currentTime = DateTime.Now;

            var currentBalance = (from u in _context.Users
                                  join w in _context.Wallet on u.Id equals w.UserId
                                  where u.Username == userName
                                  select w).SingleOrDefault();

            if (currentBalance != null)
            {
                Console.WriteLine("Current Balance for the " + userName + " is " + currentBalance.AvailableAmmount);

                // Update Wallet ammount
                // Note - I'm not actually using ammount value from the POST, instead a hardcoded 5 pounds
                currentBalance.AvailableAmmount = currentBalance.AvailableAmmount + 5m;

                // Record change to the WalletHistory table
                var walletHistory = new WalletHistory
                {
                    WalletId = currentBalance.Id,
                    UserId   = currentBalance.UserId,
                    AvailableAmountBefore = currentBalance.AvailableAmmount - 5m,   // Im doing a bit reverse logic here
                    BlockedAmountBefore   = currentBalance.BlockedAmmount,
                    AvailableAmountAfter  = currentBalance.AvailableAmmount,
                    BlockedAmountAfter    = currentBalance.BlockedAmmount,
                    TimeStamp             = currentTime,
                    Details = paymentDetails
                };
                _context.WalletHistory.Add(walletHistory);

                _context.SaveChanges();

                Console.WriteLine("New Balance for the " + userName + " is " + currentBalance.AvailableAmmount);
            }
            else
            {
                Console.WriteLine("Could not get a balance of the " + userName);
                return(NotFound());
            }

            return(Ok());
        }
        public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
        {
            var ids = request.Balances.Select(b => b.Id).Distinct().ToList();

            if (ids.Count != request.Balances.Count)
            {
                return(new Result(ErrorReason.DublicateWallets));
            }
            var allWallets = await dbContext
                             .Wallets
                             .AsNoTracking()
                             .Where(w => w.TelegramChatId == request.ChatId)
                             .ToListAsync(cancellationToken: cancellationToken);

            if (allWallets.Count != ids.Count || ids.Any(id => !allWallets.Any(w => w.Id == id)))
            {
                return(new Result(ErrorReason.NotAllWalletsFilled));
            }

            foreach (var balanceRecord in request.Balances)
            {
                var record = new WalletHistory
                {
                    AuthorTelegramId = request.AuthorId,
                    Date             = request.Date,
                    WalletId         = balanceRecord.Id,
                    Sum = balanceRecord.Balance
                };
                dbContext.WalletHistories.Add(record);
            }
            var saved = await dbContext.SaveChangesAsync();

            if (saved != request.Balances.Count)
            {
                return(new Result(ErrorReason.UnexpectedError));
            }
            else
            {
                return(new Result(null));
            }
        }
Пример #5
0
        public async Task <User> FillWalletAsync(string userId, int amount, DateTime at)
        {
            await BeginTransactionAsync();

            var user = await _context.Users.FindAsync(userId);

            if (user == null)
            {
                throw new BadRequestException("존재하지 않는 사용자입니다.");
            }

            user.Deposit += amount;
            _context.Users.Update(user);

            var history = new WalletHistory {
                UserId = userId, Amount = amount, At = at
            };
            await _context.WalletHistory.AddAsync(history);

            return(user);
        }
Пример #6
0
        public async Task <User> FillWalletAsync(string userId, int amount, DateTime at)
        {
            await BeginTransactionAsync();

            var user = await _context.Users.FindAsync(userId);

            if (user == null)
            {
                throw new UserNotFoundException();
            }

            user.Deposit += amount;
            _context.Users.Update(user);

            var history = new WalletHistory {
                Id = Guid.NewGuid().ToString(), UserId = userId, Amount = amount, At = at
            };

            _context.WalletHistory.Add(history);

            return(user);
        }
Пример #7
0
        public IActionResult PreuzmiIgru(int KupacID, int IgraID)
        {
            if (ImaIgru(KupacID, IgraID))
            {
                return(RedirectToAction("GameDetailes", new { IgraID }));
            }

            Kupac Kupac = db.Kupac.Where(k => k.KupacID == KupacID)
                          .Include(k => k.Wallet)
                          .FirstOrDefault();
            Igra Igra = db.Igra.Where(i => i.IgraID == IgraID).FirstOrDefault();

            if (Igra.PremiumStatus == true && Kupac.PretplacenNaPremium == true)
            {
                PreuzimanjeIgre preuzimanjeIgre = new PreuzimanjeIgre()
                {
                    Igra  = Igra,
                    Kupac = Kupac,
                    VrijemePreuzimanja = DateTime.Now
                };

                WalletHistory walletHistory = new WalletHistory()
                {
                    WalletID          = Kupac.Wallet.WalletID,
                    IsIgra            = true,
                    TransactionAmount = 0,
                    CurrentBalance    = Kupac.Wallet.balance,
                    IgraID            = IgraID
                };
                db.WalletHistory.Add(walletHistory);

                db.PreuzimanjeIgre.Add(preuzimanjeIgre);
                db.SaveChanges();
                db.Dispose();
            }


            return(RedirectToAction("GameDetailes", new { IgraID }));
        }
Пример #8
0
        public string Post([FromBody] RejectOffer value)
        {
            // Users name (it's actually an email) - for this to work in IdentityServer in the ApiClaims must be defined name (and email)
            var jwtuser = User.Claims.Where(x => x.Type == "name").FirstOrDefault();

            Console.WriteLine("Authenticated user name is: " + jwtuser.Value); //it's in a {key: value} format
            var userName = jwtuser.Value;

            Console.WriteLine("--Rejecting an Offer for the JobID: " + value.JobId);

            DateTime currentTime = DateTime.Now;

            var provider       = _context.Users.FirstOrDefault(u => u.Username == userName);
            var applications   = _context.Applications.Where(a => a.JobId == value.JobId);
            var job            = _context.Jobs.FirstOrDefault(j => j.Id == value.JobId);
            var currentBalance = _context.Wallet.FirstOrDefault(w => w.UserId == job.ClientId);
            var offer          = _context.Offers.Where(o => (o.JobId == value.JobId) &&
                                                       (o.ProviderId == provider.Id) &&
                                                       (o.AcceptanceStatus != 0)).FirstOrDefault();

            // Update Offers, Jobs and Applications table
            if (provider != null && offer != null)
            {
                // Update offer to Rejected
                offer.AcceptanceStatus    = 0;
                offer.AcceptanceTimestamp = currentTime;
                Console.WriteLine("Rejection Time stamp " + currentTime);

                // Changing job state back to Published
                job.JobState = 10;

                // Removing records from the Applications table
                foreach (var application in applications)
                {
                    Console.WriteLine("Deleting application: " + application.Id);
                    _context.Applications.Remove(application);
                }

                // Record change to the WalletHistory table
                var walletHistory = new WalletHistory
                {
                    WalletId = currentBalance.Id,
                    UserId   = currentBalance.UserId,
                    AvailableAmountBefore = currentBalance.AvailableAmmount,
                    BlockedAmountBefore   = currentBalance.BlockedAmmount,
                    AvailableAmountAfter  = currentBalance.AvailableAmmount + 5,
                    BlockedAmountAfter    = currentBalance.BlockedAmmount - 5,
                    TimeStamp             = currentTime
                };
                _context.WalletHistory.Add(walletHistory);

                // Update Clients' Wallet balance
                currentBalance.AvailableAmmount = currentBalance.AvailableAmmount + 5;
                currentBalance.BlockedAmmount   = currentBalance.BlockedAmmount - 5;

                // Commit changes
                _context.SaveChanges();

                // Sending an SMS message to the Client informing that the Offer has been rejected
                Console.WriteLine("Sending an SMS to inform about Offer Rejection to the tel: " + job.ContactTelephone1);

                //return jobid back to Angular (technically there is not much sense returning anything appart from Ok);
                return(offer.Id.ToString());
            }

            // Return error
            Console.WriteLine("--Offer Rejectino for the JobID " + value.JobId + " FAILED");
            this.HttpContext.Response.StatusCode = 404;
            return(null);
        }
Пример #9
0
        public string Post([FromBody] AcceptOffer value)
        {
            // Users name (it's actually an email) - for this to work in IdentityServer in the ApiClaims must be defined name (and email)
            var jwtuser = User?.Claims?.Where(x => x.Type == "name").FirstOrDefault();

            Console.WriteLine("Authenticated user name is: " + (jwtuser == null ? "unknown" : jwtuser.Value)); //it's in a {key: value} format

            // To make unit testing of the method simple
            #if DEBUG
            Console.WriteLine("Debug mode (Unit Testing)");
            var userName = "******";
            #else
            var userName = jwtuser.Value;     // value is taken from the JWT claim
            #endif

            DateTime currentTime = DateTime.Now;

            var provider       = _context.Users.FirstOrDefault(u => u.Username == userName);
            var applications   = _context.Applications.Where(a => a.JobId == value.JobId);
            var job            = _context.Jobs.FirstOrDefault(j => j.Id == value.JobId);
            var currentBalance = _context.Wallet.FirstOrDefault(w => w.UserId == job.ClientId);
            var offer          = _context.Offers.Where(o => (o.JobId == value.JobId) &&
                                                       (o.ProviderId == provider.Id) &&
                                                       (o.AcceptanceStatus != 0)).FirstOrDefault();

            // Update Offers, Jobs, Applications and Wallet tables
            if (provider != null && offer != null)
            {
                // Update offer to Accepted
                offer.AcceptanceStatus    = 1;
                offer.AcceptanceTimestamp = currentTime;
                Console.WriteLine("Acceptance Time stamp " + currentTime);

                // Changing job state to Accepted
                job.JobState = 40;

                // Removing records from the Applications table
                foreach (var application in applications)
                {
                    Console.WriteLine("Deleting application: " + application.Id);
                    _context.Applications.Remove(application);
                }

                // Record Wallet changes to the WalletHistory table
                var walletHistory = new WalletHistory
                {
                    WalletId = currentBalance.Id,
                    UserId   = currentBalance.UserId,
                    AvailableAmountBefore = currentBalance.AvailableAmmount,
                    BlockedAmountBefore   = currentBalance.BlockedAmmount,
                    AvailableAmountAfter  = currentBalance.AvailableAmmount,
                    BlockedAmountAfter    = currentBalance.BlockedAmmount - 5,
                    TimeStamp             = currentTime
                };
                _context.WalletHistory.Add(walletHistory);

                // Update Clients' Wallet balance
                currentBalance.BlockedAmmount = currentBalance.BlockedAmmount - 5;

                // Commit changes
                _context.SaveChanges();

                // Sending an SMS message to the Client informing that the Offer is Accepted
                Console.WriteLine("Sending an SMS to inform about Offer acception to the tel: " + job.ContactTelephone1);

                //return jobid back to Angular (technically there is not much sense returning anything appart from Ok);
                return(offer.Id.ToString());
            }

            // Return error
            Console.WriteLine("--Offer Acceptance for the JobID " + value.JobId + " FAILED");
            this.HttpContext.Response.StatusCode = 404;
            return(null);
        }