From 5aa85f7f60098d26f573bae99f7bf6bcc9c93371 Mon Sep 17 00:00:00 2001 From: Marcin Bilski Date: Tue, 1 Sep 2020 20:15:48 +0200 Subject: [PATCH] Remember the last accessed board and show it for /. --- README.md | 4 +-- actions/app.go | 2 +- actions/boards.go | 12 ++++++-- actions/home.go | 49 ++++++++++++++++++++++++++++++++ templates/application.plush.html | 2 +- 5 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 actions/home.go diff --git a/README.md b/README.md index 196a9b6..3b6f0ef 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,8 @@ Good luck! - [X] User can create a new board - [X] Posts are associated with a board - [X] Switching between boards - - [ ] Remove drafts - - [ ] Remove unnecessary routes, handlers + - [X] Remove drafts + - [X] Remove unnecessary routes, handlers Leave listing posts for administrative purposes but remove route for now. - [ ] Remember last dashboard - [ ] Vote budget is per-board, not global diff --git a/actions/app.go b/actions/app.go index 9f4810e..5f8585c 100644 --- a/actions/app.go +++ b/actions/app.go @@ -71,7 +71,7 @@ func App() *buffalo.App { app.Use(Authorize) postsResource := PostsResource{} - app.GET("/", BoardsResource{}.List) + app.GET("/", Home) app.GET("/posts/{post_id}/edit", postsResource.Edit) app.GET("/posts/{post_id}", postsResource.Show) diff --git a/actions/boards.go b/actions/boards.go index b943a1b..e887440 100644 --- a/actions/boards.go +++ b/actions/boards.go @@ -8,6 +8,7 @@ import ( "github.com/gobuffalo/buffalo" "github.com/gobuffalo/pop/v5" "github.com/gobuffalo/x/responder" + "github.com/gofrs/uuid" ) // This file is generated by Buffalo. It offers a basic structure for @@ -63,7 +64,10 @@ func (v BoardsResource) List(c buffalo.Context) error { // Show gets the data for one Board. This function is mapped to // the path GET /boards/{board_id} func (v BoardsResource) Show(c buffalo.Context) error { - boardId := c.Param("board_id") + boardID, err := uuid.FromString(c.Param("board_id")) + if err != nil { + return c.Error(http.StatusNotFound, err) + } // Get the DB connection from the context tx, ok := c.Value("tx").(*pop.Connection) @@ -75,10 +79,12 @@ func (v BoardsResource) Show(c buffalo.Context) error { board := &models.Board{} // To find the Board the parameter board_id is used. - if err := tx.Find(board, boardId); err != nil { + if err := tx.Find(board, boardID); err != nil { return c.Error(http.StatusNotFound, err) } + SetLastBoardID(boardID, c) + // Load board's posts. posts := &models.Posts{} @@ -90,7 +96,7 @@ func (v BoardsResource) Show(c buffalo.Context) error { if err != nil { return err } - q = q.Where("(NOT draft OR (draft AND author_id = ?)) AND board_id = ?", currentUser.ID, boardId) + q = q.Where("(NOT draft OR (draft AND author_id = ?)) AND board_id = ?", currentUser.ID, boardID.String()) order := c.Param("order") if order == "" { diff --git a/actions/home.go b/actions/home.go new file mode 100644 index 0000000..d8a10e2 --- /dev/null +++ b/actions/home.go @@ -0,0 +1,49 @@ +package actions + +import ( + "fmt" + "net/http" + "rally/models" + + "github.com/gobuffalo/buffalo" + "github.com/gofrs/uuid" +) + +func Home(c buffalo.Context) error { + boardID, found, err := GetLastBoardID(c) + if err != nil || !found { + return c.Redirect(http.StatusSeeOther, "/boards/") + } + + // TODO: Use route helper. + return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/boards/%s", boardID.String())) +} + +func GetLastBoardID(c buffalo.Context) (uuid.UUID, bool, error) { + v := c.Session().Get("last_board_id") + if v == nil { + return uuid.UUID{}, false, nil + } + s, ok := v.(string) + if !ok { + return uuid.UUID{}, false, nil + } + boardID, err := uuid.FromString(s) + if err != nil { + return uuid.UUID{}, false, err + } + + q := models.DB.Where("id = ?", boardID.String()) + exists, err := q.Exists(&models.Boards{}) + if err != nil { + return uuid.UUID{}, false, err + } + if !exists { + return uuid.UUID{}, false, fmt.Errorf("board not found") + } + return boardID, true, nil +} + +func SetLastBoardID(boardID uuid.UUID, c buffalo.Context) { + c.Session().Set("last_board_id", boardID.String()) +} diff --git a/templates/application.plush.html b/templates/application.plush.html index 0f14a1e..fcef789 100644 --- a/templates/application.plush.html +++ b/templates/application.plush.html @@ -20,7 +20,7 @@
- +