private void ApplyOrdering(ref Sql <ISqlContext> sql, Ordering ordering)
        {
            if (sql == null)
            {
                throw new ArgumentNullException(nameof(sql));
            }
            if (ordering == null)
            {
                throw new ArgumentNullException(nameof(ordering));
            }

            // TODO: although the default ordering string works for name, it wont work for others without a table or an alias of some sort
            // As more things are attempted to be sorted we'll prob have to add more expressions here
            string orderBy;

            switch (ordering.OrderBy?.ToUpperInvariant())
            {
            case "PATH":
                orderBy = SqlSyntax.GetQuotedColumn(NodeDto.TableName, "path");
                break;

            default:
                orderBy = ordering.OrderBy ?? string.Empty;
                break;
            }

            if (ordering.Direction == Direction.Ascending)
            {
                sql.OrderBy(orderBy);
            }
            else
            {
                sql.OrderByDescending(orderBy);
            }
        }
Пример #2
0
    public IEnumerable <IRelation> GetPagedRelationsByQuery(IQuery <IRelation>?query, long pageIndex, int pageSize, out long totalRecords, Ordering?ordering)
    {
        Sql <ISqlContext> sql = GetBaseQuery(false);

        if (ordering == null || ordering.IsEmpty)
        {
            ordering = Ordering.By(SqlSyntax.GetQuotedColumn(Constants.DatabaseSchema.Tables.Relation, "id"));
        }

        var translator = new SqlTranslator <IRelation>(sql, query);

        sql = translator.Translate();

        // apply ordering
        ApplyOrdering(ref sql, ordering);

        var pageIndexToFetch    = pageIndex + 1;
        Page <RelationDto>?page = Database.Page <RelationDto>(pageIndexToFetch, pageSize, sql);
        List <RelationDto>?dtos = page.Items;

        totalRecords = page.TotalItems;

        var relTypes = _relationTypeRepository.GetMany(dtos.Select(x => x.RelationType).Distinct().ToArray())?
                       .ToDictionary(x => x.Id, x => x);

        var result = dtos.Select(r =>
        {
            if (relTypes is null || !relTypes.TryGetValue(r.RelationType, out IRelationType? relType))
            {
                throw new InvalidOperationException(string.Format("RelationType with Id: {0} doesn't exist", r.RelationType));
            }

            return(DtoToEntity(r, relType));
        }).WhereNotNull().ToList();

        return(result);
    }