public void AddCourseToStudent(Guid courseId, string studentId) { Guard.WhenArgument(studentId, "StudentId can not be null!").IsNullOrWhiteSpace().Throw(); var studentCourseDto = new StudentCourseDto() { StudentId = studentId, CourseId = courseId }; var studentCourse = mapper.MapTo <StudentCourse>(studentCourseDto); Guard.WhenArgument(studentCourse, "Student Course can not be null!").IsNull().Throw(); var checkIfObjectAlreadyExists = this.studentCourses.AllAndDeleted .FirstOrDefault(sc => sc.CourseId == courseId && sc.StudentId == studentId); if (checkIfObjectAlreadyExists != null) { checkIfObjectAlreadyExists.IsDeleted = false; } else { this.studentCourses.Add(studentCourse); } this.saver.SaveChanges(); }
// -DtoQuery public async Task <StudentCourseDto> CreateStudentCourseAsync(StudentCourseDto studentCourseDto, string username) { OnCreate(studentCourseDto, username); var entity = StudentCourseDto.AsStudentCourseFunc(studentCourseDto); ToEntity(ref entity, studentCourseDto); //entity.InsertUser = entity.LastActivityUser = username; //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow; entity.AddTracker(username); _context.StudentCourses.Add(entity); OnBeforeCreate(entity, username); try { await _context.SaveChangesAsync(); } catch (Exception ex) { // _context.Entry(entity).State = EntityState.Detached; throw new Exception("Add error", ex); } finally { // _context.Entry(entity).State = EntityState.Detached; } OnAfterCreate(entity, username); // studentCourseDto = StudentCourseDto.AsStudentCourseDtoFunc(entity); studentCourseDto = await GetStudentCourseDtoAsync(entity.Id, StudentCourseDto.IncludeNavigations()); return(studentCourseDto); }
public void AddStudentCourse(StudentCourseDto courseDto) { // TODO: add validation for dependencies var entity = mapper.Map <StudentCourse>(courseDto); collegePortalDataFactory.StudentCourses.Add(entity); }
public async Task <ActionResult <StudentCourseDto> > PostStudentCourse(StudentCourseDto studentCourse) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } StudentCourseDto studentCourseDto; try { studentCourseDto = await studentCourseService.CreateStudentCourseAsync(studentCourse, User.Identity.Name); if (studentCourseDto != null) { this.RemoveCache(CacheKeys.StudentCourse); } } catch (Exception ex) { return(BadRequest(ex.Message)); } // var studentCourseDto = await _context.StudentCourses.Select(StudentCourseDto.AsStudentCourseDto).SingleOrDefaultAsync(m => m.Id == studentCourse.Id); return(CreatedAtAction("GetStudentCourse", new { id = studentCourseDto.Id }, studentCourseDto)); }
public void ThrowArgumentNullException_WhenMapperMapToMethodReturnsNull() { //Arrange var studentCoursesMock = new Mock <IRepository <StudentCourse> >(); var mapperMock = new Mock <IMappingProvider>(); var saverMock = new Mock <ISaver>(); var studentCourseDto = new StudentCourseDto() { CourseId = Guid.NewGuid(), StudentId = "uniqueId" }; var studentCourse = new StudentCourse() { CourseId = studentCourseDto.CourseId, StudentId = studentCourseDto.StudentId }; mapperMock .Setup(x => x.MapTo <StudentCourse>(studentCourseDto)) .Returns((StudentCourse)null); var studentCourseService = new StudentCourseService( studentCoursesMock.Object, mapperMock.Object, saverMock.Object); //Act && Assert Assert.ThrowsException <ArgumentNullException>(() => studentCourseService.AddCourseToStudent(studentCourseDto.CourseId, studentCourseDto.StudentId)); }
partial void OnCreate(StudentCourseDto studentCourse, string username) { if (_context.StudentCourses.Any(a => a.SessionId == studentCourse.SessionId && a.CourseId == studentCourse.CourseId && a.StudentId == studentCourse.StudentId )) { throw new Exception("Record not unique."); } }
public static Student CreateStudent(StudentCourseDto studentCourseDto) { return(new Student() { WisId = studentCourseDto.Id, WisPersonId = studentCourseDto.Person_Id, Name = studentCourseDto.Name, Email = studentCourseDto.Email, Login = studentCourseDto.Login, Update = studentCourseDto.Update }); }
public async Task <bool> Update(StudentCourseDto entity) { using (var logger = _loggerManager.CreateLogger()) { var studCrs = _mapper.Map <StudentCourse>(entity); await _repository.Update(studCrs); var result = await _repository.SaveAsync(); logger.LogInformation($"Degree {entity.Degree} of Student {studCrs.Id} is updated."); return(result); } }
public async Task <bool> DeleteStudentCourseAsync(StudentCourseDto studentCourseDto) { OnDelete(studentCourseDto); var entity = _context.StudentCourses .Where(x => x.Id == studentCourseDto.Id) .FirstOrDefault(); if (entity != null) { _context.StudentCourses.Remove(entity); OnBeforeDelete(entity); try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { // _context.Entry(entity).State = EntityState.Detached; var sqlException = ex.GetBaseException() as SqlException; if (sqlException != null) { var errorMessage = "deleting error"; var number = sqlException.Number; if (number == 547) { string table = GetErrorTable(sqlException) ?? "descendant"; errorMessage = $"Must delete {table} records before deleting Student Course"; } throw new Exception(errorMessage, ex); } } finally { // _context.Entry(entity).State = EntityState.Detached; } OnAfterDelete(entity); } else { return(false); } return(true); }
public ActionResult AddStudentCourse(StudentCourseDto studentCouseDto) { try { collegePortalManager.AddStudentCourse(studentCouseDto); ViewData["Success"] = "Student Course Added"; }catch (Exception e) { // Not recommended for production use ModelState.AddModelError(string.Empty, e.Message); return(View(studentCouseDto)); } return(RedirectToAction("Index")); }
public IHttpActionResult PostAssignCourseToStudent(int studentId, StudentCourseDto course) { var userData = IdentityHelper.FetchUserData(RequestContext); logger.Info("User {@user} requested assignment of course {@assignment} to student {studentId}", IdentityHelper.GetLoggedInUser(userData), course, studentId); var taking = service.AssignCourseToStudent(course); if (taking == null) { return(BadRequest("Course assignment failed")); } return(Ok(taking)); }
public async Task <IActionResult> PutStudentCourse(int id, StudentCourseDto studentCourse) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != studentCourse.Id) { return(BadRequest()); } try { var updated = await studentCourseService.UpdateStudentCourseAsync(studentCourse, User.Identity.Name); if (updated) { this.RemoveCache(CacheKeys.StudentCourse); } else { return(BadRequest("Update failed!.")); } } catch (DbUpdateConcurrencyException) { if (!StudentCourseExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); // return Ok(studentCourse); }
public async Task <bool> UpdateStudentCourseAsync(StudentCourseDto studentCourseDto, string username /*, String[] includeNavigations, params Expression<Func<StudentCourse, bool>>[] filters*/) { OnUpdate(studentCourseDto, username); // Get StudentCourse var entity = EntityQuery(_context, StudentCourseDto.IncludeNavigations()) .FirstOrDefault(x => x.Id == studentCourseDto.Id); if (entity != null) { entity = StudentCourseDto.ToStudentCourseFunc(entity, studentCourseDto); ToEntity(ref entity, studentCourseDto); //entity.UpdateUser = entity.LastActivityUser = username; //entity.UpdateDateTime = entity.LastActivityDateTime = DateTime.UtcNow; entity.EditTracker(username); OnBeforeUpdate(entity, username); try { await _context.SaveChangesAsync(); } catch (Exception ex) { // _context.Entry(entity).State = EntityState.Detached; throw new Exception("Update error", ex); } finally { // _context.Entry(entity).State = EntityState.Detached; } OnAfterUpdate(entity, username); } else { return(false); } return(true); }
public TakingDto AssignCourseToStudent(StudentCourseDto course) { logger.Info("Service received request for assigning a course to a student {@course}", course); StudentUser student = db.StudentsRepository.Get(s => s.Id == course.StudentId).FirstOrDefault(); if (student == null) { return(null); } Course theCourse = db.CoursesRepository.Get(c => c.Id == course.CourseId).FirstOrDefault(); if (theCourse == null) { return(null); } Program program = db.ProgramsRepository.Get(p => p.Teaching.Course.Id == course.CourseId).FirstOrDefault(); if (program == null) { return(null); } Taking taking = new Taking() { Program = program, Student = student }; db.TakingsRepository.Insert(taking); db.Save(); return(Converters.TakingsConverter.TakingToTakingDto(taking)); }
public async Task <ActionResult <IEnumerable <StudentCourseDto> > > GetStudentCourses(string searchText = null , int?sessionId = null , int?semesterId = null , int?studentId = null , int?courseId = null , double?score = null // +navigation // +Student , string studentMatricNo = null , string studentDescription = null //Basic-Nav-Property // -Student // +Course , string courseCode = null , string courseDescription = null //Basic-Nav-Property // -Course // +Session , string sessionName = null // -Session // +Semester , string semesterName = null // -Semester // -navigation /*, int pageNumber=1, int pageSize=7*/) { // var studentCourses = _context.StudentCourses.Select(StudentCourseDto.AsStudentCourseDto); List <Expression <Func <StudentCourseDto, bool> > > filters = null; if (String.IsNullOrEmpty(searchText) && (sessionId == null) && (semesterId == null) && (studentId == null) && (courseId == null) && (score == null) // +navigation // +Student && (String.IsNullOrEmpty(studentMatricNo)) && (String.IsNullOrEmpty(studentDescription)) //Basic-Nav-Property // -Student // +Course && (String.IsNullOrEmpty(courseCode)) && (String.IsNullOrEmpty(courseDescription)) //Basic-Nav-Property // -Course // +Session && (String.IsNullOrEmpty(sessionName)) // -Session // +Semester && (String.IsNullOrEmpty(semesterName)) // -Semester // -navigation ) { // return null; } else { filters = new List <Expression <Func <StudentCourseDto, bool> > >(); if (!String.IsNullOrEmpty(searchText)) { if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0) { filters.Add(x => x.Id.ToString().Contains(searchText)); } } if (sessionId != null) { filters.Add(x => x.SessionId == sessionId); } if (semesterId != null) { filters.Add(x => x.SemesterId == semesterId); } if (studentId != null) { filters.Add(x => x.StudentId == studentId); } if (courseId != null) { filters.Add(x => x.CourseId == courseId); } if (score != null) { filters.Add(x => x.Score == score); } // +navigation // +Student if (!String.IsNullOrEmpty(studentMatricNo)) { filters.Add(x => x.StudentMatricNo == studentMatricNo); } if (!String.IsNullOrEmpty(studentDescription)) { filters.Add(x => x.StudentDescription == studentDescription); } //Basic-Nav-Property // -Student // +Course if (!String.IsNullOrEmpty(courseCode)) { filters.Add(x => x.CourseCode == courseCode); } if (!String.IsNullOrEmpty(courseDescription)) { filters.Add(x => x.CourseDescription == courseDescription); } //Basic-Nav-Property // -Course // +Session if (!String.IsNullOrEmpty(sessionName)) { filters.Add(x => x.SessionName == sessionName); } // -Session // +Semester if (!String.IsNullOrEmpty(semesterName)) { filters.Add(x => x.SemesterName == semesterName); } // -Semester // -navigation } //sort //return studentCourses.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize); // OnSelectQuery(ref studentCourses); // return await studentCourses.ToListAsync(); if (filters == null) { return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations())); } else { return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations(), filters.ToArray())); } }
public async Task <ActionResult <IEnumerable <StudentCourseDto> > > GetStudentCourses(string searchText = null // + , int?sessionId = null , string sessionText = null // - // + , int?semesterId = null , string semesterText = null // - // + , int?studentId = null , string studentText = null // - // + , int?courseId = null , string courseText = null // - , double?scoreText = null /*, int pageNumber=1, int pageSize=7*/) { // var studentCourses = _context.StudentCourses.Select(StudentCourseDto.AsStudentCourseDto); List <Expression <Func <StudentCourseDto, bool> > > filters = null; if (String.IsNullOrEmpty(searchText) // + && (sessionId is null) && String.IsNullOrEmpty(sessionText) // - // + && (semesterId is null) && String.IsNullOrEmpty(semesterText) // - // + && (studentId is null) && String.IsNullOrEmpty(studentText) // - // + && (courseId is null) && String.IsNullOrEmpty(courseText) // - && scoreText == null ) { // return null; } else { filters = new List <Expression <Func <StudentCourseDto, bool> > >(); if (!String.IsNullOrEmpty(searchText)) { if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0) { filters.Add(x => x.Id.ToString().Contains(searchText)); } } // + if (!(sessionId is null)) { filters.Add(x => x.SessionId == sessionId); } if (!String.IsNullOrEmpty(sessionText)) { filters.Add(x => x.SessionName == sessionText); } // - // + if (!(semesterId is null)) { filters.Add(x => x.SemesterId == semesterId); } if (!String.IsNullOrEmpty(semesterText)) { filters.Add(x => x.SemesterName == semesterText); } // - // + if (!(studentId is null)) { filters.Add(x => x.StudentId == studentId); } if (!String.IsNullOrEmpty(studentText)) { filters.Add(x => x.StudentMatricNo == studentText); } // - // + if (!(courseId is null)) { filters.Add(x => x.CourseId == courseId); } if (!String.IsNullOrEmpty(courseText)) { filters.Add(x => x.CourseCode == courseText); } // - if (scoreText != null) { filters.Add(x => x.Score == scoreText); } } //sort //return studentCourses.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize); // OnSelectQuery(ref studentCourses); // return await studentCourses.ToListAsync(); if (filters == null) { return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations())); } else { return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations(), filters.ToArray())); } }
public void DeleteStudentCourse(StudentCourseDto courseDto) { var entity = mapper.Map <StudentCourse>(courseDto); collegePortalDataFactory.StudentCourses.Delete(entity); }
partial void OnCreate(StudentCourseDto studentCourse, string username) { throw new NotImplementedException(); }
partial void ToEntity(ref StudentCourse entity, StudentCourseDto studentCourseDto);
partial void OnDelete(StudentCourseDto studentCourseDto);
public async Task <ActionResult <StudentCourseDto> > GetStudentCourse(int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var studentCourse = await studentCourseService.GetStudentCourseDtoAsync(id, StudentCourseDto.IncludeNavigations()); if (studentCourse == null) { return(NotFound()); } return(studentCourse); }
partial void OnUpdate(StudentCourseDto studentCourseDto, string username);