Example #1
0
        public int CreateTransaction(CreateTransactionInputModel model)
        {
            var transaction = new Transaction();
            var receiver    = db.Addresses.FirstOrDefault(c => c.Id == model.ReceiverId);
            var sender      = db.Addresses.FirstOrDefault(c => c.Id == model.SenderId);
            var token       = db.Tokens.FirstOrDefault(c => c.Id == model.TokenId);

            if (receiver == null)
            {
                return(0);
            }
            if (sender == null)
            {
                return(-1);
            }
            if (token == null)
            {
                return(-2);
            }

            token.Address        = receiver;
            transaction.Receiver = receiver;
            transaction.Sender   = sender;

            db.Add(transaction);
            db.Update(token);
            db.SaveChanges();

            return(transaction.Id);
        }
Example #2
0
        public async Task <CreateTransactionInputModel> CreateTransactionInputModel(string userId)
        {
            var users = await this.usersRepository.GetAllUsers();

            var model = new CreateTransactionInputModel
            {
                SenderId = userId,
                Users    = users.Where(u => u.Id != userId).Select(u => new CreateTransactionUserInputModel
                {
                    Id       = u.Id,
                    Username = u.Email
                })
            };

            return(model);
        }
        public async Task <IActionResult> Create(CreateTransactionInputModel inputModel)
        {
            var receiverResult = await this.usersService.GetUserById(inputModel.ReceiverId);

            if (receiverResult.Data.PhoneNumber != inputModel.RecieverPhoneNumber)
            {
                string userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                var    model  = await this.transactionsService.CreateTransactionInputModel(userId);

                ModelState.AddModelError("RecieverPhoneNumber", "Incorrect receiver phone number!");

                return(this.View(model));
            }

            var senderResult = await this.usersService.GetUserById(inputModel.SenderId);

            if (senderResult.Data.Credits < inputModel.Credits)
            {
                string userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                var    model  = await this.transactionsService.CreateTransactionInputModel(userId);

                ModelState.AddModelError("Credits", "You don't have enough credits for this transaction!");

                return(this.View(model));
            }

            if (!ModelState.IsValid)
            {
                string userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                var    model  = await this.transactionsService.CreateTransactionInputModel(userId);

                return(this.View(model));
            }

            await this.transactionsService.CreateTransaction(inputModel.SenderId, inputModel.ReceiverId, inputModel.Wish, inputModel.Credits);

            return(this.RedirectToAction("Index", "Home"));
        }
        public IActionResult CreateTransaction([FromBody] CreateTransactionInputModel model)
        {
            var id = this.mainService.CreateTransaction(model);

            return(this.Ok(id));
        }