Merge en cours de la branche filtrage sur master
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
8fdaca98f2
commit
01d6dd0e8c
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model.OrderCriteria;
|
||||||
|
|
||||||
|
public enum InquiryOrderCriteria
|
||||||
|
{
|
||||||
|
None, ByTitle, ByDescription, ByIsUser
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model.OrderCriteria;
|
||||||
|
|
||||||
|
public enum LessonOrderCriteria
|
||||||
|
{
|
||||||
|
None, ByTitle, ByLastPublisher, ByLastEdit
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model.OrderCriteria;
|
||||||
|
|
||||||
|
public enum ParagraphOrderCriteria
|
||||||
|
{
|
||||||
|
None, ByTitle, ByContent, ByInfo, ByQuery, ByComment
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model.OrderCriteria;
|
||||||
|
|
||||||
|
public enum SuccessOrderCriteria
|
||||||
|
{
|
||||||
|
None, ByUserId, ByInquiryId, ByIsFinished
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model.OrderCriteria;
|
||||||
|
|
||||||
|
public enum UserOrderCriteria
|
||||||
|
{
|
||||||
|
None, ById, ByUsername, ByEmail, ByIsAdmin
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using Dto;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface IInquiryDataService : IDataService
|
||||||
|
{
|
||||||
|
public IEnumerable<InquiryDTO> GetInquiries(int page, int number, InquiryOrderCriteria orderCriteria);
|
||||||
|
public InquiryDTO GetInquiryById(int id);
|
||||||
|
public InquiryDTO GetInquiryByTitle(string title);
|
||||||
|
public bool DeleteInquiry(int id);
|
||||||
|
public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry);
|
||||||
|
public InquiryDTO CreateInquiry(string title, string description, bool isUser, int tableId, int solutionId);
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface ILessonDataService<TLesson>
|
||||||
|
{
|
||||||
|
public IEnumerable<TLesson> GetLessons(int page, int number, LessonOrderCriteria orderCriteria);
|
||||||
|
public TLesson GetLessonById(int id);
|
||||||
|
public TLesson GetLessonByTitle(string title);
|
||||||
|
public bool DeleteLesson(int id);
|
||||||
|
public TLesson UpdateLesson(int id, TLesson lesson);
|
||||||
|
public TLesson CreateLesson(string title, string lastPublisher, DateOnly? lastEdit);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using Dto;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface IParagraphDataService : IDataService
|
||||||
|
{
|
||||||
|
public IEnumerable<ParagraphDTO> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria);
|
||||||
|
public ParagraphDTO GetParagraphById(int id);
|
||||||
|
public ParagraphDTO GetParagraphByTitle(string title);
|
||||||
|
public bool DeleteParagraph(int id);
|
||||||
|
public ParagraphDTO UpdateParagraph(int id, ParagraphDTO paragraph);
|
||||||
|
public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId);
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using Dto;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface ISuccessDataService
|
||||||
|
{
|
||||||
|
public IEnumerable<SuccessDTO> GetSuccesses(int page, int number, SuccessOrderCriteria orderCriteria);
|
||||||
|
public SuccessDTO GetSuccessByUserId(int id);
|
||||||
|
public SuccessDTO GetSuccessByInquiryId(int id);
|
||||||
|
public bool DeleteSuccess(int id);
|
||||||
|
public SuccessDTO UpdateSuccess(int id, SuccessDTO success);
|
||||||
|
public SuccessDTO CreateSuccess(int userId, int inquiryId, bool isFinished);
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using Dto;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface IUserDataService : IDataService
|
||||||
|
{
|
||||||
|
public IEnumerable<UserDTO> GetUsers(int page, int number, UserOrderCriteria orderCriteria);
|
||||||
|
public UserDTO GetUserById(int id);
|
||||||
|
public UserDTO GetUserByUsername(string username);
|
||||||
|
public bool DeleteUser(int id);
|
||||||
|
public UserDTO UpdateUser(int id, UserDTO user);
|
||||||
|
public UserDTO CreateUser(string username, string password, string email, bool isAdmin);
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Dto;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using DbContextLib;
|
||||||
|
using Shared.Mapper;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public class InquiryDataService : IInquiryDataService
|
||||||
|
{
|
||||||
|
private UserDbContext DbContext { get; set; }
|
||||||
|
|
||||||
|
public InquiryDataService(UserDbContext context)
|
||||||
|
{
|
||||||
|
DbContext = context;
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<InquiryDTO> GetInquiries(int page, int number, InquiryOrderCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
IQueryable<InquiryEntity> query = DbContext.Inquiries.Skip((page - 1) * number).Take(number);
|
||||||
|
switch (orderCriteria)
|
||||||
|
{
|
||||||
|
case InquiryOrderCriteria.None:
|
||||||
|
break;
|
||||||
|
case InquiryOrderCriteria.ByTitle:
|
||||||
|
query = query.OrderBy(s => s.Title);
|
||||||
|
break;
|
||||||
|
case InquiryOrderCriteria.ByDescription:
|
||||||
|
query = query.OrderBy(s => s.Description);
|
||||||
|
break;
|
||||||
|
case InquiryOrderCriteria.ByIsUser:
|
||||||
|
query = query.OrderBy(s => s.IsUser);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var inquiries = query.ToList();
|
||||||
|
return inquiries.Select(s => s.FromEntityToDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
public InquiryDTO GetInquiryById(int id)
|
||||||
|
{
|
||||||
|
var inquiryEntity = DbContext.Inquiries.FirstOrDefault(i => i.Id == id);
|
||||||
|
if (inquiryEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver l'enquête", nameof(id));
|
||||||
|
}
|
||||||
|
return inquiryEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InquiryDTO GetInquiryByTitle(string title)
|
||||||
|
{
|
||||||
|
var inquiryEntity = DbContext.Inquiries.FirstOrDefault(i => i.Title == title);
|
||||||
|
if (inquiryEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver l'enquête", nameof(title));
|
||||||
|
}
|
||||||
|
return inquiryEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InquiryDTO CreateInquiry(string title, string description, bool isUser, int tableId, int solutionId)
|
||||||
|
{
|
||||||
|
var newInquiryEntity = new InquiryDTO(title, description, isUser, tableId, solutionId);
|
||||||
|
DbContext.Inquiries.Add(newInquiryEntity.FromDTOToEntity());
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return newInquiryEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeleteInquiry(int id)
|
||||||
|
{
|
||||||
|
var inquiryEntity = DbContext.Inquiries.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (inquiryEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
DbContext.Inquiries.Remove(inquiryEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry)
|
||||||
|
{
|
||||||
|
var updatingInquiry = DbContext.Inquiries.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (updatingInquiry == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver l'enquête", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
updatingInquiry.Title = inquiry.Title;
|
||||||
|
updatingInquiry.Description = inquiry.Description;
|
||||||
|
updatingInquiry.IsUser = inquiry.IsUser;
|
||||||
|
//updatingInquiry.Database = inquiry.Database.FromDTOToEntity();
|
||||||
|
//updatingInquiry.InquiryTable = inquiry.InquiryTable.FromDTOToEntity();
|
||||||
|
// Permet d'indiquer en Db que l'entité a été modifiée.
|
||||||
|
DbContext.Entry(updatingInquiry).State = EntityState.Modified;
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return updatingInquiry.FromEntityToModel().FromModelToDTO();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Dto;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Shared.Mapper;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public class LessonDataService : ILessonDataService
|
||||||
|
{
|
||||||
|
private UserDbContext DbContext { get; set; }
|
||||||
|
public LessonDataService(UserDbContext context)
|
||||||
|
{
|
||||||
|
DbContext = context;
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
public IEnumerable<LessonDTO> GetLessons(int page, int number, LessonOrderCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
IQueryable<LessonEntity> query = DbContext.Lessons.Skip((page - 1) * number).Take(number);
|
||||||
|
switch (orderCriteria)
|
||||||
|
{
|
||||||
|
case LessonOrderCriteria.None:
|
||||||
|
break;
|
||||||
|
case LessonOrderCriteria.ByTitle:
|
||||||
|
query = query.OrderBy(s => s.Title);
|
||||||
|
break;
|
||||||
|
case LessonOrderCriteria.ByLastPublisher:
|
||||||
|
query = query.OrderBy(s => s.LastPublisher);
|
||||||
|
break;
|
||||||
|
case LessonOrderCriteria.ByLastEdit:
|
||||||
|
query = query.OrderBy(s => s.LastEdit);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var lessons = query.ToList();
|
||||||
|
return lessons.Select(s => s.FromEntityToDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
public LessonDTO GetLessonById(int id)
|
||||||
|
{
|
||||||
|
var lessonEntity = DbContext.Lessons.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (lessonEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver la leçon", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return lessonEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LessonDTO GetLessonByTitle(string title)
|
||||||
|
{
|
||||||
|
var lessonEntity = DbContext.Lessons.FirstOrDefault(u => u.Title == title);
|
||||||
|
if (lessonEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver la leçon", nameof(title));
|
||||||
|
}
|
||||||
|
|
||||||
|
return lessonEntity.FromEntityToModel().FromModelToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeleteLesson(int id)
|
||||||
|
{
|
||||||
|
var lessonEntity = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
|
||||||
|
if (lessonEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DbContext.Lessons.Remove(lessonEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LessonDTO UpdateLesson(int id, LessonDTO lesson)
|
||||||
|
{
|
||||||
|
var updatingLesson = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
|
||||||
|
if (updatingLesson == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver la leçon", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
updatingLesson.Title = lesson.Title;
|
||||||
|
updatingLesson.LastPublisher = lesson.LastPublisher;
|
||||||
|
updatingLesson.LastEdit = lesson.LastEdit;
|
||||||
|
// Permet d'indiquer en Db que l'entité a été modifiée.
|
||||||
|
DbContext.Entry(updatingLesson).State = EntityState.Modified;
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return updatingLesson.FromEntityToModel().FromModelToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LessonDTO CreateLesson(string title, string lastPublisher, DateOnly? lastEdit)
|
||||||
|
{
|
||||||
|
var newLessonEntity = new LessonDTO()
|
||||||
|
{
|
||||||
|
Title = title,
|
||||||
|
LastPublisher = lastPublisher,
|
||||||
|
LastEdit = lastEdit,
|
||||||
|
};
|
||||||
|
DbContext.Lessons.Add(newLessonEntity.FromDTOToModel().FromModelToEntity());
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return newLessonEntity;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,117 @@
|
|||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Dto;
|
||||||
|
using DbContextLib;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Shared.Mapper;
|
||||||
|
|
||||||
|
namespace Services;
|
||||||
|
|
||||||
|
public class ParagraphDataService : IParagraphDataService
|
||||||
|
{
|
||||||
|
private UserDbContext DbContext { get; set; }
|
||||||
|
public ParagraphDataService(UserDbContext context)
|
||||||
|
{
|
||||||
|
DbContext = context;
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<ParagraphDTO> GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
IQueryable<ParagraphEntity> query = DbContext.Paragraphs.Skip((page - 1) * number).Take(number);
|
||||||
|
switch (orderCriteria)
|
||||||
|
{
|
||||||
|
case ParagraphOrderCriteria.None:
|
||||||
|
break;
|
||||||
|
case ParagraphOrderCriteria.ByTitle:
|
||||||
|
query = query.OrderBy(s => s.Title);
|
||||||
|
break;
|
||||||
|
case ParagraphOrderCriteria.ByContent:
|
||||||
|
query = query.OrderBy(s => s.Content);
|
||||||
|
break;
|
||||||
|
case ParagraphOrderCriteria.ByInfo:
|
||||||
|
query = query.OrderBy(s => s.Info);
|
||||||
|
break;
|
||||||
|
case ParagraphOrderCriteria.ByQuery:
|
||||||
|
query = query.OrderBy(s => s.Query);
|
||||||
|
break;
|
||||||
|
case ParagraphOrderCriteria.ByComment:
|
||||||
|
query = query.OrderBy(s => s.Comment);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var paragraphs = query.ToList();
|
||||||
|
return paragraphs.Select(s => s.FromEntityToDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParagraphDTO GetParagraphById(int id)
|
||||||
|
{
|
||||||
|
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (paragraphEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return paragraphEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParagraphDTO GetParagraphByTitle(string title)
|
||||||
|
{
|
||||||
|
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Title == title);
|
||||||
|
if (paragraphEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(title));
|
||||||
|
}
|
||||||
|
|
||||||
|
return paragraphEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeleteParagraph(int id)
|
||||||
|
{
|
||||||
|
var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id);
|
||||||
|
if (paragraphEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DbContext.Paragraphs.Remove(paragraphEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParagraphDTO UpdateParagraph(int id, ParagraphDTO paragraphDTO)
|
||||||
|
{
|
||||||
|
var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id);
|
||||||
|
if (updatingParagraph == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
updatingParagraph.Title = paragraphDTO.Title;
|
||||||
|
updatingParagraph.Content = paragraphDTO.Content;
|
||||||
|
updatingParagraph.Info = paragraphDTO.Info;
|
||||||
|
updatingParagraph.Query = paragraphDTO.Query;
|
||||||
|
updatingParagraph.Comment = paragraphDTO.Comment;
|
||||||
|
// Permet d'indiquer en Db que l'entité a été modifiée.
|
||||||
|
DbContext.Entry(updatingParagraph).State = EntityState.Modified;
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return updatingParagraph.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId)
|
||||||
|
{
|
||||||
|
var newParagraphEntity = new ParagraphDTO()
|
||||||
|
{
|
||||||
|
Title = title,
|
||||||
|
Content = content,
|
||||||
|
Info = info,
|
||||||
|
Query = query,
|
||||||
|
Comment = comment,
|
||||||
|
LessonId = lessonId
|
||||||
|
};
|
||||||
|
DbContext.Paragraphs.Add(newParagraphEntity.FromDTOToEntity());
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return newParagraphEntity;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Dto;
|
||||||
|
using DbContextLib;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Shared.Mapper;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public class SuccessDataService : ISuccessDataService
|
||||||
|
{
|
||||||
|
private UserDbContext DbContext { get; set; }
|
||||||
|
public SuccessDataService(UserDbContext context)
|
||||||
|
{
|
||||||
|
DbContext = context;
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
public IEnumerable<SuccessDTO> GetSuccesses(int page, int number, SuccessOrderCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
IQueryable<SuccessEntity> query = DbContext.Successes.Skip((page - 1) * number).Take(number);
|
||||||
|
switch (orderCriteria)
|
||||||
|
{
|
||||||
|
case SuccessOrderCriteria.None:
|
||||||
|
break;
|
||||||
|
case SuccessOrderCriteria.ByUserId:
|
||||||
|
query = query.OrderBy(s => s.UserId);
|
||||||
|
break;
|
||||||
|
case SuccessOrderCriteria.ByInquiryId:
|
||||||
|
query = query.OrderBy(s => s.InquiryId);
|
||||||
|
break;
|
||||||
|
case SuccessOrderCriteria.ByIsFinished:
|
||||||
|
query = query.OrderBy(s => s.IsFinished);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var successes = query.ToList();
|
||||||
|
return successes.Select(s => s.FromEntityToDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
public SuccessDTO GetSuccessByUserId(int id)
|
||||||
|
{
|
||||||
|
var userEntity = DbContext.Successes.FirstOrDefault(u => u.UserId == id);
|
||||||
|
if (userEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver le succès", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return userEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SuccessDTO GetSuccessByInquiryId(int id)
|
||||||
|
{
|
||||||
|
var userEntity = DbContext.Successes.FirstOrDefault(u => u.InquiryId == id);
|
||||||
|
if (userEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver le succès", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return userEntity.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeleteSuccess(int id)
|
||||||
|
{
|
||||||
|
var successEntity = DbContext.Successes.FirstOrDefault(u => u.UserId == id);
|
||||||
|
if (successEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DbContext.Successes.Remove(successEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SuccessDTO UpdateSuccess(int id, SuccessDTO success)
|
||||||
|
{
|
||||||
|
var updatingSuccess = DbContext.Successes.FirstOrDefault(u => u.UserId == id);
|
||||||
|
if (updatingSuccess == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver le succès", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
updatingSuccess.UserId = success.UserId;
|
||||||
|
updatingSuccess.InquiryId = success.InquiryId;
|
||||||
|
updatingSuccess.IsFinished = success.IsFinished;
|
||||||
|
// Permet d'indiquer en Db que l'entité a été modifiée.
|
||||||
|
DbContext.Entry(updatingSuccess).State = EntityState.Modified;
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return updatingSuccess.FromEntityToDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SuccessDTO CreateSuccess(int userId, int inquiryId, bool isFinished)
|
||||||
|
{
|
||||||
|
var newSuccessEntity = new SuccessDTO()
|
||||||
|
{
|
||||||
|
UserId = userId,
|
||||||
|
InquiryId = inquiryId,
|
||||||
|
IsFinished = isFinished,
|
||||||
|
};
|
||||||
|
DbContext.Successes.Add(newSuccessEntity.FromDTOToEntity());
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return newSuccessEntity;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,123 @@
|
|||||||
|
using Dto;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Entities;
|
||||||
|
using Shared.Mapper;
|
||||||
|
using DbContextLib;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared
|
||||||
|
{
|
||||||
|
public class UserDataService : IUserDataService
|
||||||
|
{
|
||||||
|
private UserDbContext DbContext { get; set; }
|
||||||
|
|
||||||
|
public UserDataService(UserDbContext context)
|
||||||
|
{
|
||||||
|
DbContext = context;
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDTO GetUserById(int id)
|
||||||
|
{
|
||||||
|
var userEntity = DbContext.Users.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (userEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return userEntity.FromEntityToModel().FromModelToDTO();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDTO GetUserByUsername(string username)
|
||||||
|
{
|
||||||
|
var userEntity = DbContext.Users.FirstOrDefault(u => u.Username == username);
|
||||||
|
if (userEntity == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(username));
|
||||||
|
}
|
||||||
|
|
||||||
|
return userEntity.FromEntityToModel().FromModelToDTO();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<UserDTO> GetUsers(int page, int number, UserOrderCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
IQueryable<UserEntity> query = DbContext.Users.Skip((page - 1) * number).Take(number);
|
||||||
|
switch (orderCriteria)
|
||||||
|
{
|
||||||
|
case UserOrderCriteria.None:
|
||||||
|
break;
|
||||||
|
case UserOrderCriteria.ById:
|
||||||
|
query = query.OrderBy(s => s.Id);
|
||||||
|
break;
|
||||||
|
case UserOrderCriteria.ByUsername:
|
||||||
|
query = query.OrderBy(s => s.Username);
|
||||||
|
break;
|
||||||
|
case UserOrderCriteria.ByEmail:
|
||||||
|
query = query.OrderBy(s => s.Email);
|
||||||
|
break;
|
||||||
|
case UserOrderCriteria.ByIsAdmin:
|
||||||
|
query = query.OrderBy(s => s.IsAdmin);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var users = query.ToList();
|
||||||
|
return users.Select(s => s.FromEntityToModel().FromModelToDTO());
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeleteUser(int id)
|
||||||
|
{
|
||||||
|
var userEntity = DbContext.Users.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (userEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DbContext.Users.Remove(userEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDTO UpdateUser(int id, UserDTO user)
|
||||||
|
{
|
||||||
|
var updatingUser = DbContext.Users.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (updatingUser == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
updatingUser.Username = user.Username;
|
||||||
|
updatingUser.Password = user.Password;
|
||||||
|
updatingUser.Email = user.Email;
|
||||||
|
updatingUser.IsAdmin = user.IsAdmin;
|
||||||
|
// Permet d'indiquer en Db que l'entité a été modifiée.
|
||||||
|
DbContext.Entry(updatingUser).State = EntityState.Modified;
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return updatingUser.FromEntityToModel().FromModelToDTO();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDTO CreateUser(string username, string password, string email, bool isAdmin)
|
||||||
|
{
|
||||||
|
var newUserEntity = new UserDTO
|
||||||
|
{
|
||||||
|
Username = username,
|
||||||
|
Password = password,
|
||||||
|
Email = email,
|
||||||
|
IsAdmin = isAdmin
|
||||||
|
};
|
||||||
|
DbContext.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity());
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return newUserEntity;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue