Feat: word card repo
This commit is contained in:
		
							
								
								
									
										59
									
								
								repos/word_cards.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								repos/word_cards.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| package repos | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"gralias/models" | ||||
|  | ||||
| 	"github.com/jmoiron/sqlx" | ||||
| ) | ||||
|  | ||||
| type WordCardsRepo interface { | ||||
| 	WordCardsCreate(ctx context.Context, card *models.WordCard) error | ||||
| 	WordCardsGetByRoomID(ctx context.Context, roomID string) ([]models.WordCard, error) | ||||
| 	WordCardGetByWordAndRoomID(ctx context.Context, word, roomID string) (*models.WordCard, error) | ||||
| 	WordCardReveal(ctx context.Context, word, roomID string) error | ||||
| 	WordCardsRevealAll(ctx context.Context, roomID string) error | ||||
| 	WordCardsDeleteByRoomID(ctx context.Context, roomID string) error | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) WordCardsCreate(ctx context.Context, card *models.WordCard) error { | ||||
| 	db := getDB(ctx, p.DB) | ||||
| 	_, err := db.ExecContext(ctx, `INSERT INTO word_cards (room_id, word, color, revealed, mime_view) VALUES (?, ?, ?, ?, ?)`, card.RoomID, card.Word, card.Color, card.Revealed, card.Mime) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) WordCardsGetByRoomID(ctx context.Context, roomID string) ([]models.WordCard, error) { | ||||
| 	cards := []models.WordCard{} | ||||
| 	err := sqlx.SelectContext(ctx, p.DB, &cards, `SELECT * FROM word_cards WHERE room_id = ?`, roomID) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return cards, nil | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) WordCardGetByWordAndRoomID(ctx context.Context, word, roomID string) (*models.WordCard, error) { | ||||
| 	card := &models.WordCard{} | ||||
| 	err := sqlx.GetContext(ctx, p.DB, card, `SELECT * FROM word_cards WHERE word = ? AND room_id = ?`, word, roomID) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return card, nil | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) WordCardReveal(ctx context.Context, word, roomID string) error { | ||||
| 	db := getDB(ctx, p.DB) | ||||
| 	_, err := db.ExecContext(ctx, `UPDATE word_cards SET revealed = TRUE WHERE word = ? AND room_id = ?`, word, roomID) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) WordCardsRevealAll(ctx context.Context, roomID string) error { | ||||
| 	db := getDB(ctx, p.DB) | ||||
| 	_, err := db.ExecContext(ctx, `UPDATE word_cards SET revealed = TRUE WHERE room_id = ?`, roomID) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) WordCardsDeleteByRoomID(ctx context.Context, roomID string) error { | ||||
| 	db := getDB(ctx, p.DB) | ||||
| 	_, err := db.ExecContext(ctx, `DELETE FROM word_cards WHERE room_id = ?`, roomID) | ||||
| 	return err | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder