Skip to content

Commit

Permalink
feat!: add filter by status
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 8, 2022
1 parent 3877867 commit f81b383
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 35 deletions.
8 changes: 6 additions & 2 deletions app/info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package app

import "fmt"
import (
"fmt"

"github.com/ItsNotGoodName/smtpbridge/core"
)

type Info struct {
AttachmentSize int64
Expand All @@ -10,7 +14,7 @@ type Info struct {
}

func (a *App) Info() (*Info, error) {
msgCount, err := a.messageREPO.Count()
msgCount, err := a.messageREPO.Count(&core.MessageParam{})
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions app/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Message struct {
UUID string `json:"uuid"`
Status core.Status `json:"status"`
Status int `json:"status"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Expand Down Expand Up @@ -42,7 +42,7 @@ func NewMessage(msg *core.Message, atts []core.Attachment) Message {
CreatedAt: msg.CreatedAt.Format(time.RFC822),
From: msg.From,
To: to,
Status: msg.Status,
Status: int(msg.Status),
Subject: subject,
Text: msg.Text,
Attachments: attachments,
Expand Down
16 changes: 13 additions & 3 deletions app/message_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package app

import (
"math"

"github.com/ItsNotGoodName/smtpbridge/core"
)

type MessageListRequest struct {
Page int
Page int
Status int
}

type MessageListResponse struct {
Expand All @@ -19,7 +22,14 @@ func (a *App) MessageList(req *MessageListRequest) (*MessageListResponse, error)
limit := 10
pageMin := 1

count, err := a.messageREPO.Count()
searchParam := &core.MessageParam{
Limit: limit,
Offset: (req.Page - pageMin) * limit,
Reverse: true,
Status: core.Status(req.Status),
}

count, err := a.messageREPO.Count(searchParam)
if err != nil {
return nil, err
}
Expand All @@ -30,7 +40,7 @@ func (a *App) MessageList(req *MessageListRequest) (*MessageListResponse, error)
req.Page = pageMin
}

msgs, err := a.messageREPO.List(limit, (req.Page-pageMin)*limit, true)
msgs, err := a.messageREPO.List(searchParam)
if err != nil {
return nil, err
}
Expand Down
16 changes: 11 additions & 5 deletions core/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ var (
)

const (
StatusCreated Status = iota
StatusAll Status = iota
StatusCreated
StatusSent
StatusFailed
StatusSkipped
)

type (
MessageParam struct {
Limit int
Offset int
Status Status
Reverse bool
}

Message struct {
UUID string // UUID of the message.
From string // From is the email address of the sender.
Expand Down Expand Up @@ -49,13 +57,11 @@ type (
// Create saves a message.
Create(msg *Message) error
// Count returns the number of messages.
Count() (int, error)
Count(search *MessageParam) (int, error)
// Get returns a message by it's UUID.
Get(uuid string) (*Message, error)
// List messages.
List(limit, offset int, reverse bool) ([]Message, error)
// ListOldest returns the oldest messages.
ListOldest(limit int) ([]Message, error)
List(search *MessageParam) ([]Message, error)
// Update a message.
Update(msg *Message, updateFN func(msg *Message) (*Message, error)) error
// Delete a message.
Expand Down
16 changes: 12 additions & 4 deletions left/router/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@ import (
)

func handleIndexGet(w left.WebRepository, a *app.App) http.HandlerFunc {
param := "page"
pageParam := "page"
statusParam := "status"

return func(rw http.ResponseWriter, r *http.Request) {
var page int
if p := r.URL.Query().Get(param); p != "" {
if p := r.URL.Query().Get(pageParam); p != "" {
if i, err := strconv.Atoi(p); err == nil {
page = i
}
}

res, err := a.MessageList(&app.MessageListRequest{Page: page})
var status int
if p := r.URL.Query().Get(statusParam); p != "" {
if i, err := strconv.Atoi(p); err == nil {
status = i
}
}

res, err := a.MessageList(&app.MessageListRequest{Page: page, Status: status})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}

data := left.IndexData{
Messages: res.Messages,
Paginate: paginate.New(*r.URL, param, res.PageMin, res.Page, res.PageMax),
Paginate: paginate.New(*r.URL, pageParam, res.PageMin, res.Page, res.PageMax),
}
render(rw, w, left.IndexPage, &data)
}
Expand Down
3 changes: 3 additions & 0 deletions left/web/template/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{{ template "layout/default" . }}
{{ define "content" }}
<div class="mb-2">
{{ template "shim/status" . }}
</div>
<div class="is-flex is-flex-wrap-wrap is-justify-content-center">
{{ range $msg := .Messages }}
<div class="is-flex-grow-1 m-1">
Expand Down
8 changes: 4 additions & 4 deletions left/web/template/message/tag-status.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{ define "message/tag-status" }}
{{ if eq .Status 0 }}
{{ if eq .Status 1 }}
<span class="tag is-warning is-medium">Created</span>
{{ else if eq .Status 1 }}
<span class="tag is-success is-medium">Sent</span>
{{ else if eq .Status 2 }}
<span class="tag is-danger is-medium">Failed</span>
<span class="tag is-success is-medium">Sent</span>
{{ else if eq .Status 3 }}
<span class="tag is-danger is-medium">Failed</span>
{{ else if eq .Status 4 }}
<span class="tag is-info is-medium">Skipped</span>
{{ end }}
{{ end }}
7 changes: 7 additions & 0 deletions left/web/template/shim/status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ define "shim/status" }}
<a class="button is-rounded is-small" href="?status=0">All</a>
<a class="button is-rounded is-small is-warning" href="?status=1">Created</a>
<a class="button is-rounded is-small is-success" href="?status=2">Sent</a>
<a class="button is-rounded is-small is-danger" href="?status=3">Failed</a>
<a class="button is-rounded is-small is-info" href="?status=4">Skipped</a>
{{ end }}
25 changes: 17 additions & 8 deletions right/repository/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,21 @@ func (m *Message) Update(msg *core.Message, updateFN func(msg *core.Message) (*c
return tx.Commit()
}

func (m *Message) List(limit, offset int, reverse bool) ([]core.Message, error) {
func (m *Message) List(search *core.MessageParam) ([]core.Message, error) {
var filters []q.Matcher
if search.Status != core.StatusAll {
filters = append(filters, q.Eq("Status", search.Status))
}

var msgsM []messageModel
query := m.db.Select().OrderBy("CreatedAt").Limit(limit).Skip(offset)
if reverse {
query := m.db.Select(filters...).OrderBy("CreatedAt")

if search.Reverse {
query = query.Reverse()
}

query = query.Limit(search.Limit).Skip(search.Offset)

err := query.Find(&msgsM)
if err != nil && err != storm.ErrNotFound {
return nil, err
Expand All @@ -119,12 +127,13 @@ func (m *Message) List(limit, offset int, reverse bool) ([]core.Message, error)
return msgs, nil
}

func (m *Message) ListOldest(limit int) ([]core.Message, error) {
return m.List(limit, 0, true)
}
func (m *Message) Count(search *core.MessageParam) (int, error) {
var filters []q.Matcher
if search.Status != core.StatusAll {
filters = append(filters, q.Eq("Status", search.Status))
}

func (m *Message) Count() (int, error) {
count, err := m.db.Count(&messageModel{})
count, err := m.db.Select(filters...).Count(&messageModel{})
if err == storm.ErrNotFound {
return 0, nil
}
Expand Down
8 changes: 2 additions & 6 deletions right/repository/mock_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ func (MessageMock) Create(msg *core.Message) error {
return nil
}

func (MessageMock) Count() (int, error) {
func (MessageMock) Count(search *core.MessageParam) (int, error) {
return 0, nil
}

func (MessageMock) Get(uuid string) (*core.Message, error) {
return nil, core.ErrMessageNotFound
}

func (MessageMock) List(limit, offset int, reverse bool) ([]core.Message, error) {
return []core.Message{}, nil
}

func (MessageMock) ListOldest(limit int) ([]core.Message, error) {
func (MessageMock) List(search *core.MessageParam) ([]core.Message, error) {
return []core.Message{}, nil
}

Expand Down
6 changes: 5 additions & 1 deletion service/janitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func (j *Janitor) clean() error {
return nil
}

msgs, err := j.messageREPO.ListOldest(5)
msgs, err := j.messageREPO.List(&core.MessageParam{
Limit: 5,
Offset: 0,
Reverse: false,
})
if err != nil {
return err
}
Expand Down

0 comments on commit f81b383

Please sign in to comment.