示例#1
0
        public HttpResponseMessage Get()
        {
            if (string.IsNullOrEmpty(User.Identity.Name))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            try
            {

                var peninsulaUser = new UserIdentity(User);

                var consultant = _consultantRepository.GetByUsername(peninsulaUser.Username, false);
                var email = consultant != null ? consultant.Email : string.Empty;

                var response = Request.CreateResponse(HttpStatusCode.OK, new {domain = peninsulaUser.Domain, firstname = peninsulaUser.Firstname, surname = peninsulaUser.Surname, email });

                return response;
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public IList<FavouriteChecklistSearchResultViewModel> SearchFavourites()
        {
            var favouriteChecklistSearchResultViewModelList = new List<FavouriteChecklistSearchResultViewModel>();

            var loggedInUser = new UserIdentity(User).Name;
            var favouriteChecklists  = _qualityControlService.SearchFavouritesChecklists(loggedInUser);

            foreach (var favChecklist in favouriteChecklists)
            {
                var can = GetClientAccountNumber(new List<CompanyDetails>(), favChecklist.Checklist.ClientId);
                var visitSite = GetVisitSiteAddress(new List<SiteAddressResponse>(), favChecklist.Checklist.ClientId, favChecklist.Checklist.SiteId);

                var favouriteChecklistSearchResultViewModel = new FavouriteChecklistSearchResultViewModel()
                {
                    Id=favChecklist.Id,
                    ChecklistId = favChecklist.Checklist.Id,
                    Title = favChecklist.Title,
                    MarkedByUser = favChecklist.MarkedByUser,
                    CAN = can,
                    TemplateName = favChecklist.Checklist.ChecklistTemplate != null ? favChecklist.Checklist.ChecklistTemplate.Name : null,
                    //Status  = favChecklist.Checklist.Deleted ? "Deleted" : favChecklist.Checklist.Status,
                    Status = favChecklist.Checklist.Status,
                    Site = new SiteViewModel() { Id = favChecklist.Checklist.SiteId.HasValue ? favChecklist.Checklist.SiteId.Value : -1 },
                    StatusDate = GetStatusDate(favChecklist.Checklist)
                };

                favouriteChecklistSearchResultViewModel.Site.Id = visitSite != null ? (int)visitSite.Id : -1;
                favouriteChecklistSearchResultViewModel.Site.Postcode = visitSite != null ? visitSite.Postcode : "";
                favouriteChecklistSearchResultViewModel.Site.SiteName = visitSite != null ? visitSite.SiteName : "";
                favouriteChecklistSearchResultViewModel.Site.Address1 = visitSite != null ? visitSite.Address1 : "";
                favouriteChecklistSearchResultViewModel.Site.Address2 = visitSite != null ? visitSite.Address2 : "";
                favouriteChecklistSearchResultViewModel.Site.Address3 = visitSite != null ? visitSite.Address3 : "";
                favouriteChecklistSearchResultViewModel.Site.Address4 = visitSite != null ? visitSite.Address4 : "";
                favouriteChecklistSearchResultViewModelList.Add(favouriteChecklistSearchResultViewModel);
            }

            return favouriteChecklistSearchResultViewModelList;
        }
        public bool RevertChecklist(Guid checklistId)
        {
            try
            {
                var loggedInUser = new UserIdentity(User).Name;
                var userForAuditing = _userForAuditingRepository.GetSystemUser();

                if (!_checklistService.CanRevertChecklist(checklistId))
                {
                    return false;
                }

                _checklistService.RevertChecklist(checklistId, userForAuditing, loggedInUser);

                return true;

            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                _businessSafeSessionManager.Rollback();
                LogManager.GetLogger(typeof(ChecklistController)).Error(ex);
                throw;
            }
        }
        public FavouriteChecklistViewModel MarkChecklistAsFavourite(Guid checklistId, FavouriteChecklistRequestViewModel request)
        {
            try
            {
                var checklist = _checklistRepository.GetById(checklistId);

                if (checklist == null)
                {
                    throw new Exception("Checklist not found");
                }

                var userForAuditing = _userForAuditingRepository.GetSystemUser();

                var loggedInUser = new UserIdentity(User).Name;

                var favouriteChecklist =_qualityControlService.GetFavouriteChecklist(checklist.Id, loggedInUser, true);

                if (favouriteChecklist == null)
                {
                    favouriteChecklist = FavouriteChecklist.Create(request.Title, checklist, loggedInUser);
                    _qualityControlService.MarkChecklistAsFavourite(favouriteChecklist);
                }
                else
                {
                    favouriteChecklist.Title = request.Title;
                    _qualityControlService.MarkChecklistAsFavourite(favouriteChecklist, userForAuditing);
                }

                return GetFavouriteChecklist(favouriteChecklist.Checklist.Id);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                _businessSafeSessionManager.Rollback();
                LogManager.GetLogger(typeof(ChecklistController)).Error(ex);
                throw;
            }
        }
        private FavouriteChecklistViewModel GetFavouriteChecklist(Guid checklistId)
        {
            try
            {
                var loggedInUser = new UserIdentity(User).Name;

                var favouriteChecklist = _qualityControlService.GetFavouriteChecklist(checklistId, loggedInUser, false);

                if (favouriteChecklist != null)
                {
                    return new FavouriteChecklistViewModel()
                    {
                        Id = favouriteChecklist.Id,
                        ChecklistId = favouriteChecklist.Checklist.Id,
                        User = favouriteChecklist.MarkedByUser,
                        Title = favouriteChecklist.Title
                    };
                }

                return null;
            }
            catch (Exception ex)
            {
                _businessSafeSessionManager.Rollback();
                LogManager.GetLogger(typeof(ChecklistController)).Error(ex);
                throw;
            }
        }
        public void CloneChecklist(Guid checklistId, int clientId, int[] siteIds)
        {
            try
            {
                var checklist = _checklistRepository.GetById(checklistId);

                if (checklist == null)
                {
                    throw new Exception("Checklist not found");
                }

                var user = _userForAuditingRepository.GetSystemUser();

                var copiedByName = new UserIdentity(User).Name;

                foreach (int siteId in siteIds)
                {
                    var newChecklist = checklist.CopyToSiteWithoutResponses(siteId, clientId, user, copiedByName, true);

                    _checklistRepository.Save(newChecklist);
                    _checklistRepository.Flush();
                }

            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                _businessSafeSessionManager.Rollback();
                LogManager.GetLogger(typeof (ChecklistController)).Error(ex);
                throw;
            }
        }
        public HttpResponseMessage UnMarkChecklistAsFavourite(Guid checklistId)
        {
            try
            {
                var checklist = _checklistRepository.GetById(checklistId);

                if (checklist == null)
                {
                    throw new Exception("Checklist not found");
                }

                var loggedInUser = new UserIdentity(User).Name;
                var userForAuditing = _userForAuditingRepository.GetSystemUser();

                _qualityControlService.UnMarkChecklistAsFavourite(checklist, loggedInUser, userForAuditing);

            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                _businessSafeSessionManager.Rollback();
                LogManager.GetLogger(typeof(ChecklistController)).Error(ex);
                throw;
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }