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 index fef180d5f1efd31df55b46ce72537a199c10f346..c2659069a5eacd7516d4c4c6d49cf6dcb06d749a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs @@ -60,20 +60,20 @@ namespace Tsi1.BusinessLayer.Services { var result = new ServiceResult<TenantPreviewDto>(); - var tenant = _mapper.Map<Tenant>(newTenant); - _context.Tenants.Add(tenant); + var existingTenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Name == newTenant.Name); - try - { - await _context.SaveChangesAsync(); - } - catch (DbUpdateException) + 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;