@ -1,4 +1,4 @@
using DbContextLib ;
using DbContextLib ;
using Model.DTO ;
using Model.DTO ;
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
@ -8,100 +8,144 @@ using System.Threading.Tasks;
using Model.Mappers ;
using Model.Mappers ;
using Model.Business ;
using Model.Business ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore ;
using Entities.SQLudeoDB ;
using ModelToEntity ;
namespace Services
namespace Services
{
{
public class UserDataService : I DataService
public class UserDataService : I User DataService< UserDTO ? >
{
{
private UserDbContext DbContext { get ; set ; }
private readonly IUserDataService < User > dataServiceEF ;
public UserDataService ( UserDbContext context )
public UserDataService ( IUserDataService< User > dataServiceEF )
{
{
DbContext = context ;
this . dataServiceEF = dataServiceEF ;
context . Database . EnsureCreated ( ) ;
}
}
public UserDTO GetUserById ( int id )
public async Task < UserDTO ? > GetUserById ( int id )
{
{
var userEntity = DbContext . Users . FirstOrDefault ( u = > u . Id = = id ) ;
var user = await dataServiceEF . GetUserById ( id ) ;
if ( userEntity = = null )
return user . FromModelToDTO ( ) ;
{
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 )
public async Task < UserDTO ? > GetUserByUsername ( string username )
{
{
return DbContext . Users . Skip ( ( page - 1 ) * number ) . Take ( number ) . ToList ( ) . Select ( u = > u . FromEntityToModel ( ) . FromModelToDTO ( ) ) ;
var user = await dataServiceEF . GetUserByUsername ( username ) ;
return user . FromModelToDTO ( ) ;
}
}
public bool DeleteUser ( int id )
public async Task < IEnumerable < UserDTO ? > > GetUsers ( int page , int number )
{
{
var userEntity = DbContext . Users . FirstOrDefault ( u = > u . Id = = id ) ;
var users = await dataServiceEF . GetUsers ( page , number ) ;
if ( userEntity = = null )
return users . Select ( u = > u . FromModelToDTO ( ) ) ;
{
return false ;
}
DbContext . Users . Remove ( userEntity ) ;
DbContext . SaveChangesAsync ( ) ;
return true ;
}
}
public UserDTO UpdateUser ( int id , UserDTO user )
public async Task < bool > DeleteUser ( int id )
{
{
var updatingUser = DbContext . Users . FirstOrDefault ( u = > u . Id = = id ) ;
var respons = await dataServiceEF . DeleteUser ( id ) ;
if ( updatingUser = = null )
return respons ;
{
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 )
public async Task < UserDTO ? > UpdateUser ( int id , UserDTO user )
{
{
var newUserEntity = new UserDTO
var updatingUser = await dataServiceEF . UpdateUser ( id , user ) ;
{
return updatingUser . FromModelToDTO ( ) ;
Username = username ,
Password = password ,
Email = email ,
IsAdmin = isAdmin
} ;
DbContext . Users . Add ( newUserEntity . FromDTOToModel ( ) . FromModelToEntity ( ) ) ;
DbContext . SaveChangesAsync ( ) ;
return newUserEntity ;
}
}
public async Task < UserDTO ? > CreateUser ( string username , string password , string email , bool isAdmin )
public IEnumerable < InquiryDTO > GetInquiries ( int page , int number )
{
{
throw new NotImplementedException ( ) ;
var newUserEntity = await dataServiceEF . CreateUser ( username , password , email , isAdmin ) ;
return newUserEntity . FromModelToDTO ( ) ;
}
public async Task < UserDTO ? > AddItem ( UserDTO ? item )
{
var newItem = await dataServiceEF . AddItem ( item . FromDTOToModel ( ) ) ;
return newItem . FromModelToDTO ( ) ;
}
public Task < bool > DeleteItem ( int id )
{
var succes = dataServiceEF . DeleteItem ( id ) ;
return succes ;
}
public async Task < UserDTO > UpdateItem < TDto > ( int? id , TDto ? newItem ) where TDto : class
{
var item = await dataServiceEF . UpdateItem < TDto > ( id , newItem ) ;
return item . FromModelToDTO ( ) ;
}
public async Task < IEnumerable < UserDTO ? > > GetItems ( int index , int count , string? orderingPropertyName = null , object? valueProperty = null )
{
try
{
var items = await dataServiceEF . GetItems ( index , count , orderingPropertyName , valueProperty ) ;
return items . Select ( item = > item . FromModelToDTO ( ) ) ;
}
catch ( KeyNotFoundException ex )
{
throw ;
}
}
public Task < IEnumerable < ParagraphDTO > > GetParagraphs ( int page , int number )
{
throw new NotImplementedException ( ) ;
}
public Task < ParagraphDTO > GetParagraphById ( int id )
{
throw new NotImplementedException ( ) ;
}
public Task < ParagraphDTO > GetParagraphByTitle ( string title )
{
throw new NotImplementedException ( ) ;
}
public Task < bool > DeleteParagraph ( int id )
{
throw new NotImplementedException ( ) ;
}
public Task < ParagraphDTO > UpdateParagraph ( int id , ParagraphDTO paragraphDTO )
{
throw new NotImplementedException ( ) ;
}
public Task < ParagraphDTO > CreateParagraph ( string title , string content , string info , string query , string comment )
{
throw new NotImplementedException ( ) ;
}
public Task < IEnumerable < UserDTO > > GetSuccesses ( int page , int number )
{
throw new NotImplementedException ( ) ;
}
public Task < UserDTO > GetSuccessByUserId ( int id )
{
throw new NotImplementedException ( ) ;
}
public Task < UserDTO > GetSuccessByInquiryId ( int id )
{
throw new NotImplementedException ( ) ;
}
public Task < bool > DeleteSuccess ( int id )
{
throw new NotImplementedException ( ) ;
}
}
public InquiryDTO GetInquiryById ( int id )
public Task< UserDTO > UpdateSuccess ( int id , SuccessDTO success )
{
{
throw new NotImplementedException ( ) ;
throw new NotImplementedException ( ) ;
}
}
public InquiryDTO GetInquiryByTitle ( string title )
public Task< UserDTO > CreateSuccess ( int userId , int inquiryId , bool isFinished )
{
{
throw new NotImplementedException ( ) ;
throw new NotImplementedException ( ) ;
}
}
}
}
}
}