public async Task <int> Count(ExamRoomFilter filter)
        {
            IQueryable <ExamRoomDAO> examRoomDAOs = examRegContext.ExamRoom.AsNoTracking();

            examRoomDAOs = DynamicFilter(examRoomDAOs, filter);
            return(await examRoomDAOs.CountAsync());
        }
        /*private async Task<bool> ValidateExist(ExamRoom examRoom)
         * {
         *  ExamRoomFilter filter = new ExamRoomFilter
         *  {
         *      Take = Int32.MaxValue,
         *      RoomNumber = new ShortFilter { Equal = examRoom.RoomNumber },
         *      AmphitheaterName = new StringFilter { Equal = examRoom.AmphitheaterName }
         *  };
         *
         *  int count = await UOW.ExamRoomRepository.Count(filter);
         *  if (count == 0)
         *  {
         *      examRoom.AddError(nameof(ExamRoomValidator), nameof(examRoom), ERROR.NotExisted);
         *      return false;
         *  }
         *  return true;
         * }*/
        public async Task <bool> ValidateId(ExamRoom examRoom)
        {
            ExamRoomFilter filter = new ExamRoomFilter
            {
                Id = new GuidFilter {
                    Equal = examRoom.Id
                }
            };

            int count = await UOW.ExamRoomRepository.Count(filter);

            if (count == 0)
            {
                examRoom.AddError(nameof(ExamRoomValidator), nameof(examRoom), ERROR.IdNotFound);
            }

            return(count == 1);
        }
        public async Task <List <ExamRoom> > List(ExamRoomFilter filter)
        {
            if (filter == null)
            {
                return(new List <ExamRoom>());
            }
            IQueryable <ExamRoomDAO> query = examRegContext.ExamRoom.AsNoTracking();

            query = DynamicFilter(query, filter);
            query = DynamicOrder(query, filter);

            List <ExamRoom> list = await query.Select(e => new ExamRoom()
            {
                Id               = e.Id,
                RoomNumber       = e.RoomNumber,
                ComputerNumber   = e.ComputerNumber,
                AmphitheaterName = e.AmphitheaterName,
                Code             = string.Format(e.AmphitheaterName + "_" + e.RoomNumber)
            }).ToListAsync();

            return(list);
        }
        private async Task <bool> ValidateNotExist(ExamRoom examRoom)
        {
            ExamRoomFilter filter = new ExamRoomFilter
            {
                Take       = Int32.MaxValue,
                RoomNumber = new ShortFilter {
                    Equal = examRoom.RoomNumber
                },
                AmphitheaterName = new StringFilter {
                    Equal = examRoom.AmphitheaterName
                }
            };

            int count = await UOW.ExamRoomRepository.Count(filter);

            if (count > 0)
            {
                examRoom.AddError(nameof(ExamRoomValidator), nameof(examRoom), ERROR.ExamRoomExisted);
                return(false);
            }
            return(true);
        }
        private IQueryable <ExamRoomDAO> DynamicOrder(IQueryable <ExamRoomDAO> query, ExamRoomFilter filter)
        {
            switch (filter.OrderType)
            {
            case OrderType.ASC:
                switch (filter.OrderBy)
                {
                case ExamRoomOrder.ComputerNumber:
                    query = query.OrderBy(q => q.ComputerNumber);
                    break;

                case ExamRoomOrder.AmphitheaterName:
                    query = query.OrderBy(q => q.AmphitheaterName);
                    break;

                case ExamRoomOrder.RoomNumber:
                    query = query.OrderBy(q => q.RoomNumber);
                    break;

                default:
                    query = query.OrderBy(q => q.CX);
                    break;
                }
                break;

            case OrderType.DESC:
                switch (filter.OrderBy)
                {
                case ExamRoomOrder.ComputerNumber:
                    query = query.OrderByDescending(q => q.ComputerNumber);
                    break;

                case ExamRoomOrder.AmphitheaterName:
                    query = query.OrderByDescending(q => q.AmphitheaterName);
                    break;

                case ExamRoomOrder.RoomNumber:
                    query = query.OrderByDescending(q => q.RoomNumber);
                    break;

                default:
                    query = query.OrderByDescending(q => q.CX);
                    break;
                }
                break;

            default:
                query = query.OrderBy(q => q.CX);
                break;
            }
            return(query.Skip(filter.Skip).Take(filter.Take));
        }
 private IQueryable <ExamRoomDAO> DynamicFilter(IQueryable <ExamRoomDAO> query, ExamRoomFilter filter)
 {
     if (filter == null)
     {
         return(query.Where(q => 1 == 0));
     }
     if (filter.Id != null)
     {
         query = query.Where(q => q.Id, filter.Id);
     }
     if (filter.AmphitheaterName != null)
     {
         query = query.Where(q => q.AmphitheaterName, filter.AmphitheaterName);
     }
     if (filter.ComputerNumber != null)
     {
         query = query.Where(q => q.ComputerNumber, filter.ComputerNumber);
     }
     if (filter.RoomNumber != null)
     {
         query = query.Where(q => q.RoomNumber, filter.RoomNumber);
     }
     if (filter.ExamDate != null)
     {
         query = query.Where(q => q.ExamRoomExamPeriods
                             .Select(e => e.ExamPeriod.ExamDate.Equals(filter.ExamDate))
                             .Contains(true));
         if (filter.ExceptStartHour != null && filter.ExceptFinishHour != null)
         {
             query = query.Where(q => !q.ExamRoomExamPeriods
                                 .Select(e =>
                                         (filter.ExceptStartHour <= e.ExamPeriod.StartHour && e.ExamPeriod.StartHour <= filter.ExceptFinishHour) ||
                                         (filter.ExceptStartHour <= e.ExamPeriod.FinishHour && e.ExamPeriod.FinishHour <= filter.ExceptFinishHour) ||
                                         (e.ExamPeriod.StartHour <= filter.ExceptStartHour && filter.ExceptFinishHour <= e.ExamPeriod.FinishHour))
                                 .Contains(true));
         }
     }
     if (filter.ExceptExamDate != null)
     {
         query = query.Where(q => !q.ExamRoomExamPeriods
                             .Select(e => e.ExamPeriod.ExamDate == filter.ExceptExamDate).Contains(true));
     }
     return(query);
 }
 public async Task <List <ExamRoom> > List(ExamRoomFilter filter)
 {
     return(await UOW.ExamRoomRepository.List(filter));
 }
 public async Task <int> Count(ExamRoomFilter filter)
 {
     return(await UOW.ExamRoomRepository.Count(filter));
 }