Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
Expand All @@ -11,18 +8,48 @@ on:

jobs:
build:

env:
ConnectionStrings__DefaultConnection: "Host=localhost;Port=5432;Database=movie;Username=postgres;Password="

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- uses: actions/checkout@v4

- name: Start PostgreSQL via Docker Compose
run: |
docker compose up -d --build
until docker compose exec -T postgres pg_isready -U postgres -d movie; do
if [ -z "$(docker compose ps --status running -q postgres)" ]; then
echo "Le conteneur postgres s'est arrêté inopinément. Logs :"
docker compose logs postgres
exit 1
fi
echo "En attente de PostgreSQL (initialisation en cours)..."
sleep 2
done
echo "PostgreSQL est prêt !"

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore

- name: Install dotnet-ef tool
run: dotnet tool install --global dotnet-ef

- name: Update database
run: dotnet ef database update --project API

- name: Test
run: dotnet test API.Tests/API.Tests.csproj --no-build --verbosity normal

- name: Stop Containers
if: always()
run: docker compose down -v
20 changes: 20 additions & 0 deletions API.Tests/API.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2026.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="4.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\API\API.csproj" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions API.Tests/Controller/ProductControllerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using API.Controller;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace API.Tests.Controller;

[TestClass]
[TestSubject(typeof(ProductController))]
public class ProductControllerTest
{
private readonly ProductController _controller;

Check warning on line 12 in API.Tests/Controller/ProductControllerTest.cs

View workflow job for this annotation

GitHub Actions / build

The field 'ProductControllerTest._controller' is never used

Check warning on line 12 in API.Tests/Controller/ProductControllerTest.cs

View workflow job for this annotation

GitHub Actions / build

The field 'ProductControllerTest._controller' is never used

[TestInitialize]
public void Init()
{

}

[TestMethod]
public void ShouldCreateProduct()
{
// Given

// When

// Then
Assert.AreEqual(1, 1);

Check warning on line 28 in API.Tests/Controller/ProductControllerTest.cs

View workflow job for this annotation

GitHub Actions / build

Review or remove the assertion as its condition is known to be always true (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0032)

Check warning on line 28 in API.Tests/Controller/ProductControllerTest.cs

View workflow job for this annotation

GitHub Actions / build

Review or remove the assertion as its condition is known to be always true (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0032)
}
}
12 changes: 8 additions & 4 deletions API/Controller/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ namespace API.Controller;

[ApiController]
[Route("api/product")]
public class ProductController : ControllerBase
public class ProductController(AppDbContext context) : ControllerBase
{
// GET api/product
[HttpGet]
public ActionResult<Product> GetProduct(int id)
public ActionResult<Product> GetProduct( int id)
{
return Ok();
var products = context.Products.ToList();
return Ok(products);
}

// POST api/product
[HttpPost]
public ActionResult<Product> CreateProduct([FromBody] Product product)
{
return Ok();
context.Products.Add(product);
context.SaveChanges();

return Ok(product);
}

// PATCH api/product
Expand Down
6 changes: 3 additions & 3 deletions API/Models/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace API.Models;

public partial class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
private DbSet<Brand> Brands { get; set; }
private DbSet<Product> Products { get; set; }
private DbSet<ProductType> ProductTypes { get; set; }
public DbSet<Brand> Brands { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
4 changes: 2 additions & 2 deletions API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql("Host=localhost;Port=5432;Database=movie;Username=postgres;Password=")
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
);

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand All @@ -22,4 +21,5 @@
}

app.UseHttpsRedirection();
app.MapControllers();
app.Run();
6 changes: 5 additions & 1 deletion API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=movie;Username=postgres;Password="
}
}

1 change: 1 addition & 0 deletions Dockerfile-postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM postgres:16-alpine
6 changes: 6 additions & 0 deletions R508.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{C3B38DC2-9752-4DE7-B514-13E5D7C5C4A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API.Tests", "API.Tests\API.Tests.csproj", "{78E9942E-3C56-47E4-A41A-95F32FD47256}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{C3B38DC2-9752-4DE7-B514-13E5D7C5C4A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3B38DC2-9752-4DE7-B514-13E5D7C5C4A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3B38DC2-9752-4DE7-B514-13E5D7C5C4A3}.Release|Any CPU.Build.0 = Release|Any CPU
{78E9942E-3C56-47E4-A41A-95F32FD47256}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78E9942E-3C56-47E4-A41A-95F32FD47256}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78E9942E-3C56-47E4-A41A-95F32FD47256}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78E9942E-3C56-47E4-A41A-95F32FD47256}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ docker run -d \
-e POSTGRES_USER=postgres \
-e POSTGRES_HOST_AUTH_METHOD=trust \
-v pgdata_movie:/var/lib/postgresql/data \
-v ./init.sql:/docker-entrypoint-initdb.d/init.sql \
postgres:16
```

Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
postgres:
build:
context: .
dockerfile: Dockerfile-postgres
container_name: postgres_db
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
POSTGRES_DB: movie
POSTGRES_USER: postgres
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:
Loading
Loading