public async Task <bool> PUT(CompanyResource companyResource) { using (var entity = new CompanyBrokerResourcesEntities()) { //-- Fetches the resource based on the informations var resource = entity.CompanyResources.Where(r => r.CompanyId == companyResource.CompanyId && r.ResourceId == companyResource.ResourceId).Single <CompanyResource>(); //- Checks if it's null if (resource != null) { //-- Changes all the values resource.Price = companyResource.Price; resource.ProductName = companyResource.ProductName; resource.ProductType = companyResource.ProductType; resource.Amount = companyResource.Amount; resource.Active = companyResource.Active; //-- tells the framework that we made changes entity.Entry(resource).State = EntityState.Modified; //-- Saves the changes await entity.SaveChangesAsync(); return(true); } else { return(false); } } }
public async Task <IList <CompanyResource> > GetResourcesByCompanyId(IEnumerable <int> companyId) { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerResourcesEntities()) { return(await entitys.CompanyResources.Where(r => companyId.Any(i => i == r.CompanyId)).ToListAsync()); } }
public async Task <IList <string> > GetAllProductNamesByTypes(IEnumerable <string> productTypes) { //-- connecting to the database using (var entitys = new CompanyBrokerResourcesEntities()) { return(await entitys.CompanyResources.Where(p => productTypes.Any(t => t == p.ProductType)).Select(x => x.ProductName).ToListAsync()); } }
public async Task <CompanyResource> Get(int companyId, string productName) { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerResourcesEntities()) { return(await entitys.CompanyResources.FirstOrDefaultAsync(a => a.CompanyId == companyId && a.ProductName.ToLower() == productName.ToLower())); } }
public async Task <IList <CompanyResource> > Get() { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerResourcesEntities()) { //-- Fetches the account list return(await entitys.CompanyResources.ToListAsync()); } }
public async Task <IList <string> > GetProductAllTypesAsync() { ////-- opens an connection to the database using (var entitys = new CompanyBrokerResourcesEntities()) { //-- fetches all resources, for product types only, and only want one of each. return(await entitys.CompanyResources.Select(pt => pt.ProductType).Distinct().ToListAsync()); } }
public async Task <IList <CompanyResource> > Get(string searchWord) { //-- opens an connection to the database with the entity using (var entitys = new CompanyBrokerResourcesEntities()) { //-- Fetches the collection based on the words in product name and type return(await entitys.CompanyResources.Where(r => r.ProductName.ToLower().Contains(searchWord.ToLower()) || r.ProductType.ToLower().Contains(searchWord.ToLower())).ToListAsync()); } }
public async Task <bool> ChangeCompanyResourceAmount(ResourceChangeAmountModelRequest resourceChangeModel) { if (resourceChangeModel != null) { using (var entity = new CompanyBrokerResourcesEntities()) { //-- Fetches the resource based on the informations var resource = entity.CompanyResources.Where(c => c.CompanyId == resourceChangeModel.companyId && c.ResourceId == resourceChangeModel.resourceId).Single <CompanyResource>(); //- Checks if it's null if (resource != null) { if (resourceChangeModel.IncreaseAmount != false) { //-- Changes the values resource.Amount++; //-- tells the framework that we made changes entity.Entry(resource).State = EntityState.Modified; //-- Saves the changes await entity.SaveChangesAsync(); //-- returns return(true); } else { if (resource.Amount > 0) { //-- Changes the values resource.Amount--; //-- tells the framework that we made changes entity.Entry(resource).State = EntityState.Modified; //-- Saves the changes await entity.SaveChangesAsync(); //-- returns return(true); } else { return(false); } } } else { return(false); } } } else { return(false); } }
public async Task <CompanyResource> Get(int id) { using (var entitys = new CompanyBrokerResourcesEntities()) { var responseResource = await entitys.CompanyResources.FirstOrDefaultAsync(p => p.ResourceId == id); return(new CompanyResource() { ProductName = responseResource.ProductName, ResourceId = responseResource.ResourceId, ProductType = responseResource.ProductType, Price = responseResource.Price, Amount = responseResource.Amount, Active = responseResource.Active, CompanyId = responseResource.CompanyId }); } }
public async Task <bool> Delete(int companyId, string productName, int resourceId) { using (var entity = new CompanyBrokerResourcesEntities()) { var resource = await entity.CompanyResources.FirstOrDefaultAsync(r => r.CompanyId == companyId && r.ProductName.ToLower() == productName.ToLower() && r.ResourceId == resourceId); if (resource != null) { entity.CompanyResources.Remove(resource); entity.Entry(resource).State = EntityState.Deleted; await entity.SaveChangesAsync(); return(true); } else { return(false); } } }
public async Task <bool> Post(CompanyResource resource) { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerResourcesEntities()) { //-- Verifys the content if (resource != null) { //-- Adds the content entitys.CompanyResources.Add(resource); await entitys.SaveChangesAsync(); return(true); } else { return(false); } } }
public async Task <IList <CompanyResource> > GetResourcesByListFilters([FromUri] CollectionFilterRequest collectionFilterRequest) { //-- Result Url = localhost:50133/api/GetResourcesByListFilters?CompanyChoices[0]=1&CompanyChoices[1]=2&ProductTypeChoices[0]=Martin //-- The new resource list var resourceList = new List <CompanyResource>(); //-- Values from the CollectionFilterRequest - Null condition operator if null var productNames = collectionFilterRequest?.ProductNameChoices ?? new string[] { }; var productTypes = collectionFilterRequest?.ProductTypeChoices ?? new string[] { }; var companyIds = collectionFilterRequest?.CompanyChoices ?? new int[] { }; var searchWords = collectionFilterRequest?.SearchWord ?? string.Empty; var LowestPrice = collectionFilterRequest?.LowestPriceChoice ?? 0; var HigestPirce = collectionFilterRequest?.HigestPriceChoice ?? 0; var ResourceIsActive = collectionFilterRequest?.ResourceActive ?? false; var PartnersOnly = collectionFilterRequest?.Partners_OnlyChoice ?? false; //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerResourcesEntities()) { //-- Fetching the results. - As queryAble, lets the database struggle with the filtering than the application var results = entitys.CompanyResources.AsQueryable(); //-- Filtering the list depending on the inputs from the collectionListRequest, and checking if they have any value if (productNames.Any()) { results = results.Where(r => productNames.Contains(r.ProductName)); } if (productTypes.Any()) { results = results.Where(r => productTypes.Contains(r.ProductType)); } if (companyIds.Any()) { results = results.Where(r => companyIds.Contains(r.CompanyId)); } if (LowestPrice != 0) { results = results.Where(r => LowestPrice < r.Price); } if (HigestPirce != 0) { results = results.Where(r => HigestPirce > r.Price); } if (ResourceIsActive != false) { results = results.Where(r => r.Active == ResourceIsActive); } //-- word filtering if (!string.IsNullOrWhiteSpace(searchWords)) { //-- Splitting the word by spaces, into array for propper filtering var Words = searchWords.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //-- Filtering the results based on the array of words results = results.Where(r => Words.Any(s => r.ProductName.ToLower().Contains(s) || r.ProductType.ToLower().Contains(s))); } //-- Sets the resource list resourceList = await results.ToListAsync(); //-- returns it return(resourceList); } }