diff --git a/API_SQLuedo/API/Controllers/NotepadController.cs b/API_SQLuedo/API/Controllers/NotepadController.cs new file mode 100644 index 0000000..d174849 --- /dev/null +++ b/API_SQLuedo/API/Controllers/NotepadController.cs @@ -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 logger, INotePadService 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(); + } + } +} \ No newline at end of file diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index 0b0944b..9e86b38 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -37,6 +37,9 @@ builder.Services.AddScoped, SuccessDataServiceApi>() builder.Services.AddScoped, LessonDataService>(); builder.Services.AddScoped, LessonDataServiceApi>(); +builder.Services.AddScoped, NotePadDataService>(); +builder.Services.AddScoped, NotePadDataServiceApi>(); + builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); builder.Services.AddIdentityApiEndpoints().AddEntityFrameworkStores(); diff --git a/API_SQLuedo/API/Service/NotePadDataServiceApi.cs b/API_SQLuedo/API/Service/NotePadDataServiceApi.cs new file mode 100644 index 0000000..c09b35b --- /dev/null +++ b/API_SQLuedo/API/Service/NotePadDataServiceApi.cs @@ -0,0 +1,11 @@ +using Dto; +using Entities; +using Shared; +using Shared.Mapper; + +namespace API.Service; + +public class NotePadDataServiceApi(INotePadService notePadService) : INotePadService +{ + public NotepadDto GetNotePadById(int id) => notePadService.GetNotePadById(id).FromEntityToDto(); +} \ No newline at end of file diff --git a/API_SQLuedo/DbDataManager/Service/NotePadDataService.cs b/API_SQLuedo/DbDataManager/Service/NotePadDataService.cs new file mode 100644 index 0000000..b7a5fc8 --- /dev/null +++ b/API_SQLuedo/DbDataManager/Service/NotePadDataService.cs @@ -0,0 +1,26 @@ +using DbContextLib; +using Entities; +using Microsoft.EntityFrameworkCore; +using Shared; + +namespace DbDataManager.Service; + +public class NotePadDataService : INotePadService +{ + 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; + } +} \ No newline at end of file diff --git a/API_SQLuedo/Shared/INotePadService.cs b/API_SQLuedo/Shared/INotePadService.cs new file mode 100644 index 0000000..c941edf --- /dev/null +++ b/API_SQLuedo/Shared/INotePadService.cs @@ -0,0 +1,7 @@ +namespace Shared; + +public interface INotePadService +{ + public TNotePad GetNotePadById(int id); + +} \ No newline at end of file