add actions and auth

This commit is contained in:
2025-05-17 12:08:44 +07:00
parent 4bd18ee756
commit e1b342d12c
14 changed files with 487 additions and 246 deletions
+61
View File
@@ -3,6 +3,7 @@ package services
import (
"context"
"database/sql"
"errors"
"evening_detective/internal/models"
_ "github.com/mattn/go-sqlite3"
@@ -21,6 +22,15 @@ func NewRepository() (*Repository, error) {
if err != nil {
return nil, err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS actions (id INTEGER PRIMARY KEY AUTOINCREMENT, place TEXT, teamId INTEGER);")
if err != nil {
return nil, err
}
// for tests
// _, err = db.Exec("insert into teams (id, name, password) values (1, \"name\", \"pass\");")
// if err != nil {
// return nil, err
// }
return &Repository{db: db}, nil
}
@@ -56,3 +66,54 @@ func (r *Repository) AddTeams(ctx context.Context, teams []*models.Team) ([]*mod
}
return teams, nil
}
func (r *Repository) GetActions(ctx context.Context, teamId int64) ([]*models.Action, error) {
rows, err := r.db.Query("select id, place from actions where teamId = $1", teamId)
if err != nil {
panic(err)
}
defer rows.Close()
actions := []*models.Action{}
for rows.Next() {
team := &models.Action{}
err := rows.Scan(&team.ID, &team.Place)
if err != nil {
return nil, err
}
actions = append(actions, team)
}
return actions, nil
}
func (r *Repository) AddActions(ctx context.Context, actions []*models.Action) error {
for _, action := range actions {
_, err := r.db.Exec("insert into actions (place, teamId) values ($1, $2)", action.Place, action.TeamID)
if err != nil {
return err
}
}
return nil
}
func (r *Repository) GetTeam(ctx context.Context, teamId any, password any) (*models.Team, error) {
rows, err := r.db.Query("select id, name from teams where id = $1 and password = $2", teamId, password)
if err != nil {
return nil, err
}
defer rows.Close()
teams := []*models.Team{}
for rows.Next() {
team := &models.Team{}
err := rows.Scan(&team.ID, &team.Name)
if err != nil {
return nil, err
}
teams = append(teams, team)
}
if len(teams) != 1 {
return nil, errors.New("bad result")
}
return teams[0], nil
}