From 701dfbe1794bbb00422fe1729c8b72715b270f98 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 13 Sep 2021 23:39:09 +0200 Subject: [PATCH] Add post model and lastviewedat to gamemember --- server/model/game_member.go | 7 ++++--- server/model/post.go | 9 +++++++++ server/services/store/game.go | 17 ++++++++++++++--- server/services/store/store.go | 9 +++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 server/model/post.go diff --git a/server/model/game_member.go b/server/model/game_member.go index 5abc731..37e00c9 100644 --- a/server/model/game_member.go +++ b/server/model/game_member.go @@ -6,7 +6,8 @@ const ( ) type GameMember struct { - GameID int `json:"game_id"` - UserID int `json:"user_id"` - Role string `json:"role"` + GameID int `json:"game_id"` + UserID int `json:"user_id"` + Role string `json:"role"` + LastViewedAt int `json:"lastviewedat"` } diff --git a/server/model/post.go b/server/model/post.go new file mode 100644 index 0000000..1c0277e --- /dev/null +++ b/server/model/post.go @@ -0,0 +1,9 @@ +package model + +type Post struct { + ID int `json:"id"` + UserID int `json:"user_id"` + GameID int `json:"game_id"` + CreatedAt int `json:"createdat"` + Body string `json:"body"` +} diff --git a/server/services/store/game.go b/server/services/store/game.go index f5f9bc6..69c67ba 100644 --- a/server/services/store/game.go +++ b/server/services/store/game.go @@ -3,14 +3,16 @@ package store import ( "database/sql" "fmt" + "time" sq "github.com/Masterminds/squirrel" "git.ctrlz.es/mgdelacroix/craban/model" + "git.ctrlz.es/mgdelacroix/craban/utils" ) var gameColumns = []string{"id", "user_id", "name"} -var gameMemberColumns = []string{"game_id", "user_id", "role"} +var gameMemberColumns = []string{"game_id", "user_id", "role", "lastviewedat"} type GameStore struct { Conn *sql.DB @@ -104,7 +106,7 @@ func (gs *GameStore) Create(name string, userID int) (*model.Game, error) { res, err = gs.Q().Insert("gamemembers"). Columns(gameMemberColumns...). - Values(game.ID, userID, model.RoleGameMaster). + Values(game.ID, userID, model.RoleGameMaster, 0). Exec() if err != nil { return nil, fmt.Errorf("cannot insert gamemember for created game: %w", err) @@ -113,6 +115,14 @@ func (gs *GameStore) Create(name string, userID int) (*model.Game, error) { return game, nil } +func (gs *GameStore) UpdateLastViewedAt(gameID, userID int) error { + _, err := gs.Q().Update("gamemembers"). + Set("lastviewedat", utils.Millis(time.Now())). + Where(sq.Eq{"game_id": gameID, "user_id": userID}). + Exec() + return err +} + func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, error) { gameMembers := []*model.GameMember{} @@ -123,6 +133,7 @@ func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, e &gameMember.GameID, &gameMember.UserID, &gameMember.Role, + &gameMember.LastViewedAt, ) if err != nil { return nil, err @@ -181,7 +192,7 @@ func (gs *GameStore) GetMember(gameID, userID int) (*model.GameMember, error) { func (gs *GameStore) AddMember(gameID, userID int, role string) (*model.GameMember, error) { _, err := gs.Q().Insert("gamemembers"). Columns(gameMemberColumns...). - Values(gameID, userID, role). + Values(gameID, userID, role, 0). Exec() if err != nil { return nil, fmt.Errorf("cannot insert gamemember: %w", err) diff --git a/server/services/store/store.go b/server/services/store/store.go index 5c9fa9f..0576e33 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -77,9 +77,18 @@ CREATE TABLE IF NOT EXISTS gamemembers ( game_id INTEGER NOT NULL, user_id INTEGER NOT NULL, role VARCHAR(255) NOT NULL, + lastviewedat INTEGER NOT NULL, PRIMARY KEY (game_id, user_id) ); + +CREATE TABLE IF NOT EXISTS posts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL, + game_id INTEGER NOT NULL, + createdat INTEGER NOT NULL, + body TEXT NOT NULL +); ` _, err := s.Conn.Exec(schema)