Feat: remove rooms with no action
This commit is contained in:
		| @@ -3,6 +3,7 @@ package repos | ||||
| import ( | ||||
| 	"context" | ||||
| 	"gralias/models" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/jmoiron/sqlx" | ||||
| ) | ||||
| @@ -13,6 +14,7 @@ type ActionsRepo interface { | ||||
| 	ActionGetLastClue(ctx context.Context, roomID string) (*models.Action, error) | ||||
| 	ActionDeleteByRoomID(ctx context.Context, roomID string) error | ||||
| 	ActionDeleteOrphaned(ctx context.Context) error | ||||
| 	ActionGetLastTimeByRoomID(ctx context.Context, roomID string) (time.Time, error) | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) ActionList(ctx context.Context, roomID string) ([]models.Action, error) { | ||||
| @@ -26,10 +28,20 @@ func (p *RepoProvider) ActionList(ctx context.Context, roomID string) ([]models. | ||||
|  | ||||
| func (p *RepoProvider) ActionCreate(ctx context.Context, a *models.Action) error { | ||||
| 	db := getDB(ctx, p.DB) | ||||
| 	_, err := db.ExecContext(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, a.RoomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number, a.CreatedAt.UnixNano()) | ||||
| 	_, err := db.ExecContext(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, a.RoomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number, time.Now()) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) ActionGetLastTimeByRoomID(ctx context.Context, roomID string) (time.Time, error) { | ||||
| 	lastTime := time.Time{} | ||||
| 	err := sqlx.GetContext(ctx, p.DB, &lastTime, | ||||
| 		`SELECT created_at FROM actions WHERE room_id = ? ORDER BY created_at DESC LIMIT 1`, roomID) | ||||
| 	if err != nil { | ||||
| 		return lastTime, err | ||||
| 	} | ||||
| 	return lastTime, nil | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) ActionGetLastClue(ctx context.Context, roomID string) (*models.Action, error) { | ||||
| 	action := &models.Action{} | ||||
| 	err := sqlx.GetContext(ctx, p.DB, action, `SELECT actor, actor_color, action_type, word, word_color, number_associated, created_at FROM actions WHERE room_id = ? AND action_type = 'gave_clue' ORDER BY created_at DESC LIMIT 1`, roomID) | ||||
|   | ||||
| @@ -14,33 +14,25 @@ type CardMarksRepo interface { | ||||
| 	CardMarksByRoomID(ctx context.Context, roomID string) ([]models.CardMark, error) | ||||
| } | ||||
|  | ||||
| type cardMarksRepo struct { | ||||
| 	db *sqlx.DB | ||||
| } | ||||
|  | ||||
| func NewCardMarksRepo(db *sqlx.DB) CardMarksRepo { | ||||
| 	return &cardMarksRepo{db: db} | ||||
| } | ||||
|  | ||||
| func (r *cardMarksRepo) CardMarksByCardID(ctx context.Context, cardID uint32) ([]models.CardMark, error) { | ||||
| func (r *RepoProvider) CardMarksByCardID(ctx context.Context, cardID uint32) ([]models.CardMark, error) { | ||||
| 	var cardMarks []models.CardMark | ||||
| 	err := sqlx.SelectContext(ctx, getDB(ctx, r.db), &cardMarks, "SELECT * FROM card_marks WHERE card_id = ?", cardID) | ||||
| 	err := sqlx.SelectContext(ctx, getDB(ctx, r.DB), &cardMarks, "SELECT * FROM card_marks WHERE card_id = ?", cardID) | ||||
| 	return cardMarks, err | ||||
| } | ||||
|  | ||||
| func (r *cardMarksRepo) CardMarksAdd(ctx context.Context, cm *models.CardMark) error { | ||||
| 	_, err := getDB(ctx, r.db).ExecContext(ctx, "INSERT INTO card_marks (card_id, username) VALUES (?, ?)", cm.CardID, cm.Username) | ||||
| func (r *RepoProvider) CardMarksAdd(ctx context.Context, cm *models.CardMark) error { | ||||
| 	_, err := getDB(ctx, r.DB).ExecContext(ctx, "INSERT INTO card_marks (card_id, username) VALUES (?, ?)", cm.CardID, cm.Username) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (r *cardMarksRepo) CardMarksRemove(ctx context.Context, cardID uint32, username string) error { | ||||
| 	db := getDB(ctx, r.db) | ||||
| func (r *RepoProvider) CardMarksRemove(ctx context.Context, cardID uint32, username string) error { | ||||
| 	db := getDB(ctx, r.DB) | ||||
| 	_, err := db.ExecContext(ctx, "DELETE FROM card_marks WHERE card_id = ? AND username = ?", cardID, username) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (r *cardMarksRepo) CardMarksByRoomID(ctx context.Context, roomID string) ([]models.CardMark, error) { | ||||
| func (r *RepoProvider) CardMarksByRoomID(ctx context.Context, roomID string) ([]models.CardMark, error) { | ||||
| 	var cardMarks []models.CardMark | ||||
| 	err := sqlx.SelectContext(ctx, getDB(ctx, r.db), &cardMarks, "SELECT * FROM card_marks WHERE card_id IN (select id from word_cards where room_id = ?)", roomID) | ||||
| 	err := sqlx.SelectContext(ctx, getDB(ctx, r.DB), &cardMarks, "SELECT * FROM card_marks WHERE card_id IN (select id from word_cards where room_id = ?)", roomID) | ||||
| 	return cardMarks, err | ||||
| } | ||||
|   | ||||
| @@ -26,7 +26,6 @@ type RepoProvider struct { | ||||
| 	DB       *sqlx.DB | ||||
| 	mu       sync.RWMutex | ||||
| 	pathToDB string | ||||
| 	CardMarksRepo | ||||
| } | ||||
|  | ||||
| func NewRepoProvider(pathToDB string) *RepoProvider { | ||||
| @@ -44,7 +43,6 @@ func NewRepoProvider(pathToDB string) *RepoProvider { | ||||
| 	rp := &RepoProvider{ | ||||
| 		DB:       db, | ||||
| 		pathToDB: pathToDB, | ||||
| 		CardMarksRepo: NewCardMarksRepo(db), | ||||
| 	} | ||||
|  | ||||
| 	go rp.pingLoop() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder