コード例 #1
0
ファイル: FormController.cs プロジェクト: njs/FetchClimate
        // GET: /form?v=variables
        // POST: /form (by 'Download results', 'View results', 'Download request' or 'Upload request')
        public ActionResult Form()
        {
            var method = HttpContext.Request.HttpMethod;
            var config = WebApiApplication.GetExtendedFetchConfiguration(DateTime.MaxValue);

            if (method == "GET")
            {
                return(View("Form", new RequestFormModel(config, Request.QueryString, false)));
            }
            else if (method == "POST")
            {
                if (Request.Form["uploadRequest"] != null)
                {
                    if (Request.Files == null || Request.Files.Count < 1)
                    {
                        var model = new RequestFormModel(config);
                        model.RequestUploadErrors = "No file with request is specified";
                        return(View("Form", model));
                    }
                    else
                    {
                        return(View("Form", new RequestFormModel(config, Request.Files[0].InputStream)));
                    }
                }
                else
                {
                    var model = new RequestFormModel(config, Request.Form, true);
                    if (model.HasErrors)
                    {
                        return(View("Form", model));
                    }

                    if (Request.Form["downloadRequest"] != null)
                    {
                        return(File(
                                   Encoding.UTF8.GetBytes(model.GetRequestText()),
                                   "text/plain",
                                   "request.txt"));
                    }
                    else if (Request.Form["view"] != null)
                    {
                        return(Redirect("v1/FetchClimate2.html#" + model.GetClientUrlParameters()));
                    }

                    // Download data
                    int    minPtsPerPartition          = FrontendSettings.Current.MinPtsPerPartition;
                    int    maxPtsPerPartition          = FrontendSettings.Current.MaxPtsPerPartition;
                    double jobRegistrationPermitedTime = FrontendSettings.Current.AllowedJobRegistrationSpan;
                    int    totalWorkers = RoleEnvironment.Roles["FetchWorker"].Instances.Count;

                    string query      = "";
                    var    jobManager = WebApiApplication.GetSharedJobManager(HttpContext);
                    if (model.Points.Count > 0)
                    {
                        string points = "";
                        foreach (var fr in model.GetRequestsForPoints())
                        {
                            var jobStatus = jobManager.Submit(fr, fr.GetSHAHash(), jobRegistrationPermitedTime, minPtsPerPartition, maxPtsPerPartition, totalWorkers);
                            if (points.Length > 0)
                            {
                                points += ",";
                            }
                            points += jobStatus.Hash;
                        }
                        query += "?p=" + HttpUtility.UrlEncode(points);
                    }
                    int index = 1;
                    foreach (var g in model.Grids)
                    {
                        string hashes = "";
                        foreach (var fr in model.GetRequestsForGrid(g))
                        {
                            var jobStatus = jobManager.Submit(fr, fr.GetSHAHash(), jobRegistrationPermitedTime, minPtsPerPartition, maxPtsPerPartition, totalWorkers);
                            if (hashes.Length > 0)
                            {
                                hashes += ",";
                            }
                            hashes += jobStatus.Hash;
                        }
                        if (query.Length > 0)
                        {
                            query += "&";
                        }
                        else
                        {
                            query += "?";
                        }
                        query = String.Concat(query, "g", index++, "=", HttpUtility.UrlEncode(hashes));
                    }

                    return(Redirect("results" + query));
                }
            }
            else
            {
                throw new Exception("Method is not allowed");
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> GetSearchResults([FromBody] RequestModel request)
        {
            if (request == null)
            {
                return(Content(HttpStatusCode.BadRequest, "Request shouldn't be null"));
            }

            var requestForm = new RequestFormModel();

            if (!string.IsNullOrEmpty(request.Title))
            {
                requestForm.TitleSearchValue   = request.Title;
                requestForm.IsSearchingByTitle = "on";
            }

            if (!string.IsNullOrEmpty(request.Author))
            {
                requestForm.AuthorSearchValue   = request.Author;
                requestForm.IsSearchingByAuthor = "on";
            }

            if (!string.IsNullOrEmpty(request.Text))
            {
                requestForm.TextSearchValue   = request.Text;
                requestForm.IsSearchingByText = "on";
            }

            if (!string.IsNullOrEmpty(request.Keyword))
            {
                requestForm.KeyWordSearchValue   = request.Keyword;
                requestForm.IsSearchingByKeyWord = "on";
            }

            if (request.Year > 900)
            {
                requestForm.YearSearchValue   = request.Year.ToString();
                requestForm.IsSearchingByYear = "on";
            }

            requestForm.Database = request.Database;

            try
            {
                var modelToQueryString = requestForm.ToQueryString();

                int numberOfRecordsFound = await _helper.GetNumberOfRecordsAsync(modelToQueryString);

                if (numberOfRecordsFound == 0)
                {
                    return(Content(HttpStatusCode.NotFound, "No records were found matching the terms you entered."));
                }

                requestForm.Limit = numberOfRecordsFound;
                var newModelToQueryString = requestForm.ToQueryString();

                var result = await _helper.GetListOfBooksAsync(newModelToQueryString);

                return(Ok(result));
            }
            catch (Exception exc)
            {
                return(Content(HttpStatusCode.InternalServerError, exc.Message));
            }
        }