Skip to content

Commit

Permalink
feat(project): add pagination and sorting to project list
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Jan 23, 2025
1 parent 71b995a commit 7e4b9b6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/discord/command/project/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (e *ProjectCmd) Execute(message *model.DiscordMessage) error {
return e.GetProjectCommissionModels(message)
case "help", "h":
return e.Help(message)
case "list", "l":
case "list", "ls":
return e.GetProjectList(message)
default:
return e.Help(message)
Expand Down
21 changes: 19 additions & 2 deletions pkg/discord/command/project/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package project

import (
"sort"
"strconv"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

Expand All @@ -17,16 +20,30 @@ func (e *ProjectCmd) GetProjectCommissionModels(message *model.DiscordMessage) e

func (e *ProjectCmd) GetProjectList(message *model.DiscordMessage) error {
status := "active"
page := 1
if len(message.ContentArgs) >= 3 {
status = message.ContentArgs[2]
}
if len(message.ContentArgs) >= 4 {
p := message.ContentArgs[3]
if p != "" {
var err error
page, err = strconv.Atoi(p)
if err != nil {
return e.view.Error().Raise(message, "Invalid page number.")
}
}
}
// using curl to get project pnls
projs, err := e.svc.Project().GetProjectList(status)
if err != nil {
return e.view.Error().Raise(message, "Failed to get project PnLs.")
return e.view.Error().Raise(message, "Failed to get project list.")
}
sort.Slice(projs, func(i, j int) bool {
return projs[i].Name < projs[j].Name
})

return e.view.Project().List(message, projs)
return e.view.Project().List(message, projs, page)
}

func (e *ProjectCmd) GetProjectPnL(message *model.DiscordMessage) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/discord/view/project/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ type ProjectViewer interface {
MissingArgsMilestones(original *model.DiscordMessage) error
CommissionModels(original *model.DiscordMessage, commissionModel []model.ProjectCommissionModel) error
PnL(original *model.DiscordMessage, pnls []model.ProjectPnL) error
List(original *model.DiscordMessage, projects []model.Project) error
List(original *model.DiscordMessage, projects []model.Project, page int) error
Help(original *model.DiscordMessage) error
}
45 changes: 38 additions & 7 deletions pkg/discord/view/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (e *Project) Help(message *model.DiscordMessage) error {
"**Project Commands**",
"",
"`?project` - Show project commands help",
"`?project list` - List all projects",
"`?project list [status] [page]` - List projects (default: active status, page 1). Status: active, paused, closed",
"`?project pnl` - Show project P&L information",
"`?project commission` - Show project commission models",
"",
"**Aliases**",
"`?project l` - Alias for list",
"`?project ls [status] [page]` - Alias for list (same arguments as list)",
"`?project com` - Alias for commission",
"`?project h` - Alias for help",
}
Expand All @@ -43,21 +43,52 @@ func (e *Project) Help(message *model.DiscordMessage) error {
return base.SendEmbededMessage(e.ses, message, msg)
}

func (e *Project) List(original *model.DiscordMessage, projects []model.Project) error {
func (e *Project) List(original *model.DiscordMessage, projects []model.Project, page int) error {
var content string
pages := len(projects) / 10
if len(projects)%10 != 0 {
pages++
}

if len(projects) > 10 {
projects = projects[(page-1)*10 : page*10]
}

for i := range projects {
if projects[i].Name == "" {
continue
}

p := projects[i]
content += fmt.Sprintf("`Name. ` **%s**\n", p.Name)
content += fmt.Sprintf("`Code. ` **%s**\n", p.Code)
content += fmt.Sprintf("- %s", p.Name)

if p.ArtifactLink != "" {
content += fmt.Sprintf("`Artifacts. ` [Link](%s)\n", p.ArtifactLink)
content += fmt.Sprintf(" | [file](%s)", p.ArtifactLink)
} else {
content += " | file*"
}

if p.SourceLink != "" {
content += fmt.Sprintf(" | [src](%s)", p.SourceLink)
} else {
content += " | src*"
}

if p.DocLink != "" {
content += fmt.Sprintf(" | [doc](%s)", p.DocLink)
} else {
content += " | doc*"
}

content += "\n"
}

msg := &discordgo.MessageEmbed{
Title: fmt.Sprintf("<:pepe_ping:1028964391690965012> Projects <:pepe_ping:1028964391690965012>"),
Title: "Projects",
Description: content,
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("Page %d of %d", page, pages),
},
}

base.SendEmbededMessage(e.ses, original, msg)
Expand Down
20 changes: 14 additions & 6 deletions pkg/model/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package model

import "github.com/shopspring/decimal"
import (
"time"

"github.com/shopspring/decimal"
)

type ProjectCommissionModel struct {
Beneficiary BasicEmployeeInfo `json:"beneficiary"`
Expand All @@ -15,11 +19,15 @@ type ProjectCommissionModelsResponse struct {
}

type Project struct {
ID string `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Status string `json:"status"`
ArtifactLink string `json:"artifactLink"`
ID string `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Status string `json:"status"`
ArtifactLink string `json:"artifactLink"`
SourceLink string `json:"sourceLink"`
DocLink string `json:"docLink"`
StartDate *time.Time `json:"startDate"`
EndDate *time.Time `json:"endDate"`
}

type ProjectListResponse struct {
Expand Down

0 comments on commit 7e4b9b6

Please sign in to comment.