public async Task <Result <Account> > RegisterAccountAsync(int customerId, RegisterAccountOptions options) { if (string.IsNullOrWhiteSpace(options.Currency)) { return new Result <Account>() { Code = ResultCodes.BadRequest, Message = $"Currency is empty!" } } ; if (string.IsNullOrWhiteSpace(options.Description)) { return new Result <Account>() { Code = ResultCodes.BadRequest, Message = $"Einai ipoxreotiko na doseis perigrafi" } } ; var customer = (await _customer.GetCustomerByIdAsync(customerId)).Data; if (customer != null) { var account = new Account() { Currency = options.Currency, Description = options.Description }; customer.Accounts.Add(account); _dBContext.Update <Customer>(customer); await _dBContext.SaveChangesAsync(); return(new Result <Account>() { Code = ResultCodes.Success, Data = account }); } else { return(new Result <Account>() { Code = ResultCodes.BadRequest, Message = $"Den iparxei o pelatis me kodiko {customerId}." }); } }
public async Task <Result <Transaction> > RegisterTranactionAsync(int accountId, RegisterTransactionOptions options) { if (options.Amount < 0) { return new Result <Transaction>() { Code = ResultCodes.BadRequest, Message = $"To poso pou edoses einai < 0." } } ; var account = (await _account.GetAccountByIdAsync(accountId)).Data; if (options.Type == TransactionType.Debit) { if (options.Amount > account.Balance) { return(new Result <Transaction>() { Code = ResultCodes.BadRequest, Message = $"Thes na xreoseis {options.Amount} evro eno o logariamos exei {account.Balance}. Akiro." }); } } if (account != null) { var transaction = new Transaction() { Type = options.Type, Amount = options.Amount }; account.Transactions.Add(transaction); account.Balance = account.Balance + ((int)options.Type) * options.Amount; _dBContext.Update <Account>(account); await _dBContext.SaveChangesAsync(); return(new Result <Transaction>() { Code = ResultCodes.Success, Data = transaction }); } else { return(new Result <Transaction>() { Code = ResultCodes.BadRequest, Message = $"Den vrethike account me kodiko {accountId}" }); } }
public async Task <Result <Customer> > RegisterCustomerAsync(RegisterCustomerOptions options) { if (string.IsNullOrWhiteSpace(options.Name)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Customer name is empty!" } } ; if (string.IsNullOrWhiteSpace(options.SurName)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Customer sure name is empty!" } } ; if (options.VatNumber.Length != 9) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"VAT Number length is invalid!" } } ; long vatNumber = 0; if (!long.TryParse(options.VatNumber, out vatNumber)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"VAT number must be numeric!" } } ; var customer = new Customer() { Name = options.Name, SurName = options.SurName, VatNumber = options.VatNumber, CustType = options.CustType, Address = options.Address }; await _dbContext.AddAsync <Customer>(customer); await _dbContext.SaveChangesAsync(); return(new Result <Customer>() { Code = ResultCodes.Success, Data = customer }); }