@ -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,98 +8,143 @@ 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 ;
using Utils ;
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 ( ) ;
}
public async Task < UserDTO ? > GetUserByUsername ( string username )
{
{
throw new ArgumentException ( "Impossible de trouver l'utilisateur" , nameof ( id ) ) ;
var user = await dataServiceEF . GetUserByUsername ( username ) ;
return user . FromModelToDTO ( ) ;
}
}
return userEntity . FromEntityToModel ( ) . FromModelToDTO ( ) ;
public async Task < IEnumerable < UserDTO ? > > GetUsers ( int page , int number )
{
var users = await dataServiceEF . GetUsers ( page , number ) ;
return users . Select ( u = > u . FromModelToDTO ( ) ) ;
}
}
public UserDTO GetUserByUsername ( string username )
public async Task < bool > DeleteUser ( int id )
{
{
var userEntity = DbContext . Users . FirstOrDefault ( u = > u . Username = = username ) ;
var respons = await dataServiceEF . DeleteUser ( id ) ;
if ( userEntity = = null )
return respons ;
}
public async Task < UserDTO ? > UpdateUser ( int id , UserDTO user )
{
{
throw new ArgumentException ( "Impossible de trouver l'utilisateur" , nameof ( username ) ) ;
var updatingUser = await dataServiceEF . UpdateUser ( id , user ) ;
return updatingUser . FromModelToDTO ( ) ;
}
}
return userEntity . FromEntityToModel ( ) . FromModelToDTO ( ) ;
public async Task < UserDTO ? > CreateUser ( string username , string password , string email , bool isAdmin )
{
var newUserEntity = await dataServiceEF . CreateUser ( username , password , email , isAdmin ) ;
return newUserEntity . FromModelToDTO ( ) ;
}
}
public IEnumerable < UserDTO > GetUsers ( int page , int number )
public async Task < UserDTO ? > AddItem ( UserDTO ? item )
{
{
return DbContext . Users . Skip ( ( page - 1 ) * number ) . Take ( number ) . ToList ( ) . Select ( u = > u . FromEntityToModel ( ) . FromModelToDTO ( ) ) ;
var newItem = await dataServiceEF . AddItem ( item . FromDTOToModel ( ) ) ;
if ( newItem = = null )
{
throw new InvalidOperationException ( ) ;
}
return newItem . FromModelToDTO ( ) ;
}
}
public bool DeleteUser ( int id )
public Task < bool > DeleteItem ( int id )
{
{
var userEntity = DbContext . Users . FirstOrDefault ( u = > u . Id = = id ) ;
var succes = dataServiceEF . DeleteItem ( id ) ;
if ( userEntity = = null )
return succes ;
}
public async Task < UserDTO > UpdateItem < TDto > ( int? id , TDto ? newItem ) where TDto : class
{
{
return false ;
var item = await dataServiceEF . UpdateItem < TDto > ( id , newItem ) ;
return item . FromModelToDTO ( ) ;
}
}
DbContext . Users . Remove ( userEntity ) ;
DbContext . SaveChangesAsync ( ) ;
public async Task < IEnumerable < UserDTO ? > > GetItems ( int page , int count , string? orderingPropertyName = null , object? valueProperty = null )
return true ;
{
var items = await dataServiceEF . GetItems ( page , count , orderingPropertyName , valueProperty ) ;
if ( items = = null )
{
throw new NoDataFoundException ( ) ;
}
return items . Select ( item = > item . FromModelToDTO ( ) ) ;
}
}
public UserDTO UpdateUser ( int id , UserDTO user )
public Task< IEnumerable < ParagraphDTO > > GetParagraphs ( int page , int numb er)
{
{
var updatingUser = DbContext . Users . FirstOrDefault ( u = > u . Id = = id ) ;
throw new NotImplementedException ( ) ;
if ( updatingUser = = null )
}
public Task < ParagraphDTO > GetParagraphById ( int id )
{
{
throw new ArgumentException ( "Impossible de trouver l'utilisateur" , nameof ( id ) ) ;
throw new NotImplementedException ( ) ;
}
}
updatingUser . Username = user . Username ;
updatingUser . Password = user . Password ;
public Task < ParagraphDTO > GetParagraphByTitle ( string title )
updatingUser . Email = user . Email ;
{
updatingUser . IsAdmin = user . IsAdmin ;
throw new NotImplementedException ( ) ;
// 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 Task < bool > DeleteParagraph ( int id )
{
{
var newUserEntity = new UserDTO
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 )
{
{
Username = username ,
throw new NotImplementedException ( ) ;
Password = password ,
}
Email = email ,
IsAdmin = isAdmin
public Task < UserDTO > GetSuccessByInquiryId ( int id )
} ;
{
DbContext . Users . Add ( newUserEntity . FromDTOToModel ( ) . FromModelToEntity ( ) ) ;
throw new NotImplementedException ( ) ;
DbContext . SaveChangesAsync ( ) ;
return newUserEntity ;
}
}
public IEnumerable < InquiryDTO > GetInquiries ( int page , int number )
public Task< bool > DeleteSuccess ( int id )
{
{
throw new NotImplementedException ( ) ;
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 ( ) ;
}
}