Skip to content

Commit 4a05639

Browse files
committed
Tilføjede Catalog API controllers og Swagger setup
1 parent 01a0b16 commit 4a05639

File tree

7 files changed

+154
-21
lines changed

7 files changed

+154
-21
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<PackageVersion Include="System.Security.Claims" Version="4.3.0" />
5151
<PackageVersion Include="System.Text.Json" Version="8.0.3" />
5252
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
53-
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
53+
<PackageVersion Include="Swashbuckle.AspNetCore" Version="9.0.6" />
5454
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
5555
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
5656
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.1.2" />
@@ -71,4 +71,4 @@
7171
<PackageVersion Include="MSTest.TestFramework" Version="3.2.2" />
7272
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
7373
</ItemGroup>
74-
</Project>
74+
</Project>

src/Microservices/CatalogMicroservice/CatalogMicroservice.API/CatalogMicroservice.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" />
1112
</ItemGroup>
1213

1314
<ItemGroup>
Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,68 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Microservice.Catalog.Infrastructure.Interfaces;
3+
using Microservice.Catalog.Common.Models;
24

3-
namespace CatalogMicroservice.API.Controllers
5+
namespace CatalogMicroservice.API.Controllers;
6+
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
public class ItemsController : ControllerBase
410
{
5-
public class CatalogAPI : Controller
11+
private readonly ICatalogItemRepository _repo;
12+
13+
public ItemsController(ICatalogItemRepository repo) => _repo = repo;
14+
15+
[HttpGet]
16+
public async Task<ActionResult<IEnumerable<CatalogItem>>> GetPage(
17+
// en ide til at lave pagination og filtrering, her fra starten, skal nok ændres ERH
18+
[FromQuery] int pageNo = 1,
19+
[FromQuery] int pageSize = 20,
20+
[FromQuery] int? brandId = null,
21+
[FromQuery] int? typeId = null,
22+
CancellationToken ct = default)
23+
{
24+
if (pageNo < 1 || pageSize < 1) return BadRequest("pageNo og pageSize skal være >= 1.");
25+
26+
var items = await _repo.GetItemPageAsync(pageNo, pageSize, brandId, typeId, ct);
27+
return Ok(items);
28+
}
29+
30+
[HttpGet("{id:int}")]
31+
public async Task<ActionResult<CatalogItem>> GetById(int id, CancellationToken ct)
32+
{
33+
var item = await _repo.GetItemAsync(id, ct);
34+
return item is null ? NotFound() : Ok(item);
35+
}
36+
37+
[HttpPost]
38+
public async Task<ActionResult<CatalogItem>> Create([FromBody] CreateCatalogItem dto, CancellationToken ct)
639
{
7-
public IActionResult Index()
8-
{
9-
return View();
10-
}
40+
if (dto is null) return BadRequest("Body mangler.");
41+
42+
var created = await _repo.CreateItemAsync(dto, ct);
43+
return CreatedAtAction(nameof(GetById), new { id = created.Id }, created);
44+
}
45+
46+
[HttpPut("{id:int}")]
47+
public async Task<IActionResult> Update(int id, [FromBody] CatalogItem model, CancellationToken ct)
48+
{
49+
if (model is null || model.Id != id)
50+
return BadRequest("Id i route og body skal matche.");
51+
52+
var existing = await _repo.GetItemAsync(id, ct);
53+
if (existing is null) return NotFound();
54+
55+
await _repo.UpdateItemAsync(model, ct);
56+
return NoContent();
57+
}
58+
59+
[HttpDelete("{id:int}")]
60+
public async Task<IActionResult> Delete(int id, CancellationToken ct)
61+
{
62+
var existing = await _repo.GetItemAsync(id, ct);
63+
if (existing is null) return NotFound();
64+
65+
await _repo.DeleteItemAsync(id, ct);
66+
return NoContent();
1167
}
1268
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microservice.Catalog.Infrastructure.Interfaces;
3+
using Microservice.Catalog.Common.Models;
4+
5+
namespace CatalogMicroservice.API.Controllers;
6+
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
public class BrandsController : ControllerBase
10+
{
11+
private readonly ICatalogBrandRepository _repo;
12+
13+
public BrandsController(ICatalogBrandRepository repo) => _repo = repo;
14+
15+
[HttpGet]
16+
public async Task<ActionResult<IEnumerable<CatalogBrand>>> GetAll(CancellationToken ct)
17+
{
18+
var brands = await _repo.GetBrandsAsync(ct);
19+
return Ok(brands);
20+
}
21+
22+
[HttpGet("{id:int}")]
23+
public async Task<ActionResult<CatalogBrand>> GetById(int id, CancellationToken ct)
24+
{
25+
var brand = await _repo.GetBrandByIdAsync(id, ct);
26+
return brand is null ? NotFound() : Ok(brand);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microservice.Catalog.Infrastructure.Interfaces;
3+
using Microservice.Catalog.Common.Models;
4+
5+
namespace CatalogMicroservice.API.Controllers;
6+
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
public class TypesController : ControllerBase
10+
{
11+
private readonly ICatalogTypeRepository _repo;
12+
13+
public TypesController(ICatalogTypeRepository repo) => _repo = repo;
14+
15+
[HttpGet]
16+
public async Task<ActionResult<IEnumerable<CatalogType>>> GetAll(CancellationToken ct)
17+
{
18+
var types = await _repo.GetCatalogTypesAsync(ct);
19+
return Ok(types);
20+
}
21+
22+
[HttpGet("{id:int}")]
23+
public async Task<ActionResult<CatalogType>> GetById(int id, CancellationToken ct)
24+
{
25+
var type = await _repo.GetCatalogTypeAsync(id, ct);
26+
return type is null ? NotFound() : Ok(type);
27+
}
28+
}
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
using Microservice.Catalog.Infrastructure.DependencyInjection;
1+
using System.Reflection;
2+
using Microsoft.OpenApi.Models;
3+
using Microservice.Catalog.Infrastructure.DependencyInjection;
24

35
var builder = WebApplication.CreateBuilder(args);
46

5-
// Add services to the container.
6-
77
builder.Services.AddControllers();
8-
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
9-
builder.Services.AddOpenApi();
108

11-
var connectionString = Environment.GetEnvironmentVariable("SQL_CONNECTION_STRING", EnvironmentVariableTarget.Process) ?? "";
9+
builder.Services.AddEndpointsApiExplorer();
10+
builder.Services.AddSwaggerGen(c =>
11+
{
12+
c.SwaggerDoc("v1", new OpenApiInfo
13+
{
14+
Title = "Catalog API",
15+
Version = "v1",
16+
Description = "eShopOnWeb – Catalog microservice"
17+
});
18+
19+
var xmlName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
20+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlName);
21+
if (File.Exists(xmlPath))
22+
c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
23+
});
24+
25+
var connectionString = Environment.GetEnvironmentVariable(
26+
"SQL_CONNECTION_STRING",
27+
EnvironmentVariableTarget.Process
28+
) ?? "";
1229

1330
builder.Services.AddCatalogBrandRepository(connectionString);
1431
builder.Services.AddCatalogItemRepository(connectionString);
@@ -18,19 +35,22 @@
1835

1936
var app = builder.Build();
2037

21-
// Configure the HTTP request pipeline.
2238
if (app.Environment.IsDevelopment())
2339
{
24-
app.MapOpenApi();
40+
app.UseSwagger();
41+
app.UseSwaggerUI(c =>
42+
{
43+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Catalog API v1");
44+
});
2545
}
2646

27-
using var scope = app.Services.CreateScope();
28-
scope.SetupCatalogDatabase();
47+
using (var scope = app.Services.CreateScope())
48+
{
49+
scope.SetupCatalogDatabase();
50+
}
2951

3052
app.UseHttpsRedirection();
31-
3253
app.UseAuthorization();
33-
3454
app.MapControllers();
3555

3656
app.Run();

src/Microservices/CatalogMicroservice/CatalogMicroservice.Infrastructure/DependencyInjection/CatalogItemDependencyInjection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microservice.Catalog.Infrastructure.Interfaces;
1+
using Microservice.Catalog.Infrastructure.Interfaces;
22
using Microservice.Catalog.Infrastructure.Repositories;
33
using Microsoft.Extensions.DependencyInjection;
44

0 commit comments

Comments
 (0)