-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb0fc78
commit 2ef9039
Showing
41 changed files
with
499 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package model | ||
package article | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package model | ||
package article | ||
|
||
import "time" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package model | ||
package article | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package article | ||
|
||
import ( | ||
"github.com/go-chi/render" | ||
"github.com/rwlist/rwcore/resp" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type Controller struct { | ||
Service Service | ||
} | ||
|
||
func NewController(s Service) *Controller { | ||
return &Controller{ | ||
Service: s, | ||
} | ||
} | ||
|
||
func (c *Controller) get(w http.ResponseWriter, r *http.Request) { | ||
article := R(r) | ||
render.Respond(w, r, article) | ||
} | ||
|
||
func (c *Controller) addURL(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
|
||
var url string | ||
err := render.Decode(r, &url) | ||
if err != nil { | ||
render.Render(w, r, resp.ErrBadRequest.With(err)) | ||
return | ||
} | ||
res, err := c.Service.AddURL(db, url) | ||
resp.QuickRespond(w, r, res, err) | ||
} | ||
|
||
func (c *Controller) getAll(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
|
||
all, err := db.GetAll() | ||
resp.QuickRespond(w, r, all, err) | ||
} | ||
|
||
func (c *Controller) onClick(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
article := R(r) | ||
|
||
res, err := c.Service.OnClick(db, article) | ||
resp.QuickRespond(w, r, res, err) | ||
} | ||
|
||
func (c *Controller) setReadStatus(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
article := R(r) | ||
|
||
newStatus := r.URL.Query().Get("newStatus") | ||
res, err := c.Service.SetReadStatus(db, article, newStatus) | ||
resp.QuickRespond(w, r, res, err) | ||
} | ||
|
||
func (c *Controller) changeRating(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
article := R(r) | ||
|
||
delta, err := strconv.Atoi(r.URL.Query().Get("delta")) | ||
if err != nil { | ||
render.Render(w, r, resp.ErrBadRequest.With(err)) | ||
return | ||
} | ||
|
||
res, err := c.Service.ChangeRating(db, article, delta) | ||
resp.QuickRespond(w, r, res, err) | ||
} | ||
|
||
func (c *Controller) removeTag(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
article := R(r) | ||
tag := r.URL.Query().Get("tag") | ||
|
||
res, err := c.Service.RemoveTag(db, article, tag) | ||
resp.QuickRespond(w, r, res, err) | ||
} | ||
|
||
func (c *Controller) addTag(w http.ResponseWriter, r *http.Request) { | ||
db := DB(r) | ||
article := R(r) | ||
tag := r.URL.Query().Get("tag") | ||
|
||
res, err := c.Service.AddTag(db, article, tag) | ||
resp.QuickRespond(w, r, res, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package article | ||
|
||
import ( | ||
"github.com/globalsign/mgo/bson" | ||
"github.com/google/wire" | ||
) | ||
|
||
type ArticleUpdate struct { | ||
ArticleID bson.ObjectId `json:"id"` | ||
Article Article `json:"article"` | ||
} | ||
|
||
type ArticlesActionResp struct { | ||
AddedArticles []Article `json:"addedArticles"` | ||
Errors []error `json:"errors"` | ||
} | ||
|
||
var All = wire.NewSet( // TODO | ||
NewController, | ||
NewMiddleware, | ||
NewRouter, | ||
NewService, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package article | ||
|
||
import ( | ||
"github.com/go-chi/chi" | ||
"github.com/rwlist/rwcore/auth" | ||
) | ||
|
||
type Router struct{ *chi.Mux } | ||
|
||
func NewRouter(c *Controller, auth *auth.Middleware, mid *Middleware) Router { | ||
r := chi.NewRouter() | ||
r.Use(auth.HasRole("article")) | ||
|
||
// r.Post("/add", m.addOne) | ||
// r.Post("/addMany", m.addMany) | ||
r.Get("/all", c.getAll) | ||
r.Post("/addURL", c.addURL) | ||
r.Route("/{id}", func(r chi.Router) { | ||
r.Use(mid.UpdateContext) | ||
r.Get("/", c.get) | ||
r.Post("/click", c.onClick) | ||
r.Post("/read/status", c.setReadStatus) | ||
r.Post("/rating/change", c.changeRating) | ||
r.Post("/tags/remove", c.removeTag) | ||
r.Post("/tags/add", c.addTag) | ||
// r.Patch("/patch", m.patch) | ||
}) | ||
return Router{r} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package article | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/rwlist/rwcore/utils" | ||
) | ||
|
||
// TODO: queue/mutex | ||
type Service struct{} | ||
|
||
func NewService() Service { | ||
return Service{} | ||
} | ||
|
||
func (s Service) AddURL(db Store, url string) (*ArticlesActionResp, error) { | ||
resp := &ArticlesActionResp{} | ||
|
||
title, err := utils.ParseTitleByURL(url) | ||
if err != nil { | ||
resp.Errors = append(resp.Errors, err) | ||
} | ||
|
||
if title == "" { | ||
title = "Unknown HTML Title" | ||
} | ||
|
||
article := NewArticle(url, title, map[string]interface{}{ | ||
"added": "url", | ||
}) | ||
|
||
err = db.InsertOne(&article) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp.AddedArticles = append(resp.AddedArticles, article) | ||
|
||
return resp, nil | ||
} | ||
|
||
func (s Service) OnClick(db Store, article Article) (*ArticleUpdate, error) { | ||
now := time.Now() | ||
|
||
article.Status.Clicks++ | ||
article.Status.LastClick = &now | ||
if article.Status.ReadStatus == "unopened" { | ||
article.Status.ReadStatus = "viewed" | ||
article.Status.ReadStatusChange = &now | ||
} | ||
|
||
err := db.UpdateOne(&article) | ||
return asUpdate(article), err | ||
} | ||
|
||
func (s Service) SetReadStatus(db Store, article Article, newStatus string) (*ArticleUpdate, error) { | ||
now := time.Now() | ||
|
||
if !ValidArticleReadStatus(newStatus) { | ||
return nil, errors.New("invalid read status") | ||
} | ||
|
||
article.Status.ReadStatus = newStatus | ||
article.Status.ReadStatusChange = &now | ||
|
||
err := db.UpdateOne(&article) | ||
return asUpdate(article), err | ||
} | ||
|
||
func (s Service) ChangeRating(db Store, article Article, delta int) (*ArticleUpdate, error) { | ||
article.Status.Rating += delta | ||
|
||
err := db.UpdateOne(&article) | ||
return asUpdate(article), err | ||
} | ||
|
||
func (s Service) RemoveTag(db Store, article Article, tag string) (*ArticleUpdate, error) { | ||
now := time.Now() | ||
|
||
err := article.RemoveTag(tag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
article.Modified = &now | ||
|
||
err = db.UpdateOne(&article) | ||
return asUpdate(article), err | ||
} | ||
|
||
func (s Service) AddTag(db Store, article Article, tag string) (*ArticleUpdate, error) { | ||
now := time.Now() | ||
|
||
err := article.AddTag(tag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
article.Modified = &now | ||
|
||
err = db.UpdateOne(&article) | ||
return asUpdate(article), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.