Add post model and lastviewedat to gamemember
This commit is contained in:
parent
4c7e7f4add
commit
701dfbe179
4 changed files with 36 additions and 6 deletions
|
@ -6,7 +6,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type GameMember struct {
|
type GameMember struct {
|
||||||
GameID int `json:"game_id"`
|
GameID int `json:"game_id"`
|
||||||
UserID int `json:"user_id"`
|
UserID int `json:"user_id"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
|
LastViewedAt int `json:"lastviewedat"`
|
||||||
}
|
}
|
||||||
|
|
9
server/model/post.go
Normal file
9
server/model/post.go
Normal file
|
@ -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"`
|
||||||
|
}
|
|
@ -3,14 +3,16 @@ package store
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
sq "github.com/Masterminds/squirrel"
|
sq "github.com/Masterminds/squirrel"
|
||||||
|
|
||||||
"git.ctrlz.es/mgdelacroix/craban/model"
|
"git.ctrlz.es/mgdelacroix/craban/model"
|
||||||
|
"git.ctrlz.es/mgdelacroix/craban/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var gameColumns = []string{"id", "user_id", "name"}
|
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 {
|
type GameStore struct {
|
||||||
Conn *sql.DB
|
Conn *sql.DB
|
||||||
|
@ -104,7 +106,7 @@ func (gs *GameStore) Create(name string, userID int) (*model.Game, error) {
|
||||||
|
|
||||||
res, err = gs.Q().Insert("gamemembers").
|
res, err = gs.Q().Insert("gamemembers").
|
||||||
Columns(gameMemberColumns...).
|
Columns(gameMemberColumns...).
|
||||||
Values(game.ID, userID, model.RoleGameMaster).
|
Values(game.ID, userID, model.RoleGameMaster, 0).
|
||||||
Exec()
|
Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot insert gamemember for created game: %w", err)
|
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
|
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) {
|
func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, error) {
|
||||||
gameMembers := []*model.GameMember{}
|
gameMembers := []*model.GameMember{}
|
||||||
|
|
||||||
|
@ -123,6 +133,7 @@ func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, e
|
||||||
&gameMember.GameID,
|
&gameMember.GameID,
|
||||||
&gameMember.UserID,
|
&gameMember.UserID,
|
||||||
&gameMember.Role,
|
&gameMember.Role,
|
||||||
|
&gameMember.LastViewedAt,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (gs *GameStore) AddMember(gameID, userID int, role string) (*model.GameMember, error) {
|
||||||
_, err := gs.Q().Insert("gamemembers").
|
_, err := gs.Q().Insert("gamemembers").
|
||||||
Columns(gameMemberColumns...).
|
Columns(gameMemberColumns...).
|
||||||
Values(gameID, userID, role).
|
Values(gameID, userID, role, 0).
|
||||||
Exec()
|
Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot insert gamemember: %w", err)
|
return nil, fmt.Errorf("cannot insert gamemember: %w", err)
|
||||||
|
|
|
@ -77,9 +77,18 @@ CREATE TABLE IF NOT EXISTS gamemembers (
|
||||||
game_id INTEGER NOT NULL,
|
game_id INTEGER NOT NULL,
|
||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
role VARCHAR(255) NOT NULL,
|
role VARCHAR(255) NOT NULL,
|
||||||
|
lastviewedat INTEGER NOT NULL,
|
||||||
|
|
||||||
PRIMARY KEY (game_id, user_id)
|
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)
|
_, err := s.Conn.Exec(schema)
|
||||||
|
|
Loading…
Reference in a new issue