From 5948f2f4539b16df045f33d4a2c7db3432ee96ff Mon Sep 17 00:00:00 2001 From: esantangelo <enzo020895@gmail.com> Date: Mon, 2 Nov 2020 20:25:19 -0300 Subject: [PATCH] add create and delete folders --- .../Tsi1.Api/Controllers/CourseController.cs | 16 ++++++++++++++-- .../Tsi1.Api/Controllers/TenantController.cs | 9 ++++++++- .../Helpers/MappingProfile.cs | 2 +- .../Interfaces/ICourseService.cs | 2 +- .../Interfaces/IFileService.cs | 6 ++++-- .../Interfaces/ITenantService.cs | 2 ++ .../Services/CourseService.cs | 8 ++++---- .../Services/FileService.cs | 14 ++++++++++++++ .../Services/TenantService.cs | 19 +++++++++++++++++++ 9 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 540ab08..d65d1f9 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; @@ -19,9 +20,12 @@ namespace Tsi1.Api.Controllers { private readonly ICourseService _courseService; - public CourseController(ICourseService courseService) + private readonly IFileService _fileService; + + public CourseController(ICourseService courseService, IFileService fileService) { _courseService = courseService; + _fileService = fileService; } [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] @@ -54,6 +58,10 @@ namespace Tsi1.Api.Controllers return BadRequest(result.Message); } + var path = Path.Combine(tenantId.ToString(), result.Data.Id.ToString()); + + _fileService.CreateFolder(path); + return Ok(); } @@ -197,11 +205,15 @@ namespace Tsi1.Api.Controllers return BadRequest(result.Message); } - if (result.Data == false) + if (result.Data == null) { return NotFound(result.Message); } + var path = Path.Combine(result.Data.TenantId.ToString(), result.Data.Id.ToString()); + + _fileService.DeleteFolder(path); + return Ok(); } diff --git a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs index 738bc5b..8b4373a 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs @@ -17,9 +17,12 @@ namespace Tsi1.Api.Controllers { private readonly ITenantService _tenantService; - public TenantController(ITenantService tenantService) + private readonly IFileService _fileService; + + public TenantController(ITenantService tenantService, IFileService fileService) { _tenantService = tenantService; + _fileService = fileService; } [Authorize(Roles = UserTypes.UdelarAdmin)] @@ -47,6 +50,8 @@ namespace Tsi1.Api.Controllers return BadRequest(result.Message); } + _fileService.CreateFolder(result.Data.Id.ToString()); + return Ok(result.Data); } @@ -85,6 +90,8 @@ namespace Tsi1.Api.Controllers return NotFound(result.Message); } + _fileService.DeleteFolder(tenantId.ToString()); + return Ok(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index df85a72..9e1822f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -45,7 +45,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<UserModifyDto, User>(); CreateMap<StudentPreviewDto, Student>(); CreateMap<ProfessorPreviewDto, Professor>(); - CreateMap<CourseCreateDto, Course>().ForMember(x => x.TenantId, opt => opt.Ignore()); + CreateMap<CourseCreateDto, Course>(); CreateMap<CoursePreviewDto, Course>(); CreateMap<TenantPreviewDto, Tenant>(); CreateMap<TenantCreateDto, Tenant>(); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index 885c3b3..f13641f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -22,7 +22,7 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<bool>> Modify(int courseId, CourseCreateDto courseDto); - Task<ServiceResult<bool>> Delete(int courseId); + Task<ServiceResult<Course>> Delete(int courseId); Task<ServiceResult<bool>> DropOutFromCourse(int userId, int courseId); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs index 594623d..ed40c63 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs @@ -10,8 +10,10 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface IFileService { - //Task<ServiceResult<FileDto>> SaveFile(IFormFile file); - Task<ServiceResult<FileDto>> Create(IFormFile file, int tenantId, int courseId); + + void CreateFolder(string folderPath); + + void DeleteFolder(string folderPath); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs index ea42bbc..a54ceb1 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs @@ -11,6 +11,8 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<ServiceResult<int>> GetByName(string tenantName); + Task<ServiceResult<int>> GetById(int tenantId); + Task<ServiceResult<List<TenantPreviewDto>>> GetAll(); Task<ServiceResult<TenantPreviewDto>> Create(TenantCreateDto newTenant); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index e385562..6930b54 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -41,7 +41,7 @@ namespace Tsi1.BusinessLayer.Services } var course = _mapper.Map<Course>(newCourse); - + _context.Courses.Add(course); await _context.SaveChangesAsync(); @@ -219,9 +219,9 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<bool>> Delete(int courseId) + public async Task<ServiceResult<Course>> Delete(int courseId) { - var result = new ServiceResult<bool>(); + var result = new ServiceResult<Course>(); var course = await _context.Courses .FirstOrDefaultAsync(x => x.Id == courseId); @@ -236,7 +236,7 @@ namespace Tsi1.BusinessLayer.Services await _context.SaveChangesAsync(); - result.Data = true; + result.Data = course; return result; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs index 5e7fcd6..3152501 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs @@ -121,5 +121,19 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public void CreateFolder(string folderPath) + { + var path = Path.Combine(_path, folderPath); + + Directory.CreateDirectory(path); + } + + public void DeleteFolder(string folderPath) + { + var path = Path.Combine(_path, folderPath); + + Directory.Delete(path); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs index 8f62967..d749ea8 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs @@ -122,5 +122,24 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<int>> GetById(int tenantId) + { + var result = new ServiceResult<int>(); + + var tenant = await _context.Tenants + .FirstOrDefaultAsync(x => x.Id == tenantId); + + if (tenant == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.TenantDoesNotExist, tenantId); + return result; + } + + result.Data = tenant.Id; + + return result; + } } } -- GitLab