public async Task <IActionResult> AddOrUpdateStudentRouteMappingAsync([FromBody] AddOrUpdateStudentRouteMappingAc studentRouteMapping) { var user = await _userManager.FindByNameAsync(User.Identity.Name); return(Ok(await _studentRouteMappingRepository.AddOrUpdateStudentRouteMappingAsync(studentRouteMapping, user))); }
/// <summary> /// Method to add and update student route mapping - SS /// </summary> /// <param name="studentRouteMapping">student route detail</param> /// <param name="loggedInUser">logged in user</param> /// <returns>response</returns> public async Task <StudentRouteMappingResponse> AddOrUpdateStudentRouteMappingAsync(AddOrUpdateStudentRouteMappingAc studentRouteMapping, ApplicationUser loggedInUser) { var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true); if (!await _iMSDbContext.Routes.AnyAsync(x => x.InstituteId == instituteId && x.Id == studentRouteMapping.RouteId)) { return new StudentRouteMappingResponse() { HasError = true, Message = "Route not found", ErrorType = StudentRouteMappingResponseType.RouteId } } ; else { var studentIds = studentRouteMapping.StudentIds.Distinct().ToList(); var studentCount = await _iMSDbContext.StudentBasicInformation.CountAsync(x => x.InstituteId == instituteId && studentIds.Contains(x.Id)); if (studentIds.Count != studentCount) { return new StudentRouteMappingResponse() { HasError = true, Message = "Student not found", ErrorType = StudentRouteMappingResponseType.StudentId } } ; else { var previous = await _iMSDbContext.StudentRouteMappings.Where(x => x.RouteId == studentRouteMapping.RouteId).ToListAsync(); _iMSDbContext.StudentRouteMappings.RemoveRange(previous); await _iMSDbContext.SaveChangesAsync(); List <DomainModel.Models.StudentRouteMapping> studentRouteMappings = new List <DomainModel.Models.StudentRouteMapping>(); foreach (var student in studentRouteMapping.StudentIds) { studentRouteMappings.Add(new DomainModel.Models.StudentRouteMapping() { CreatedOn = DateTime.UtcNow, RouteId = studentRouteMapping.RouteId, StudentId = student, UpdatedById = loggedInUser.Id }); } _iMSDbContext.StudentRouteMappings.AddRange(studentRouteMappings); await _iMSDbContext.SaveChangesAsync(); return(new StudentRouteMappingResponse() { HasError = false, Message = "Student route detail updated successfully" }); } } }