diff --git a/panel/handlers/advertiserhandler.go b/panel/handlers/advertiserhandler.go index cabcb05..0308217 100644 --- a/panel/handlers/advertiserhandler.go +++ b/panel/handlers/advertiserhandler.go @@ -16,16 +16,17 @@ import ( ) const ( - INTBASE = 10 - INTBIT32 = 32 - INTBIT64 = 64 + INTBASE = 10 + INTBIT32 = 32 + INTBIT64 = 64 + itemsPerPage = 4 ) func AdvertiserPanel(c *gin.Context) { advertiserUserName := c.Param("username") var advertiser models.Advertiser - result := database.DB.Preload("Ads").Preload("Transactions").Where("username = ?", advertiserUserName).First(&advertiser) + result := database.DB.Preload("Ads").Where("username = ?", advertiserUserName).First(&advertiser) if result.Error != nil { if result.Error == gorm.ErrRecordNotFound { fmt.Printf("No advertiser found with username %s, creating a new one.\n", advertiserUserName) @@ -46,11 +47,30 @@ func AdvertiserPanel(c *gin.Context) { } } + // Pagination logic + pageStr := c.DefaultQuery("page", "1") + page, err := strconv.Atoi(pageStr) + if err != nil || page < 1 { + page = 1 + } + + var totalTransactions int64 + database.DB.Model(&models.Transaction{}).Where("customer_id = ? AND customer_type = ?", advertiser.ID, models.Customer_Advertiser).Count(&totalTransactions) + totalPages := int((totalTransactions + itemsPerPage - 1) / itemsPerPage) + + var transactions []models.Transaction + database.DB.Model(&models.Transaction{}).Where("customer_id = ? AND customer_type = ?", advertiser.ID, models.Customer_Advertiser). + Offset((page - 1) * itemsPerPage). + Limit(itemsPerPage). + Find(&transactions) + c.HTML(http.StatusOK, "advertiser_panel.html", gin.H{ "Balance": advertiser.Balance, "Ads": advertiser.Ads, "Username": advertiserUserName, - "Transactions": advertiser.Transactions, + "Transactions": transactions, + "TotalPages": totalPages, + "CurrentPage": page + 1, }) } diff --git a/panel/handlers/advertiserhandler_test.go b/panel/handlers/advertiserhandler_test.go index 155bde3..020a06c 100644 --- a/panel/handlers/advertiserhandler_test.go +++ b/panel/handlers/advertiserhandler_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "html/template" "io" "mime/multipart" "net/http" @@ -48,6 +49,19 @@ func TestSequentialTests(t *testing.T) { func TestAdvertiserPanel(t *testing.T) { // Initialize a new Gin router r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.LoadHTMLGlob("../templates/*") r.GET("/advertiser/:username/panel", AdvertiserPanel) @@ -73,6 +87,19 @@ func TestAdvertiserPanel(t *testing.T) { // TestAddFunds tests the AddFunds handler func testAddFunds(t *testing.T) { r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.POST("/advertiser/:username/add-funds", AddFunds) // Create a test advertiser @@ -108,6 +135,19 @@ func testAddFunds(t *testing.T) { // TestCreateAd tests the CreateAd handler func testCreateAd(t *testing.T) { r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.POST("/advertiser/:username/create-ad", CreateAd) // Create a test advertiser @@ -158,6 +198,19 @@ func testCreateAd(t *testing.T) { // TestToggleAd tests the ToggleAd handler func TestToggleAd(t *testing.T) { r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.POST("/advertiser/:username/toggle-ad", ToggleAd) // Create a test advertiser and ad @@ -203,6 +256,19 @@ func TestToggleAd(t *testing.T) { // TestAdReport tests the AdReport handler func TestAdReport(t *testing.T) { r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.LoadHTMLGlob("../templates/*") r.GET("/advertiser/ad-report/:id", AdReport) @@ -245,6 +311,19 @@ func TestAdReport(t *testing.T) { // TestHandleEditAd tests the HandleEditAd handler func testHandleEditAd(t *testing.T) { r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.POST("/advertiser/:username/edit-ad", HandleEditAd) // Create a test advertiser and ad diff --git a/panel/handlers/publisherhandler_test.go b/panel/handlers/publisherhandler_test.go index 36d3982..7a41623 100644 --- a/panel/handlers/publisherhandler_test.go +++ b/panel/handlers/publisherhandler_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "html/template" "net/http" "net/http/httptest" "net/url" @@ -22,7 +23,21 @@ import ( func testPublisherPanel(t *testing.T) { os.Setenv("YEKTANET_PORTION", "20") r := gin.Default() + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.LoadHTMLGlob("../templates/*") + r.GET("/publisher/:username/panel", PublisherPanel) // Test Case 1: Existing Publisher diff --git a/panel/main.go b/panel/main.go index 5466414..dff5af9 100644 --- a/panel/main.go +++ b/panel/main.go @@ -1,6 +1,7 @@ package main import ( + "html/template" "log" "os" "strconv" @@ -36,6 +37,19 @@ func main() { AllowAllOrigins: true, })) r.Use(grafana.PrometheusMiddleware()) + r.SetFuncMap(template.FuncMap{ + "add": func(a, b int) int { + return a + b + }, + "until": func(count int) []int { + var i int + var items []int + for i = 0; i < count; i++ { + items = append(items, i) + } + return items + }, + }) r.GET("/metrics", gin.WrapH(promhttp.Handler())) r.LoadHTMLGlob("templates/*") diff --git a/panel/static/css/styles-advertiser.css b/panel/static/css/styles-advertiser.css index 510f5ff..8449391 100644 --- a/panel/static/css/styles-advertiser.css +++ b/panel/static/css/styles-advertiser.css @@ -88,4 +88,39 @@ button:hover { .transactions-table td { vertical-align: middle; -} \ No newline at end of file +} + +.pagination { + margin: 20px 0; + text-align: center; +} + +.pagination ul { + list-style-type: none; + padding: 0; + display: inline-block; +} + +.pagination li { + display: inline; + margin-right: 5px; +} + +.pagination li a { + text-decoration: none; + padding: 8px 12px; + background-color: #f4f4f4; + color: #333; + border: 1px solid #ddd; + border-radius: 3px; +} + +.pagination li.active a { + background-color: #333; + color: #fff; + border-color: #333; +} + +.pagination li a:hover { + background-color: #ddd; +} diff --git a/panel/templates/advertiser_panel.html b/panel/templates/advertiser_panel.html index 4a4008c..51d77fc 100644 --- a/panel/templates/advertiser_panel.html +++ b/panel/templates/advertiser_panel.html @@ -54,6 +54,18 @@

Transactions

{{ end }} + +
@@ -96,4 +108,4 @@

Edit Ad

- \ No newline at end of file +