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); }
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 <CompanyResponse> Get(int id) { //-- Uses the CompanyBrokerCompaniesEntities to connect to the database using (var entitys = new CompanyBrokerCompaniesEntities()) { //-- Fetches an company based on the Id user has entered var company = await entitys.Companies.FirstOrDefaultAsync(c => c.CompanyId == id); //-- creates the new companyResponse based on the information fetched //-- Returns the results if (company != null) { return(new CompanyResponse(company)); } else { return(null); } } }
public async Task <IList <CompanyResponse> > Get() { //-- Creates new list with content of CompanyResponse var companyList = new List <CompanyResponse>(); //-- Uses the CompanyBrokerCompaniesEntities to connect to the database using (var entitys = new CompanyBrokerCompaniesEntities()) { //-- Fetches all companies var companies = await entitys.Companies.ToListAsync(); //-- Loops the results foreach (Company company in companies) { //-- Adds the restult through the model, to remove sensitive data companyList.Add(new CompanyResponse(company)); } } //-- returns the list return(companyList); }