Beispiel #1
0
        public async Task <Room?> Find([DisallowNull] RoomQueryFilter query)
        {
            if (Logger.IsEnabled(LogLevel.Trace))
            {
                Logger.LogTrace($"find({JsonConvert.SerializeObject(query)})");
            }
            var roomList = await FindAll(query);

            if (roomList.Count > 1)
            {
                Logger.LogWarning($"find() got more than one({roomList.Count}) result");
            }
            for (var i = 0; i < roomList.Count; i++)
            {
                var room = roomList[i];
                if (await Puppet.RoomValidate(room.Id))
                {
                    if (Logger.IsEnabled(LogLevel.Trace))
                    {
                        Logger.LogTrace($"find() confirm room[#{i}] with id={room.Id} is valid result, return it.");
                    }
                    return(room);
                }
                else
                {
                    if (Logger.IsEnabled(LogLevel.Trace))
                    {
                        Logger.LogTrace($"find() confirm room[#{i}] with id={room.Id} is INVALID result, try next");
                    }
                }
            }
            Logger.LogWarning($"find() got {roomList.Count} rooms but no one is valid.");
            return(null);
        }
        public ValidationData ValidateGetRooms(
            RoomQueryFilter filter,
            RoomQuerySort sort,
            RoomQueryProjection projection,
            RoomQueryPaging paging,
            RoomQueryOptions options)
        {
            var validationData = new ValidationData();

            if (filter.empty)
            {
                if (filter.date == null ||
                    filter.from_time == null ||
                    filter.to_time == null ||
                    filter.num_of_people == null || filter.room_type == null)
                {
                    validationData.Fail(mess: "Invalid input data", AppResultCode.FailValidation);
                }
                if (filter.from_time >= filter.to_time)
                {
                    validationData.Fail(mess: "Time range is not valid", AppResultCode.FailValidation);
                }
                if (validationData.IsValid)
                {
                    DateTime currentTime = DateTime.UtcNow;
                    var      bookedTime  = filter.date?.AddMinutes(filter.from_time.Value.TotalMinutes);
                    if (currentTime >= bookedTime)
                    {
                        validationData.Fail(mess: "Booked time must be greater than current", AppResultCode.FailValidation);
                    }
                }
            }
            return(validationData);
        }
Beispiel #3
0
        public async Task <IActionResult> Get([FromQuery][QueryObject] RoomQueryFilter filter,
                                              [FromQuery] RoomQuerySort sort,
                                              [FromQuery] RoomQueryProjection projection,
                                              [FromQuery] RoomQueryPaging paging,
                                              [FromQuery] RoomQueryOptions options)
        {
            var validationData = _service.ValidateGetRooms(
                filter, sort, projection, paging, options);

            if (!validationData.IsValid)
            {
                return(BadRequest(AppResult.FailValidation(data: validationData)));
            }
            var result = await _service.QueryRoomDynamic(UserId,
                                                         projection, validationData.TempData, filter, sort, paging, options);

            if (options.single_only && result == null)
            {
                return(NotFound(AppResult.NotFound()));
            }
            return(Ok(AppResult.Success(data: result)));
        }
        public async Task <QueryResult <IDictionary <string, object> > > QueryRoomDynamic(
            string userId,
            RoomQueryProjection projection,
            IDictionary <string, object> tempData = null,
            RoomQueryFilter filter   = null,
            RoomQuerySort sort       = null,
            RoomQueryPaging paging   = null,
            RoomQueryOptions options = null)
        {
            var query = Rooms.AsNoTracking();

            if (filter != null)
            {
                query = await query.FilterAsync(filter, userId, tempData, context.Booking);
            }
            int?totalCount = null; Task <int> countTask = null;
            var countQuery = query;

            query = query.Project(projection);
            if (options != null && !options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.Sort(sort);
                }
                if (paging != null && (!options.load_all || !RoomQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SelectPage(paging.page, paging.limit);
                }
                #endregion
                #region Count query
                if (options.count_total)
                {
                    countTask = countQuery.CountAsync();
                }
                #endregion
            }
            if (options != null && options.count_total)
            {
                totalCount = await countTask;
            }
            var queryResult = await query.ToListAsync();

            if (options != null && options.single_only)
            {
                var single = queryResult.FirstOrDefault();
                if (single == null)
                {
                    return(null);
                }
                var singleResult = GetRoomDynamic(single, projection, options);
                return(new QueryResult <IDictionary <string, object> >()
                {
                    SingleResult = singleResult
                });
            }
            var results = GetRoomDynamic(queryResult, projection, options);
            return(new QueryResult <IDictionary <string, object> >()
            {
                Results = results,
                TotalCount = totalCount
            });
        }
        public static async Task <IQueryable <Room> > FilterAsync(this IQueryable <Room> query, RoomQueryFilter model,
                                                                  string userId,
                                                                  IDictionary <string, object> tempData, IQueryable <Booking> bookingQuery)
        {
            var available = model.is_available ?? BoolOptions.B;

            if (available != BoolOptions.B)
            {
                query = query.Available(available == BoolOptions.T);
            }
            var archived = model.archived ?? BoolOptions.F;

            if (archived != BoolOptions.B)
            {
                query = query.Archived(!(archived == BoolOptions.F));
            }
            if (model.code != null)
            {
                query = query.Code(model.code);
            }
            if (model.name_contains != null)
            {
                query = query.NameContains(model.name_contains);
            }
            if (model.room_type != null)
            {
                query = query.OfRoomType(model.room_type);
            }
            if (model.num_of_people != null)
            {
                query = query.CanHandle(model.num_of_people.Value);
            }
            if (model.search != null)
            {
                query = query.Search(model.search);
            }
            if (model.empty)
            {
                //required fields
                var now = DateTime.UtcNow;
                var notAvailableRoomCode = bookingQuery.ActiveStatus()
                                           .Overlapped(model.date.Value, model.from_time.Value, model.to_time.Value)
                                           .Select(b => b.RoomCode).ToList();
                var dateLocal           = model.date.Value.ToDefaultTimeZone();
                var fapBookedRoomInDate = await Global.FapClient.GetFAPScheduleInDateRangeAsync(
                    dateLocal, dateLocal);

                var fapNotAvailableRoom = fapBookedRoomInDate.AsQueryable()
                                          .ActiveStatus()
                                          .Overlapped(model.date.Value, model.from_time.Value, model.to_time.Value)
                                          .Select(b => b.RoomCode).ToList();
                notAvailableRoomCode = notAvailableRoomCode.Union(fapNotAvailableRoom).ToList();
                query = query.ExceptCodes(notAvailableRoomCode)
                        .InActiveTime(model.from_time.Value, model.to_time.Value)
                        //not hanging by someone else
                        .NotHangingExcept(now, userId);

                //Already filter below
                //.Where(o => o.RoomTypeCode == model.room_type)
                //.Where(o => o.PeopleCapacity >= model.num_of_people);
            }
            return(query);
        }