diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 0dbeef495bbeaf299bc1edeeeac6b1b653eaa583..05dcfdbbcd5e6013d14e474cc5c9c735780e4f4d 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 827cd381abf5ff45ff7dc02e1ab5029ae9e127f7..ec1a569579f6fcaf088a3e36df4554b901c0d5e3 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; }