BuildFeed/Controllers/apiController.cs

122 lines
5.1 KiB
C#
Raw Normal View History

2014-10-11 06:57:02 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BuildFeed.Models;
2014-11-28 03:34:20 +08:00
using BuildFeed.Models.ApiModel;
2014-10-11 06:57:02 +08:00
namespace BuildFeed.Controllers
{
public class apiController : ApiController
{
public IEnumerable<Build> GetBuilds()
{
return Build.SelectInBuildOrder();
}
public IEnumerable<string> GetWin10Labs()
{
2014-11-28 03:34:20 +08:00
List<string> labs = new List<string>();
labs.AddRange(Build.SelectBuildLabs(6, 4));
labs.AddRange(Build.SelectBuildLabs(10, 0));
2014-11-28 03:51:58 +08:00
return labs.GroupBy(l => l).Select(l => l.Key).ToArray();
2014-11-28 03:34:20 +08:00
}
public IEnumerable<SearchResult> GetSearchResult(string query)
{
if (string.IsNullOrWhiteSpace(query))
{
return new SearchResult[0];
}
2014-11-28 03:34:20 +08:00
List<SearchResult> results = new List<SearchResult>();
var sourceResults = from s in Enum.GetValues(typeof(BuildFeed.Models.TypeOfSource)).Cast<BuildFeed.Models.TypeOfSource>().Select(s => new { Text = DisplayHelpers.GetDisplayTextForEnum(s), Value = s })
where s.Text.ToLower().Contains(query.ToLower())
orderby s.Text.ToLower().IndexOf(query.ToLower()) ascending
select new SearchResult()
{
Url = Url.Route("Source Root", new { controller = "build", action = "source", source = s.Value }),
Label = s.Text.Replace(query, "<strong>" + query + "</strong>"),
Title = s.Text,
Group = "Source"
};
results.AddRange(sourceResults);
var versionResults = from v in Build.SelectBuildVersions()
where string.Format("{0}.{1}", v.Major, v.Minor).StartsWith(query)
orderby v.Major descending, v.Minor descending
select new SearchResult()
{
Url = Url.Route("Version Root", new { controller = "build", action = "version", major = v.Major, minor = v.Minor }),
Label = string.Format("{0}.{1}", v.Major, v.Minor).Replace(query, "<strong>" + query + "</strong>"),
2014-12-11 20:24:42 +08:00
Title = "",
Group = "Version"
};
results.AddRange(versionResults);
2014-11-28 03:34:20 +08:00
var yearResults = from y in Build.SelectBuildYears()
where y.ToString().Contains(query)
orderby y descending
select new SearchResult()
{
Url = Url.Route("Year Root", new { controller = "build", action = "year", year = y }),
Label = y.ToString().Replace(query, "<strong>" + query + "</strong>"),
2014-12-11 20:24:42 +08:00
Title = "",
2014-11-28 03:34:20 +08:00
Group = "Year"
};
results.AddRange(yearResults);
var labResults = from l in Build.SelectBuildLabs()
where l.ToLower().Contains(query.ToLower())
orderby l.ToLower().IndexOf(query.ToLower()) ascending
2014-11-28 03:34:20 +08:00
select new SearchResult()
{
Url = Url.Route("Lab Root", new { controller = "build", action = "lab", lab = l }),
Label = l.Replace(query, "<strong>" + query + "</strong>"),
2014-12-11 20:24:42 +08:00
Title = l,
2014-11-28 03:34:20 +08:00
Group = "Lab"
};
results.AddRange(labResults);
var buildResults = from b in Build.Select()
where b.FullBuildString.ToLower().Contains(query.ToLower())
orderby b.FullBuildString.ToLower().IndexOf(query.ToLower()) ascending,
2014-11-28 03:34:20 +08:00
b.BuildTime descending
select new SearchResult()
{
Url = Url.Route("Actions", new { controller = "build", action = "info", id = b.Id }),
Label = b.FullBuildString.Replace(query, "<strong>" + query + "</strong>"),
2014-12-11 20:24:42 +08:00
Title = b.FullBuildString,
2014-11-28 03:34:20 +08:00
Group = "Build"
};
results.AddRange(buildResults);
if (results.Count == 0)
{
results.Add(new SearchResult()
{
Url = "/",
Label = "No Results found",
Group = ""
});
}
2014-11-28 03:34:20 +08:00
return results.Take(6);
}
2014-10-11 06:57:02 +08:00
}
}