diff --git a/API_SQLuedo/API/Controllers/InquiriesController.cs b/API_SQLuedo/API/Controllers/InquiriesController.cs index dc2201d..e29ef16 100644 --- a/API_SQLuedo/API/Controllers/InquiriesController.cs +++ b/API_SQLuedo/API/Controllers/InquiriesController.cs @@ -10,26 +10,25 @@ namespace API.Controllers [ApiController] public class InquiriesController : Controller { - private IDataService _inquiryDataService; + private IDataService _dataService; private readonly ILogger _logger; - - public InquiriesController(IDataService inquiryDataService) + public InquiriesController(IDataService dataService) { - _inquiryDataService = inquiryDataService; + _dataService = _dataService; } [HttpGet("inquiries/{page}/{number}")] public IActionResult GetInquiries(int page, int number) { - var nbInquiry = _inquiryDataService.GetInquiries(page, number).Count(); + var nbInquiry = _dataService.InquiryDataService.GetInquiries(page, number).Count(); if (nbInquiry == 0) { _logger.LogError("[ERREUR] Aucune enquête trouvée."); return StatusCode(204); } _logger.LogInformation("[INFORMATION] {nb} Enquête(s) trouvée(s)", nbInquiry); - return Ok(_inquiryDataService.GetInquiries(page, number)); + return Ok(_dataService.InquiryDataService.GetInquiries(page, number)); } [HttpGet("inquiry/id/{id}")] @@ -38,7 +37,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] L'enquête avec l'id {id} a été trouvé.", id); - return Ok(_inquiryDataService.GetInquiryById(id)); + return Ok(_dataService.InquiryDataService.GetInquiryById(id)); } catch (ArgumentException) { @@ -53,7 +52,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] L'enquête avec le titre {title} a été trouvé.", title); - return Ok(_inquiryDataService.GetInquiryByTitle(title)); + return Ok(_dataService.InquiryDataService.GetInquiryByTitle(title)); } catch (ArgumentException) { @@ -65,11 +64,11 @@ namespace API.Controllers [HttpDelete] public IActionResult DeleteInquiry(int id) { - var success = _inquiryDataService.DeleteInquiry(id); + var success = _dataService.InquiryDataService.DeleteInquiry(id); if (success) { _logger.LogInformation("[INFORMATION] L'enquête avec l'id {id} a été supprimé.", id); - return Ok(_inquiryDataService.DeleteInquiry(id)); + return Ok(_dataService.InquiryDataService.DeleteInquiry(id)); } else { @@ -87,7 +86,7 @@ namespace API.Controllers return BadRequest(); } _logger.LogInformation("[INFORMATION] Une enquête a été créé : title - {title}, description - {description}, isUser - {isUser}, database - {database}, inquiryTable - {inquiryTable}", dto.Title, dto.Description, dto.IsUser, dto.Database, dto.InquiryTable); - return Created(nameof(GetInquiries), _inquiryDataService.CreateInquiry(dto.Title, dto.Description, dto.IsUser, dto.Database, dto.InquiryTable)); + return Created(nameof(GetInquiries), _dataService.InquiryDataService.CreateInquiry(dto.Title, dto.Description, dto.IsUser, dto.Database, dto.InquiryTable)); } [HttpPut] @@ -106,7 +105,7 @@ namespace API.Controllers if (inquiryDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de l'enquête avec l'id {id} a été effectuée", id); - return Ok(_inquiryDataService.UpdateInquiry(id, inquiryDTO)); + return Ok(_dataService.InquiryDataService.UpdateInquiry(id, inquiryDTO)); } _logger.LogError("[ERREUR] Aucune enquête trouvée avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/API/Controllers/LessonsController.cs b/API_SQLuedo/API/Controllers/LessonsController.cs index a408f26..02b6a07 100644 --- a/API_SQLuedo/API/Controllers/LessonsController.cs +++ b/API_SQLuedo/API/Controllers/LessonsController.cs @@ -10,26 +10,25 @@ namespace API.Controllers [ApiController] public class LessonsController : Controller { - private IDataService _lessonDataService; + private IDataService _dataService; private readonly ILogger _logger; - - public LessonsController(IDataService lessonDataService) + public LessonsController(IDataService dataService) { - _lessonDataService = lessonDataService; + _dataService = dataService; } [HttpGet("lessons/{page}/{number}")] public IActionResult GetLessons(int page, int number) { - var nbLesson = _lessonDataService.GetInquiries(page, number).Count(); + var nbLesson = _dataService.LessonDataService.GetLessons(page, number).Count(); if (nbLesson == 0) { _logger.LogError("[ERREUR] Aucune leçon trouvée."); return StatusCode(204); } _logger.LogInformation("[INFORMATION] {nb} Leçon(s) trouvée(s)", nbLesson); - return Ok(_lessonDataService.GetLessons(page, number)); + return Ok(_dataService.LessonDataService.GetLessons(page, number)); } [HttpGet("lesson/id/{id}")] @@ -38,7 +37,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été trouvé.", id); - return Ok(_lessonDataService.GetLessonById(id)); + return Ok(_dataService.LessonDataService.GetLessonById(id)); } catch (ArgumentException) { @@ -53,7 +52,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] La leçon avec le titre {title} a été trouvé.", title); - return Ok(_lessonDataService.GetLessonByTitle(title)); + return Ok(_dataService.LessonDataService.GetLessonByTitle(title)); } catch (ArgumentException) { @@ -65,11 +64,11 @@ namespace API.Controllers [HttpDelete] public IActionResult DeleteLesson(int id) { - var success = _lessonDataService.DeleteLesson(id); + var success = _dataService.LessonDataService.DeleteLesson(id); if (success) { _logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été supprimé.", id); - return Ok(_lessonDataService.DeleteLesson(id)); + return Ok(_dataService.LessonDataService.DeleteLesson(id)); } else { @@ -87,7 +86,7 @@ namespace API.Controllers return BadRequest(); } _logger.LogInformation("[INFORMATION] Une leçon a été créé : title - {title}, lastPublisher - {publisher}, lastEdit - {lastEdit}", dto.Title, dto.LastPublisher, dto.LastEdit); - return Created(nameof(GetLessons), _lessonDataService.CreateLesson(dto.Title, dto.LastPublisher, dto.LastEdit)); + return Created(nameof(GetLessons), _dataService.LessonDataService.CreateLesson(dto.Title, dto.LastPublisher, dto.LastEdit)); } [HttpPut] @@ -106,7 +105,7 @@ namespace API.Controllers if (lessonDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de la leçon avec l'id {id} a été effectuée", id); - return Ok(_lessonDataService.UpdateLesson(id, lessonDTO)); + return Ok(_dataService.LessonDataService.UpdateLesson(id, lessonDTO)); } _logger.LogError("[ERREUR] Aucune leçon trouvée avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/API/Controllers/UsersController.cs b/API_SQLuedo/API/Controllers/UsersController.cs index ca36890..6a4b462 100644 --- a/API_SQLuedo/API/Controllers/UsersController.cs +++ b/API_SQLuedo/API/Controllers/UsersController.cs @@ -1,9 +1,12 @@ using DbContextLib; +using Entities.SQLudeoDB; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Model; using Model.Business; using Model.DTO; +using Model.Mappers; using Services; namespace API.Controllers @@ -13,27 +16,26 @@ namespace API.Controllers [ApiController] public class UsersController : Controller { - private IDataService _userDataService; + private IDataService _dataService; private readonly ILogger _logger; - - public UsersController(IDataService userDataService, ILogger logger) + public UsersController(IDataService dataService, ILogger logger) { - _userDataService = userDataService; + _dataService = dataService; _logger = logger; } [HttpGet("users/{page}/{number}")] public async Task GetUsers(int page, int number) { - var nbUser = (await _userDataService.GetUsers(page, number)).ToList().Count(); + var nbUser = (await _dataService.UserService.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(_userDataService.GetUsers(page, number)); + return Ok(_dataService.UserService.GetItems(page, number)); } [HttpGet("user/id/{id}")] @@ -42,7 +44,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id); - return Ok(_userDataService.GetUserById(id)); + return Ok(_dataService.UserService.GetItems(1, 1, UserProperty.Id.ToString(),id)); } catch (ArgumentException) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); @@ -56,7 +58,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username); - return Ok(_userDataService.GetUserByUsername(username)); + return Ok(_dataService.UserService.GetItems(1, 1,UserProperty.Username.ToString(), username)); }catch (ArgumentException) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username); @@ -68,11 +70,11 @@ namespace API.Controllers [HttpDelete] public async Task DeleteUser(int id) { - var success = await _userDataService.DeleteUser(id); + var success = await _dataService.UserService.DeleteItem(id); if(success) { _logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id); - return Ok(_userDataService.DeleteUser(id)); + return Ok(_dataService.UserService.DeleteUser(id)); } else { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); @@ -88,9 +90,9 @@ namespace API.Controllers { return BadRequest(); } - // return Ok(_userDataService.CreateUser(username, password, email, isAdmin)); + _logger.LogInformation("[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", dto.Username, dto.Password, dto.Email, dto.IsAdmin); - return Created(nameof(GetUsers), _userDataService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin)); + return Created(nameof(GetUsers), _dataService.UserService.AddItem(dto.FromDTOToModel().FromModelToEntity())); } [HttpPut] @@ -109,7 +111,7 @@ namespace API.Controllers if(userDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de l'utilisateur avec l'id {id} a été effectuée", id); - return Ok(_userDataService.UpdateUser(id, userDTO)); + return Ok(_dataService.UserService.UpdateItem(id, userDTO)); } _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index eb3ee3c..c3d1a86 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -3,9 +3,12 @@ using DbContextLib; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; +using Model.DTO; +using Model.Business; using ModelToEntity; using Services; using System.Data.Common; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -15,8 +18,9 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped, UserDataService>(); +builder.Services.AddScoped, DataService>(); +builder.Services.AddScoped, DbDataManager>(); builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); builder.Services.AddIdentityApiEndpoints().AddEntityFrameworkStores(); diff --git a/API_SQLuedo/API_SQLuedo.sln b/API_SQLuedo/API_SQLuedo.sln index f9b50ec..0d2bfac 100644 --- a/API_SQLuedo/API_SQLuedo.sln +++ b/API_SQLuedo/API_SQLuedo.sln @@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services", "Services\Services.csproj", "{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelToEntity", "ModelToEntity\ModelToEntity.csproj", "{8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModelToEntity", "ModelToEntity\ModelToEntity.csproj", "{8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{B28FD539-6FE4-4B13-9C1F-D88F4771CC69}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -57,6 +59,10 @@ Global {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Debug|Any CPU.Build.0 = Debug|Any CPU {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Release|Any CPU.ActiveCfg = Release|Any CPU {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Release|Any CPU.Build.0 = Release|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/API_SQLuedo/DbContextLib/UserDbContext.cs b/API_SQLuedo/DbContextLib/UserDbContext.cs index 4bf3c4e..a5acbf6 100644 --- a/API_SQLuedo/DbContextLib/UserDbContext.cs +++ b/API_SQLuedo/DbContextLib/UserDbContext.cs @@ -25,7 +25,7 @@ namespace DbContextLib { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=motdepasse"); + optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=motdepasse").EnableSensitiveDataLogging(); } base.OnConfiguring(optionsBuilder); } diff --git a/API_SQLuedo/Model/UserProperty.cs b/API_SQLuedo/Model/UserProperty.cs new file mode 100644 index 0000000..75ecf9c --- /dev/null +++ b/API_SQLuedo/Model/UserProperty.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public enum UserProperty + { + Id, + Username, + Password, + Email, + IsAdmin + + } +} diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs index 7ae486b..c56dcf4 100644 --- a/API_SQLuedo/ModelToEntity/DbDataManager.cs +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -6,10 +6,13 @@ 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 : IDataServiceEF + public class DbDataManager : IUserDataService { public async Task GetUserById(int id) { @@ -93,7 +96,7 @@ namespace ModelToEntity IsAdmin = isAdmin }; context.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity()); - context.SaveChangesAsync(); + context.SaveChanges(); return await Task.FromResult(newUserEntity.FromDTOToModel()); } @@ -157,6 +160,39 @@ namespace ModelToEntity { throw new NotImplementedException(); } - } - + + public async Task AddItem(T? item) where T : class + { + using (var context = new UserDbContext()) + { + return await context.CreateItemAsync(item); + } + + } + + public async Task DeleteItem(int id) where T : class + { + using(var context = new UserDbContext()) + { + return await context.DeleteItemAsync(id); + } + } + public async Task UpdateItem(int? id, TDto? newItem) + where T : class + where TDto : class + { + using(var context = new UserDbContext()) + { + return await context.UpdateItemAsync(id, newItem); + } + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class + { + using (var context = new UserDbContext()) + { + return await context.GetItemsWithFilter(index, count, orderingPropertyName, valueProperty); + } + } + } } diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index b039fe4..508e872 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -7,10 +7,98 @@ using System.Threading.Tasks; using Entities; using Entities.SQLudeoDB; using Model.Business; +using System.Diagnostics; +using DbContextLib; +using Microsoft.EntityFrameworkCore; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace ModelToEntity { - static class Extension + internal static class Extension { + internal static Task CreateItemAsync(this DbContext context, T? item) where T : class + { + if (item == null || context.Set().Contains(item)) + { + return Task.FromResult(default(T)); + } + + context.Set().Add(item); + context.SaveChangesAsync(); + + return Task.FromResult(item); + } + internal static async Task DeleteItemAsync(this DbContext context, int? id) where T : class + { + if (id == null) + { + return false; + } + + var entity = await context.Set().FindAsync(id); + if (entity == null) + { + return false; + } + + context.Set().Remove(entity); + await context.SaveChangesAsync(); + + return true; + } + internal static Task UpdateItemAsync(this DbContext context, int? id, TDto dto) + where T : class + where TDto : class + { + var entity = context.Set().Find(id); + if (entity == null) + { + throw new ArgumentException("Impossible de trouver l'entité", nameof(id)); + } + + var propertiesToUpdate = typeof(TDto).GetProperties() + .Where(p => p.CanRead && p.CanWrite); + + foreach (var property in propertiesToUpdate) + { + var value = property.GetValue(dto); + typeof(T).GetProperty(property.Name)?.SetValue(entity, value); + } + + context.Entry(entity).State = EntityState.Modified; + context.SaveChanges(); + + return Task.FromResult(entity); + } + internal static Task> GetItemsWithFilter(this DbContext context, + int index, int count, string? orderingPropertyName = null, object valueProperty = null) where T : class + { + IQueryable query = context.Set(); + + if (valueProperty != null && !string.IsNullOrEmpty(orderingPropertyName)) + { + var prop = typeof(T).GetProperty(orderingPropertyName); + if (prop != null) + { + var idProp = typeof(T).GetProperty(orderingPropertyName); + if (idProp != null) + { + var parameter = Expression.Parameter(typeof(T), "entity"); + var propertyAccess = Expression.Property(parameter, prop); + var constant = Expression.Constant(valueProperty); + var equality = Expression.Equal(propertyAccess, constant); + var lambda = Expression.Lambda>(equality, parameter); + var filteredEntity = context.Set().FirstOrDefault(lambda); + if (filteredEntity != null) + { + return Task.FromResult>(new List { filteredEntity }); + } + } + } + } + var items = query.Skip(index).Take(count).ToList(); + return Task.FromResult>(items); + } } -} +} \ No newline at end of file diff --git a/API_SQLuedo/ModelToEntity/ModelToEntity.csproj b/API_SQLuedo/ModelToEntity/ModelToEntity.csproj index 4cd7b1b..472379f 100644 --- a/API_SQLuedo/ModelToEntity/ModelToEntity.csproj +++ b/API_SQLuedo/ModelToEntity/ModelToEntity.csproj @@ -23,6 +23,7 @@ + diff --git a/API_SQLuedo/Services/DataService.cs b/API_SQLuedo/Services/DataService.cs new file mode 100644 index 0000000..cb0e072 --- /dev/null +++ b/API_SQLuedo/Services/DataService.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Model.Business; +using Model.DTO; +using Shared; + +namespace Services +{ + public class DataService : IDataService + { + public IUserDataService UserService { get; } + public DataService(IUserDataService userDataService) + { + UserService = userDataService; + } + + public IInquiryDataService InquiryDataService { get; } + + public ILessonDataService LessonDataService { get; } + } +} diff --git a/API_SQLuedo/Services/LessonDataService.cs b/API_SQLuedo/Services/LessonDataService.cs new file mode 100644 index 0000000..a21a5a7 --- /dev/null +++ b/API_SQLuedo/Services/LessonDataService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public class LessonDataService + { + } +} diff --git a/API_SQLuedo/Services/Services.csproj b/API_SQLuedo/Services/Services.csproj index dd690cb..4f252a2 100644 --- a/API_SQLuedo/Services/Services.csproj +++ b/API_SQLuedo/Services/Services.csproj @@ -24,6 +24,7 @@ + diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 995010a..82d6c0d 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -1,119 +1,83 @@ -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) +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 : IUserDataService + { + private readonly IUserDataService dataServiceEF; + public UserDataService(IUserDataService 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 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 InquiryDTO GetInquiryById(int id) + public async Task AddItem(T? item) where T : class { - throw new NotImplementedException(); + var newItem = dataServiceEF.AddItem(item); + return await newItem; } - public InquiryDTO GetInquiryByTitle(string title) + public Task DeleteItem(int id) where T : class { - throw new NotImplementedException(); - } - public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable) - { - throw new NotImplementedException(); + var succes = dataServiceEF.DeleteItem(id); + return succes; } - public bool DeleteInquiry(int id) + public Task UpdateItem(int? id, TDto? newItem) where T : class where TDto : class { - throw new NotImplementedException(); + var item = dataServiceEF.UpdateItem(id, newItem); + return item; } - public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry) + public Task> GetItems(int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class { - throw new NotImplementedException(); - } - - public IEnumerable 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(); + var items = dataServiceEF.GetItems(index, count, orderingPropertyName, valueProperty); + return items; } public Task> GetParagraphs(int page, int number) diff --git a/API_SQLuedo/Shared/IDataService.cs b/API_SQLuedo/Shared/IDataService.cs new file mode 100644 index 0000000..7c3b82b --- /dev/null +++ b/API_SQLuedo/Shared/IDataService.cs @@ -0,0 +1,13 @@ +using Entities.SQLudeoDB; +using Model.Business; +using Model.DTO; + +namespace Services +{ + public interface IDataService where T : class + { + IUserDataService UserService { get; } + IInquiryDataService InquiryDataService { get; } + ILessonDataService LessonDataService { get; } + } +} diff --git a/API_SQLuedo/Shared/IGenericService.cs b/API_SQLuedo/Shared/IGenericService.cs new file mode 100644 index 0000000..4e84eaf --- /dev/null +++ b/API_SQLuedo/Shared/IGenericService.cs @@ -0,0 +1,10 @@ +namespace Shared +{ + public interface IGenericService where T : class + { + public Task AddItem(T? item) where T : class; + public Task DeleteItem(int id) where T : class; + public Task UpdateItem(int? id, TDto? newItem) where T : class where TDto : class; + public Task> GetItems(int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class; + } +} diff --git a/API_SQLuedo/Shared/IInquiryDataService.cs b/API_SQLuedo/Shared/IInquiryDataService.cs new file mode 100644 index 0000000..0e71be5 --- /dev/null +++ b/API_SQLuedo/Shared/IInquiryDataService.cs @@ -0,0 +1,21 @@ +using Entities.SQLudeoDB; +using Model.DTO; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public interface IInquiryDataService : IGenericService + { + public IEnumerable GetInquiries(int page, int number); + public InquiryDTO GetInquiryById(int id); + public InquiryDTO GetInquiryByTitle(string title); + public bool DeleteInquiry(int id); + public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry); + public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable); + } +} diff --git a/API_SQLuedo/Shared/ILessonDataService.cs b/API_SQLuedo/Shared/ILessonDataService.cs new file mode 100644 index 0000000..7321547 --- /dev/null +++ b/API_SQLuedo/Shared/ILessonDataService.cs @@ -0,0 +1,20 @@ +using Model.DTO; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public interface ILessonDataService : IGenericService + { + public IEnumerable GetLessons(int page, int number); + public InquiryDTO GetLessonById(int id); + public InquiryDTO GetLessonByTitle(string title); + public bool DeleteLesson(int id); + public InquiryDTO UpdateLesson(int id, LessonDTO lesson); + public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit); + } +} diff --git a/API_SQLuedo/Shared/IUserDataService.cs b/API_SQLuedo/Shared/IUserDataService.cs new file mode 100644 index 0000000..bf2f2e2 --- /dev/null +++ b/API_SQLuedo/Shared/IUserDataService.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Services; +using Model.DTO; +using Shared; +using Model.Business; + +namespace Services +{ + public interface IUserDataService : IGenericService where T : class + { + public Task> GetUsers(int page, int number); + public Task GetUserById(int id); + public Task GetUserByUsername(string username); + public Task DeleteUser(int id); + public Task UpdateUser(int id, UserDTO user); + public Task CreateUser(string username, string password, string email, bool isAdmin); + + } +} diff --git a/API_SQLuedo/Shared/Shared.csproj b/API_SQLuedo/Shared/Shared.csproj new file mode 100644 index 0000000..b3751de --- /dev/null +++ b/API_SQLuedo/Shared/Shared.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + +