Beispiel #1
0
        public async Task <IActionResult> Get([FromQuery] AppUserQueryFilter filter,
                                              [FromQuery] AppUserQuerySort sort,
                                              [FromQuery] AppUserQueryProjection projection,
                                              [FromQuery] AppUserQueryPaging paging,
                                              [FromQuery] AppUserQueryOptions options)
        {
            var validationResult = _service.ValidateGetAppUsers(
                User, filter, sort, projection, paging, options);

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

            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)));
        }
Beispiel #2
0
        public async Task <QueryResult <IDictionary <string, object> > > QueryAppUserDynamic(
            AppUserQueryProjection projection,
            AppUserQueryOptions options,
            AppUserQueryFilter filter = null,
            AppUserQuerySort sort     = null,
            AppUserQueryPaging paging = null)
        {
            var query = Users;

            #region General
            if (filter != null)
            {
                query = query.Filter(filter);
            }
            query = query.Project(projection);
            int?totalCount = null;
            if (options.count_total)
            {
                totalCount = query.Count();
            }
            #endregion
            if (!options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.Sort(sort);
                }
                if (paging != null && (!options.load_all || !AppUserQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SelectPage(paging.page, paging.limit);
                }
                #endregion
            }

            if (options.single_only)
            {
                var single = query.SingleOrDefault();
                if (single == null)
                {
                    return(null);
                }
                var singleResult = GetAppUserDynamic(single, projection, options);
                return(new QueryResult <IDictionary <string, object> >()
                {
                    Single = singleResult
                });
            }
            var entities = query.ToList();
            var list     = GetAppUserDynamic(entities, projection, options);
            var result   = new QueryResult <IDictionary <string, object> >();
            result.List = list;
            if (options.count_total)
            {
                result.Count = totalCount;
            }
            return(result);
        }
Beispiel #3
0
 public ValidationData ValidateGetAppUsers(
     ClaimsPrincipal principal,
     AppUserQueryFilter filter,
     AppUserQuerySort sort,
     AppUserQueryProjection projection,
     AppUserQueryPaging paging,
     AppUserQueryOptions options)
 {
     return(new ValidationData());
 }
Beispiel #4
0
 public ValidationResult ValidateGetAppUsers(
     ClaimsPrincipal principal,
     AppUserQueryFilter filter,
     AppUserQuerySort sort,
     AppUserQueryProjection projection,
     AppUserQueryPaging paging,
     AppUserQueryOptions options)
 {
     return(ValidationResult.Pass());
 }
Beispiel #5
0
        public List <IDictionary <string, object> > GetAppUserDynamic(
            IEnumerable <AppUser> rows, AppUserQueryProjection projection,
            AppUserQueryOptions options)
        {
            var list = new List <IDictionary <string, object> >();

            foreach (var o in rows)
            {
                var obj = GetAppUserDynamic(o, projection, options);
                list.Add(obj);
            }
            return(list);
        }
Beispiel #6
0
 public static IQueryable <AppUser> Project(
     this IQueryable <AppUser> query, AppUserQueryProjection projection)
 {
     foreach (var f in projection.GetFieldsArr())
     {
         if (AppUserQueryProjection.MAPS.ContainsKey(f))
         {
             foreach (var prop in AppUserQueryProjection.MAPS[f])
             {
                 query = query.Include(prop);
             }
         }
     }
     return(query);
 }
Beispiel #7
0
        public static DynamicSql SqlJoin(
            this DynamicSql query, AppUserQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var joins = model.GetFieldsArr()
                        .Where(f => AppUserQueryProjection.Joins.ContainsKey(f))
                        .Select(f => AppUserQueryProjection.Joins[f]);

            if (joins.Any())
            {
                var joinClause = string.Join('\n', joins);
                query.DynamicForm = query.DynamicForm
                                    .Replace(DynamicSql.JOIN, joinClause);
            }
            return(query);
        }
Beispiel #8
0
        public IDictionary <string, object> GetAppUserDynamic(
            AppUser row, AppUserQueryProjection projection,
            AppUserQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case AppUserQueryProjection.INFO:
                {
                    var entity = row;
                    obj["id"]        = entity.Id;
                    obj["username"]  = entity.UserName;
                    obj["full_name"] = entity.FullName;
                }
                break;

                case AppUserQueryProjection.SELECT:
                {
                    var entity = row;
                    obj["id"]        = entity.Id;
                    obj["username"]  = entity.UserName;
                    obj["full_name"] = entity.FullName;
                }
                break;

                case AppUserQueryProjection.ROLE:
                {
                    var entity = row.UserRoles.FirstOrDefault()?.Role;
                    if (entity != null)
                    {
                        obj["role"] = new
                        {
                            id   = entity.Id,
                            name = entity.Name
                        }
                    }
                    ;
                }
                break;
                }
            }
            return(obj);
        }
Beispiel #9
0
        public static DynamicSql SqlExtras(
            this DynamicSql query, AppUserQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var extras = model.GetFieldsArr()
                         .Where(f => AppUserQueryProjection.Extras.ContainsKey(f))
                         .Select(f => AppUserQueryProjection.Extras[f]);

            if (extras.Any())
            {
                var extraSqls     = string.Join(';', extras);
                var originalQuery = query.PreparedViewForm;
                query.DynamicForm += ";\n" + extraSqls;
                query.DynamicForm  = query.DynamicForm
                                     .Replace(AppUserQueryPlaceholder.USER_SUB_QUERY, originalQuery);
            }
            return(query);
        }
Beispiel #10
0
        public QueryResult <IDictionary <string, object> > GetAppUserDynamic(
            IEnumerable <AppUserQueryRow> rows, AppUserQueryProjection projection,
            AppUserQueryOptions options, int?totalCount = null, string currentAccId = null)
        {
            var list = new List <IDictionary <string, object> >();

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

            resp.Results = list;
            if (options.count_total)
            {
                resp.TotalCount = totalCount;
            }
            return(resp);
        }
Beispiel #11
0
        public static DynamicSql SqlProjectFields(
            this DynamicSql query, AppUserQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var finalFields = model.GetFieldsArr()
                              .Where(f => AppUserQueryProjection.Projections.ContainsKey(f))
                              .Select(f => AppUserQueryProjection.Projections[f]);

            if (finalFields.Any())
            {
                var projectionClause = string.Join(',', finalFields);
                query.DynamicForm = query.DynamicForm
                                    .Replace(DynamicSql.PROJECTION, projectionClause);
            }
            var finalResults = model.GetFieldsArr()
                               .Where(f => AppUserQueryProjection.Results.ContainsKey(f))
                               .Select(f => AppUserQueryProjection.Results[f]);

            query.MultiResults.AddRange(finalResults);
            return(query);
        }
        public async Task <IActionResult> Get([FromQuery][QueryObject] AppUserQueryFilter filter,
                                              [FromQuery] AppUserQuerySort sort,
                                              [FromQuery] AppUserQueryProjection projection,
                                              [FromQuery] AppUserQueryPaging paging,
                                              [FromQuery] AppUserQueryOptions options)
        {
            var validationData = _service.ValidateGetAppUsers(
                User, filter, sort, projection, paging, options);

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

            if (options.single_only && result == null)
            {
                return(NotFound(AppResult.NotFound()));
            }
            return(Ok(AppResult.Success(result)));
        }
Beispiel #13
0
        public IDictionary <string, object> GetAppUserDynamic(
            AppUser row, AppUserQueryProjection projection,
            AppUserQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case AppUserQueryProjection.INFO:
                {
                    var entity = row;
                    obj["id"]        = entity.Id;
                    obj["username"]  = entity.UserName;
                    obj["full_name"] = entity.FullName;
                }
                break;
                }
            }
            return(obj);
        }
Beispiel #14
0
        public IDictionary <string, object> GetAppUserDynamic(
            AppUserQueryRow row, AppUserQueryProjection projection,
            AppUserQueryOptions options, string currentAccId = null)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case AppUserQueryProjection.INFO:
                {
                    var entity = row.AppUser;
                    obj["username"]          = entity.UserName;
                    obj["full_name"]         = entity.FullName;
                    obj["phone_number"]      = entity.PhoneNumber;
                    obj["current_logged_in"] = entity.Id == currentAccId;
                }
                break;

                case AppUserQueryProjection.ROLES:
                {
                    var entities = row.AppUser.UserRoles?
                                   .Select(o => o.Role);
                    obj["roles"] = entities?.Select(o => new
                        {
                            name         = o.Name,
                            display_name = o.DisplayName,
                            role_type    = o.RoleType
                        }).ToList();
                }
                break;
                }
            }
            return(obj);
        }
Beispiel #15
0
        public async Task <QueryResult <AppUserQueryRow> > QueryAppUser(
            AppUserQueryFilter filter         = null,
            AppUserQuerySort sort             = null,
            AppUserQueryProjection projection = null,
            AppUserQueryPaging paging         = null,
            AppUserQueryOptions options       = null)
        {
            var conn     = context.Database.GetDbConnection();
            var openConn = conn.OpenAsync();
            var query    = AppUserQuery.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 || !AppUserQueryOptions.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 => AppUserQueryProjection.Extras.ContainsKey(f));
                    IEnumerable <AppUserRoleQueryRow> userRoles = null;
                    foreach (var key in extraKeys)
                    {
                        switch (key)
                        {
                        case AppUserQueryProjection.ROLES:
                            userRoles = GetAppUserRoleQueryResult(multipleResult);
                            break;
                        }
                    }
                    ProcessExtras(queryResult, userRoles);
                }
                if (options != null && options.single_only)
                {
                    var single = queryResult.FirstOrDefault();
                    if (single == null)
                    {
                        return(null);
                    }
                    return(new QueryResult <AppUserQueryRow>
                    {
                        SingleResult = single
                    });
                }
                if (options != null && options.count_total)
                {
                    totalCount = await countTask;
                }
                return(new QueryResult <AppUserQueryRow>
                {
                    Results = queryResult,
                    TotalCount = totalCount
                });
            }
        }