Exemple #1
0
        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);
            }
        }
Exemple #2
0
        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);
                }
            }
        }
Exemple #3
0
        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);
                }
            }
        }
Exemple #4
0
        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);
                }
            }
        }