Repository navigation
Expand file tree
/
Copy pathappsettings.cs
More file actions
112 lines (96 loc) · 3.08 KB
/
Copy pathappsettings.cs
File metadata and controls
112 lines (96 loc) · 3.08 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
// Profiles/TreeMappingProfile.cs
using AutoMapper;
using TreeApp.Models;
namespace TreeApp.Profiles
{
public class TreeMappingProfile : Profile
{
public TreeMappingProfile()
{
CreateMap<TreeNode, TreeNodeDto>()
.ForMember(dest => dest.Children, opt => opt.Ignore())
.ForMember(dest => dest.Level, opt => opt.Ignore())
.ForMember(dest => dest.Path, opt => opt.Ignore())
.ForMember(dest => dest.HasChildren, opt => opt.Ignore())
.ForMember(dest => dest.IsExpanded, opt => opt.Ignore());
CreateMap<CreateTreeNodeRequest, TreeNode>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.IsActive, opt => opt.MapFrom(src => true))
.ForMember(dest => dest.CreatedAt, opt => opt.Ignore())
.ForMember(dest => dest.UpdatedAt, opt => opt.Ignore());
CreateMap<UpdateTreeNodeRequest, TreeNode>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.CreatedAt, opt => opt.Ignore())
.ForMember(dest => dest.UpdatedAt, opt => opt.Ignore());
}
}
}
// Program.cs
using MongoDB.Driver;
using TreeApp.Services;
using TreeApp.Profiles;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// MongoDB Configuration
builder.Services.AddSingleton<IMongoClient>(serviceProvider =>
{
var connectionString = builder.Configuration.GetConnectionString("MongoDb")
?? "mongodb://localhost:27017";
return new MongoClient(connectionString);
});
builder.Services.AddScoped<IMongoDatabase>(serviceProvider =>
{
var client = serviceProvider.GetRequiredService<IMongoClient>();
var databaseName = builder.Configuration.GetValue<string>("MongoDbSettings:DatabaseName")
?? "TreeApp";
return client.GetDatabase(databaseName);
});
// Register services
builder.Services.AddScoped<ITreeRepository, TreeRepository>();
builder.Services.AddScoped<ITreeService, TreeService>();
// AutoMapper
builder.Services.AddAutoMapper(typeof(TreeMappingProfile));
// CORS for Angular app
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularApp", policy =>
{
policy.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("AllowAngularApp");
app.UseAuthorization();
app.MapControllers();
app.Run();
// appsettings.json
/*
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MongoDb": "mongodb://localhost:27017"
},
"MongoDbSettings": {
"DatabaseName": "TreeApp"
}
}
*/