Skip to content

Commit 003e6f4

Browse files
committed
Available templates will be listed in input field using selectize.
1 parent 97d9d02 commit 003e6f4

File tree

6 files changed

+97
-27
lines changed

6 files changed

+97
-27
lines changed

src/Controllers/ApiController.cs

+12-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using GitAttributesWeb.Utils;
67
using Microsoft.AspNet.Hosting;
78
using Microsoft.AspNet.Mvc;
89
using Microsoft.Framework.Runtime;
@@ -13,25 +14,20 @@ namespace GitAttributesWeb.Controllers
1314
[Route("api")]
1415
public class ApiController : Controller
1516
{
16-
private readonly IHostingEnvironment env;
17+
private readonly AppData data;
1718

18-
public ApiController(IHostingEnvironment env)
19+
public ApiController(AppData data)
1920
{
20-
this.env = env;
21+
this.data = data;
2122
}
2223

2324
// GET: api/list
2425
[HttpGet]
2526
[Route("list")]
2627
public IEnumerable<string> Get()
2728
{
28-
var dataPath = Path.Combine(this.env.WebRootPath, "data");
29-
var files = PathResolver.PerformWildcardSearch(dataPath, "*.gitattributes");
30-
31-
var q = from file in files
32-
let name = Path.GetFileNameWithoutExtension(file).ToLowerInvariant()
33-
orderby name
34-
select name;
29+
var q = from file in this.data.Files
30+
select file.Id;
3531

3632
return q.ToList();
3733
}
@@ -41,12 +37,8 @@ orderby name
4137
[Route("{id}")]
4238
public IActionResult Get(string id)
4339
{
44-
var dataPath = Path.Combine(this.env.WebRootPath, "data");
45-
var files = PathResolver.PerformWildcardSearch(dataPath, "*.gitattributes");
46-
47-
var q = from file in files
48-
let name = Path.GetFileNameWithoutExtension(file).ToLowerInvariant()
49-
where name == id
40+
var q = from file in this.data.Files
41+
where file.Id == id
5042
select file;
5143

5244
var validFile = q.FirstOrDefault();
@@ -55,7 +47,7 @@ public IActionResult Get(string id)
5547
return new NoContentResult();
5648
}
5749

58-
string content = System.IO.File.ReadAllText(validFile);
50+
string content = System.IO.File.ReadAllText(validFile.Path);
5951

6052
return Content(content);
6153
}
@@ -65,12 +57,8 @@ public IActionResult Get(string id)
6557
[Route("f/{id}")]
6658
public IActionResult GetFile(string id)
6759
{
68-
var dataPath = Path.Combine(this.env.WebRootPath, "data");
69-
var files = PathResolver.PerformWildcardSearch(dataPath, "*.gitattributes");
70-
71-
var q = from file in files
72-
let name = Path.GetFileNameWithoutExtension(file).ToLowerInvariant()
73-
where name == id
60+
var q = from file in this.data.Files
61+
where file.Id == id
7462
select file;
7563

7664
var validFile = q.FirstOrDefault();
@@ -79,7 +67,7 @@ public IActionResult GetFile(string id)
7967
return new NoContentResult();
8068
}
8169

82-
var content = System.IO.File.ReadAllBytes(validFile);
70+
var content = System.IO.File.ReadAllBytes(validFile.Path);
8371

8472
return File(content, contentType: "text/plain", fileDownloadName: "gitattributes");
8573
}

src/Properties/launchSettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"IIS Express": {
44
"commandName": "IISExpress",
55
"environmentVariables": {
6-
"ASPNET_ENV": "prod"
6+
"ASPNET_ENV": "dev"
77
}
88
}
99
}

src/Startup.cs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void ConfigureServices(IServiceCollection services)
5353
options.OutputFormatters.Add(textOutput);
5454
});
5555

56+
services.AddSingleton<AppData>();
57+
5658
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
5759
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
5860
// services.AddWebApiConventions();

src/Utils/AppData.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using Microsoft.AspNet.Hosting;
5+
using Newtonsoft.Json;
6+
using NuGet;
7+
8+
namespace GitAttributesWeb.Utils
9+
{
10+
public class AppData
11+
{
12+
private IEnumerable<FileTemplateInfo> files;
13+
14+
public AppData(IHostingEnvironment env)
15+
{
16+
this.Init(env);
17+
}
18+
19+
private void Init(IHostingEnvironment env)
20+
{
21+
var dataPath = Path.Combine(env.WebRootPath, "data");
22+
var files = PathResolver.PerformWildcardSearch(dataPath, "*.gitattributes");
23+
24+
var q = from file in files
25+
let filename = Path.GetFileNameWithoutExtension(file)
26+
let name = filename.ToLowerInvariant()
27+
orderby name
28+
select new FileTemplateInfo
29+
{
30+
Id = name,
31+
Name = filename,
32+
Path = file
33+
};
34+
35+
this.files = q.ToList();
36+
}
37+
38+
public IEnumerable<FileTemplateInfo> Files
39+
{
40+
get { return this.files; }
41+
}
42+
43+
public string FilesJson
44+
{
45+
get
46+
{
47+
return JsonConvert.SerializeObject(this.Files);
48+
}
49+
}
50+
}
51+
}

src/Utils/FileTemplateInfo.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Newtonsoft.Json;
6+
7+
namespace GitAttributesWeb.Utils
8+
{
9+
public class FileTemplateInfo
10+
{
11+
[JsonProperty("id")]
12+
public string Id { get; set; }
13+
14+
[JsonProperty("name")]
15+
public string Name { get; set; }
16+
17+
[JsonIgnore]
18+
public string Path { get; set; }
19+
}
20+
}

src/Views/Home/Index.cshtml

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
@{
1+
@inject GitAttributesWeb.Utils.AppData AppData
2+
3+
@{
24
ViewBag.Title = "Create .gitattributes files for your project";
35
}
46

@@ -12,7 +14,14 @@
1214
$(document).ready(function() {
1315
$('#select').selectize({
1416
plugins: ['restore_on_backspace', 'remove_button'],
15-
delimiter: ','
17+
options: @Html.Raw(AppData.FilesJson),
18+
valueField: 'id',
19+
labelField: 'name',
20+
searchField: ['name'],
21+
delimiter: ',',
22+
persist: false,
23+
create: false,
24+
selectOnTab: true
1625
});
1726
})
1827
</script>

0 commit comments

Comments
 (0)