Example #1
0
        // -DtoQuery

        public async Task <StudentDto> CreateStudentAsync(StudentDto studentDto, string username)
        {
            OnCreate(studentDto, username);

            var entity = StudentDto.AsStudentFunc(studentDto);

            ToEntity(ref entity, studentDto);
            //entity.InsertUser = entity.LastActivityUser = username;
            //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
            entity.AddTracker(username);

            _context.Students.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);

            // studentDto = StudentDto.AsStudentDtoFunc(entity);
            studentDto = await GetStudentDtoAsync(entity.Id, StudentDto.IncludeNavigations());

            return(studentDto);
        }
        public async Task <ActionResult <StudentDto> > GetStudent(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var student = await studentService.GetStudentDtoAsync(id, StudentDto.IncludeNavigations());

            if (student == null)
            {
                return(NotFound());
            }

            return(student);
        }
Example #3
0
        public async Task <bool> UpdateStudentAsync(StudentDto studentDto, string username /*, String[] includeNavigations, params Expression<Func<Student, bool>>[] filters*/)
        {
            OnUpdate(studentDto, username);

            // Get Student
            var entity = EntityQuery(_context, StudentDto.IncludeNavigations())
                         .FirstOrDefault(x => x.Id == studentDto.Id);

            if (entity != null)
            {
                entity = StudentDto.ToStudentFunc(entity, studentDto);

                ToEntity(ref entity, studentDto);
                //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 async Task <ActionResult <IEnumerable <StudentDto> > > GetStudents(string searchText   = null
                                                                                  , string matricNo   = null
                                                                                  , string lastname   = null
                                                                                  , string firstname  = null
                                                                                  , string middlename = null
                                                                                  /*, int pageNumber=1, int pageSize=7*/)
        {
            // var students = _context.Students.Select(StudentDto.AsStudentDto);
            List <Expression <Func <StudentDto, bool> > > filters = null;

            if (String.IsNullOrEmpty(searchText) &&
                (String.IsNullOrEmpty(matricNo)) &&
                (String.IsNullOrEmpty(lastname)) &&
                (String.IsNullOrEmpty(firstname)) &&
                (String.IsNullOrEmpty(middlename))

                )
            {
                // return null;
            }
            else
            {
                filters = new List <Expression <Func <StudentDto, bool> > >();

                if (!String.IsNullOrEmpty(searchText))
                {
                    if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0)
                    {
                        filters.Add(x => x.Id.ToString().Contains(searchText));
                    }
                }

                if (!String.IsNullOrEmpty(matricNo))
                {
                    filters.Add(x => x.MatricNo == matricNo);
                }

                if (!String.IsNullOrEmpty(lastname))
                {
                    filters.Add(x => x.Lastname == lastname);
                }

                if (!String.IsNullOrEmpty(firstname))
                {
                    filters.Add(x => x.Firstname == firstname);
                }

                if (!String.IsNullOrEmpty(middlename))
                {
                    filters.Add(x => x.Middlename == middlename);
                }
            }

            //sort
            //return students.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize);

            // OnSelectQuery(ref students);

            // return await students.ToListAsync();

            if (filters == null)
            {
                return(await studentService.GetStudentDtoesAsync(StudentDto.IncludeNavigations()));
            }
            else
            {
                return(await studentService.GetStudentDtoesAsync(StudentDto.IncludeNavigations(), filters.ToArray()));
            }
        }