Enha: use of db methods
This commit is contained in:
		| @@ -6,15 +6,15 @@ import ( | ||||
| ) | ||||
|  | ||||
| type RoomsRepo interface { | ||||
| 	ListRooms(ctx context.Context) ([]*models.Room, error) | ||||
| 	GetRoomByID(ctx context.Context, id string) (*models.Room, error) | ||||
| 	GetRoomExtended(ctx context.Context, id string) (*models.Room, error) | ||||
| 	CreateRoom(ctx context.Context, room *models.Room) error | ||||
| 	DeleteRoomByID(ctx context.Context, id string) error | ||||
| 	UpdateRoom(ctx context.Context, room *models.Room) error | ||||
| 	RoomList(ctx context.Context) ([]*models.Room, error) | ||||
| 	RoomGetByID(ctx context.Context, id string) (*models.Room, error) | ||||
| 	RoomGetExtended(ctx context.Context, id string) (*models.Room, error) | ||||
| 	RoomCreate(ctx context.Context, room *models.Room) error | ||||
| 	RoomDeleteByID(ctx context.Context, id string) error | ||||
| 	RoomUpdate(ctx context.Context, room *models.Room) error | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) ListRooms(ctx context.Context) ([]*models.Room, error) { | ||||
| func (p *RepoProvider) RoomList(ctx context.Context) ([]*models.Room, error) { | ||||
| 	rooms := []*models.Room{} | ||||
| 	err := p.DB.SelectContext(ctx, &rooms, `SELECT * FROM rooms`) | ||||
| 	if err != nil { | ||||
| @@ -23,7 +23,7 @@ func (p *RepoProvider) ListRooms(ctx context.Context) ([]*models.Room, error) { | ||||
| 	return rooms, nil | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) GetRoomByID(ctx context.Context, id string) (*models.Room, error) { | ||||
| func (p *RepoProvider) RoomGetByID(ctx context.Context, id string) (*models.Room, error) { | ||||
| 	room := &models.Room{} | ||||
| 	err := p.DB.GetContext(ctx, room, `SELECT * FROM rooms WHERE id = ?`, id) | ||||
| 	if err != nil { | ||||
| @@ -32,28 +32,27 @@ func (p *RepoProvider) GetRoomByID(ctx context.Context, id string) (*models.Room | ||||
| 	return room, nil | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) CreateRoom(ctx context.Context, r *models.Room) error { | ||||
| func (p *RepoProvider) RoomCreate(ctx context.Context, r *models.Room) error { | ||||
| 	_, err := p.DB.ExecContext(ctx, `INSERT INTO rooms (id, created_at, creator_name, team_turn, this_turn_limit, opened_this_turn, blue_counter, red_counter, red_turn, mime_done, , is_running, is_over, team_won, room_link) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, r.ID, r.CreatedAt, r.CreatorName, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) DeleteRoomByID(ctx context.Context, id string) error { | ||||
| func (p *RepoProvider) RoomDeleteByID(ctx context.Context, id string) error { | ||||
| 	_, err := p.DB.ExecContext(ctx, `DELETE FROM rooms WHERE id = ?`, id) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) UpdateRoom(ctx context.Context, r *models.Room) error { | ||||
| func (p *RepoProvider) RoomUpdate(ctx context.Context, r *models.Room) error { | ||||
| 	_, err := p.DB.ExecContext(ctx, `UPDATE rooms SET team_turn = ?, this_turn_limit = ?, opened_this_turn = ?, blue_counter = ?, red_counter = ?, red_turn = ?, mime_done = ?,  = ?, is_running = ?, is_over = ?, team_won = ?, room_link = ? WHERE id = ?`, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink, r.ID) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) GetRoomExtended(ctx context.Context, id string) (*models.Room, error) { | ||||
| func (p *RepoProvider) RoomGetExtended(ctx context.Context, id string) (*models.Room, error) { | ||||
| 	room := &models.Room{} | ||||
| 	err := p.DB.GetContext(ctx, room, `SELECT * FROM rooms WHERE id = ?`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// Get players | ||||
| 	players := []*models.Player{} | ||||
| 	err = p.DB.SelectContext(ctx, &players, `SELECT * FROM players WHERE room_id = ?`, id) | ||||
| @@ -86,23 +85,20 @@ func (p *RepoProvider) GetRoomExtended(ctx context.Context, id string) (*models. | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// Get word cards | ||||
| 	wordCards := []*models.WordCard{} | ||||
| 	wordCards := []models.WordCard{} | ||||
| 	err = p.DB.SelectContext(ctx, &wordCards, `SELECT * FROM word_cards WHERE room_id = ?`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.Cards = wordCards | ||||
|  | ||||
| 	// Get actions | ||||
| 	actions := []*models.Action{} | ||||
| 	actions := []models.Action{} | ||||
| 	err = p.DB.SelectContext(ctx, &actions, `SELECT * FROM actions WHERE room_id = ? ORDER BY created_at ASC`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.ActionHistory = actions | ||||
|  | ||||
| 	// Get settings | ||||
| 	settings := &models.GameSettings{} | ||||
| 	err = p.DB.GetContext(ctx, settings, `SELECT * FROM settings WHERE room_id = ?`, id) | ||||
| @@ -110,6 +106,5 @@ func (p *RepoProvider) GetRoomExtended(ctx context.Context, id string) (*models. | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.Settings = *settings | ||||
|  | ||||
| 	return room, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder