Skip to content

Commit 11e110b

Browse files
committed
Refactor entry validation
1 parent 806b954 commit 11e110b

File tree

14 files changed

+189
-199
lines changed

14 files changed

+189
-199
lines changed

api/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"github.com/gorilla/mux"
1414
)
1515

16+
type handler struct {
17+
store *storage.Storage
18+
pool *worker.Pool
19+
}
20+
1621
// Serve declares API routes for the application.
1722
func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
1823
handler := &handler{store, pool}

api/doc.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

api/entry.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package api // import "miniflux.app/api"
66

77
import (
8+
json_parser "encoding/json"
89
"errors"
910
"net/http"
1011
"time"
@@ -13,6 +14,7 @@ import (
1314
"miniflux.app/http/response/json"
1415
"miniflux.app/model"
1516
"miniflux.app/storage"
17+
"miniflux.app/validator"
1618
)
1719

1820
func (h *handler) getFeedEntry(w http.ResponseWriter, r *http.Request) {
@@ -68,27 +70,27 @@ func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
6870
func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int64) {
6971
statuses := request.QueryStringParamList(r, "status")
7072
for _, status := range statuses {
71-
if err := model.ValidateEntryStatus(status); err != nil {
73+
if err := validator.ValidateEntryStatus(status); err != nil {
7274
json.BadRequest(w, r, err)
7375
return
7476
}
7577
}
7678

7779
order := request.QueryStringParam(r, "order", model.DefaultSortingOrder)
78-
if err := model.ValidateEntryOrder(order); err != nil {
80+
if err := validator.ValidateEntryOrder(order); err != nil {
7981
json.BadRequest(w, r, err)
8082
return
8183
}
8284

8385
direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection)
84-
if err := model.ValidateDirection(direction); err != nil {
86+
if err := validator.ValidateDirection(direction); err != nil {
8587
json.BadRequest(w, r, err)
8688
return
8789
}
8890

8991
limit := request.QueryIntParam(r, "limit", 100)
9092
offset := request.QueryIntParam(r, "offset", 0)
91-
if err := model.ValidateRange(offset, limit); err != nil {
93+
if err := validator.ValidateRange(offset, limit); err != nil {
9294
json.BadRequest(w, r, err)
9395
return
9496
}
@@ -132,18 +134,18 @@ func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int
132134
}
133135

134136
func (h *handler) setEntryStatus(w http.ResponseWriter, r *http.Request) {
135-
entryIDs, status, err := decodeEntryStatusRequest(r.Body)
136-
if err != nil {
137-
json.BadRequest(w, r, errors.New("Invalid JSON payload"))
137+
var entriesStatusUpdateRequest model.EntriesStatusUpdateRequest
138+
if err := json_parser.NewDecoder(r.Body).Decode(&entriesStatusUpdateRequest); err != nil {
139+
json.BadRequest(w, r, err)
138140
return
139141
}
140142

141-
if err := model.ValidateEntryStatus(status); err != nil {
143+
if err := validator.ValidateEntriesStatusUpdateRequest(&entriesStatusUpdateRequest); err != nil {
142144
json.BadRequest(w, r, err)
143145
return
144146
}
145147

146-
if err := h.store.SetEntriesStatus(request.UserID(r), entryIDs, status); err != nil {
148+
if err := h.store.SetEntriesStatus(request.UserID(r), entriesStatusUpdateRequest.EntryIDs, entriesStatusUpdateRequest.Status); err != nil {
147149
json.ServerError(w, r, err)
148150
return
149151
}

api/handler.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

api/payload.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
package api // import "miniflux.app/api"
66

77
import (
8-
"encoding/json"
9-
"fmt"
10-
"io"
11-
128
"miniflux.app/model"
139
)
1410

@@ -26,19 +22,3 @@ type entriesResponse struct {
2622
type feedCreationResponse struct {
2723
FeedID int64 `json:"feed_id"`
2824
}
29-
30-
func decodeEntryStatusRequest(r io.ReadCloser) ([]int64, string, error) {
31-
type payload struct {
32-
EntryIDs []int64 `json:"entry_ids"`
33-
Status string `json:"status"`
34-
}
35-
36-
var p payload
37-
decoder := json.NewDecoder(r)
38-
defer r.Close()
39-
if err := decoder.Decode(&p); err != nil {
40-
return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
41-
}
42-
43-
return p.EntryIDs, p.Status, nil
44-
}

model/entry.go

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
package model // import "miniflux.app/model"
66

77
import (
8-
"fmt"
98
"time"
109
)
1110

12-
// Entry statuses
11+
// Entry statuses and default sorting order.
1312
const (
1413
EntryStatusUnread = "unread"
1514
EntryStatusRead = "read"
@@ -35,52 +34,15 @@ type Entry struct {
3534
ShareCode string `json:"share_code"`
3635
Starred bool `json:"starred"`
3736
ReadingTime int `json:"reading_time"`
38-
Enclosures EnclosureList `json:"enclosures,omitempty"`
37+
Enclosures EnclosureList `json:"enclosures"`
3938
Feed *Feed `json:"feed,omitempty"`
4039
}
4140

4241
// Entries represents a list of entries.
4342
type Entries []*Entry
4443

45-
// ValidateEntryStatus makes sure the entry status is valid.
46-
func ValidateEntryStatus(status string) error {
47-
switch status {
48-
case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
49-
return nil
50-
}
51-
52-
return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
53-
}
54-
55-
// ValidateEntryOrder makes sure the sorting order is valid.
56-
func ValidateEntryOrder(order string) error {
57-
switch order {
58-
case "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id":
59-
return nil
60-
}
61-
62-
return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id"`)
63-
}
64-
65-
// ValidateDirection makes sure the sorting direction is valid.
66-
func ValidateDirection(direction string) error {
67-
switch direction {
68-
case "asc", "desc":
69-
return nil
70-
}
71-
72-
return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
73-
}
74-
75-
// ValidateRange makes sure the offset/limit values are valid.
76-
func ValidateRange(offset, limit int) error {
77-
if offset < 0 {
78-
return fmt.Errorf(`Offset value should be >= 0`)
79-
}
80-
81-
if limit < 0 {
82-
return fmt.Errorf(`Limit value should be >= 0`)
83-
}
84-
85-
return nil
44+
// EntriesStatusUpdateRequest represents a request to change entries status.
45+
type EntriesStatusUpdateRequest struct {
46+
EntryIDs []int64 `json:"entry_ids"`
47+
Status string `json:"status"`
8648
}

model/entry_test.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

tests/entry_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ func TestFilterEntriesByStatuses(t *testing.T) {
154154
t.Fatal(err)
155155
}
156156

157-
if err := client.UpdateEntries([]int64{results.Entries[0].ID}, "read"); err != nil {
157+
if err := client.UpdateEntries([]int64{results.Entries[0].ID}, miniflux.EntryStatusRead); err != nil {
158158
t.Fatal(err)
159159
}
160160

161-
if err := client.UpdateEntries([]int64{results.Entries[1].ID}, "removed"); err != nil {
161+
if err := client.UpdateEntries([]int64{results.Entries[1].ID}, miniflux.EntryStatusRemoved); err != nil {
162162
t.Fatal(err)
163163
}
164164

165-
results, err = client.Entries(&miniflux.Filter{Statuses: []string{"read", "removed"}})
165+
results, err = client.Entries(&miniflux.Filter{Statuses: []string{miniflux.EntryStatusRead, miniflux.EntryStatusRemoved}})
166166
if err != nil {
167167
t.Fatal(err)
168168
}
@@ -282,7 +282,12 @@ func TestUpdateStatus(t *testing.T) {
282282

283283
err = client.UpdateEntries([]int64{result.Entries[0].ID}, "invalid")
284284
if err == nil {
285-
t.Fatal(`Invalid entry status should ne be accepted`)
285+
t.Fatal(`Invalid entry status should not be accepted`)
286+
}
287+
288+
err = client.UpdateEntries([]int64{}, miniflux.EntryStatusRead)
289+
if err == nil {
290+
t.Fatal(`An empty list of entry should not be accepted`)
286291
}
287292
}
288293

ui/entry_update_status.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@
55
package ui // import "miniflux.app/ui"
66

77
import (
8-
"errors"
8+
json_parser "encoding/json"
99
"net/http"
1010

1111
"miniflux.app/http/request"
1212
"miniflux.app/http/response/json"
13+
"miniflux.app/model"
14+
"miniflux.app/validator"
1315
)
1416

1517
func (h *handler) updateEntriesStatus(w http.ResponseWriter, r *http.Request) {
16-
entryIDs, status, err := decodeEntryStatusPayload(r.Body)
17-
if err != nil {
18+
var entriesStatusUpdateRequest model.EntriesStatusUpdateRequest
19+
if err := json_parser.NewDecoder(r.Body).Decode(&entriesStatusUpdateRequest); err != nil {
1820
json.BadRequest(w, r, err)
1921
return
2022
}
2123

22-
if len(entryIDs) == 0 {
23-
json.BadRequest(w, r, errors.New("The list of entry IDs is empty"))
24+
if err := validator.ValidateEntriesStatusUpdateRequest(&entriesStatusUpdateRequest); err != nil {
25+
json.BadRequest(w, r, err)
2426
return
2527
}
2628

27-
err = h.store.SetEntriesStatus(request.UserID(r), entryIDs, status)
28-
if err != nil {
29+
if err := h.store.SetEntriesStatus(request.UserID(r), entriesStatusUpdateRequest.EntryIDs, entriesStatusUpdateRequest.Status); err != nil {
2930
json.ServerError(w, r, err)
3031
return
3132
}

ui/payload.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)