-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (87 loc) · 4.13 KB
/
Program.cs
File metadata and controls
101 lines (87 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Reflection;
using System.Text.Json.Serialization;
using Answer.King.Api.Common.JsonConverters;
using Answer.King.Api.Common.Validators;
using Answer.King.Api.Extensions.DependencyInjection;
using Answer.King.Api.OpenApi;
using Answer.King.Api.Services;
using Answer.King.Domain.Repositories;
using Answer.King.Infrastructure.Extensions.DependencyInjection;
using Answer.King.Infrastructure.Repositories;
using Answer.King.Infrastructure.Repositories.Mappings;
using Answer.King.Infrastructure.SeedData;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.OpenApi.Models;
namespace Answer.King.Api;
// <- Note the namespaces here
public partial class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var corsAllowAnyPolicy = "AllowAnyOrigin";
builder.Services.AddCors(options =>
{
options.AddPolicy(corsAllowAnyPolicy,
policy =>
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.Converters.Add(new ProductIdJsonConverter());
options.JsonSerializerOptions.Converters.Add(new CategoryIdJsonConverter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.UseCustomSchemaIdSelectorStrategy();
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Answer King API", Version = "v1" });
options.DocumentFilter<TagDescriptionsDocumentFilter>();
options.SchemaFilter<ValidationProblemDetailsSchemaFilter>();
options.SchemaFilter<EnumSchemaFilter>();
options.SchemaFilter<ProductCategorySchemaFilter>();
options.SchemaFilter<RemoveSchemasFilter>();
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.EnableAnnotations();
});
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
builder.Services.AddFluentValidationAutoValidation(
_ => ValidatorOptions.Global.PropertyNameResolver = CamelCasePropertyNameResolver.ResolvePropertyName);
builder.Services.ConfigureLiteDb(options =>
{
options.RegisterEntityMappingsFromAssemblyContaining<IEntityMapping>();
options.RegisterDataSeedingFromAssemblyContaining<ISeedData>();
});
builder.Services.AddTransient<IOrderRepository, OrderRepository>();
builder.Services.AddTransient<IProductRepository, ProductRepository>();
builder.Services.AddTransient<ICategoryRepository, CategoryRepository>();
builder.Services.AddTransient<IPaymentRepository, PaymentRepository>();
builder.Services.AddTransient<IOrderService, OrderService>();
builder.Services.AddTransient<IPaymentService, PaymentService>();
builder.Services.AddTransient<IProductService, ProductService>();
builder.Services.AddTransient<ICategoryService, CategoryService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "Answer King API V1"));
}
app.UseLiteDb();
app.UseHttpsRedirection();
app.UseCors(corsAllowAnyPolicy);
app.UseAuthorization();
app.MapControllers();
app.Run();
}
//Add services , etc - the whole content of Program.cs
}