diff --git a/API_SQLuedo/API/Controllers/InquiryController.cs b/API_SQLuedo/API/Controllers/InquiriesController.cs similarity index 94% rename from API_SQLuedo/API/Controllers/InquiryController.cs rename to API_SQLuedo/API/Controllers/InquiriesController.cs index c6326ad..dc2201d 100644 --- a/API_SQLuedo/API/Controllers/InquiryController.cs +++ b/API_SQLuedo/API/Controllers/InquiriesController.cs @@ -8,13 +8,13 @@ namespace API.Controllers [Route("api/[controller]")] [Authorize] [ApiController] - public class InquiryController : Controller + public class InquiriesController : Controller { private IDataService _inquiryDataService; - private readonly ILogger _logger; + private readonly ILogger _logger; - public InquiryController(IDataService inquiryDataService) + public InquiriesController(IDataService inquiryDataService) { _inquiryDataService = inquiryDataService; } diff --git a/API_SQLuedo/API/Controllers/LessonController.cs b/API_SQLuedo/API/Controllers/LessonsController.cs similarity index 94% rename from API_SQLuedo/API/Controllers/LessonController.cs rename to API_SQLuedo/API/Controllers/LessonsController.cs index 912196e..a408f26 100644 --- a/API_SQLuedo/API/Controllers/LessonController.cs +++ b/API_SQLuedo/API/Controllers/LessonsController.cs @@ -8,13 +8,13 @@ namespace API.Controllers [Route("api/[controller]")] [Authorize] [ApiController] - public class LessonController : Controller + public class LessonsController : Controller { private IDataService _lessonDataService; - private readonly ILogger _logger; + private readonly ILogger _logger; - public LessonController(IDataService lessonDataService) + public LessonsController(IDataService lessonDataService) { _lessonDataService = lessonDataService; } diff --git a/API_SQLuedo/API/Controllers/ParagraphsController.cs b/API_SQLuedo/API/Controllers/ParagraphsController.cs new file mode 100644 index 0000000..11b0b72 --- /dev/null +++ b/API_SQLuedo/API/Controllers/ParagraphsController.cs @@ -0,0 +1,117 @@ +using DbContextLib; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Model.Business; +using Model.DTO; +using Services; + +namespace API.Controllers +{ + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class ParagraphsController : Controller + { + private IDataService _paragraphDataService; + + private readonly ILogger _logger; + + public ParagraphsController(IDataService paragraphDataService, ILogger logger) + { + _paragraphDataService = paragraphDataService; + _logger = logger; + } + + [HttpGet("paragraphs/{page}/{number}")] + public async Task GetParagraphs(int page, int number) + { + var nbParagraph = (await _paragraphDataService.GetParagraphs(page, number)).ToList().Count(); + if(nbParagraph == 0) + { + _logger.LogError("[ERREUR] Aucun paragraphe trouvé."); + return StatusCode(204); + } + _logger.LogInformation("[INFORMATION] {nb} Paragraphe(s) trouvé(s)", nbParagraph); + return Ok(_paragraphDataService.GetParagraphs(page, number)); + } + + [HttpGet("paragraph/id/{id}")] + public async Task GetParagraphById(int id) + { + try + { + _logger.LogInformation("[INFORMATION] Le paragraphe avec l'id {id} a été trouvé.", id); + return Ok(_paragraphDataService.GetParagraphById(id)); + } catch (ArgumentException) + { + _logger.LogError("[ERREUR] Aucun paragraphe trouvé avec l'id {id}.", id); + return NotFound(); + } + } + + [HttpGet("paragraph/title/{title}")] + public async Task GetParagraphByTitle(string title) + { + try + { + _logger.LogInformation("[INFORMATION] Le paragraphe avec le titre {title} a été trouvé.", title); + return Ok(_paragraphDataService.GetUserByUsername(title)); + }catch (ArgumentException) + { + _logger.LogError("[ERREUR] Aucun paragraphe trouvé avec le titre {title}.", title); + return NotFound(); + } + + } + + [HttpDelete] + public async Task DeleteParagraph(int id) + { + var success = await _paragraphDataService.DeleteParagraph(id); + if(success) + { + _logger.LogInformation("[INFORMATION] Le paragraphe avec l'id {id} a été supprimé.", id); + return Ok(_paragraphDataService.DeleteParagraph(id)); + } else + { + _logger.LogError("[ERREUR] Aucun paragraphe trouvé avec l'id {id}.", id); + return NotFound(); + } + + } + + [HttpPost] + public async Task CreateParagraph([FromBody] ParagraphDTO dto) + { + if (dto.Title == null || dto.Content == null || dto.Info == null || dto.Query == null || dto.Comment == null) + { + return BadRequest(); + } + _logger.LogInformation("[INFORMATION] Un paragraphe a été créé : title - {title}, content - {content}, info - {info}, query - {query}, comment - {comment}", dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment); + return Created(nameof(GetParagraphs), _paragraphDataService.CreateParagraph(dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment)); + } + + [HttpPut] + public async Task UpdateParagraph(int id, [FromBody] ParagraphDTO paragraphDTO) + { + if(id != paragraphDTO.Id) + { + _logger.LogError("[ERREUR] Problème ID - La mise à jour du paragraphe avec l'id {id} a échouée.", id); + return BadRequest(); + } + if(!ModelState.IsValid) + { + _logger.LogError("[ERREUR] Problème controlleur - La mise à jour du paragraphe avec l'id {id} a échouée.", id); + return BadRequest(); + } + if(paragraphDTO != null) + { + _logger.LogInformation("[INFORMATION] La mise à jour du paragraphe avec l'id {id} a été effectuée", id); + return Ok(_paragraphDataService.UpdateParagraph(id, paragraphDTO)); + } + _logger.LogError("[ERREUR] Aucun paragraphe trouvé avec l'id {id}.", id); + return NotFound(); + } + } +} diff --git a/API_SQLuedo/API/Controllers/SuccessesController.cs b/API_SQLuedo/API/Controllers/SuccessesController.cs new file mode 100644 index 0000000..c4f0d72 --- /dev/null +++ b/API_SQLuedo/API/Controllers/SuccessesController.cs @@ -0,0 +1,117 @@ +using DbContextLib; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Model.Business; +using Model.DTO; +using Services; + +namespace API.Controllers +{ + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class SuccessesController : Controller + { + private IDataService _successDataService; + + private readonly ILogger _logger; + + public SuccessesController(IDataService successDataService, ILogger logger) + { + _successDataService = successDataService; + _logger = logger; + } + + [HttpGet("successes/{page}/{number}")] + public async Task GetSuccesses(int page, int number) + { + var nbUser = (await _successDataService.GetUsers(page, number)).ToList().Count(); + if(nbUser == 0) + { + _logger.LogError("[ERREUR] Aucun utilisateur trouvé."); + return StatusCode(204); + } + _logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", nbUser); + return Ok(_successDataService.GetUsers(page, number)); + } + + [HttpGet("success/user/{id}")] + public async Task GetSuccessByUserId(int userId) + { + try + { + _logger.LogInformation("[INFORMATION] Le succès avec l'id de l'utilisateur {id} a été trouvé.", userId); + return Ok(_successDataService.GetSuccessByUserId(userId)); + } catch (ArgumentException) + { + _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id de l'utilisateur {id}.", userId); + return NotFound(); + } + } + + [HttpGet("success/inquiry/{inquiryId}")] + public async Task GetSuccessByInquiryId(int inquiryId) + { + try + { + _logger.LogInformation("[INFORMATION] Utilisateur avec l'id de l'enquête {inquiryId} a été trouvé.", inquiryId); + return Ok(_successDataService.GetSuccessByInquiryId(inquiryId)); + }catch (ArgumentException) + { + _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id de l'enquête {inquiryId}.", inquiryId); + return NotFound(); + } + + } + + [HttpDelete] + public async Task DeleteSuccess(int id) + { + var success = await _successDataService.DeleteSuccess(id); + if(success) + { + _logger.LogInformation("[INFORMATION] Le succès avec l'id {id} a été supprimé.", id); + return Ok(_successDataService.DeleteSuccess(id)); + } else + { + _logger.LogError("[ERREUR] Aucun succès trouvé avec l'id {id}.", id); + return NotFound(); + } + + } + + [HttpPost] + public async Task CreateSuccess([FromBody] SuccessDTO dto) + { + /*if (dto.UserId == null || dto.InquiryId == null) + { + return BadRequest(); + }*/ + _logger.LogInformation("[INFORMATION] Un succès a été créé : userId - {userId}, inquiryId - {inquiryId}, isFinished - {isFinished}", dto.UserId, dto.InquiryId, dto.IsFinished); + return Created(nameof(GetSuccesses), _successDataService.CreateSuccess(dto.UserId, dto.InquiryId, dto.IsFinished)); + } + + [HttpPut] + public async Task UpdateSuccess(int id, [FromBody] SuccessDTO successDTO) + { + if(id != successDTO.UserId) + { + _logger.LogError("[ERREUR] Problème ID - La mise à jour du succès avec l'id de l'utilisateur {id} a échouée.", id); + return BadRequest(); + } + if(!ModelState.IsValid) + { + _logger.LogError("[ERREUR] Problème controlleur - La mise à jour du succès avec l'id de l'utilisateur {id} a échouée.", id); + return BadRequest(); + } + if(successDTO != null) + { + _logger.LogInformation("[INFORMATION] La mise à jour du succès avec l'id de l'utilisateur {id} a été effectuée", id); + return Ok(_successDataService.UpdateSuccess(id, successDTO)); + } + _logger.LogError("[ERREUR] Aucun succès trouvé avec l'id de l'utilisateur {id}.", id); + return NotFound(); + } + } +} diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UsersController.cs similarity index 93% rename from API_SQLuedo/API/Controllers/UserController.cs rename to API_SQLuedo/API/Controllers/UsersController.cs index 450c64a..ca36890 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UsersController.cs @@ -11,13 +11,13 @@ namespace API.Controllers [Route("api/[controller]")] [Authorize] [ApiController] - public class UserController : Controller + public class UsersController : Controller { private IDataService _userDataService; - private readonly ILogger _logger; + private readonly ILogger _logger; - public UserController(IDataService userDataService, ILogger logger) + public UsersController(IDataService userDataService, ILogger logger) { _userDataService = userDataService; _logger = logger; diff --git a/API_SQLuedo/Model/Business/Paragraph.cs b/API_SQLuedo/Model/Business/Paragraph.cs new file mode 100644 index 0000000..7b54a9b --- /dev/null +++ b/API_SQLuedo/Model/Business/Paragraph.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model.Business +{ + public class Paragraph + { + public int Id { get; } + public string Title { get; set; } + public string Content { get; set; } + public string Info { get; set; } + public string Query { get; set; } + public string Comment { get; set; } + + public Paragraph() { } + + public Paragraph(int id, string title, string content, string info, string query, string comment) + { + Id = id; + Title = title; + Content = content; + Info = info; + Query = query; + Comment = comment; + } + public Paragraph(string title, string content, string info, string query, string comment) + { + Title = title; + Content = content; + Info = info; + Query = query; + Comment = comment; + } + } +} diff --git a/API_SQLuedo/Model/Business/Success.cs b/API_SQLuedo/Model/Business/Success.cs new file mode 100644 index 0000000..ca79bad --- /dev/null +++ b/API_SQLuedo/Model/Business/Success.cs @@ -0,0 +1,34 @@ +using Entities.SQLudeoDB; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model.Business +{ + public class Success + { + public int UserId { get; set; } + public UserEntity User { get; set; } + public int InquiryId { get; set; } + public InquiryEntity Inquiry { get; set; } + public bool IsFinished { get; set; } + + public Success() { } + + public Success(int userId, int inquiryId, bool isFinished) + { + UserId = userId; + InquiryId = inquiryId; + IsFinished = isFinished; + } + + public Success(UserEntity user, InquiryEntity inquiry, bool isFinished) + { + User = user; + Inquiry = inquiry; + IsFinished = isFinished; + } + } +} diff --git a/API_SQLuedo/Model/DTO/ParagraphDTO.cs b/API_SQLuedo/Model/DTO/ParagraphDTO.cs new file mode 100644 index 0000000..f0a7149 --- /dev/null +++ b/API_SQLuedo/Model/DTO/ParagraphDTO.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model.DTO +{ + public class ParagraphDTO + { + public int Id { get; } + public string Title { get; set; } + public string Content { get; set; } + public string Info { get; set; } + public string Query { get; set; } + public string Comment { get; set; } + + public ParagraphDTO() { } + + public ParagraphDTO(int id, string title, string content, string info, string query, string comment) + { + Id = id; + Title = title; + Content = content; + Info = info; + Query = query; + Comment = comment; + } + + } +} diff --git a/API_SQLuedo/Model/DTO/SuccessDTO.cs b/API_SQLuedo/Model/DTO/SuccessDTO.cs new file mode 100644 index 0000000..c22b63b --- /dev/null +++ b/API_SQLuedo/Model/DTO/SuccessDTO.cs @@ -0,0 +1,28 @@ +using Entities.SQLudeoDB; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model.DTO +{ + public class SuccessDTO + { + public int UserId { get; set; } + public UserEntity User { get; set; } + public int InquiryId { get; set; } + public InquiryEntity Inquiry { get; set; } + public bool IsFinished { get; set; } + + public SuccessDTO() { } + + public SuccessDTO(int userId, int inquiryId, bool isFinished) + { + UserId = userId; + InquiryId = inquiryId; + IsFinished = isFinished; + } + } +} diff --git a/API_SQLuedo/Model/Mappers/IMapper.cs b/API_SQLuedo/Model/Mappers/IMapper.cs deleted file mode 100644 index 01790e5..0000000 --- a/API_SQLuedo/Model/Mappers/IMapper.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices.ObjectiveC; -using System.Text; -using System.Threading.Tasks; - -namespace Model.Mappers -{ - public interface IMapper - { - public Object FromEntityToModel(object o); - public Object FromModelToDTO(object o); - public Object FromDTOToModel(object o); - public Object FromModelToEntity(object o); - } -} diff --git a/API_SQLuedo/Model/Mappers/ParagraphMapper.cs b/API_SQLuedo/Model/Mappers/ParagraphMapper.cs new file mode 100644 index 0000000..8a49ea2 --- /dev/null +++ b/API_SQLuedo/Model/Mappers/ParagraphMapper.cs @@ -0,0 +1,34 @@ +using Entities.SQLudeoDB; +using Model.Business; +using Model.DTO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model.Mappers +{ + public static class ParagraphMapper + { + public static Paragraph FromDTOToModel(this ParagraphDTO dto) + { + return new Paragraph(dto.Id, dto.Title, dto.Content, dto.Info, dto.Query, dto.Comment); + } + + public static Paragraph FromEntityToModel(this ParagraphEntity entity) + { + return new Paragraph(entity.Id, entity.Title, entity.Content, entity.Info, entity.Query, entity.Comment); + } + + public static ParagraphDTO FromModelToDTO(this Paragraph paragraph) + { + return new ParagraphDTO(paragraph.Id, paragraph.Title, paragraph.Content, paragraph.Info, paragraph.Query, paragraph.Comment); + } + + public static ParagraphEntity FromModelToEntity(this Paragraph paragraph) + { + return new ParagraphEntity(paragraph.Id, paragraph.Title, paragraph.Content, paragraph.Info, paragraph.Query, paragraph.Comment); + } + } +} diff --git a/API_SQLuedo/Model/Mappers/SuccessMapper.cs b/API_SQLuedo/Model/Mappers/SuccessMapper.cs new file mode 100644 index 0000000..f0d1eab --- /dev/null +++ b/API_SQLuedo/Model/Mappers/SuccessMapper.cs @@ -0,0 +1,29 @@ +using Entities.SQLudeoDB; +using Model.Business; +using Model.DTO; + +namespace Model.Mappers +{ + public static class SuccessMapper + { + public static Success FromDTOToModel(this SuccessDTO dto) + { + return new Success(dto.UserId, dto.InquiryId, dto.IsFinished); + } + + public static Success FromEntityToModel(this SuccessEntity entity) + { + return new Success(entity.UserId, entity.InquiryId, entity.IsFinished); + } + + public static SuccessDTO FromModelToDTO(this Success success) + { + return new SuccessDTO(success.UserId, success.InquiryId, success.IsFinished); + } + + public static SuccessEntity FromModelToEntity(this Success success) + { + return new SuccessEntity(success.UserId, success.InquiryId, success.IsFinished); + } + } +} diff --git a/API_SQLuedo/Services/IDataService.cs b/API_SQLuedo/Services/IDataService.cs index c296774..308ffaf 100644 --- a/API_SQLuedo/Services/IDataService.cs +++ b/API_SQLuedo/Services/IDataService.cs @@ -24,5 +24,17 @@ namespace Services public bool DeleteLesson(int id); public InquiryDTO UpdateLesson(int id, LessonDTO lesson); public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit); + public Task> GetParagraphs(int page, int number); + public Task GetParagraphById(int id); + public Task GetParagraphByTitle(string title); + public Task DeleteParagraph(int id); + public Task UpdateParagraph(int id, ParagraphDTO paragraphDTO); + public Task CreateParagraph(string title, string content, string info, string query, string comment); + public Task> GetSuccesses(int page, int number); + public Task GetSuccessByUserId(int id); + public Task GetSuccessByInquiryId(int id); + public Task DeleteSuccess(int id); + public Task UpdateSuccess(int id, SuccessDTO success); + public Task CreateSuccess(int userId, int inquiryId, bool isFinished); } } diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 4bffa90..995010a 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -1,65 +1,65 @@ -using DbContextLib; -using Model.DTO; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Model.Mappers; -using Model.Business; -using Microsoft.EntityFrameworkCore; -using Entities.SQLudeoDB; -using ModelToEntity; - -namespace Services -{ - public class UserDataService : IDataService - { - private UserDbContext DbContext { get; set; } - private readonly IDataServiceEF dataServiceEF; - public UserDataService(IDataServiceEF dataServiceEF) - { - this.dataServiceEF = dataServiceEF; - } - - public async Task GetUserById(int id) - { - var user = await dataServiceEF.GetUserById(id); - return user.FromModelToDTO(); - } - - public async Task GetUserByUsername(string username) - { - var user = await dataServiceEF.GetUserByUsername(username); - return user.FromModelToDTO(); - } - - public async Task> GetUsers(int page, int number) - { - var users = await dataServiceEF.GetUsers(page, number); - return users.Select(u => u.FromModelToDTO()); - } - - public async Task DeleteUser(int id) - { - var respons = await dataServiceEF.DeleteUser(id); - return respons; - } - - public async Task UpdateUser(int id, UserDTO user) - { - var updatingUser = await dataServiceEF.UpdateUser(id, user); +using DbContextLib; +using Model.DTO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Model.Mappers; +using Model.Business; +using Microsoft.EntityFrameworkCore; +using Entities.SQLudeoDB; +using ModelToEntity; + +namespace Services +{ + public class UserDataService : IDataService + { + private UserDbContext DbContext { get; set; } + private readonly IDataServiceEF dataServiceEF; + public UserDataService(IDataServiceEF dataServiceEF) + { + this.dataServiceEF = dataServiceEF; + } + + public async Task GetUserById(int id) + { + var user = await dataServiceEF.GetUserById(id); + return user.FromModelToDTO(); + } + + public async Task GetUserByUsername(string username) + { + var user = await dataServiceEF.GetUserByUsername(username); + return user.FromModelToDTO(); + } + + public async Task> GetUsers(int page, int number) + { + var users = await dataServiceEF.GetUsers(page, number); + return users.Select(u => u.FromModelToDTO()); + } + + public async Task DeleteUser(int id) + { + var respons = await dataServiceEF.DeleteUser(id); + return respons; + } + + public async Task UpdateUser(int id, UserDTO user) + { + var updatingUser = await dataServiceEF.UpdateUser(id, user); return updatingUser.FromModelToDTO(); - } - public async Task CreateUser(string username, string password, string email, bool isAdmin) - { - var newUserEntity = await dataServiceEF.CreateUser(username, password, email, isAdmin); - return newUserEntity.FromModelToDTO(); - } - - public IEnumerable GetInquiries(int page, int number) - { - throw new NotImplementedException(); + } + public async Task CreateUser(string username, string password, string email, bool isAdmin) + { + var newUserEntity = await dataServiceEF.CreateUser(username, password, email, isAdmin); + return newUserEntity.FromModelToDTO(); + } + + public IEnumerable GetInquiries(int page, int number) + { + throw new NotImplementedException(); } public InquiryDTO GetInquiryById(int id) @@ -115,5 +115,65 @@ namespace Services { throw new NotImplementedException(); } - } -} + + public Task> GetParagraphs(int page, int number) + { + throw new NotImplementedException(); + } + + public Task GetParagraphById(int id) + { + throw new NotImplementedException(); + } + + public Task GetParagraphByTitle(string title) + { + throw new NotImplementedException(); + } + + public Task DeleteParagraph(int id) + { + throw new NotImplementedException(); + } + + public Task UpdateParagraph(int id, ParagraphDTO paragraphDTO) + { + throw new NotImplementedException(); + } + + public Task CreateParagraph(string title, string content, string info, string query, string comment) + { + throw new NotImplementedException(); + } + + public Task> GetSuccesses(int page, int number) + { + throw new NotImplementedException(); + } + + public Task GetSuccessByUserId(int id) + { + throw new NotImplementedException(); + } + + public Task GetSuccessByInquiryId(int id) + { + throw new NotImplementedException(); + } + + public Task DeleteSuccess(int id) + { + throw new NotImplementedException(); + } + + public Task UpdateSuccess(int id, SuccessDTO success) + { + throw new NotImplementedException(); + } + + public Task CreateSuccess(int userId, int inquiryId, bool isFinished) + { + throw new NotImplementedException(); + } + } +}