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

            if (id != data.checklist_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_ref GetChecklist(int id)
        {
            if (id == 0)
            {
                return(GetEmptyChecklist());         //Used to create empty structure for configuration_ref for ADD-NEW-Record
            }
            checklist_ref item = db.CheckLists.Find(id);

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

            return(item);
        }
        public HttpResponseMessage PostChecklist(checklist_ref data)
        {
            if (ModelState.IsValid)
            {
                db.CheckLists.Add(data);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, data);
                //response.Headers.Location = new Uri(Url.Link("ApiByName", new { id = configuration.config_id }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
        public HttpResponseMessage DeleteChecklist(int id)
        {
            checklist_ref data = db.CheckLists.Find(id);

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

            db.CheckLists.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));
        }