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); } }
public async Task <bool> CreateCompany(CreateCompanyRequest createCompanyRequest) { //-- the result wheter or not the creation was successfull bool resultProcess = false; //-- checks if the company request is null if (createCompanyRequest != null) { //-- new instance of a Company var company = new Company() { CompanyName = createCompanyRequest.CompanyName, CompanyBalance = 0, Active = true }; //-- Connecting to the database using the CompaniesEntities using (var entitys = new CompanyBrokerCompaniesEntities()) { //-- adds a new company to the Companies table entitys.Companies.Add(company); //-- Saves the changes to the database await entitys.SaveChangesAsync(); //-- Sets the result process to true resultProcess = true; } } //-- returns the result return(resultProcess); }