From 0b1cbe5197b3f6f0af0878a31095b02c6e5a8904 Mon Sep 17 00:00:00 2001 From: esantangelo <enzo020895@gmail.com> Date: Sat, 24 Oct 2020 17:19:23 -0300 Subject: [PATCH] add ErrorMessages --- .../Helpers/ErrorMessages.cs | 11 +++++++ .../Services/ForumService.cs | 32 +++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 0dbeef4..05dcfdb 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -9,13 +9,24 @@ namespace Tsi1.BusinessLayer.Helpers public const string UserDoesNotExist = "El usuario '{0}' no existe"; public const string IncorrectPassword = "Contraseña incorrecta"; public const string UserTypeDoesNotExist = "El tipo de usuario con id '{0}' no existe"; + public const string StudentDoesNotExist = "El estudiante con Id de usuario: '{0}' no existe"; + public const string ProffesorDoesNotExist = "El profesor con Id de usuario: '{0}' no existe"; + public const string ForumDoesNotExist = "El foro con id '{0}' no existe"; + public const string DuplicateForumName = "Ya existe un foro con nombre '{0}'"; + public const string DuplicateForumUser = "El usuario '{0}' ya se encuentra matriculado al foro '{1}'"; + + public const string PostDoesNotExist = "El post con id '{0}' no existe"; public const string PostMessageDoesNotExist = "El mensage con id '{0}' no existe"; public const string CannotConnectToSmtpServer = "No se pudo conectar al servidor SMTP"; public const string CannotAuthenticateToSmtpServer = "No se pudo autenticar en el servidor SMTP"; public const string CannotSendEmail = "No se pudo mandar el mail con asunto {0}"; + + public const string CourseDoesNotExist = "El curso '{0}' no existe"; + public const string DuplicateCourseName = "Ya existe un curso con nombre '{0}'"; + } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs index 827cd38..ec1a569 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs @@ -33,7 +33,17 @@ namespace Tsi1.BusinessLayer.Services var forum = _mapper.Map<Forum>(newForum); _context.Forums.Add(forum); - await _context.SaveChangesAsync(); + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.DuplicateForumName, newForum.Name); + return result; + } result.Data = forum; @@ -102,6 +112,15 @@ namespace Tsi1.BusinessLayer.Services return result; } + var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId); + + if (user == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId); + return result; + } + var forumUser = new ForumUser { ForumId = forumId, @@ -110,7 +129,16 @@ namespace Tsi1.BusinessLayer.Services _context.ForumUsers.Add(forumUser); - await _context.SaveChangesAsync(); + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.DuplicateForumUser, user.Username, forum.Name); + return result; + } return result; } -- GitLab