public HttpResponseMessage PutCheckListTemplateItem(int id, checklist_template_xref data)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != data.checklist_template_xref_id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            db.Entry(data).State = System.Data.Entity.EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public checklist_template_xref GetCheckListTemplateItem(int id)
        {
            checklist_template_xref item = db.CheckListTemplatesXref.Find(id);

            if (item == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return(item);
        }
        public HttpResponseMessage DeleteCheckListTemplateItem(int id)
        {
            checklist_template_xref data = db.CheckListTemplatesXref.Find(id);

            if (data == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            db.CheckListTemplatesXref.Remove(data);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, data));
        }
        public HttpResponseMessage PostSaveCheckListTemplateMapping(JObject paramList)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            short checklist_template_id = Convert.ToInt16(paramList["checklist_template_id"].ToString());

            var checklistitemsData = Newtonsoft.Json.JsonConvert.DeserializeObject(paramList["datatoadd"].ToString());
            List <checklist_template_xref> addchecklistItemsData = new List <checklist_template_xref>();

            foreach (var item in (JArray)checklistitemsData)
            {
                checklist_template_xref row = new checklist_template_xref();
                row.checklist_template_id = checklist_template_id;
                row.checklist_id          = Convert.ToInt16(((JValue)(item["checklist_id"])).Value);

                //Get default powershell settings from check_list_ref
                checklist_ref chkref = db.CheckLists.Find(row.checklist_id);
                if (chkref != null)
                {
                    row.powershell_script          = chkref.default_powershell_script;
                    row.powershell_script_timedout = chkref.default_powershell_script_timedout;
                }

                addchecklistItemsData.Add(row);
            }

            var checklistitemsDataToDelete = Newtonsoft.Json.JsonConvert.DeserializeObject(paramList["datatodelete"].ToString());


            List <checklist_template_xref> deletechecklistItemsData = new List <checklist_template_xref>();

            foreach (var item in (JArray)checklistitemsDataToDelete)
            {
                checklist_template_xref row = new checklist_template_xref();
                row.checklist_template_id = checklist_template_id;
                row.checklist_id          = Convert.ToInt16(((JValue)(item["checklist_id"])).Value);
                deletechecklistItemsData.Add(row);
            }


            try
            {
                db.SaveChanges();

                /*************************************************************************************
                *  //Delete items
                *************************************************************************************/
                var checkTemplateXreflocal = (from chktempxref in db.CheckListTemplatesXref
                                              where chktempxref.checklist_template_id == checklist_template_id
                                              select chktempxref).ToList();

                var recordsToRemove_UnMapped = (from chktmpxref in checkTemplateXreflocal
                                                where deletechecklistItemsData.Any(s => s.checklist_id == chktmpxref.checklist_id && chktmpxref.checklist_template_id == checklist_template_id)
                                                select chktmpxref).Distinct().ToList();

                db.CheckListTemplatesXref.RemoveRange(recordsToRemove_UnMapped);
                db.SaveChanges();

                /*************************************************************************************
                *  //Add newly mapped items
                *************************************************************************************/
                db.CheckListTemplatesXref.AddRange(addchecklistItemsData);
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }