Skip to content

Commit 1cc895c

Browse files
author
Highlander Paiva
committed
fake api provider
1 parent 1f5bbf3 commit 1cc895c

File tree

10 files changed

+178
-71
lines changed

10 files changed

+178
-71
lines changed

AspDotnetVueJS/.editorconfig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ root = true
33
[*]
44
charset = utf-8
55
end_of_line = lf
6+
indent_style = space
67
insert_final_newline = true
78
trim_trailing_whitespace = true
8-
9-
[*.cs]
109
indent_size = 4
11-
indent_style = tab
1210

13-
[*.{js,json,vue}]
14-
indent_size = 4
11+
[*.{js,json,vue,sass,scss,css}]
1512
indent_style = tab

AspDotnetVueJS/AspDotnetVueJS.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<Content Include=".template.config\template.json" />
15+
</ItemGroup>
16+
1317
</Project>

AspDotnetVueJS/Controllers/ValuesController.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Linq;
2+
using AspDotnetVueJS.Providers;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace AspDotnetVueJS.Controllers
6+
{
7+
public class WeatherController : ControllerBase
8+
{
9+
private readonly IWeatherProvider weatherProvider;
10+
11+
public WeatherController(IWeatherProvider weatherProvider)
12+
{
13+
this.weatherProvider = weatherProvider;
14+
}
15+
16+
[HttpGet("[action]")]
17+
public IActionResult Forecasts([FromQuery(Name = "from")] int from = 0, [FromQuery(Name = "to")] int to = 4)
18+
{
19+
var quantity = to - from;
20+
21+
// We should also avoid going too far in the list.
22+
if (quantity <= 0) return BadRequest("You cannot have the 'to' parameter higher than 'from' parameter.");
23+
24+
if (from < 0) return BadRequest("You cannot go in the negative with the 'from' parameter");
25+
26+
var allForecasts = weatherProvider.GetForecasts();
27+
var result = new
28+
{
29+
Total = allForecasts.Count,
30+
Forecasts = allForecasts.Skip(from).Take(quantity).ToArray()
31+
};
32+
33+
return Ok(result);
34+
}
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AspDotnetVueJS.Providers;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace AspDotnetVueJS.Extensions
5+
{
6+
public static class ServiceCollectionExtensions
7+
{
8+
public static IServiceCollection AddWeather(this IServiceCollection services)
9+
{
10+
services.AddSingleton<IWeatherProvider, WeatherProviderFake>();
11+
12+
return services;
13+
}
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace AspDotnetVueJS.Models
2+
{
3+
public class WeatherForecast
4+
{
5+
public string DateFormatted { get; set; }
6+
7+
public string Summary { get; set; }
8+
9+
public int TemperatureC { get; set; }
10+
11+
public int TemperatureF => 32 + (int) (TemperatureC / 0.5556);
12+
}
13+
}

AspDotnetVueJS/Program.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore;
1+
using Microsoft.AspNetCore;
72
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.Logging;
103

114
namespace AspDotnetVueJS
125
{
@@ -17,8 +10,10 @@ public static void Main(string[] args)
1710
CreateWebHostBuilder(args).Build().Run();
1811
}
1912

20-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21-
WebHost.CreateDefaultBuilder(args)
13+
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
14+
{
15+
return WebHost.CreateDefaultBuilder(args)
2216
.UseStartup<Startup>();
17+
}
2318
}
24-
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using AspDotnetVueJS.Models;
3+
4+
namespace AspDotnetVueJS.Providers
5+
{
6+
public interface IWeatherProvider
7+
{
8+
List<WeatherForecast> GetForecasts();
9+
}
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using AspDotnetVueJS.Models;
5+
6+
namespace AspDotnetVueJS.Providers
7+
{
8+
public class WeatherProviderFake : IWeatherProvider
9+
{
10+
private readonly string[] summaries =
11+
{
12+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
13+
};
14+
15+
public WeatherProviderFake()
16+
{
17+
Initialize(100);
18+
}
19+
20+
private List<WeatherForecast> WeatherForecasts { get; set; }
21+
22+
public List<WeatherForecast> GetForecasts()
23+
{
24+
return WeatherForecasts;
25+
}
26+
27+
private void Initialize(int quantity)
28+
{
29+
var rng = new Random();
30+
WeatherForecasts = Enumerable.Range(1, quantity).Select(index => new WeatherForecast
31+
{
32+
DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
33+
TemperatureC = rng.Next(-20, 55),
34+
Summary = summaries[rng.Next(summaries.Length)]
35+
}).ToList();
36+
}
37+
}
38+
}

AspDotnetVueJS/Startup.cs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using System.IO.Compression;
2+
using AspDotnetVueJS.Extensions;
53
using Microsoft.AspNetCore.Builder;
64
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.HttpsPolicy;
85
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.ResponseCompression;
7+
using Microsoft.AspNetCore.SpaServices.Webpack;
98
using Microsoft.Extensions.Configuration;
109
using Microsoft.Extensions.DependencyInjection;
11-
using Microsoft.Extensions.Logging;
12-
using Microsoft.Extensions.Options;
1310

1411
namespace AspDotnetVueJS
1512
{
@@ -26,6 +23,21 @@ public Startup(IConfiguration configuration)
2623
public void ConfigureServices(IServiceCollection services)
2724
{
2825
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
26+
27+
// Enable the Gzip compression especially for Kestrel
28+
services.Configure<GzipCompressionProviderOptions>(options =>
29+
options.Level = CompressionLevel.Optimal);
30+
services.AddResponseCompression(options =>
31+
{
32+
#if (!NoHttps)
33+
options.EnableForHttps = true;
34+
#endif
35+
});
36+
37+
services.AddSpaStaticFiles(config => { config.RootPath = "wwwroot/"; });
38+
39+
// Example with dependency injection for a data provider.
40+
services.AddWeather();
2941
}
3042

3143
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -37,12 +49,44 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3749
}
3850
else
3951
{
40-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
52+
app.UseExceptionHandler("/Error");
53+
#if (!NoHttps)
4154
app.UseHsts();
4255
}
4356

4457
app.UseHttpsRedirection();
45-
app.UseMvc();
58+
#else
59+
}
60+
#endif
61+
62+
app.UseResponseCompression(); // No need if you use IIS, but really something good for Kestrel!
63+
64+
// Idea: https://code.msdn.microsoft.com/How-to-fix-the-routing-225ac90f
65+
// This avoid having a real mvc view. You have other way of doing, but this one works
66+
// properly.
67+
// app.UseSpa();
68+
app.UseDefaultFiles();
69+
app.UseStaticFiles();
70+
app.UseSpaStaticFiles();
71+
app.UseMvc(routes =>
72+
{
73+
routes.MapRoute(
74+
"default",
75+
"{controller}/{action=Index}/{id?}");
76+
});
77+
78+
app.UseSpa(spa =>
79+
{
80+
spa.Options.SourcePath = "ClientApp";
81+
82+
if (env.IsDevelopment())
83+
spa.ApplicationBuilder.UseWebpackDevMiddleware(
84+
new WebpackDevMiddlewareOptions
85+
{
86+
HotModuleReplacement = true,
87+
ConfigFile = "./build/webpack.config.js"
88+
});
89+
});
4690
}
4791
}
48-
}
92+
}

0 commit comments

Comments
 (0)