ajout des services pour notePad, du controller et de la methode GetById
continuous-integration/drone/push Build is passing Details

notePad
Victor GABORIT 1 year ago
parent c6819ae2f8
commit 79f898cd47

@ -0,0 +1,31 @@
using Asp.Versioning;
using Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shared;
namespace API.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize]
[ApiVersion("1.0")]
[ApiController]
public class NotepadController(ILogger<NotepadController> logger, INotePadService<NotepadDto> notePadService) : ControllerBase
{
[HttpGet("notePad/{id:int}")]
[ProducesResponseType(typeof(UserDto), 200)]
[ProducesResponseType(typeof(string), 404)]
public IActionResult GetNotePadById(int id)
{
try
{
logger.LogInformation(" [INFORMATION] Le bloc note avec l'id {id} a été trouvé.", id);
return Ok(notePadService.GetNotePadById(id));
}
catch (ArgumentException)
{
logger.LogError("[ERREUR] Aucun bloc n'a été trouvé pour l'id {id}", id);
return NotFound();
}
}
}

@ -37,6 +37,9 @@ builder.Services.AddScoped<ISuccessService<SuccessDto>, SuccessDataServiceApi>()
builder.Services.AddScoped<ILessonService<LessonEntity>, LessonDataService>();
builder.Services.AddScoped<ILessonService<LessonDto>, LessonDataServiceApi>();
builder.Services.AddScoped<INotePadService<NotepadEntity>, NotePadDataService>();
builder.Services.AddScoped<INotePadService<NotepadDto>, NotePadDataServiceApi>();
builder.Services.AddDbContext<DbContext, UserDbContext>();
builder.Services.AddDbContext<WebAPIDbContext>(options => options.UseInMemoryDatabase("appDb"));
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddEntityFrameworkStores<WebAPIDbContext>();

@ -0,0 +1,11 @@
using Dto;
using Entities;
using Shared;
using Shared.Mapper;
namespace API.Service;
public class NotePadDataServiceApi(INotePadService<NotepadEntity> notePadService) : INotePadService<NotepadDto>
{
public NotepadDto GetNotePadById(int id) => notePadService.GetNotePadById(id).FromEntityToDto();
}

@ -0,0 +1,26 @@
using DbContextLib;
using Entities;
using Microsoft.EntityFrameworkCore;
using Shared;
namespace DbDataManager.Service;
public class NotePadDataService : INotePadService<NotepadEntity>
{
private UserDbContext DbContext { get; }
public NotePadDataService(UserDbContext context)
{
DbContext = context;
context.Database.EnsureCreated();
}
public NotepadEntity GetNotePadById(int id)
{
var notePadEntity = DbContext.Notepads.FirstOrDefault(n => n.Id == id);
if (notePadEntity == null)
{
throw new ArgumentException("impossible de trouver le bloc note", nameof(id));
}
return notePadEntity;
}
}

@ -0,0 +1,7 @@
namespace Shared;
public interface INotePadService<TNotePad>
{
public TNotePad GetNotePadById(int id);
}
Loading…
Cancel
Save