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 #2
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 static IQueryable <Room> Sort(this IQueryable <Room> query, RoomQuerySort model)
 {
     foreach (var s in model._sortsArr)
     {
         var asc       = s[0] == 'a';
         var fieldName = s.Remove(0, 1);
         switch (fieldName)
         {
         case RoomQuerySort.CODE:
         {
             if (asc)
             {
                 query = query.OrderBy(o => o.Code);
             }
             else
             {
                 query = query.OrderByDescending(o => o.Code);
             }
         }
         break;
         }
     }
     return(query);
 }
        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
            });
        }