public async Task <IActionResult> PutCompany(long id, Company company)
        {
            if (id != company.CompanyId)
            {
                return(BadRequest());
            }
            if (CheckIfDataIsFull(company))
            {
                _context.Entry(company).State = EntityState.Modified;

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CompanyExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(NoContent());
        }
        public async Task <IHttpActionResult> PutCompany(int id, Company company)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != company.companyID)
            {
                return(BadRequest());
            }

            db.Entry(company).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CompanyExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates the ExchangeIDs of a given CopmanyID
        /// </summary>
        /// <param name="companyId">the CompanyId</param>
        /// <param name="exchangeIds">the new exchange Id's that should be related to the companyId</param>
        /// <returns>The updated list of company exchanges</returns>
        public async Task <List <CompanyExchange> > UpdateCompanyExchanges(int companyId, List <int> exchangeIds)
        {
            //Get current existing
            var companyExchanges = await this.GetCompanyExchangesByCompanyId(companyId);

            //Exchange Ids in CompanyExchange that be removed as were deleted/overwritten
            List <CompanyExchange> removedCompanyExchanges = companyExchanges.Where(e => !exchangeIds.Contains(e.ExchangeId)).ToList();

            dbContext.RemoveRange(removedCompanyExchanges);

            //Newly added exchange Ids
            List <int>             newCompanyExchanges = exchangeIds.Where(e => !companyExchanges.Select(ce => ce.ExchangeId).ToList().Contains(e)).ToList();
            List <CompanyExchange> newExchanges        = new List <CompanyExchange>();

            foreach (int exchangeId in newCompanyExchanges)
            {
                newExchanges.Add(new CompanyExchange {
                    CompanyId = companyId, ExchangeId = exchangeId
                });
            }

            await dbContext.AddRangeAsync(newExchanges);

            await dbContext.SaveChangesAsync();

            return(await this.GetCompanyExchangesByCompanyId(companyId));
        }