public CourseDE CreateCourse(CourseDto courseDto) { // validation if (string.IsNullOrWhiteSpace(courseDto.CourseCode)) { throw new AppException("Course Code is required"); } if (_context.tbl_course.Any(x => x.CourseCode == courseDto.CourseCode)) { throw new AppException("Course Code \"" + courseDto.CourseCode + "\" is unavailable"); } UserDE user = _userRepository.GetUsers(new List <Guid>(new Guid[] { courseDto.CreatedBy })).FirstOrDefault(); if (user == null || string.Equals(user.Role, "student", StringComparison.OrdinalIgnoreCase)) { throw new AppException("User \"" + courseDto.CreatedBy + "\" does not exist"); } CourseDE course = new CourseDE(); course = _mapper.Map <CourseDto, CourseDE>(courseDto); course.CourseId = new Guid(); course.CreatedOn = DateTime.Now; _context.tbl_course.Add(course); _context.SaveChanges(); return(course); }
public void DeleteCourse(Guid courseId) { CourseDE course = _context.tbl_course.Where(x => x.CourseId == courseId).FirstOrDefault(); if (course == null) { throw new AppException("CourseId does not exist"); } _context.tbl_course.Remove(course); _context.SaveChanges(); }
public CourseDE UpdateCourse(CourseDto courseDto) { CourseDE course = _context.tbl_course.Where(x => x.CourseId == courseDto.CourseId).FirstOrDefault(); if (course == null) { throw new AppException("CourseId is invalid"); } course.CourseCode = string.IsNullOrEmpty(courseDto.CourseCode) ? course.CourseCode : courseDto.CourseCode; course.CourseName = string.IsNullOrEmpty(courseDto.CourseName) ? course.CourseName : courseDto.CourseName; course.CourseDescription = string.IsNullOrEmpty(courseDto.CourseDescription) ? course.CourseCode : courseDto.CourseDescription; _context.tbl_course.Update(course); _context.SaveChanges(); return(course); }