Skip to content

Commit 205720f

Browse files
committed
get cards - endpoint pagination
1 parent 00be62c commit 205720f

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

App/AL/Controller/Card/CardController.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Micron.DL.Module.Controller;
88
using Micron.DL.Module.Http;
99
using Micron.DL.Module.Validator;
10+
using Newtonsoft.Json.Linq;
1011

1112
namespace App.AL.Controller.Card {
1213
public sealed class CardController : BaseController {
@@ -33,8 +34,21 @@ public CardController() {
3334
if (errors.Count > 0) return HttpResponse.Errors(errors);
3435

3536
var column = BoardColumnRepository.FindByGuid(GetRequestStr("column_guid"));
36-
37-
return HttpResponse.Item("cards", new CardTransformer().Many(column.Cards()));
37+
38+
var page = GetRequestInt("page");
39+
page = page > 0 ? page : 1;
40+
41+
var pageSize = 25;
42+
43+
return HttpResponse.Data(new JObject() {
44+
["data"] = new JObject() {
45+
["cards"] = new CardTransformer().Many(column.Cards(page, pageSize))
46+
},
47+
["meta"] = new JObject() {
48+
["pages_count"] = (column.CardsCount() / pageSize)+1,
49+
["current_page"] = page
50+
}
51+
});
3852
});
3953
}
4054
}

App/DL/Model/BoardColumn/BoardColumn.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ public BoardColumn Save() {
5656

5757
public BoardColumn Refresh() => Find(id);
5858

59-
public static int Count() => ExecuteScalarInt("SELECT count(*) FROM board_columns WHERE id = @id");
59+
public static int Count() => ExecuteScalarInt("SELECT COUNT(*) FROM board_columns WHERE id = @id");
6060

6161
public void Delete() => ExecuteScalarInt("DELETE FROM board_columns WHERE id = @id", new {id});
6262

6363
public BoardModel Board() => BoardModel.Find(board_id);
6464

65-
public Card.Card[] Cards(int limit = 25)
65+
public Card.Card[] Cards(int page = 1, int limit = 25)
6666
=> Connection().Query<Card.Card>(
67-
@"SELECT * FROM cards WHERE column_id = @column_id LIMIT @limit",
68-
new {column_id = id, limit}
67+
@"SELECT * FROM cards WHERE column_id = @column_id OFFSET @offset LIMIT @limit",
68+
new {column_id = id, offset = ((page - 1) * limit), limit}
6969
).ToArray();
70+
71+
public int CardsCount()
72+
=> ExecuteScalarInt("SELECT COUNT(*) FROM cards WHERE column_id = @id", new { id });
7073
}
7174
}

0 commit comments

Comments
 (0)