Skip to content

Commit

Permalink
Merge pull request #79 from YellowBloomKnapsack/fix/transactions-pagi…
Browse files Browse the repository at this point in the history
…nator

Add paginator to advertiser transactions and fix tests
  • Loading branch information
aparsak authored Jul 30, 2024
2 parents 6c64499 + 9faa3ce commit 14c2d77
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 7 deletions.
30 changes: 25 additions & 5 deletions panel/handlers/advertiserhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
})
}

Expand Down
79 changes: 79 additions & 0 deletions panel/handlers/advertiserhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions panel/handlers/publisherhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions panel/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"html/template"
"log"
"os"
"strconv"
Expand Down Expand Up @@ -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/*")
Expand Down
37 changes: 36 additions & 1 deletion panel/static/css/styles-advertiser.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,39 @@ button:hover {

.transactions-table td {
vertical-align: middle;
}
}

.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;
}
14 changes: 13 additions & 1 deletion panel/templates/advertiser_panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ <h3>Transactions</h3>
{{ end }}
</tbody>
</table>

<div class="pagination">
{{ if gt .TotalPages 1 }}
<ul>
{{ range $i := until .TotalPages }}
<li class="{{ if eq (add $i 2) $.CurrentPage }}active{{ end }}">
<a href="?page={{ add $i 1 }}">{{ add $i 1 }}</a>
</li>
{{ end }}
</ul>
{{ end }}
</div>
</div>

<div class="ad-list">
Expand Down Expand Up @@ -96,4 +108,4 @@ <h4>Edit Ad</h4>
</script>
</body>

</html>
</html>

0 comments on commit 14c2d77

Please sign in to comment.