Пример #1
0
        public static HttpResponseMessage Find(HttpRequestMessage request, string filter, string orderby, bool desc,
                                               string top, string id)
        {
            Uri uri = request.RequestUri;

            string title = id.ToLowerInvariant();
            List<Package> packages = PackageDAO.Read(p => p.Title.ToLowerInvariant() == title).Result;
            entry[] entries = filter == "IsLatestVersion"
                ? new[] {PackageDetails.GetPackageEntry(packages.FirstOrDefault(p => p.LatestVersion), uri)}
                : packages.Select(p => PackageDetails.GetPackageEntry(p, uri)).ToArray();
            DateTime updated = packages.Any()
                ? packages.OrderByDescending(p => p.DateUpdated).First().DateUpdated
                : DateTime.UtcNow;

            string baseUri = uri.Scheme + "://" + uri.Host + ":" + uri.Port;
            string apiUri = baseUri + "/api/v2/";

            feed feed = new feed
            {
                @base = apiUri,
                id = WebUtility.HtmlDecode(uri.ToString()),
                title = new feedTitle("FindPackagesById"),
                updated = updated,
                link = new feedLink("FindPackagesById", "FindPackagesById"),
                entry = entries
            };

            XmlSerializer serializer = new XmlSerializer(typeof (feed));
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, feed);
            ms.Position = 0;
            string result = new StreamReader(ms).ReadToEnd();

            HttpResponseMessage res = request.CreateResponse(HttpStatusCode.OK);
            res.Content = new StringContent(result);

            res.Content.Headers.Remove("Content-Type");
            res.Content.Headers.Add("Content-Type", "application/atom+xml;type=entry;charset=utf-8");
            res.Headers.Add("DataServiceVersion", "2.0;");

            return res;
        }
        private static string GetFindPackageResult(string url, List<Package> packages, bool latestVersion)
        {
            string apiUri = url.Replace("&", "&amp;");
            Uri uri = new Uri(apiUri);

            DateTime updated = packages.Any()
                                   ? packages.OrderByDescending(p => p.DateUpdated).First().DateUpdated
                                   : DateTime.UtcNow;

            entry[] entries = latestVersion
                ? new[] { PackageDetails.GetPackageEntry(packages.FirstOrDefault(p => p.LatestVersion), uri) }
                : packages.Select(p => PackageDetails.GetPackageEntry(p, uri)).ToArray();

            feed feed = new feed
            {
                @base = InvokeUrl + "/",
                id = WebUtility.HtmlDecode(uri.ToString()),
                title = new feedTitle("FindPackagesById"),
                updated = updated,
                link = new feedLink("FindPackagesById", "FindPackagesById"),
                entry = entries
            };

            XmlSerializer serializer = new XmlSerializer(typeof(feed));
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, feed);
            ms.Position = 0;
            return new StreamReader(ms).ReadToEnd();
        }