public HttpResponseMessage Search([FromBody] CompanySearchCriteria criteria) { Console.WriteLine("[HttpDelete] -> /company/search"); Console.WriteLine(criteria.ToString()); var response = new HttpResponseMessage(); var serviceOperationResult = companyService.SearchCompanies(criteria); if (serviceOperationResult.Result) { response = Request.CreateResponse <List <CompanyWrapper> >(HttpStatusCode.OK, serviceOperationResult.CompanyWrappers); } else { response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, serviceOperationResult.Message); } return(response); }
/// <summary>Wyszukiwanie encji Company</summary> /// <param name="searchCriteria">Kryteria wyszukiwania encji Company</param> public ServiceOperationResult SearchCompanies(CompanySearchCriteria searchCriteria) { var serviceOperationResult = new ServiceOperationResult(); try { using (var session = NHibernateHelper.OpenSession()) { ICriterion crCompanyName, crFirstName, crLastName; ICriterion crKeywordOr = null; ICriterion crDateBetween = null; ICriterion crJobTitle = null; ICriterion crAllOr = null; if (!String.IsNullOrEmpty(searchCriteria.Keyword)) { crCompanyName = Restrictions.Like("c.CompanyName", "%" + searchCriteria.Keyword + "%"); crFirstName = Restrictions.Like("e.FirstName", "%" + searchCriteria.Keyword + "%"); crLastName = Restrictions.Like("e.LastName", "%" + searchCriteria.Keyword + "%"); crKeywordOr = Restrictions.Disjunction() .Add(crCompanyName) .Add(crFirstName) .Add(crLastName); } if (!String.IsNullOrEmpty(searchCriteria.EmployeeJobTitles.ToString())) { crJobTitle = Restrictions.Eq("e.JobTitle", searchCriteria.EmployeeJobTitles); } if (searchCriteria.EmployeeDateOfBirthFrom.HasValue && searchCriteria.EmployeeDateOfBirthTo.HasValue) { crDateBetween = Restrictions.Between("e.DateOfBirth", searchCriteria.EmployeeDateOfBirthFrom.Value, searchCriteria.EmployeeDateOfBirthTo.Value); } if (crKeywordOr != null && crJobTitle != null && crDateBetween != null) { crAllOr = Restrictions.Disjunction() .Add(crKeywordOr) .Add(crJobTitle) .Add(crDateBetween); } if (crKeywordOr != null && crJobTitle == null && crDateBetween == null) { crAllOr = crKeywordOr; } if (crKeywordOr != null & crJobTitle != null && crDateBetween == null) { crAllOr = Restrictions.Disjunction() .Add(crKeywordOr) .Add(crJobTitle); } if (crKeywordOr != null && crJobTitle == null && crDateBetween != null) { crAllOr = Restrictions.Disjunction() .Add(crKeywordOr) .Add(crDateBetween); } if (crKeywordOr == null && crJobTitle != null && crDateBetween != null) { crAllOr = Restrictions.Disjunction() .Add(crJobTitle) .Add(crDateBetween); } if (crKeywordOr == null && crJobTitle == null && crDateBetween != null) { crAllOr = Restrictions.Disjunction() .Add(crDateBetween); } if (crKeywordOr == null && crJobTitle != null && crDateBetween == null) { crAllOr = Restrictions.Disjunction() .Add(crJobTitle); } var query = session.CreateCriteria <Company>("c") .Fetch(SelectMode.Fetch, "c.Employees"); query.CreateCriteria("c.Employees", "e"); query.Add(crAllOr); var companySearchList = query.List <Company>().ToList <Company>(); companyWrapper(serviceOperationResult, companySearchList); } } catch { serviceOperationResult.Result = false; } return(serviceOperationResult); }