Skip to content

Commit

Permalink
Remember the last accessed board and show it for /.
Browse files Browse the repository at this point in the history
  • Loading branch information
bilus committed Sep 1, 2020
1 parent 8b1775c commit 5aa85f7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion actions/app.go
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions actions/boards.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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{}

Expand All @@ -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 == "" {
Expand Down
49 changes: 49 additions & 0 deletions 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())
}
2 changes: 1 addition & 1 deletion templates/application.plush.html
Expand Up @@ -20,7 +20,7 @@
<div class="container">
<a href="/" class="navbar-brand">
<div class="logo-image">
<i class="icon-lightbulb-o"></i>
<i class="icon-home"></i>
</div>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
Expand Down

0 comments on commit 5aa85f7

Please sign in to comment.