Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
|
e6bcf72fd1 | 1 year ago |
|
0ac95dfae6 | 1 year ago |
|
a1ea2de7bb | 1 year ago |
|
79f898cd47 | 1 year ago |
@ -0,0 +1,77 @@
|
||||
using Asp.Versioning;
|
||||
using Dto;
|
||||
using Entities;
|
||||
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();
|
||||
}
|
||||
}
|
||||
[HttpGet("notePad/{idUser:int}/{idInquiry:int}")]
|
||||
[ProducesResponseType(typeof(UserDto), 200)]
|
||||
[ProducesResponseType(typeof(string), 404)]
|
||||
public IActionResult GetNotePadByIdInquiryAndIdUser(int idUser, int idInquiry)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.LogInformation(" [INFORMATION] Le bloc note a été trouvé.");
|
||||
return Ok(notePadService.GetNotePadByIdUserAndIdInquiry(idUser, idInquiry));
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
logger.LogError("[ERREUR] Aucun bloc n'a été trouvé.");
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(UserDto), 201)]
|
||||
[ProducesResponseType(typeof(string), 409)]
|
||||
public IActionResult CreateNotePad([FromBody] NotepadDto notepadDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
var existingNotePad = notePadService.GetNotePadByIdUserAndIdInquiry(notepadDto.UserId, notepadDto.InquiryId);
|
||||
logger.LogError("[ERREUR] Le bloc note existe déjà impossible de le crée");
|
||||
return StatusCode(409, existingNotePad);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return Ok(notePadService.CreateNotePad(notepadDto.Id, notepadDto.UserId, notepadDto.InquiryId, notepadDto.Notes));
|
||||
}
|
||||
}
|
||||
[HttpDelete]
|
||||
[ProducesResponseType(typeof(UserDto), 200)]
|
||||
[ProducesResponseType(typeof(string), 404)]
|
||||
public IActionResult DeleteNotePad(int id)
|
||||
{
|
||||
if (notePadService.DeleteNotePad(id))
|
||||
{
|
||||
logger.LogInformation("[INFORMATION] le bloc note avec l'id {id} a été supprimé.", id);
|
||||
return Ok();
|
||||
}
|
||||
logger.LogError("[ERREUR] Aucun bloc note trouvé avec l'id {id}.", id);
|
||||
return NotFound();
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
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();
|
||||
|
||||
public NotepadDto GetNotePadByIdUserAndIdInquiry(int idUser, int idInquiry) =>
|
||||
notePadService.GetNotePadByIdUserAndIdInquiry(idUser, idInquiry).FromEntityToDto();
|
||||
|
||||
public NotepadDto CreateNotePad(int id, int idUser, int idInquiry, string note) =>
|
||||
notePadService.CreateNotePad(id, idUser, idInquiry, note).FromEntityToDto();
|
||||
|
||||
public bool DeleteNotePad(int id) => notePadService.DeleteNotePad(id);
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
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;
|
||||
}
|
||||
|
||||
public NotepadEntity GetNotePadByIdUserAndIdInquiry(int idUser, int idInquiry)
|
||||
{
|
||||
var notPadEntity = DbContext.Notepads.FirstOrDefault(n => n.InquiryId == idInquiry && n.UserId == idUser);
|
||||
if (notPadEntity == null)
|
||||
{
|
||||
throw new ArgumentException("impossible de trouver le bloc note", nameof(idInquiry));
|
||||
}
|
||||
|
||||
return notPadEntity;
|
||||
}
|
||||
|
||||
public NotepadEntity CreateNotePad(int id, int idUser, int idInquiry, string note)
|
||||
{
|
||||
var notePadEntity = new NotepadEntity
|
||||
{
|
||||
Id = id,
|
||||
UserId = idUser,
|
||||
InquiryId = idInquiry,
|
||||
Notes = note
|
||||
};
|
||||
DbContext.Notepads.Add(notePadEntity);
|
||||
DbContext.SaveChangesAsync();
|
||||
return notePadEntity;
|
||||
}
|
||||
|
||||
public bool DeleteNotePad(int id)
|
||||
{
|
||||
var notePadEntity = DbContext.Notepads.FirstOrDefault(n => n.Id == id);
|
||||
if (notePadEntity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DbContext.Notepads.Remove(notePadEntity);
|
||||
DbContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using Dto;
|
||||
|
||||
namespace Shared;
|
||||
|
||||
public interface INotePadService<TNotePad>
|
||||
{
|
||||
public TNotePad GetNotePadById(int id);
|
||||
public TNotePad GetNotePadByIdUserAndIdInquiry(int idUser, int idInquiry);
|
||||
public TNotePad CreateNotePad(int id, int idUser, int idInquiry, string note);
|
||||
public bool DeleteNotePad(int id);
|
||||
|
||||
}
|
Loading…
Reference in new issue