public async Task <bool> ChangeCompanyBalance(CompanyChangeBalanceRequest companyBalanceRequest) { if (companyBalanceRequest != null) { using (var entity = new CompanyBrokerCompaniesEntities()) { //-- Fetches an company based on the CompanyId var company = entity.Companies.Where(c => c.CompanyId == companyBalanceRequest.companyId).Single <Company>(); //-- Checks if the company is null if (company != null) { //-- Checks wheter or not we want to increase or decrease an balance if (companyBalanceRequest.increaseBalance != false) { //-- Changes the values company.CompanyBalance = company.CompanyBalance + companyBalanceRequest.price; //-- Tells the framework that there has been an change entity.Entry(company).State = EntityState.Modified; //-- Saves the changes await entity.SaveChangesAsync(); return(true); } else { if (company.CompanyBalance > 0) { //-- Changes the values company.CompanyBalance = company.CompanyBalance - companyBalanceRequest.price; //-- Tells the framework that there has been an change entity.Entry(company).State = EntityState.Modified; //-- Saves the changes await entity.SaveChangesAsync(); return(true); } else { return(false); } } } else { return(false); } } } else { return(false); } }