public async Task <Result <Customer> > FindCustomerAsync(FindCustomerOptions options) { var q = _dbContext.Set <Customer>() .AsQueryable(); if (!string.IsNullOrWhiteSpace(options.Name)) { q = q.Where(c => c.Name == options.Name); } if (!string.IsNullOrWhiteSpace(options.Surname)) { q = q.Where(c => c.Surname == options.Surname); } if (!string.IsNullOrWhiteSpace(options.VatNumber)) { q = q.Where(c => c.VatNumber == options.VatNumber); } if (options.CustomerId != 0) { q = q.Where(c => c.CustomerId == options.CustomerId); } var result = await q.SingleOrDefaultAsync(); if (result == null) { return(new Result <Customer>() { Data = null, Code = Constants.ResultCode.NotFound }); } return(new Result <Customer>() { Data = result, Code = Constants.ResultCode.Success }); }
// Note: Initial implemementation. Will be modified // to return a more structured and informative type. public async Task <Result <Transaction> > CreateTransactionAsync(CreateTransactionOptions options) { if (string.IsNullOrWhiteSpace(options.AccountId)) { return(new Result <Transaction>() { ErrorMessage = "AccountId is empty", Code = Constants.ResultCode.BadRequest }); } if (options.TrnAmount == 0) { return(new Result <Transaction>() { ErrorMessage = "TrnAmount is empty", Code = Constants.ResultCode.BadRequest }); } if (options.Category == 0) { return(new Result <Transaction>() { ErrorMessage = "Category is empty", Code = Constants.ResultCode.BadRequest }); } if (options.CustomerId == 0) { return(new Result <Transaction>() { ErrorMessage = "CustomerId is empty", Code = Constants.ResultCode.BadRequest }); } //υπάρχει καταχωρημένος ο πελάτης? var opt = new FindCustomerOptions() { CustomerId = options.CustomerId }; var q = new CustomerService(_dbContext).FindCustomerAsync(opt).Result.Data; if (q == null) { return(new Result <Transaction>() { ErrorMessage = "Customer not registered", Code = Constants.ResultCode.NotFound }); } //υπάρχει καταχωρημένος ο λογαριασμός? var acc = new AccountService(_dbContext).FindAccountAsync(options.AccountId).Result.Data; if (acc == null) { return(new Result <Transaction>() { ErrorMessage = "Account not registered", Code = Constants.ResultCode.NotFound }); } if (options.Category.Equals(TrnCategory.Pistwsh)) { var trn = new Transaction { AccountId = options.AccountId, TrnAmount = options.TrnAmount, Category = options.Category }; acc.AddTrn(trn); _dbContext.Add(trn); try { await _dbContext.SaveChangesAsync(); } catch (Exception) { return(new Result <Transaction>() { Code = Constants.ResultCode.InternalServerError, ErrorMessage = "Transaction could not be saved" }); } return(new Result <Transaction>() { Data = trn, Code = Constants.ResultCode.Success, }); } if (options.Category.Equals(TrnCategory.Xrewsh) & (acc.TotalAmount - options.TrnAmount >= 0) & (q.CustomerId == acc.CustomerId)) { var trn = new Transaction { AccountId = options.AccountId, TrnAmount = options.TrnAmount, Category = options.Category }; acc.AddTrn(trn); _dbContext.Add(trn); try { await _dbContext.SaveChangesAsync(); } catch (Exception) { return(new Result <Transaction>() { Code = Constants.ResultCode.InternalServerError, ErrorMessage = "Transaction could not be saved" }); } return(new Result <Transaction>() { Data = trn, Code = Constants.ResultCode.Success, }); } else { return(new Result <Transaction>() { Code = Constants.ResultCode.InternalServerError, ErrorMessage = "Transaction could not be saved" }); } }
public async Task <Result <Account> > CreateAccountAsync(CreateAccountOptions options) { if (string.IsNullOrWhiteSpace(options.AccountId)) { //turn null; return(new Result <Account>() { ErrorMessage = "AccountId is required", Code = Constants.ResultCode.BadRequest }); } if (options.CustomerId == 0) { return(new Result <Account>() { ErrorMessage = "Customer is required", Code = Constants.ResultCode.BadRequest }); } var opt = new FindCustomerOptions() { CustomerId = options.CustomerId }; var q = new CustomerService(_dbContext).FindCustomerAsync(opt).Result.Data; //var q = _customers.FindCustomer(opt); if (q == null) { return(new Result <Account>() { ErrorMessage = "Customer is not registered", Code = Constants.ResultCode.NotFound }); } var account = new Account() { AccountId = options.AccountId, CustomerId = options.CustomerId }; _dbContext.Add(account); try { await _dbContext.SaveChangesAsync(); } catch (Exception) { return(new Result <Account>() { Code = Constants.ResultCode.InternalServerError, ErrorMessage = "Account could not be saved" }); } return(new Result <Account>() { Code = Constants.ResultCode.Success, Data = account }); }