// -DtoQuery public async Task <CourseDto> CreateCourseAsync(CourseDto courseDto, string username) { OnCreate(courseDto, username); var entity = CourseDto.AsCourseFunc(courseDto); ToEntity(ref entity, courseDto); //entity.InsertUser = entity.LastActivityUser = username; //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow; entity.AddTracker(username); _context.Courses.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); // courseDto = CourseDto.AsCourseDtoFunc(entity); courseDto = await GetCourseDtoAsync(entity.Id, CourseDto.IncludeNavigations()); return(courseDto); }
public async Task <ActionResult <IEnumerable <CourseDto> > > GetCourses(string searchText = null , string codeText = null , string titleText = null , double?unitText = null /*, int pageNumber=1, int pageSize=7*/) { // var courses = _context.Courses.Select(CourseDto.AsCourseDto); List <Expression <Func <CourseDto, bool> > > filters = null; if (String.IsNullOrEmpty(searchText) && String.IsNullOrEmpty(codeText) && String.IsNullOrEmpty(titleText) && unitText == null ) { // return null; } else { filters = new List <Expression <Func <CourseDto, bool> > >(); if (!String.IsNullOrEmpty(searchText)) { if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0) { filters.Add(x => x.Id.ToString().Contains(searchText)); } } if (!String.IsNullOrEmpty(codeText)) { filters.Add(x => x.Code == codeText); } if (!String.IsNullOrEmpty(titleText)) { filters.Add(x => x.Title == titleText); } if (unitText != null) { filters.Add(x => x.Unit == unitText); } } //sort //return courses.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize); // OnSelectQuery(ref courses); // return await courses.ToListAsync(); if (filters == null) { return(await courseService.GetCourseDtoesAsync(CourseDto.IncludeNavigations())); } else { return(await courseService.GetCourseDtoesAsync(CourseDto.IncludeNavigations(), filters.ToArray())); } }
public async Task <ActionResult <CourseDto> > GetCourse(int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var course = await courseService.GetCourseDtoAsync(id, CourseDto.IncludeNavigations()); if (course == null) { return(NotFound()); } return(course); }
public async Task <bool> UpdateCourseAsync(CourseDto courseDto, string username /*, String[] includeNavigations, params Expression<Func<Course, bool>>[] filters*/) { OnUpdate(courseDto, username); // Get Course var entity = EntityQuery(_context, CourseDto.IncludeNavigations()) .FirstOrDefault(x => x.Id == courseDto.Id); if (entity != null) { entity = CourseDto.ToCourseFunc(entity, courseDto); ToEntity(ref entity, courseDto); //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); }