Compare commits

...

2 Commits
main ... dev

4
.gitignore vendored

@ -800,4 +800,6 @@ pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/rider,intellij,intellij+all,dotnetcore,csharp,python # End of https://www.toptal.com/developers/gitignore/api/rider,intellij,intellij+all,dotnetcore,csharp,python
**/appsettings*.json **/appsettings*.json
.DS_Store

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup> </PropertyGroup>

@ -0,0 +1,70 @@
using AutoMapper;
using CatalogService.Data;
using CatalogService.DTOs;
using CatalogService.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Shared.DTOs;
namespace CatalogService.Controllers;
[ApiController]
[Authorize]
[Route("api/catalog/[controller]")]
public class ExercicesController : ControllerBase
{
private readonly CatalogDbContext _context;
private readonly IMapper _mapper;
public ExercicesController(CatalogDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Create([FromBody] CreateExerciceTemplateDto dto)
{
var exercice = _mapper.Map<Exercice>(dto);
_context.Exercices.Add(exercice);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = exercice.Id }, _mapper.Map<ExerciceTemplateDto>(exercice));
}
[HttpPut("{id}")]
[AllowAnonymous]
public async Task<IActionResult> Update(string id, [FromBody] UpdateExerciceTemplateDto dto)
{
var exercice = await _context.Exercices.FindAsync(id);
if (exercice == null) return NotFound();
_mapper.Map(dto, exercice);
exercice.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
return NoContent();
}
[HttpDelete("{id}")]
[AllowAnonymous]
public async Task<IActionResult> Delete(string id)
{
var exercice = await _context.Exercices.FindAsync(id);
if (exercice == null) return NotFound();
_context.Exercices.Remove(exercice);
await _context.SaveChangesAsync();
return NoContent();
}
[HttpGet("{id}")]
[AllowAnonymous]
public async Task<ActionResult<ExerciceTemplateDto>> GetById(string id)
{
var exercice = await _context.Exercices.FindAsync(id);
if (exercice == null) return NotFound();
return _mapper.Map<ExerciceTemplateDto>(exercice);
}
}

@ -6,10 +6,9 @@ namespace CatalogService.DTOs;
public class CreateExerciceTemplateDto public class CreateExerciceTemplateDto
{ {
[Required] [Required]
public string Name { get; set; } public required string Name { get; set; }
[Required] public string? Description { get; set; } = default;
public string Description { get; set; }
public ETarget? Target { get; set; } = ETarget.None; public ETarget? Target { get; set; } = ETarget.None;

@ -1,6 +1,6 @@
using Shared.Enum; using Shared.Enum;
namespace Shared.DTOs; namespace CatalogService.DTOs;
public class ExerciceTemplateDto public class ExerciceTemplateDto
{ {

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
using Shared.Enum;
namespace CatalogService.DTOs;
public class UpdateExerciceTemplateDto
{
public string? Name { get; set; }
public string? Description { get; set; }
public ETarget? Target { get; set; }
public string? ImageUrl { get; set; }
public string? VideoUrl { get; set; }
}

@ -10,6 +10,7 @@ builder.Services.AddDbContext<CatalogDbContext>(opt =>
opt.UseNpgsql(builder.Configuration.GetConnectionString("CatalogDb")); opt.UseNpgsql(builder.Configuration.GetConnectionString("CatalogDb"));
}); });
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
var app = builder.Build(); var app = builder.Build();

@ -0,0 +1,16 @@
using AutoMapper;
using CatalogService.DTOs;
using CatalogService.Entities;
namespace CatalogService.RequestHelpers;
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<Exercice, ExerciceTemplateDto>();
CreateMap<ExerciceTemplateDto, Exercice>();
CreateMap<CreateExerciceTemplateDto, Exercice>();
CreateMap<UpdateExerciceTemplateDto, Exercice>();
}
}

@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;
namespace Shared.DTOs;
public class DeleteDto
{
[Required]
public required string Id { get; set; }
}

@ -0,0 +1,231 @@
{
"info": {
"_postman_id": "c019ebd5-bed6-49ea-a584-086e91bfe503",
"name": "Catalog Exercices",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Créer un exercice",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = pm.response.json();",
"pm.test('Status code is 201', function() {",
" pm.response.to.have.status(201);",
"});",
"pm.test('L\'exercice a un id', function() {",
" pm.expect(jsonData.id).to.be.a('string');",
"});",
"pm.environment.set('exerciceId', jsonData.id);"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"Pompes\",\n \"description\": \"Exercice de musculation pour les pectoraux\",\n \"target\": 1,\n \"imageUrl\": \"images/pompes.jpg\",\n \"videoUrl\": \"https://www.youtube.com/watch?v=IODxDxX7oi4\"\n}"
},
"url": {
"raw": "{{catalogApi}}/api/exercices",
"host": [
"{{catalogApi}}"
],
"path": [
"api",
"exercices"
]
}
}
},
{
"name": "Récupérer l'exercice par id",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = pm.response.json();",
"pm.test('Status code is 200', function() {",
" pm.response.to.have.status(200);",
"});",
"pm.test('Nom de l\\'exercice est Pompes', function() {",
" pm.expect(jsonData.name).to.eq('Pompes');",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{catalogApi}}/api/exercices/{{exerciceId}}",
"host": [
"{{catalogApi}}"
],
"path": [
"api",
"exercices",
"{{exerciceId}}"
]
}
}
},
{
"name": "Mettre à jour l'exercice",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test('Status code is 204', function() {",
" pm.response.to.have.status(204);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"id\": \"{{exerciceId}}\",\n \"name\": \"Pompes modifiées\",\n \"description\": \"Exercice modifié\",\n \"target\": 2,\n \"imageUrl\": \"images/pompes_mod.jpg\",\n \"videoUrl\": \"https://www.youtube.com/watch?v=abcd\"\n}"
},
"url": {
"raw": "{{catalogApi}}/api/exercices/{{exerciceId}}",
"host": [
"{{catalogApi}}"
],
"path": [
"api",
"exercices",
"{{exerciceId}}"
]
}
}
},
{
"name": "Vérifier la mise à jour",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = pm.response.json();",
"pm.test('Status code is 200', function() {",
" pm.response.to.have.status(200);",
"});",
"pm.test('Nom de l\\'exercice est Pompes modifiées', function() {",
" pm.expect(jsonData.name).to.eq('Pompes modifiées');",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{catalogApi}}/api/exercices/{{exerciceId}}",
"host": [
"{{catalogApi}}"
],
"path": [
"api",
"exercices",
"{{exerciceId}}"
]
}
}
},
{
"name": "Supprimer l'exercice",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test('Status code is 204', function() {",
" pm.response.to.have.status(204);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "{{catalogApi}}/api/exercices/{{exerciceId}}",
"host": [
"{{catalogApi}}"
],
"path": [
"api",
"exercices",
"{{exerciceId}}"
]
}
}
},
{
"name": "Vérifier la suppression",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test('Status code is 404', function() {",
" pm.response.to.have.status(404);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{catalogApi}}/api/exercices/{{exerciceId}}",
"host": [
"{{catalogApi}}"
],
"path": [
"api",
"exercices",
"{{exerciceId}}"
]
}
}
}
],
"variable": [
{
"key": "catalogApi",
"value": "http://localhost:5000"
}
]
}
Loading…
Cancel
Save