public IActionResult OnGet(int?caseId, int?editContactId)
        {
            if (!caseId.HasValue)
            {
                return(NotFound());
            }
            if (caseId.HasValue && !CaseListItems.AssignedCases.Any(x => x.CaseID == caseId))
            {
                return(Unauthorized());
            }

            if (caseId.HasValue)
            {
                CaseListItems.AssignedCases.FirstOrDefault(x => x.CaseID == caseId).IsActive = true;

                var currentCase = _caseService.GetCase(caseId.Value);
                if (currentCase != null)
                {
                    CurrentAssignedCase = new CaseDetail
                    {
                        Name   = currentCase.GetFullName(),
                        CaseID = currentCase.CaseID
                    };
                }
            }


            var parentCase = _caseService.GetCase(caseId.Value);

            if (parentCase == null)
            {
                return(NotFound());
            }
            AddContactForm.CaseId = caseId.Value;
            if (editContactId.HasValue)
            {
                var editContact = _contactService.GetContact(editContactId.Value);
                if (editContact == null)
                {
                    return(NotFound());
                }
                AddContactForm.ContactId = editContactId.Value;
                AddContactForm.Forename  = editContact.Forename;
                AddContactForm.Surname   = editContact.Surname;
                AddContactForm.Phone     = editContact.Phone;
                AddContactForm.Email     = editContact.Email;
            }
            return(Page());
        }
        public IActionResult OnPost()
        {
            var currentCase = _caseService.GetCase(AddContactForm.CaseId);

            if (currentCase != null)
            {
                CurrentAssignedCase = new CaseDetail
                {
                    Name   = currentCase.GetFullName(),
                    CaseID = currentCase.CaseID
                };
            }
            if (ModelState.IsValid)
            {
                var extraValid = true;
                if (AddContactForm.Email == null && AddContactForm.Phone == null)
                {
                    ModelState.AddModelError("AddContactForm.Email", "You must supply either an email address or phone number");
                    ModelState.AddModelError("AddContactForm.Phone", "You must supply either an email address or phone number");
                    extraValid = false;
                }
                if (extraValid)
                {
                    if (AddContactForm.ContactId.HasValue)
                    {
                        var editContact = _contactService.GetContact(AddContactForm.ContactId.Value);
                        if (editContact == null || editContact.CaseID != currentCase.CaseID)
                        {
                            return(new RedirectToPageResult("Dashboard", new { caseId = AddContactForm.CaseId }));
                        }
                        _contactService.Update(AddContactForm.getContact(editContact));
                        return(new RedirectToPageResult("Dashboard", new { caseId = AddContactForm.CaseId }));
                    }
                    else
                    {
                        _contactService.Add(AddContactForm.getContact());
                        return(new RedirectToPageResult("Dashboard", new { caseId = AddContactForm.CaseId }));
                    }
                }
            }
            return(Page());
        }
        public IActionResult OnGet(int?caseId, int?dropCaseId, int?completeCaseId, int?deleteContactId, int?manualMarkContactId, int?manualUnmarkContactId)
        {
            var claims      = HttpContext.User.Claims;
            var currentUser = userService.GetUserByUserName(claims.Single(x => x.Type == "preferred_username").Value, int.Parse(claims.Single(x => x.Type == "usrtype").Value));

            if (dropCaseId != null && CaseListItems.AssignedCases.Any(x => x.CaseID == dropCaseId))
            {
                caseService.Drop(dropCaseId.Value, currentUser.UserId);
                return(new RedirectToPageResult("Dashboard"));
            }
            if (completeCaseId != null && CaseListItems.AssignedCases.Any(x => x.CaseID == completeCaseId))
            {
                var completeResult = caseService.Complete(completeCaseId.Value, currentUser.UserId);
                if (completeResult)
                {
                    return(new RedirectToPageResult("Dashboard"));
                }
                else
                {
                    CloseFailed = true;
                }
            }

            if (manualMarkContactId.HasValue)
            {
                var manualMarkContact = contactService.GetContact(manualMarkContactId.Value);
                if (manualMarkContact != null && CaseListItems.AssignedCases.Any(x => x.CaseID == manualMarkContact.CaseID))
                {
                    if (manualMarkContact.ContactedDate == null)
                    {
                        manualMarkContact.ContactedDate = DateTime.Now;
                    }
                    contactService.Update(manualMarkContact);
                }
            }
            if (manualUnmarkContactId.HasValue)
            {
                var manualUnmarkContact = contactService.GetContact(manualUnmarkContactId.Value);
                if (manualUnmarkContact != null && CaseListItems.AssignedCases.Any(x => x.CaseID == manualUnmarkContact.CaseID))
                {
                    manualUnmarkContact.ContactedDate = null;
                    contactService.Update(manualUnmarkContact);
                }
            }

            if (caseId.HasValue && !CaseListItems.AssignedCases.Any(x => x.CaseID == caseId))
            {
                return(Unauthorized());
            }


            if (caseId.HasValue)
            {
                if (deleteContactId.HasValue)
                {
                    var deleteContact = contactService.GetContact(deleteContactId.Value);
                    if (deleteContact != null && CaseListItems.AssignedCases.Any(x => x.CaseID == deleteContact.CaseID))
                    {
                        contactService.Delete(deleteContact.ContactID);
                    }
                }
                CaseListItems.AssignedCases.FirstOrDefault(x => x.CaseID == caseId).IsActive = true;

                var currentCase = caseService.GetCase(caseId.Value);
                if (currentCase != null)
                {
                    CurrentAssignedCase = new CaseDetail
                    {
                        Name         = currentCase.GetFullName(),
                        CaseID       = currentCase.CaseID,
                        contacts     = caseService.GetTracedContacts(currentCase.CaseID).Select(MapToContactDetail).OrderBy(x => x.DateTraced),
                        EmailAddress = currentCase.Email,
                        PhoneNumber  = currentCase.Phone,
                        PhoneNumber2 = currentCase.Phone2
                    };
                }
            }

            return(Page());
        }