-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
76 lines (64 loc) · 2.5 KB
/
Program.cs
File metadata and controls
76 lines (64 loc) · 2.5 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
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Supermarket.API.Domain.Repositories;
using Supermarket.API.Domain.Services;
using Supermarket.API.Models;
using Supermarket.API.Persistence.Contexts;
using Supermarket.API.Persistence.Repositories;
using Supermarket.API.Services;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers()
.AddJsonOptions(x =>
{
x.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
x.JsonSerializerOptions.WriteIndented = true;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase("supermarket-api"));
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
SeedDatabase(context);
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
void SeedDatabase(AppDbContext context)
{
if (!context.Category.Any())
{
context.Category.AddRange(
new Category { Id = 1, Name = "Fruits" },
new Category { Id = 2, Name = "Vegetables" },
new Category { Id = 3, Name = "Dairy" }
);
context.SaveChanges();
}
if (!context.Products.Any())
{
context.Products.AddRange(
new Products { Id = 1, Name = "Apple", quantityInPackage = 10, unitOfMeasurement = EUnitOfMeasurement.Unity, CategoryId = 1 },
new Products { Id = 2, Name = "Carrot", quantityInPackage = 5, unitOfMeasurement = EUnitOfMeasurement.Liter, CategoryId = 2 },
new Products { Id = 3, Name = "Milk", quantityInPackage = 1, unitOfMeasurement = EUnitOfMeasurement.Kilogram, CategoryId = 3 }
);
context.SaveChanges();
}
}