Esempio n. 1
0
        public async Task <ActionResult <MessageModel> > CreateDebitCard(ClaimsPrincipal currentUser, string username, ChargeAccount bankAccount, Card card)
        {
            string role = "";

            if (currentUser.HasClaim(c => c.Type == "Roles"))
            {
                string userRole = currentUser.Claims.FirstOrDefault(currentUser => currentUser.Type == "Roles").Value;
                role = userRole;
            }

            if (role == "Admin")
            {
                var userAuthenticate = await dbContext.Users.FirstOrDefaultAsync(x => x.Username == username);

                Card          cardExists        = null;
                ChargeAccount bankAccountExists = null;

                if (userAuthenticate != null)
                {
                    try
                    {
                        bankAccountExists = await dbContext.ChargeAccounts.FirstOrDefaultAsync(x => x.Iban == bankAccount.Iban);

                        if (bankAccountExists == null)
                        {
                            responseMessage.Message = "No Bank Account found! Invalid Iban!";
                            return(StatusCode(404, responseMessage));
                        }
                        cardExists = await dbContext.Cards.FirstOrDefaultAsync(x => x.ChargeAccountId == bankAccountExists.Id);
                    }
                    catch (NullReferenceException)
                    {
                        responseMessage.Message = "No Bank Account found! Invalid Iban!";
                        return(StatusCode(404, responseMessage));
                    }
                }


                if (cardExists == null)
                {
                    try
                    {
                        if (ValidateUser(userAuthenticate))
                        {
                            if (card == null)
                            {
                                card = new Card();
                            }

                            card.ChargeAccountId = bankAccountExists.Id;
                            card.CardNumber      = GenerateCardInfo.GenerateNumber(11);
                            var CVV = GenerateCardInfo.GenerateCVV(3);
                            card.Cvv = _BCrypt.HashPassword(CVV);
                            card.CardExpirationDate = DateTime.Now.AddMonths(60);
                            dbContext.Add(card);
                            await dbContext.SaveChangesAsync();

                            responseMessage.Message = "Debit Card created succesfully!";
                            return(StatusCode(200, responseMessage));
                        }
                        else if (ValidateUser(userAuthenticate) == false)
                        {
                            responseMessage.Message = "User not found!";
                            return(StatusCode(404, responseMessage));
                        }
                    }
                    catch (NullReferenceException)
                    {
                        responseMessage.Message = "User not found!";
                        return(StatusCode(404, responseMessage));
                    }
                }

                responseMessage.Message = "User already has a Debit Card!";
                return(StatusCode(400, responseMessage));
            }
            else
            {
                responseMessage.Message = "You are not autorized to do such actions!";
                return(StatusCode(403, responseMessage));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult <MessageModel> > CreateWallet(ClaimsPrincipal currentUser, WalletRequestModel requestModel)
        {
            string role     = "";
            var    username = requestModel.Username;
            Wallet wallet   = requestModel.Wallet;
            BCryptPasswordHasher _BCrypt = new BCryptPasswordHasher();

            if (currentUser.HasClaim(c => c.Type == "Roles"))
            {
                string userRole = currentUser.Claims.FirstOrDefault(currentUser => currentUser.Type == "Roles").Value;
                role = userRole;
            }

            if (role == "Admin")
            {
                var userAuthenticate = await dbContext.Users.FirstOrDefaultAsync(x => x.Username == username);

                if (userAuthenticate != null)
                {
                    try
                    {
                        if (dbContext.Wallets.Where(x => x.UserId == userAuthenticate.Id).Count() < 7)
                        {
                            if (ValidateUser(userAuthenticate) && ValidateWallet(wallet))
                            {
                                wallet.UserId     = userAuthenticate.Id;
                                wallet.Iban       = IBANGenerator.GenerateIBANInVitoshaBank("Wallet", dbContext);
                                wallet.CardNumber = GenerateCardInfo.GenerateNumber(11);
                                var CVV = GenerateCardInfo.GenerateCVV(3);
                                wallet.Cvv = (_BCrypt.HashPassword(CVV));
                                wallet.CardExpirationDate = DateTime.Now.AddMonths(60);

                                await dbContext.AddAsync(wallet);

                                await dbContext.SaveChangesAsync();

                                SendEmail(userAuthenticate.Email, _config);
                                responseMessage.Message = "Wallet created succesfully!";
                                return(StatusCode(200, responseMessage));
                            }
                            else if (ValidateUser(userAuthenticate) == false)
                            {
                                responseMessage.Message = "User not found!";
                                return(StatusCode(404, responseMessage));
                            }
                            else if (ValidateWallet(wallet) == false)
                            {
                                responseMessage.Message = "Don't put negative value!";
                                return(StatusCode(400, responseMessage));
                            }
                        }
                    }
                    catch (NullReferenceException)
                    {
                        responseMessage.Message = "User not found!";
                        return(StatusCode(404, responseMessage));
                    }
                }

                responseMessage.Message = "User already has 7 wallets!";
                return(StatusCode(400, responseMessage));
            }
            else
            {
                responseMessage.Message = "You are not autorized to do such actions!";
                return(StatusCode(403, responseMessage));
            }
        }