public async Task <ActionResult <BestPractice> > PostBestPractice([FromBody] BestPractice bp)
        {
            string[] comps = removeChars(bp.Company).Split(",");
            string   dept  = removeChars(bp.Department);

            bp.Department = dept;
            db.BestPractice.Add(bp);
            int id = bp.BestPracticeId;
            // addCompanies
            IList <Company> companyList = new List <Company>();

            for (int i = 0; i < comps.Count(); i++)
            {
                companyList.Add(db.Company.Where(entity => entity.Name.Equals(comps[i], StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault());
            }
            for (int i = 0; i < companyList.Count(); i++)
            {
                db.BestPracticeCompany.Add(new BestPracticeCompany {
                    BestPracticeId = id,
                    CompanyId      = companyList[i].CompanyId
                });
            }
            await db.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetBestPractice), new { id }, bp));
        }
Beispiel #2
0
        public async Task <IActionResult> DeleteBestPracticeCompany(int id)
        {
            var bpc = await db.BestPracticeCompany
                      .Where(x => x.BestPracticeId == id)
                      .Select(x => x)
                      .ToListAsync();

            if (bpc == null)
            {
                return(NotFound());
            }
            foreach (BestPracticeCompany bpc_id in bpc)
            {
                db.BestPracticeCompany.Remove(bpc_id);
            }
            await db.SaveChangesAsync();

            return(NoContent());
        }