-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (58 loc) · 2.84 KB
/
Copy pathProgram.cs
File metadata and controls
71 lines (58 loc) · 2.84 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
using Microsoft.EntityFrameworkCore;
using NguyenVietTuanAnh0204068De1.DbContexts;
using NguyenVietTuanAnh0204068De1.Middleware;
using NguyenVietTuanAnh0204068De1.Services.Implementations;
using NguyenVietTuanAnh0204068De1.Services.Interfaces;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<AppDbContext0204068De1>(options =>
options.UseSqlite("Data Source=test.db")
);
builder.Services.AddScoped<IDoanhNghiepService0204068De1, DoanhNghiepService0204068De1>();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<AppDbContext0204068De1>();
context.Database.EnsureCreated();
if (!context.DoanhNghieps.Any())
{
var dn1 = new NguyenVietTuanAnh0204068De1.Entities.DoanhNghiep0204068De1 { TenDoanhNghiep = "Cong ty A", MaSoThue = "0123456789", DiaChi = "Ha Noi" };
var dn2 = new NguyenVietTuanAnh0204068De1.Entities.DoanhNghiep0204068De1 { TenDoanhNghiep = "Cong ty B", MaSoThue = "0987654321", DiaChi = "TP HCM" };
context.DoanhNghieps.AddRange(dn1, dn2);
context.SaveChanges();
var sp1 = new NguyenVietTuanAnh0204068De1.Entities.SanPham0204068De1 { TenSanPham = "Laptop Dell", MaSanPham = "SP001", NgayNhap = DateTime.Now };
var sp2 = new NguyenVietTuanAnh0204068De1.Entities.SanPham0204068De1 { TenSanPham = "Ban phim co", MaSanPham = "SP002", NgayNhap = DateTime.Now };
context.SanPhams.AddRange(sp1, sp2);
context.SaveChanges();
context.DoanhNghiepSanPhams.AddRange(
new NguyenVietTuanAnh0204068De1.Entities.DoanhNghiepSanPham0204068De1 { DoanhNghiepId = dn1.Id, SanPhamId = sp1.Id, SoLuong = 100 },
new NguyenVietTuanAnh0204068De1.Entities.DoanhNghiepSanPham0204068De1 { DoanhNghiepId = dn1.Id, SanPhamId = sp2.Id, SoLuong = 50 },
new NguyenVietTuanAnh0204068De1.Entities.DoanhNghiepSanPham0204068De1 { DoanhNghiepId = dn2.Id, SanPhamId = sp1.Id, SoLuong = 10 },
new NguyenVietTuanAnh0204068De1.Entities.DoanhNghiepSanPham0204068De1 { DoanhNghiepId = dn2.Id, SanPhamId = sp2.Id, SoLuong = 200 }
);
context.SaveChanges();
}
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "NguyenVietTuanAnh0204068De1 API v1");
options.RoutePrefix = "swagger";
});
}
app.UseMiddleware<GlobalExceptionMiddleware0204068De1>();
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseAuthorization();
app.MapControllers();
app.Run();