private void AssignDefaults(TbChklistval currentval, BLL_CheckListParameter newEntity)
        {
            DateTime dtchekdatetime = DateTime.Now;

            try
            {
                IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-US", true);
                dtchekdatetime      = DateTime.ParseExact(newEntity.CompletedDateTime, "MM/dd/yyyy HH:mm:ss", theCultureInfo);
                newEntity.Completed = dtchekdatetime;
            }
            catch (Exception ex)
            {
                throw new NotFoundAPIException("Invalid Date format to MM/dd/yyyy HH:mm:ss");
            }

            newEntity.PersonName = GetPersonNameByID(newEntity.PersonId);

            currentval.Key1       = newEntity.Dealnumber;
            currentval.Chklistnum = newEntity.Checklistnum;
            currentval.Completed  = Convert.ToDateTime(newEntity.Completed);
            currentval.Entitynum  = newEntity.Entitynum ?? 1;//default to deal
            currentval.PersonId   = newEntity.PersonId;
            currentval.Userid     = newEntity.PersonName;
            currentval.Key2       = -1;
            currentval.Key3       = -1;
        }
        public EntityResult <BLL_ChkCategoryTree> UpdateCheckList(BLL_CheckListParameter bll_checklist)
        {
            int workflow = -1;

            try
            {
                int dealcheckedstaus = _CheckListRepository.IsValidDealCheckedStatus(bll_checklist.Dealnumber, bll_checklist.Checklistnum);
                if (dealcheckedstaus == -1)
                {
                    throw new NotFoundAPIException("Deal and Checklistnumber combination are not found");
                }
                int          checklistAddstatus = -1;
                TbChklistval chklistvaldata     = null;

                if (bll_checklist.check == true && dealcheckedstaus == 0)
                {
                    checklistAddstatus = AddChekcList(bll_checklist);
                    workflow           = 1;
                }
                else if (bll_checklist.check == true && dealcheckedstaus == 1)
                {
                    chklistvaldata = _CheckListRepository.Get(d => d.Key1 == bll_checklist.Dealnumber && d.Chklistnum == bll_checklist.Checklistnum);
                    if (chklistvaldata.PersonId == bll_checklist.PersonId)
                    {
                        OnApplyChanges(chklistvaldata, bll_checklist);
                        _CheckListRepository.Save(chklistvaldata);
                    }
                    else
                    {
                        throw new NotFoundAPIException("Update is failed due to PersonID is different.");
                    }
                    workflow = 2;
                }
                else if (bll_checklist.check == false && dealcheckedstaus == 1)
                {
                    DeleteCheckListByTaskID(bll_checklist.Checklistnum, bll_checklist.Dealnumber);
                    workflow = 3;
                    return(null);
                }
                else if (bll_checklist.check == false && dealcheckedstaus == 0)
                {
                    throw new NotFoundAPIException("Checklist number not found");
                }
                if (workflow != -1)
                {
                    return(GetCheckNumByDealChecklists(bll_checklist.Dealnumber, bll_checklist.Checklistnum));
                }
                else
                {
                    throw new NotFoundAPIException("Record not found");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private void OnApplyChanges(TbChklistval currentView, BLL_CheckListParameter newEntity)
 {
     newEntity.PersonName = GetPersonNameByID(newEntity.PersonId);
     if (newEntity.Notes == null)
     {
         throw new NotFoundAPIException("Note Parameter is not found");
     }
     else if (newEntity.Notes.Length > 200)
     {
         throw new NotFoundAPIException("Note Character cannot be more than 200");
     }
     else
     {
         currentView.Notes      = newEntity.Notes;
         currentView.Key1       = newEntity.Dealnumber;
         currentView.Chklistnum = newEntity.Checklistnum;
     }
 }
        private int AddChekcList(BLL_CheckListParameter bLL_chklist)
        {
            int status = -1;

            try
            {
                TbChklistval tbUserView = new TbChklistval();
                AssignDefaults(tbUserView, bLL_chklist);
                _CheckListRepository.Add(tbUserView);
                _CheckListRepository.Save(tbUserView);
                status = 1;
            }
            catch (Exception ex)
            {
                throw new NotFoundAPIException("Input value is not valid");
            }
            return(status);
        }
        public void ChecklistsController_Put_Returns_NotFoundResponseCode(int dealnumber, int checklistnum, int chkstatus, ChkCategoryTree checklist)
        {
            #region Arrange
            SetupUserIdentity();

            var httpRequest = new HttpRequestMessage(new HttpMethod(AppSettings.HTTPGET), $"{AppSettings.BASEURL}{RouteHelper.DealsRoutePrefix}/{dealnumber}/checklists/{checklistnum}");

            Mock <ITblCheckListRepository> checkListRepository = new Mock <ITblCheckListRepository>();

            IList <grs_VGrsChecklistsByDeal> data = new List <grs_VGrsChecklistsByDeal>
            {
                new grs_VGrsChecklistsByDeal
                {
                    Dealnum       = 1392041,
                    Entitynum     = 1,
                    Category      = 17,
                    CategoryName  = "Pre-Bind Processing",
                    Catorder      = 14,
                    Chklistnum    = 276,
                    ChecklistName = "Actuarial Analysis",
                    Sortorder     = 3,
                    Readonly      = false,
                    Checked       = true,
                    PersonId      = 714027,
                    PersonName    = "Dhanraj Patil",
                    Note          = "Test DP",
                    FirstName     = "Dhanraj",
                    LastName      = "Patil",
                    MiddleName    = null
                }
            };

            checkListRepository.Setup(p => p.IsValidDealCheckedStatus(data[0].Dealnum, data[0].Chklistnum)).Returns(chkstatus);
            checkListRepository.Setup(p => p.GetPersonByUserId(data[0].PersonId ?? 0)).Returns(data[0].PersonName);
            if (checklist.Checklists[0].ChkListNum == 999)
            {
                data.Clear();
                checkListRepository.Setup(p => p.GetCheckNumByDealChecklists(dealnumber, checklistnum)).Returns(data);
            }
            else
            {
                checkListRepository.Setup(p => p.GetCheckNumByDealChecklists(data[0].Dealnum, data[0].Chklistnum)).Returns(data);
            }
            var chklistval = new TbChklistval {
                PersonId = 714027
            };
            if (checklist.Checklists[0].PersonId > 0)
            {
                checkListRepository.Setup(p => p.Get(It.IsAny <Expression <Func <TbChklistval, bool> > >())).Returns(chklistval);
            }

            CheckListsController checkListsController = CreateChecklistsController(httpRequest, checkListRepository.Object);

            #endregion

            #region Act & Assert

            Assert.Throws(typeof(NotFoundAPIException), delegate { checkListsController.Put(dealnumber, checklistnum, checklist); });

            #endregion
        }
        public void ChecklistsController_Put_Returns_OKResponseCode(int dealnumber, int checklistnum, bool chkstatus, ChkCategoryTree checklist)
        {
            #region Arrange
            SetupUserIdentity();

            var httpRequest = new HttpRequestMessage(new HttpMethod(AppSettings.HTTPGET), $"{AppSettings.BASEURL}{RouteHelper.DealsRoutePrefix}/{dealnumber}/checklists/{checklistnum}");

            Mock <ITblCheckListRepository> checkListRepository = new Mock <ITblCheckListRepository>();

            IList <grs_VGrsChecklistsByDeal> data = new List <grs_VGrsChecklistsByDeal>
            {
                new grs_VGrsChecklistsByDeal
                {
                    Dealnum       = 1392041,
                    Entitynum     = 1,
                    Category      = 17,
                    CategoryName  = "Pre-Bind Processing",
                    Catorder      = 14,
                    Chklistnum    = 276,
                    ChecklistName = "Actuarial Analysis",
                    Sortorder     = 3,
                    Readonly      = false,
                    Checked       = true,
                    PersonId      = 714027,
                    PersonName    = "Dhanraj Patil",
                    Note          = "Test DP",
                    FirstName     = "Dhanraj",
                    LastName      = "Patil",
                    MiddleName    = null
                }
            };
            if (chkstatus)
            {
                checkListRepository.Setup(p => p.IsValidDealCheckedStatus(data[0].Dealnum, data[0].Chklistnum)).Returns(1);
            }
            else
            {
                checkListRepository.Setup(p => p.IsValidDealCheckedStatus(data[0].Dealnum, data[0].Chklistnum)).Returns(0);
            }

            checkListRepository.Setup(p => p.GetPersonByUserId(data[0].PersonId ?? 0)).Returns(data[0].PersonName);
            checkListRepository.Setup(p => p.GetCheckNumByDealChecklists(data[0].Dealnum, data[0].Chklistnum)).Returns(data);
            var chklistval = new TbChklistval {
                PersonId = 714027
            };
            checkListRepository.Setup(p => p.Get(It.IsAny <Expression <Func <TbChklistval, bool> > >())).Returns(chklistval);

            CheckListsController checkListsController = CreateChecklistsController(httpRequest, checkListRepository.Object);

            #endregion

            #region Act

            var response = checkListsController.Put(dealnumber, checklistnum, checklist);


            #endregion

            #region Assert

            #region Expected Data

            var expectedchktree = new CheckListTree
            {
                ChkListNum  = 276,
                ChkListName = "Actuarial Analysis",
                SortOrder   = 3,
                ReadOnly    = false,
                Checked     = true,
                PersonId    = 714027,
                PersonName  = "Dhanraj Patil",
                Note        = "Test DP",
                FirstName   = "Dhanraj",
                LastName    = "Patil",
                MiddleName  = null
            };

            var expectedData = new List <ChkCategoryTree> {
                new ChkCategoryTree
                {
                    DealNumber   = 1392041,
                    EntityNum    = 1,
                    CategoryID   = 17,
                    CategoryName = "Pre-Bind Processing",
                    CatOrder     = 14,
                    Checklists   = new List <CheckListTree> {
                        expectedchktree
                    }
                }
            };
            #endregion

            if (chkstatus && !(checklist.Checklists[0].Checked ?? false))
            {
                var contentResult = response as NegotiatedContentResult <Response>;
                Assertions.AssertOkResponse(contentResult);
            }
            else
            {
                var contentResult = response as NegotiatedContentResult <ResponseItem <ChkCategoryTree> >;
                var chklistdata   = contentResult.Content.data;
                Assertions.AssertData(expectedData[0], chklistdata);
                Assertions.AssertOkResponse(contentResult);
            }



            #endregion
        }