diff --git a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs new file mode 100644 index 0000000000000000000000000000000000000000..f89bb6beb7a7a425bda20d76669b2315ba465083 --- /dev/null +++ b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.BusinessLayer.Interfaces; + +namespace Tsi1.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TenantController : ControllerBase + { + private readonly ITenantService _tenantService; + + public TenantController(ITenantService tenantService) + { + _tenantService = tenantService; + } + + [Authorize(Roles = UserTypes.UdelarAdmin)] + [HttpGet("GetAll")] + public async Task<IActionResult> GetAll() + { + var result = await _tenantService.GetAll(); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + + [Authorize(Roles = UserTypes.UdelarAdmin)] + [HttpPost("Create")] + public async Task<IActionResult> Create(TenantCreateDto newTenant) + { + var result = await _tenantService.Create(newTenant); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + } +} diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index baf9426f46af9bdbe6eac3097ff236335403fffe..11bb6a7cd802555d29bda2710d556137f6f3dc04 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -22,19 +22,40 @@ namespace Tsi1.Api.Controllers private readonly IJwtAuthManager _jwtAuthManager; private readonly IUserService _userService; private readonly IUserTypeService _userTypeService; + private readonly ITenantService _tenantService; - public UserController(IJwtAuthManager jwtAuthManager, IUserService userService, IUserTypeService userTypeService) + public UserController(IJwtAuthManager jwtAuthManager, IUserService userService, + IUserTypeService userTypeService, ITenantService tenantService) { _jwtAuthManager = jwtAuthManager; _userService = userService; _userTypeService = userTypeService; + _tenantService = tenantService; } [AllowAnonymous] [HttpPost("Login")] public async Task<IActionResult> Login(LoginRequest request) { - var result = await _userService.Authenticate(request.UserName, request.Password, request.TenantId); + var resultSplit = request.UserName.Split("@"); + + if (resultSplit.Count() != 2) + { + return BadRequest(ErrorMessages.InvalidUsername); + } + + var userName = resultSplit[0]; + + var tenantName = resultSplit[1]; + + var tenantId = await _tenantService.GetByName(tenantName); + + if (tenantId.HasError) + { + return BadRequest(tenantId.Message); + } + + var result = await _userService.Authenticate(userName, request.Password, tenantId.Data); if (result.HasError) { @@ -63,8 +84,8 @@ namespace Tsi1.Api.Controllers }); } + [AllowAnonymous] [HttpPost("RefreshToken")] - [Authorize] public async Task<ActionResult> RefreshToken([FromBody] RefreshTokenRequest request) { try @@ -100,10 +121,38 @@ namespace Tsi1.Api.Controllers public async Task<IActionResult> Register(UserRegisterDto dto) { var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); - dto.TenantId = tenantId; var userTypeResult = await _userTypeService.GetById(dto.UserTypeId); + if (userTypeResult.HasError) + { + return BadRequest(userTypeResult.Message); + } + + var userType = userTypeResult.Data; + + if (userType.Name == UserTypes.UdelarAdmin || + userType.Name == UserTypes.FacultyAdmin) + { + return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name)); + } + + var userServiceResult = await _userService.Create(dto, userType.Name, tenantId); + + if (userServiceResult.HasError) + { + return BadRequest(userServiceResult.Message); + } + + return Ok(); + } + + [Authorize(Roles = UserTypes.UdelarAdmin)] + [HttpPost("RegisterAdmin/{tenantId}")] + public async Task<IActionResult> RegisterAdmin(UserRegisterDto dto, int tenantId) + { + var userTypeResult = await _userTypeService.GetById(dto.UserTypeId); + if (userTypeResult.HasError) { BadRequest(userTypeResult.Message); @@ -111,7 +160,13 @@ namespace Tsi1.Api.Controllers var userType = userTypeResult.Data; - var userServiceResult = await _userService.Create(dto, userType.Name); + if (userType.Name == UserTypes.Student || + userType.Name == UserTypes.Professor) + { + return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name)); + } + + var userServiceResult = await _userService.Create(dto, userType.Name, tenantId); if (userServiceResult.HasError) { @@ -152,5 +207,21 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } + [Authorize(Roles = UserTypes.UdelarAdmin + ", " + UserTypes.FacultyAdmin)] + [HttpGet("GetUserTypes")] + public async Task<IActionResult> GetUserTypes() + { + var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value; + + var result = await _userTypeService.GetAll(userType); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + } } diff --git a/Tsi1.Api/Tsi1.Api/Models/LoginRequest.cs b/Tsi1.Api/Tsi1.Api/Models/LoginRequest.cs index 0ce83cfe5fb8a94cd72d654e67cf93019638fdd8..96e87b18d03e7185430f760fb0a5c9caefe6611f 100644 --- a/Tsi1.Api/Tsi1.Api/Models/LoginRequest.cs +++ b/Tsi1.Api/Tsi1.Api/Models/LoginRequest.cs @@ -16,9 +16,5 @@ namespace Tsi1.Api.Models [Required] [JsonPropertyName("password")] public string Password { get; set; } - - [Required] - [JsonPropertyName("tenantId")] - public int TenantId { get; set; } } } diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index 178ffe222fb38c8c9782e043d565acfdbcef60f9..5c6ae0b04bd61a10dddc678a0ce18549b4b1be73 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -60,7 +60,8 @@ namespace Tsi1.Api services.AddScoped<ICourseService, CourseService>(); services.AddScoped<IForumService, ForumService>(); services.AddScoped<IPostService, PostService>(); - services.AddScoped<IPostMessageService, PostMessageService>(); + services.AddScoped<IPostMessageService, PostMessageService>(); + services.AddScoped<ITenantService, TenantService>(); services.Configure<MailSettings>(Configuration.GetSection("MailSettings")); services.AddScoped<IEmailService, EmailService>(); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCreateDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..1906971794cc8058b47049cbc22f6e4a7ab84dd2 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCreateDto.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class TenantCreateDto + { + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..74b8300acafec93c16aac87e34da1af457055766 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class TenantPreviewDto + { + public int Id { get; set; } + + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs index 2cc5d2740edf2171ffa30d17e60389bc4290a17a..d01877d97c514b123db7163125918cf0d920e131 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs @@ -29,9 +29,6 @@ namespace Tsi1.BusinessLayer.Dtos [Required] public string IdentityCard { get; set; } - [JsonIgnore] - public int TenantId { get; set; } - public int Age { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..5ba1793dd70f29f35fee0498b858c45a7e3b4a8a --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class UserTypeDto + { + public int Id { get; set; } + + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 880795c242b472b8ec7df424f0bc42ac33b789f7..99c521a4d670fd66eb3eee6753c7b97c4afc902b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -13,7 +13,8 @@ namespace Tsi1.BusinessLayer.Helpers public const string StudentCourseAlreadyExists = "El estudiante '{0}' ya se encuentra matriculado en el curso '{1}'"; public const string ProffesorDoesNotExist = "El profesor con Id de usuario: '{0}' no existe"; public const string ProfessorCourseAlreadyExists = "El profesor '{0}' ya es docente del curso '{1}'"; - + public const string InvalidUsername = "El nombre de usuario debe ser de la forma: 'usuario@facultad'"; + public const string ForumDoesNotExist = "El foro con id '{0}' no existe"; public const string DuplicateForumName = "Ya existe un foro con nombre '{0}'"; @@ -29,6 +30,11 @@ namespace Tsi1.BusinessLayer.Helpers public const string CourseDoesNotExist = "El curso '{0}' no existe"; public const string DuplicateCourseName = "Ya existe un curso con nombre '{0}'"; - + + public const string TenantDoesNotExist = "La Facultad '{0}' no existe"; + public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'"; + + public const string InvalidUserType = "Tipo de usuario invalido '{0}'"; + } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index 8fab13c1e816f8afb5cbf38d9981684b7555ab7b..5d666d16c6f9578c22a8c22b867329345ee444ae 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -26,8 +26,10 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<Professor, ProfessorPreviewDto>(); CreateMap<Course, CourseCreateDto>(); CreateMap<Course, CoursePreviewDto>(); + CreateMap<Tenant, TenantPreviewDto>(); + CreateMap<Tenant, TenantCreateDto>(); + CreateMap<UserType, UserTypeDto>(); - CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumPreviewDto, Forum>(); CreateMap<PostCreateDto, Post>(); @@ -41,7 +43,10 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<StudentPreviewDto, Student>(); CreateMap<ProfessorPreviewDto, Professor>(); CreateMap<CourseCreateDto, Course>(); - CreateMap<CoursePreviewDto, Course>(); + CreateMap<CoursePreviewDto, Course>(); + CreateMap<TenantPreviewDto, Tenant>(); + CreateMap<TenantCreateDto, Tenant>(); + CreateMap<UserTypeDto, UserType>(); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/UserTypes.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/UserTypes.cs index 9fb3b0e8bfd691ed0833bde757acb92626676838..d57a241d16e176b62ab7bdd51b1416cb8b0d42bb 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/UserTypes.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/UserTypes.cs @@ -9,5 +9,6 @@ namespace Tsi1.BusinessLayer.Helpers public const string FacultyAdmin = nameof(FacultyAdmin); public const string Professor = nameof(Professor); public const string Student = nameof(Student); + public const string UdelarAdmin = nameof(UdelarAdmin); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs new file mode 100644 index 0000000000000000000000000000000000000000..90bf95b9d814421c99c54eff4ff0a2e20885af0f --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; + +namespace Tsi1.BusinessLayer.Interfaces +{ + public interface ITenantService + { + Task<ServiceResult<int>> GetByName(string tenantName); + + Task<ServiceResult<List<TenantPreviewDto>>> GetAll(); + + Task<ServiceResult<TenantPreviewDto>> Create(TenantCreateDto newTenant); + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs index ed780959cdca1def549471d306c0ad64ef0f7ac6..12444e66d7fe3e492bbcbdb8b0d7353730c89beb 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs @@ -12,7 +12,7 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId); - Task<ServiceResult<User>> Create(UserRegisterDto dto, string type); + Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId); Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs index 733e3d045d6811b1d29fcdae88e6427ab51876b0..e84218a454134b3cc6e5e2e9497059a8c9bbc2fb 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs @@ -2,13 +2,16 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; using Tsi1.BusinessLayer.Helpers; -using Tsi1.DataLayer.Entities; + namespace Tsi1.BusinessLayer.Interfaces { public interface IUserTypeService { - public Task<ServiceResult<UserType>> GetById(int id); + public Task<ServiceResult<UserTypeDto>> GetById(int id); + + public Task<ServiceResult<List<UserTypeDto>>> GetAll(string userType); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index 4f8db3fc7b9bc1dfac00432c60f33e838baa8dc5..4c1f4ceec956384a2df10f16c45ab66fb0f6cc8f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -30,20 +30,22 @@ namespace Tsi1.BusinessLayer.Services { var result = new ServiceResult<Course>(); - var course = _mapper.Map<Course>(newCourse); - - _context.Courses.Add(course); - try - { - await _context.SaveChangesAsync(); - } - catch (DbUpdateException) + var existingCourse = await _context.Courses + .FirstOrDefaultAsync(x => x.Name == newCourse.Name && x.TenantId == newCourse.TenantId); + + if (existingCourse != null) { result.HasError = true; result.Message = string.Format(ErrorMessages.DuplicateCourseName, newCourse.Name); return result; } - + + var course = _mapper.Map<Course>(newCourse); + + _context.Courses.Add(course); + + await _context.SaveChangesAsync(); + result.Data = course; return result; } @@ -109,25 +111,26 @@ namespace Tsi1.BusinessLayer.Services return result; } - var studentCourse = new StudentCourse - { - Course = course, - Student = user.Student - }; - - _context.StudentCourses.Add(studentCourse); + var existingStudentCourse = await _context.StudentCourses + .FirstOrDefaultAsync(x => x.StudentId == user.StudentId && x.CourseId == course.Id); - try - { - await _context.SaveChangesAsync(); - } - catch (DbUpdateException) + if (existingStudentCourse != null) { result.HasError = true; result.Message = string.Format(ErrorMessages.StudentCourseAlreadyExists, user.Username, course.Name); return result; } + var studentCourse = new StudentCourse + { + Course = course, + Student = user.Student + }; + + _context.StudentCourses.Add(studentCourse); + + await _context.SaveChangesAsync(); + return result; } @@ -156,6 +159,16 @@ namespace Tsi1.BusinessLayer.Services return result; } + var existingProfessorCourse = await _context.ProfessorCourses + .FirstOrDefaultAsync(x => x.ProfessorId == user.ProfessorId && x.CourseId == course.Id); + + if (existingProfessorCourse != null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.ProfessorCourseAlreadyExists, user.Username, course.Name); + return result; + } + var professorCourse = new ProfessorCourse { Course = course, @@ -164,16 +177,7 @@ namespace Tsi1.BusinessLayer.Services _context.ProfessorCourses.Add(professorCourse); - try - { - await _context.SaveChangesAsync(); - } - catch (DbUpdateException) - { - result.HasError = true; - result.Message = string.Format(ErrorMessages.ProfessorCourseAlreadyExists, user.Username, course.Name); - return result; - } + await _context.SaveChangesAsync(); return result; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs index ec1a569579f6fcaf088a3e36df4554b901c0d5e3..4d204ca1ff885a61661eb6b996acb95195bf71d1 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs @@ -30,21 +30,22 @@ namespace Tsi1.BusinessLayer.Services { var result = new ServiceResult<Forum>(); - var forum = _mapper.Map<Forum>(newForum); - - _context.Forums.Add(forum); + var existingForum = _context.Forums + .FirstOrDefaultAsync(x => x.Name == newForum.Name && x.CourseId == newForum.CourseId); - try - { - await _context.SaveChangesAsync(); - } - catch (DbUpdateException) + if (existingForum != null) { result.HasError = true; result.Message = string.Format(ErrorMessages.DuplicateForumName, newForum.Name); return result; } + var forum = _mapper.Map<Forum>(newForum); + + _context.Forums.Add(forum); + + await _context.SaveChangesAsync(); + result.Data = forum; return result; @@ -121,6 +122,16 @@ namespace Tsi1.BusinessLayer.Services return result; } + var existingForumUser = await _context.ForumUsers + .FirstOrDefaultAsync(x => x.ForumId == forum.Id && x.UserId == user.Id); + + if (existingForumUser != null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.DuplicateForumUser, user.Username, forum.Name); + return result; + } + var forumUser = new ForumUser { ForumId = forumId, @@ -129,16 +140,7 @@ namespace Tsi1.BusinessLayer.Services _context.ForumUsers.Add(forumUser); - try - { - await _context.SaveChangesAsync(); - } - catch (DbUpdateException) - { - result.HasError = true; - result.Message = string.Format(ErrorMessages.DuplicateForumUser, user.Username, forum.Name); - return result; - } + await _context.SaveChangesAsync(); return result; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs new file mode 100644 index 0000000000000000000000000000000000000000..c2659069a5eacd7516d4c4c6d49cf6dcb06d749a --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs @@ -0,0 +1,83 @@ +using AutoMapper; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.BusinessLayer.Interfaces; +using Tsi1.DataLayer; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.BusinessLayer.Services +{ + public class TenantService : ITenantService + { + private readonly Tsi1Context _context; + + private readonly IMapper _mapper; + + public TenantService(Tsi1Context context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task<ServiceResult<List<TenantPreviewDto>>> GetAll() + { + var result = new ServiceResult<List<TenantPreviewDto>>(); + + var tenants = await _context.Tenants.ToListAsync(); + + var tenantsDto = _mapper.Map<List<TenantPreviewDto>>(tenants); + + result.Data = tenantsDto; + + return result; + } + + public async Task<ServiceResult<int>> GetByName(string tenantName) + { + var result = new ServiceResult<int>(); + + var tenant = await _context.Tenants + .FirstOrDefaultAsync(x => x.Name == tenantName); + + if (tenant == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.TenantDoesNotExist, tenantName); + return result; + } + + result.Data = tenant.Id; + + return result; + } + + public async Task<ServiceResult<TenantPreviewDto>> Create(TenantCreateDto newTenant) + { + var result = new ServiceResult<TenantPreviewDto>(); + + var existingTenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Name == newTenant.Name); + + if (existingTenant != null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.DuplicateTenantName, newTenant.Name); + return result; + } + + var tenant = _mapper.Map<Tenant>(newTenant); + _context.Tenants.Add(tenant); + + await _context.SaveChangesAsync(); + + result.Data = _mapper.Map<TenantPreviewDto>(tenant); + + return result; + } + + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs index b38e807990555358a68b41e281e3131b7865be78..3aa39cc07fc53d7fae54d12517381a2e411e7f53 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs @@ -52,19 +52,19 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type) + public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId) { var result = new ServiceResult<User>(); var user = _mapper.Map<User>(dto); - + user.TenantId = tenantId; + if (type == UserTypes.Student) { user.Student = new Student() { IdentityCard = dto.IdentityCard, Age = dto.Age, - TenantId = dto.TenantId }; } @@ -73,7 +73,6 @@ namespace Tsi1.BusinessLayer.Services user.Professor = new Professor() { IdentityCard = dto.IdentityCard, - TenantId = dto.TenantId }; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs index 18180fd57ad7c186d9ff40588009a08457956f56..e11f550b6dd13589a7379b8579efa715632b989b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs @@ -1,8 +1,11 @@ -using Microsoft.EntityFrameworkCore; +using AutoMapper; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; using Tsi1.BusinessLayer.Helpers; using Tsi1.BusinessLayer.Interfaces; using Tsi1.DataLayer; @@ -14,14 +17,43 @@ namespace Tsi1.BusinessLayer.Services { private readonly Tsi1Context _context; - public UserTypeService(Tsi1Context context) + private readonly IMapper _mapper; + + public UserTypeService(Tsi1Context context, IMapper mapper) { _context = context; + _mapper = mapper; } - public async Task<ServiceResult<UserType>> GetById(int id) + public async Task<ServiceResult<List<UserTypeDto>>> GetAll(string userType) { - var result = new ServiceResult<UserType>(); + var result = new ServiceResult<List<UserTypeDto>>(); + + List<UserType> userTypes = null; + + if (userType == UserTypes.UdelarAdmin) + { + userTypes = await _context.UserTypes + .Where(x => x.Name == UserTypes.UdelarAdmin || + x.Name == UserTypes.FacultyAdmin) + .ToListAsync(); + } + else if (userType == UserTypes.FacultyAdmin) + { + userTypes = await _context.UserTypes + .Where(x => x.Name == UserTypes.Student || + x.Name == UserTypes.Professor) + .ToListAsync(); + } + + result.Data = _mapper.Map<List<UserTypeDto>>(userTypes); + + return result; + } + + public async Task<ServiceResult<UserTypeDto>> GetById(int id) + { + var result = new ServiceResult<UserTypeDto>(); var userType = await _context.UserTypes.FirstOrDefaultAsync(x => x.Id == id); @@ -30,10 +62,11 @@ namespace Tsi1.BusinessLayer.Services result.HasError = true; result.Message = string.Format(ErrorMessages.UserTypeDoesNotExist, id); } - - result.Data = userType; + + result.Data = _mapper.Map<UserTypeDto>(userType); return result; } + } }