using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace EPiServer.Templates.Alloy.UIExtensions.Rest
{
[RoutePrefix("api/authors")]
public class AuthorWebStoreController : ApiController
{
private List<string> _editors = new List<string>{
"Adrian", "Ann", "Anna", "Anne", "Linus", "Per",
"Joel", "Shahram", "Ted", "Patrick", "Erica", "Konstantin", "Abraham", "Tiger"
};
[Route]
public IEnumerable<AuthorSearchResult> GetAuthors(string name)
{
//Remove * in the end of name
if (name.EndsWith("*"))
{
name = name.Substring(0, name.Length - 1);
}
return QueryNames(name);
}
[Route("{name}")]
public AuthorSearchResult GetAuthorByName(string name)
{
string author = _editors.FirstOrDefault(e => e.StartsWith(name, StringComparison.OrdinalIgnoreCase));
return String.IsNullOrEmpty(author) ? null : new AuthorSearchResult { name = author, id = author };
}
private IEnumerable<AuthorSearchResult> QueryNames(string name)
{
IEnumerable<string> matches;
if (String.IsNullOrEmpty(name) || String.Equals(name, "*", StringComparison.OrdinalIgnoreCase))
{
matches = _editors;
}
else
{
matches = _editors.Where(e => e.StartsWith(name, StringComparison.OrdinalIgnoreCase));
}
return matches
.OrderBy(m => m)
.Take(10)
.Select(m => new AuthorSearchResult { name = m, id = m });
}
}
public class AuthorSearchResult
{
public string name { get; set; }
public string id { get; set; }
}
}