-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
235 lines (188 loc) · 8.03 KB
/
Program.cs
File metadata and controls
235 lines (188 loc) · 8.03 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System.Data;
using System.Reflection;
using Microsoft.Data.SqlClient;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Microsoft.OpenApi.Models;
using FluentValidation;
using FluentValidation.AspNetCore;
using HotelManagementAPI.Services;
var builder = WebApplication.CreateBuilder(args);
// Cấu hình chuỗi kết nối
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Đăng ký IDbConnection với DI
builder.Services.AddScoped<IDbConnection>(sp => new SqlConnection(connectionString));
builder.Logging.AddConsole();
// Cấu hình CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", policy =>
{
policy.AllowAnyOrigin() // Cho phép mọi domain
.AllowAnyHeader() // Cho phép mọi header
.AllowAnyMethod(); // Cho phép mọi HTTP method (GET, POST, PUT, DELETE...)
});
});
builder.Services.AddHttpClient();
// Đọc cấu hình SentimentModelPath từ appsettings.json
var sentimentModelPath = builder.Configuration["SentimentModelPath"];
builder.Services.AddSingleton(new SentimentModelConfig { ModelPath = sentimentModelPath });
// Thêm Swagger và các dịch vụ khác
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.UseInlineDefinitionsForEnums(); // ✅ Hiển thị enum trong Swagger
options.EnableAnnotations(); // ✅ Bật hỗ trợ ghi chú trong Swagger
options.DescribeAllParametersInCamelCase(); // ✅ Hiển thị tham số theo camelCase
options.CustomSchemaIds(type => type.FullName); // Sử dụng tên đầy đủ của lớp làm ID schema
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Nhập 'Bearer' [space] và sau đó là token của bạn.",
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
},
new string[] {}
}
});
// Thêm thông tin mô tả API
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "🏨 Hệ thống Quản lý Khách sạn",
Version = "v1",
Description = @"
📘 **Đề tài:** Xây dựng hệ thống quản lý khách sạn.
🔧 **Công nghệ sử dụng:**
- ASP.NET Core Web API
- SQL Server
- Entity Framework/Dapper
- JWT Authentication
- Swagger UI
- Nuxt 3
🎯 **Chức năng chính:**
- Đăng ký người dùng bao gồm Quản Trị Viên, nhân viên, khách hàng
+ Quản Trị Viên (admin) có quyền thêm sửa xóa phòng, xem trạng thái phòng, thêm tiện nghi
+ Nhân Viên có quyền sửa phòng
+ Khách Hàng có quyền đặt phòng
- Quản lý phòng khách sạn (thêm, sửa, xoá, xem chi tiết)
- Quản lý đặt phòng (thêm, sửa, xoá, xem chi tiết)
- Quản lý khách hàng (thêm, sửa, xoá, xem chi tiết)
- Quản lý nhân viên (thêm, sửa, xoá, xem chi tiết)
- Quản lý dịch vụ (thêm, sửa, xoá, xem chi tiết)
- Quản lý tiện nghi phòng (thêm, sửa, xoá, xem chi tiết)
- Quản lý hoá đơn (thêm, sửa, xoá, xem chi tiết)
- Đặt phòng, thanh toán, và xuất hoá đơn, và xem lịch sử giao dịch
- Quản lý dịch vụ đi kèm
- Xem phản hồi từ khách hàng (feedback)
- Phân quyền người dùng (admin, nhân viên, khách hàng)
🔗 **GitHub Repository:** [https://github.com/Persinus/HotelManagementAPI](https://github.com/Persinus/HotelManagementAPI)
"
});
});
builder.Services.AddControllers();
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddFluentValidationClientsideAdapters();
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
// Đăng ký IMemoryCache
builder.Services.AddMemoryCache();
// Cấu hình Cloudinary
builder.Services.Configure<CloudinarySettings>(
builder.Configuration.GetSection("CloudinarySettings"));
// Cấu hình JWT
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true, // Kiểm tra Issuer
ValidateAudience = true, // Kiểm tra Audience
ValidateLifetime = true, // Kiểm tra thời gian sống của token
ValidateIssuerSigningKey = true, // Kiểm tra chữ ký của token
ValidIssuer = "your-issuer", // Thay thế với Issuer của bạn
ValidAudience = "your-audience", // Thay thế với Audience của bạn
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes("your_super_secret_key_1234567890") // Khóa bí mật của bạn
),
NameClaimType = "sub", // Chỉ định claim `sub` làm `Name`
RoleClaimType = "Vaitro" // Chỉ định claim `Vaitro` làm `Role`
};
options.Events = new JwtBearerEvents
{
OnChallenge = context =>
{
if (!context.Request.Headers.ContainsKey("Authorization"))
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
context.Response.ContentType = "application/json";
return context.Response.WriteAsync("{\"error\":\"Unauthorized. Authorization header is missing.\"}");
}
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
context.Response.ContentType = "application/json";
return context.Response.WriteAsync("{\"error\":\"Unauthorized. Token is missing or invalid.\"}");
},
OnAuthenticationFailed = context =>
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
context.Response.ContentType = "application/json";
return context.Response.WriteAsync("{\"error\":\"Authentication failed. Invalid token.\"}");
}
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("QuanTriVienPolicy", policy =>
policy.RequireClaim("Vaitro", "QuanTriVien"));
options.AddPolicy("NhanVienPolicy", policy =>
policy.RequireClaim("Vaitro", "NhanVien", "QuanTriVien"));
options.AddPolicy("KhachHangPolicy", policy =>
policy.RequireClaim("Vaitro", "KhachHang","NhanVien", "QuanTriVien"));
});
builder.Services.AddSingleton<IVnpayService, VnpayService>();
var app = builder.Build();
// Cấu hình Swagger
var enableSwagger = builder.Configuration.GetValue<bool>("Swagger:Enable");
if (enableSwagger || app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Hotel Management API V1");
c.RoutePrefix = string.Empty;
c.DocumentTitle = "Hotel Management API Documentation";
c.DisplayRequestDuration();
c.ConfigObject.PersistAuthorization = true;
c.ConfigObject.ShowExtensions = true;
c.ConfigObject.ShowCommonExtensions = true;
c.ConfigObject.DisplayOperationId = true;
c.ConfigObject.DisplayRequestDuration = true;
c.ConfigObject.DeepLinking = true;
});
}
app.UseCors("AllowAllOrigins"); // Áp dụng chính sách CORS đã cấu hình
// Thêm middleware xác thực và phân quyền
app.UseAuthentication();
app.UseMiddleware<RoleMiddleware>();
app.UseAuthorization();
// Định tuyến các controller
app.MapControllers();
// Cấu hình chuyển hướng HTTPS
app.UseHttpsRedirection();
app.Run();
// Lớp cấu hình cho SentimentModelPath
public class SentimentModelConfig
{
public string ModelPath { get; set; }
}