aboutsummaryrefslogtreecommitdiff
path: root/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/OrderByClause.cs
blob: 734d04476b18b79d66d6764f1979f19b0c052d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace CrupestApi.Commons.Crud;

public class OrderByItem
{
    public OrderByItem(string columnName, bool isAscending)
    {
        ColumnName = columnName;
        IsAscending = isAscending;
    }

    public string ColumnName { get; }
    public bool IsAscending { get; }

    public string GenerateSql()
    {
        return $"{ColumnName} {(IsAscending ? "ASC" : "DESC")}";
    }
}

public interface IOrderByClause : IClause
{
    List<OrderByItem> Items { get; }
    // Contains "ORDER BY" keyword!
    string GenerateSql(string? dbProviderId = null);
}

public class OrderByClause : IOrderByClause
{
    public List<OrderByItem> Items { get; } = new List<OrderByItem>();

    public OrderByClause(params OrderByItem[] items)
    {
        Items.AddRange(items);
    }

    public static OrderByClause Create(params OrderByItem[] items)
    {
        return new OrderByClause(items);
    }

    public List<string> GetRelatedColumns()
    {
        return Items.Select(x => x.ColumnName).ToList();
    }

    public string GenerateSql(string? dbProviderId = null)
    {
        return "ORDER BY " + string.Join(", ", Items.Select(i => i.GenerateSql()));
    }
}