Пример #1
0
        public async Task <IActionResult> Get([FromQuery] ScheduleDetailQueryFilter filter,
                                              [FromQuery] ScheduleDetailQuerySort sort,
                                              [FromQuery] ScheduleDetailQueryProjection projection,
                                              [FromQuery] ScheduleDetailQueryPaging paging,
                                              [FromQuery] ScheduleDetailQueryOptions options)
        {
            var validationResult = _service.ValidateGetScheduleDetails(
                User, filter, sort, projection, paging, options);

            if (!validationResult.Valid)
            {
                return(BadRequest(validationResult.Result));
            }
            var result = await _service.QueryScheduleDetailDynamic(
                projection, options, filter, sort, paging);

            if (options.single_only)
            {
                if (result == null)
                {
                    return(NotFound(new AppResultBuilder().NotFound()));
                }
                return(Ok(new AppResultBuilder().Success(result.SingleResult)));
            }
            return(Ok(new AppResultBuilder().Success(result)));
        }
Пример #2
0
 public ValidationResult ValidateGetScheduleDetails(
     ClaimsPrincipal principal,
     ScheduleDetailQueryFilter filter,
     ScheduleDetailQuerySort sort,
     ScheduleDetailQueryProjection projection,
     ScheduleDetailQueryPaging paging,
     ScheduleDetailQueryOptions options)
 {
     return(ValidationResult.Pass());
 }
Пример #3
0
        public QueryResult <IDictionary <string, object> > GetScheduleDetailDynamic(
            IEnumerable <ScheduleDetailQueryRow> rows, ScheduleDetailQueryProjection projection,
            ScheduleDetailQueryOptions options, int?totalCount = null)
        {
            var list = new List <IDictionary <string, object> >();

            foreach (var o in rows)
            {
                var obj = GetScheduleDetailDynamic(o, projection, options);
                list.Add(obj);
            }
            var resp = new QueryResult <IDictionary <string, object> >();

            resp.Results = list;
            if (options.count_total)
            {
                resp.TotalCount = totalCount;
            }
            return(resp);
        }
Пример #4
0
        public IDictionary <string, object> GetScheduleDetailDynamic(
            ScheduleDetailQueryRow row, ScheduleDetailQueryProjection projection,
            ScheduleDetailQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case ScheduleDetailQueryProjection.INFO:
                {
                    var entity = row.ScheduleDetail;
                    obj["id"]   = entity.Id;
                    obj["name"] = entity.Name;
                    var fromTime = entity.FromTime?
                                   .ToTimeZone(options.time_zone, options.culture);
                    var fromTimeStr = fromTime?
                                      .ToString(options.date_format, options.culture);
                    var toTime = entity.ToTime?
                                 .ToTimeZone(options.time_zone, options.culture);
                    var toTimeStr = toTime?
                                    .ToString(options.date_format, options.culture);
                    obj["from_time"] = new
                    {
                        display = fromTimeStr,
                        iso     = $"{fromTime?.ToUniversalTime():s}Z"
                    };
                    obj["to_time"] = new
                    {
                        display = toTimeStr,
                        iso     = $"{toTime?.ToUniversalTime():s}Z"
                    };
                    obj["is_default"]  = entity.IsDefault;
                    obj["schedule_id"] = entity.ScheduleId;
                }
                break;

                case ScheduleDetailQueryProjection.CONFIGS:
                {
                    var entities = row.ScheduleDetail.ScheduleWeekConfigs;
                    obj["schedule_week_configs"] = entities?
                                                   .Select(c => new
                        {
                            to_time          = c.ToTime,
                            to_day_of_week   = c.ToDayOfWeek,
                            id               = c.Id,
                            from_time        = c.FromTime,
                            from_day_of_week = c.FromDayOfWeek,
                            all_day          = c.AllDay,
                            config_id        = c.ConfigId
                        }).ToList();
                }
                break;

                case ScheduleDetailQueryProjection.SCHEDULE:
                {
                    var entity = row.Schedule;
                    obj["schedule"] = new
                    {
                        id   = entity.Id,
                        name = entity.Name,
                        code = entity.Code
                    };
                }
                break;
                }
            }
            return(obj);
        }
Пример #5
0
        public async Task <QueryResult <ScheduleDetailQueryRow> > QueryScheduleDetail(
            ScheduleDetailQueryFilter filter         = null,
            ScheduleDetailQuerySort sort             = null,
            ScheduleDetailQueryProjection projection = null,
            ScheduleDetailQueryPaging paging         = null,
            ScheduleDetailQueryOptions options       = null)
        {
            var conn     = context.Database.GetDbConnection();
            var openConn = conn.OpenAsync();
            var query    = ScheduleDetailQuery.CreateDynamicSql();

            #region General
            if (filter != null)
            {
                query = query.SqlFilter(filter);
            }
            if (projection != null)
            {
                query = query.SqlJoin(projection);
            }
            DynamicSql countQuery = null; int?totalCount = null; Task <int> countTask = null;
            if (options != null && options.count_total)
            {
                countQuery = query.SqlCount("*");
            }
            if (projection != null)
            {
                query = query.SqlProjectFields(projection);
            }
            #endregion
            await openConn;
            if (options != null && !options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.SqlSort(sort);
                }
                if (paging != null && (!options.load_all || !ScheduleDetailQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SqlSelectPage(paging.page, paging.limit);
                }
                #endregion
                #region Count query
                if (options.count_total)
                {
                    countTask = conn.ExecuteScalarAsync <int>(
                        sql: countQuery.PreparedForm,
                        param: countQuery.DynamicParameters);
                }
                #endregion
            }
            if (projection != null)
            {
                query = query.SqlExtras(projection);
            }
            var multipleResult = await conn.QueryMultipleAsync(
                sql: query.PreparedForm,
                param : query.DynamicParameters);

            using (multipleResult)
            {
                var queryResult = multipleResult.Read(
                    types: query.GetTypesArr(),
                    map: (objs) => ProcessMultiResults(query, objs),
                    splitOn: string.Join(',', query.GetSplitOns()));
                if (projection != null)
                {
                    var extraKeys = projection.GetFieldsArr()
                                    .Where(f => ScheduleDetailQueryProjection.Extras.ContainsKey(f));
                    IEnumerable <ScheduleWeekConfig> configs = null;
                    foreach (var key in extraKeys)
                    {
                        switch (key)
                        {
                        case ScheduleDetailQueryProjection.CONFIGS:
                            configs = multipleResult.Read <ScheduleWeekConfig>().ToList();
                            break;
                        }
                    }
                    ProcessExtras(queryResult, configs);
                }
                if (options != null && options.single_only)
                {
                    var single = queryResult.FirstOrDefault();
                    if (single == null)
                    {
                        return(null);
                    }
                    return(new QueryResult <ScheduleDetailQueryRow>
                    {
                        SingleResult = single
                    });
                }
                if (options != null && options.count_total)
                {
                    totalCount = await countTask;
                }
                return(new QueryResult <ScheduleDetailQueryRow>
                {
                    Results = queryResult,
                    TotalCount = totalCount
                });
            }
        }