Note Onion - 4 - API - Program

Here is the source code for NoteTypeController.cs This is placed in the Controllers folder of the Note.Api project

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
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Note.Application.Services;
using Note.Domain.Repositories;
using Note.Infrastructure.Repositories;
using Note.Infrastructure.Data;
using Swashbuckle.AspNetCore.Filters;
using System.Reflection;
using Serilog;


// serilog stuff - see
// https://codewithmukesh.com/blog/structured-logging-with-serilog-in-aspnet-core/
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();

try
{
var builder = WebApplication.CreateBuilder(args);

//more serilog
// add Serilog to our ASP.NET Core Application’s DI Container. We
// have defined 2 configurations here, which are to write to Console,
// and to read configurations from appsettings.json. This will be
// applied to the entire application wherever we use the
// ILogger<> interface. Also, this will ignore the Logging configuration
// from the appsettings file and consider only the Serilog section in
// appsettings.json.
//
builder.Host.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext() // from copilot
.WriteTo.Console());

// get appSettings.json data
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

// load up the config file, load AppSettings.json and the environment specific one.
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environmentName}.json", true, reloadOnChange: true)
.Build();

// Add services to the container.
builder.Services.AddControllers()
.AddNewtonsoftJson();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{ Title = "Notes API", Version = "v1" });
c.EnableAnnotations();
c.ExampleFilters();

try
{
var filePath = Path.Combine(AppContext.BaseDirectory, "Note.API.xml");
c.IncludeXmlComments(filePath);
}
catch
{
}

});

builder.Services.AddSwaggerExamplesFromAssemblies(Assembly.GetEntryAssembly());

// Register other services and dependencies
builder.Services.AddScoped<INoteTypeService, NoteTypeService>();
builder.Services.AddScoped<INoteService, NoteService>();
builder.Services.AddScoped<INoteTypeRepository, NoteTypeRepository>();
builder.Services.AddScoped<INoteRepository, NoteRepository>();

// hook in database
var connectionString = config.GetConnectionString("NotesDatabaseSettings:ConnectionString");
builder.Services.AddDbContext<NoteDataContext>(options =>
options.UseSqlServer(connectionString));

// hook in automapper
//builder.Services.AddAutoMapper(typeof(Program).Assembly);
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
// this creates the 2 urls
// https://localhost:7230/swagger/v1/swagger.json - to see the json
// https://localhost:7230/swagger/index.html - to see the stuff
app.UseSwaggerUI(c => c.SwaggerEndpoint(
"/swagger/v1/swagger.json",
"Notes API V1"));
}

app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();

app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

// serilog - to log ASP.NET Core HTTP requests to the sinks.
app.UseSerilogRequestLogging();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "server terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}