-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
118 lines (94 loc) · 3.23 KB
/
Program.cs
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
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Services;
using Interfaces;
using Repositories;
using Decorators;
using Models;
using Endpoints;
using Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "API",
Description = "Description"
});
});
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration.GetConnectionString("Redis");
options.InstanceName = "RedisInstance";
});
/* Postgres */
builder.Services.AddDbContext<IDatabaseContext, DatabaseContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("database")));
/* Brug InMemoryCacheService */
//builder.Services.AddSingleton<ICacheService, InMemoryCacheService>();
/* Brug RedisCacheService */
builder.Services.AddSingleton<ICacheService, RedisCacheService>();
/* Registrer ProductService */
builder.Services.AddScoped<IProductService, ProductService>();
// Register ProductRepository
builder.Services.AddScoped<ProductRepository>();
// Register FakeProductRepository
//builder.Services.AddSingleton<FakeProductRepository>();
// Register the decorator for IProductRepository
builder.Services.AddScoped<IRepository<Product>>(provider =>
{
var baseRepository = provider.GetRequiredService<ProductRepository>();
var cacheService = provider.GetRequiredService<ICacheService>();
return new CachingRepositoryDecorator<Product>(baseRepository, cacheService);
});
var app = builder.Build();
/* Seeding the database */
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var productRepository = services.GetRequiredService<IRepository<Product>>();
if (await productRepository.IsEmpty())
{
var seedData = IRepository<Product>.GenerateSeedData(10, index =>
new Product
{
Name = Faker.Internet.DomainWord(),
Price = Faker.RandomNumber.Next(100, 10000),
Quantity = Faker.RandomNumber.Next(1, 100)
}
);
foreach (var product in seedData)
{
await productRepository.AddAsync(product);
}
}
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
/* Tilføjelse af Produktendpoints */
ProductEndpoints.Map(app);
app.MapGet("/", async (HttpContext httpContext) =>
{
await Task.Delay(TimeSpan.FromSeconds(3));
httpContext.Response.Headers.CacheControl = "public, max-age=120";
return Results.Ok($"Hello!");
});
app.MapGet("/image", async (HttpContext httpContext) =>
{
var url = httpContext.Request.Query["url"].ToString();
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return Results.Problem("Kunne ikke hente billedet.");
}
var imageContent = await response.Content.ReadAsByteArrayAsync();
httpContext.Response.Headers.CacheControl = "public, max-age=120";
return Results.File(imageContent, response.Content.Headers.ContentType?.ToString());
});
app.Run();