You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
API_SQLuedo/API_SQLuedo/ModelToEntity/DbDataManager.cs

206 lines
6.7 KiB

using Entities.SQLudeoDB;
using Microsoft.EntityFrameworkCore;
using Model.DTO;
using Services;
using DbContextLib;
using Model.Business;
using Model;
using Model.Mappers;
using System;
using Microsoft.AspNetCore.Identity;
using System.Runtime.InteropServices.ObjectiveC;
namespace ModelToEntity
{
public class DbDataManager : IUserDataService<User?>
{
public async Task<User> GetUserById(int id)
{
using(var context = new UserDbContext())
{
var userEntity = context.Users.FirstOrDefault(u => u.Id == id);
if(userEntity == null)
{
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id));
}
return await Task.FromResult(userEntity.FromEntityToModel());
}
}
public async Task<User> GetUserByUsername(string username)
{
using (var context = new UserDbContext())
{
var userEntity = context.Users.FirstOrDefault(u => u.Username == username);
if (userEntity == null)
{
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(username));
}
return await Task.FromResult(userEntity.FromEntityToModel());
}
}
public async Task<IEnumerable<User>> GetUsers(int page, int number)
{
using (var context = new UserDbContext())
{
return await Task.FromResult(context.Users.Skip((page - 1) * number).Take(number).ToList().Select(u => u.FromEntityToModel()));
}
}
public Task<bool> DeleteUser(int id)
{
using (var context = new UserDbContext()) {
var userEntity = context.Users.FirstOrDefault(u => u.Id == id);
if (userEntity == null)
{
return Task.FromResult(false);
}
context.Users.Remove(userEntity);
context.SaveChangesAsync();
}
return Task.FromResult(true);
}
public async Task<User> UpdateUser(int id, UserDTO user)
{
using (var context = new UserDbContext())
{
var updatingUser = context.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.
context.Entry(updatingUser).State = EntityState.Modified;
context.SaveChangesAsync();
return await Task.FromResult(updatingUser.FromEntityToModel());
}
}
public async Task<User> CreateUser(string username, string password, string email, bool isAdmin)
{
using (var context = new UserDbContext())
{
var newUserEntity = new UserDTO
{
Username = username,
Password = password,
Email = email,
IsAdmin = isAdmin
};
context.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity());
context.SaveChanges();
return await Task.FromResult(newUserEntity.FromDTOToModel());
}
}
public IEnumerable<InquiryDTO> GetInquiries(int page, int number)
{
throw new NotImplementedException();
}
public InquiryDTO GetInquiryById(int id)
{
throw new NotImplementedException();
}
public InquiryDTO GetInquiryByTitle(string title)
{
throw new NotImplementedException();
}
public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable)
{
throw new NotImplementedException();
}
public bool DeleteInquiry(int id)
{
throw new NotImplementedException();
}
public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry)
{
throw new NotImplementedException();
}
public IEnumerable<InquiryDTO> GetLessons(int page, int number)
{
throw new NotImplementedException();
}
public InquiryDTO GetLessonById(int id)
{
throw new NotImplementedException();
}
public InquiryDTO GetLessonByTitle(string title)
{
throw new NotImplementedException();
}
public bool DeleteLesson(int id)
{
throw new NotImplementedException();
}
public InquiryDTO UpdateLesson(int id, LessonDTO lesson)
{
throw new NotImplementedException();
}
public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit)
{
throw new NotImplementedException();
}
public async Task<User?> AddItem(User? item)
{
using (var context = new UserDbContext())
{
var user = await context.CreateItemAsync(item.FromModelToEntity());
if (user == null)
{
return null;
}
return user.FromEntityToModel() ;
}
}
public async Task<bool> DeleteItem(int id)
{
using(var context = new UserDbContext())
{
return await context.DeleteItemAsync<UserEntity>(id);
}
}
public async Task<User?> UpdateItem<TDto>(int? id, TDto? newItem) where TDto : class
{
using(var context = new UserDbContext())
{
return (await context.UpdateItemAsync<UserEntity, TDto>(id, newItem)).FromEntityToModel();
}
}
public async Task<IEnumerable<User?>> GetItems(int page, int count, string? orderingPropertyName = null, object? valueProperty = null)
{
using (var context = new UserDbContext())
{
var users = (await context.GetItemsWithFilter<UserEntity>(page, count, orderingPropertyName, valueProperty));
if(users == null)
{
return null;
}
return users.Select(item => item.FromEntityToModel());
}
}
}
}