public async Task <ActionResult <UserWithToken> > Login([FromBody] Customer customer)
        {
            //    string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
            //    password: customer.Password,
            //    salt: salt,
            //    prf: KeyDerivationPrf.HMACSHA1,
            //    iterationCount: 10000,
            //    numBytesRequested: 256 / 8));
            // Console.WriteLine($"Hashed: {hashed}");

            customer = await _context.Customers
                       .Where(user => user.Email == customer.Email && user.Password == customer.Password)
                       .FirstOrDefaultAsync();

            UserWithToken userWithToken = null;

            if (customer != null)
            {
                RefreshToken refreshToken = GenerateRefreshToken();
                customer.RefreshTokens.Add(refreshToken);
                await _context.SaveChangesAsync();

                userWithToken = new UserWithToken(customer);
                userWithToken.RefreshToken = refreshToken.Token;
            }
            if (userWithToken == null)
            {
                return(NotFound());
            }

            //sign your token here
            userWithToken.AccessToken = GenerateAccessToken(customer.Id);

            return(userWithToken);
        }
        public async Task <ActionResult <Role> > AddRole(Role role)
        {
            var addRole = new Role
            {
                RoleName    = role.RoleName,
                Description = role.Description,
                TimeCreate  = DateTime.Now
            };

            _context.Roles.Add(addRole);;
            await _context.SaveChangesAsync();

            //return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
            return(CreatedAtAction("GetRole", new { id = addRole.RoleId }, addRole));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

            // _context.Entry(todoItem).State = EntityState.Modified;
            var cus = await _context.Customers.FindAsync(id);

            if (cus == null)
            {
                return(NotFound());
            }

            cus.Name       = customer.Name;
            cus.Lastname   = customer.Lastname;
            cus.Phone      = customer.Phone;;
            cus.Address    = customer.Address;
            cus.Status     = customer.Status;
            cus.TimeUpdate = DateTime.Now;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!CustomerExists(id))
            {
                return(NotFound());
            }
            return(NoContent());
        }
        public async Task <IActionResult> PutTransaction(int id, Transaction transaction)
        {
            if (id != transaction.TransId)
            {
                return(BadRequest());
            }

            // _context.Entry(todoItem).State = EntityState.Modified;
            var tran = await _context.Transactions.FindAsync(id);

            if (tran == null)
            {
                return(NotFound());
            }

            tran.WalletId    = transaction.WalletId;
            tran.WalletNo    = transaction.WalletNo;
            tran.Amount      = transaction.Amount;
            tran.Destination = transaction.Destination;
            tran.Type        = transaction.Type;
            tran.TimeCreate  = DateTime.Now;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!TransactionExists(id))
            {
                return(NotFound());
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutWallet(string walletNo, Wallet wallet)
        {
            if (walletNo != wallet.WalletNo)
            {
                return(BadRequest());
            }

            // _context.Entry(todoItem).State = EntityState.Modified;
            var wal = await _context.Wallets.Where(x => x.WalletNo == walletNo).FirstOrDefaultAsync();

            if (wal == null)
            {
                return(NotFound());
            }

            wal.Balance      = wallet.Balance;
            wal.WalletStatus = wallet.WalletStatus;
            wal.TimeUpdate   = DateTime.Now;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!WalletExists(wal.WalletId))
            {
                return(NotFound());
            }

            return(NoContent());
        }