Skip to content

Commit 719aa3b

Browse files
authored
Merge pull request #1 from Mathavana/develop
code cleanup
2 parents aaa0eaf + 87b0390 commit 719aa3b

File tree

6 files changed

+126
-25
lines changed

6 files changed

+126
-25
lines changed

Supermarket/Extensions/EnumExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Linq;
1+
using System.ComponentModel;
52
using System.Reflection;
6-
using System.Threading.Tasks;
73

84
namespace Supermarket.Extensions
95
{

Supermarket/Extensions/ModelStateExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Microsoft.AspNetCore.Mvc.ModelBinding;
2-
using System;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Threading.Tasks;
64

75
namespace Supermarket.Extensions
86
{
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using AutoMapper;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Supermarket.Domain.Services;
7+
using Supermarket.Domain.Services.Contracts;
8+
using Supermarket.Persistent.Context;
9+
using Supermarket.Persistent.Contracts;
10+
using Supermarket.Persistent.Repositories;
11+
using Swashbuckle.AspNetCore.Swagger;
12+
13+
namespace Supermarket.Extensions
14+
{
15+
public static class ServiceExtensions
16+
{
17+
public static void ConfigureCors(this IServiceCollection services)
18+
{
19+
services.AddCors(options =>
20+
{
21+
options.AddPolicy("CorsPolicy",
22+
builder => builder.AllowAnyOrigin()
23+
.AllowAnyMethod()
24+
.AllowAnyHeader()
25+
.AllowCredentials());
26+
});
27+
}
28+
29+
public static void ConfigureIISIntegration(this IServiceCollection services)
30+
{
31+
services.Configure<IISOptions>(options => { });
32+
}
33+
34+
public static void ConfigureMSSQLContext(this IServiceCollection services, IConfiguration configuration)
35+
{
36+
services.AddDbContext<RepositoryContext>(options =>
37+
{
38+
options.UseInMemoryDatabase("supermarket-api-in-memory");
39+
});
40+
}
41+
42+
public static void ConfigureRepositoryWrapper(this IServiceCollection services)
43+
{
44+
services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
45+
}
46+
47+
public static void ConfigureServicesWrapper(this IServiceCollection services)
48+
{
49+
services.AddScoped<IServiceWrapper, ServiceWrapper>();
50+
}
51+
52+
public static void ConfigureAutoMapper(this IServiceCollection services)
53+
{
54+
services.AddAutoMapper();
55+
}
56+
57+
public static void ConfigureSwagger(this IServiceCollection services)
58+
{
59+
services.AddSwaggerGen(c =>
60+
{
61+
c.SwaggerDoc("v1", new Info
62+
{
63+
Title = "Supermarket API",
64+
Version = "v1",
65+
Description = "A simple example ASP.NET Core Web API",
66+
TermsOfService = "None",
67+
Contact = new Contact
68+
{
69+
Name = "Mathavan N",
70+
Email = "mathavan@gmail.com",
71+
Url = "https://github.com/Mathavana"
72+
},
73+
License = new License
74+
{
75+
Name = "Use under LICX",
76+
Url = "https://example.com/license"
77+
}
78+
});
79+
});
80+
}
81+
}
82+
}

Supermarket/Properties/launchSettings.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:50383",
6+
"applicationUrl": "http://localhost:5000",
77
"sslPort": 0
88
}
99
},
@@ -12,15 +12,14 @@
1212
"IIS Express": {
1313
"commandName": "IISExpress",
1414
"launchBrowser": true,
15-
"launchUrl": "api/values",
15+
"launchUrl": "swagger",
1616
"environmentVariables": {
1717
"ASPNETCORE_ENVIRONMENT": "Development"
1818
}
1919
},
2020
"Supermarket": {
2121
"commandName": "Project",
2222
"launchBrowser": true,
23-
"launchUrl": "api/values",
2423
"environmentVariables": {
2524
"ASPNETCORE_ENVIRONMENT": "Development"
2625
},

Supermarket/Startup.cs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using AutoMapper;
2-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
32
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.HttpOverrides;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.EntityFrameworkCore;
65
using Microsoft.Extensions.Configuration;
76
using Microsoft.Extensions.DependencyInjection;
8-
using Supermarket.Domain.Services;
9-
using Supermarket.Domain.Services.Contracts;
10-
using Supermarket.Persistent.Context;
11-
using Supermarket.Persistent.Contracts;
12-
using Supermarket.Persistent.Repositories;
7+
using Newtonsoft.Json.Serialization;
8+
using Supermarket.Extensions;
9+
using Swashbuckle.AspNetCore.Swagger;
1310

1411
namespace Supermarket
1512
{
@@ -25,17 +22,25 @@ public Startup(IConfiguration configuration)
2522
// This method gets called by the runtime. Use this method to add services to the container.
2623
public void ConfigureServices(IServiceCollection services)
2724
{
28-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
25+
services.ConfigureCors();
2926

30-
services.AddDbContext<RepositoryContext>(options =>
27+
services.ConfigureIISIntegration();
28+
29+
services.ConfigureMSSQLContext(Configuration);
30+
31+
services.ConfigureRepositoryWrapper();
32+
33+
services.ConfigureServicesWrapper();
34+
35+
services.AddMvc().AddJsonOptions(options =>
3136
{
32-
options.UseInMemoryDatabase("supermarket-api-in-memory");
33-
});
37+
//Force Camel Case to JSON
38+
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
39+
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
3440

35-
services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
36-
services.AddScoped<IServiceWrapper, ServiceWrapper>();
41+
services.ConfigureAutoMapper();
3742

38-
services.AddAutoMapper();
43+
services.ConfigureSwagger();
3944
}
4045

4146
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -52,7 +57,27 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5257
}
5358

5459
app.UseHttpsRedirection();
60+
61+
//CORS configuration has beend added to the application pipeline
62+
app.UseCors("CorsPolicy");
63+
64+
app.UseForwardedHeaders(new ForwardedHeadersOptions
65+
{
66+
ForwardedHeaders = ForwardedHeaders.All
67+
});
68+
69+
app.UseStaticFiles();
5570
app.UseMvc();
71+
72+
// Enable middleware to serve generated Swagger as a JSON endpoint.
73+
app.UseSwagger();
74+
75+
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
76+
// specifying the Swagger JSON endpoint.
77+
app.UseSwaggerUI(c =>
78+
{
79+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Supermarket API V1");
80+
});
5681
}
5782
}
5883
}

Supermarket/Supermarket.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1212
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

0 commit comments

Comments
 (0)