예제 #1
0
        public ActionResult FindTargets(TargetSearchOptions options)
        {
            List <DbTarget> toReturn = new List <DbTarget>();

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/sjon"));
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Request.Cookies["access_token"].Value);
                client.BaseAddress = new Uri("http://localhost:24082");

                HttpResponseMessage msg = client.PostAsJsonAsync("/api/Targets/Search", options).Result;

                if (msg.IsSuccessStatusCode)
                {
                    toReturn = msg.Content.ReadAsAsync <List <DbTarget> >().Result;
                }
            }

            ViewData["elements"] = toReturn;

            return(PartialView("_SearchTarget"));
        }
예제 #2
0
        public IHttpActionResult Post([FromBody] TargetSearchOptions options)
        {
            try
            {
                MongoClient client     = new MongoClient();
                var         db         = client.GetDatabase("scheduler");
                var         collection = db.GetCollection <TargetFacade>("targetFacades");

                List <TargetFacade> query = null;
                if (options.Title == null)
                {
                    options.Title = "";
                }

                if (String.IsNullOrEmpty(options.Title) && (options.Tags == null || options.Tags.Count() == 0))
                {
                    query = collection.AsQueryable().ToList()
                            .Where(
                        x => x.UserEmail == options.UserName &&
                        String.Compare(x.Id, options.LastObjectId, true) > 0
                        ).Take(10).ToList();
                }
                else
                {
                    if (options.Title.Trim() == "")
                    {
                        query = collection.AsQueryable().ToList().Where(
                            x => (x.Tags.Intersect(options.Tags).Count() != 0) &&
                            x.UserEmail == options.UserName &&
                            String.Compare(x.Id, options.LastObjectId, true) > 0
                            ).Take(10).ToList();
                    }
                    else if (options.Tags == null || options.Tags.Count() == 0)
                    {
                        query = collection.AsQueryable().ToList().Where(
                            x => x.Title.Trim().ToLower().Contains(options.Title.Trim().ToLower()) &&
                            x.UserEmail == options.UserName &&
                            String.Compare(x.Id, options.LastObjectId, true) > 0
                            ).Take(10).ToList();
                    }
                    else
                    {
                        query = collection.AsQueryable().ToList().Where(
                            x => (x.Title.Trim().ToLower().Contains(options.Title.Trim().ToLower()) ||
                                  x.Tags.Intersect(options.Tags).Count() != 0) &&
                            x.UserEmail == options.UserName &&
                            String.Compare(x.Id, options.LastObjectId, true) > 0
                            ).Take(10).ToList();
                    }
                }

                if (options.OrderBy == "date")
                {
                    if (query.Count() != 0)
                    {
                        query = query.OrderByDescending(x => x.StartDate).ToList();
                        string maxId = query.Max(x => x.Id);
                        query[query.Count() - 1].Id = maxId;
                    }

                    var toReturn = query
                                   .Select(x => new
                    {
                        Name       = x.Title,
                        Difficulty = x.Difficulty,
                        StartDate  = x.StartDate,
                        Id         = x.Id
                    })
                                   .ToList();

                    return(Ok(toReturn));
                }
                else
                {
                    if (query.Count() != 0)
                    {
                        query = query.OrderByDescending(y => y.Difficulty).ToList();
                        string maxId = query.Max(x => x.Id);
                        query[query.Count() - 1].Id = maxId;
                    }

                    var toReturn = query
                                   .Select(x => new
                    {
                        Name       = x.Title,
                        Difficulty = x.Difficulty,
                        StartDate  = x.StartDate,
                        Id         = x.Id
                    })
                                   .ToList();

                    return(Ok(toReturn));
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }