This commit is contained in:
2025-05-19 03:18:22 +07:00
parent 107504317e
commit e409a69a54
23 changed files with 336 additions and 199 deletions
+45 -10
View File
@@ -26,6 +26,10 @@ func NewRepository() (*Repository, error) {
if err != nil {
return nil, err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS applications (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, teamId INTEGER, state TEXT);")
if err != nil {
return nil, err
}
// for tests
// _, err = db.Exec("insert into teams (id, name, password) values (1, \"name\", \"pass\");")
// if err != nil {
@@ -43,12 +47,12 @@ func (r *Repository) GetTeams(ctx context.Context) ([]*models.Team, error) {
teams := []*models.Team{}
for rows.Next() {
team := &models.Team{}
err := rows.Scan(&team.ID, &team.Name, &team.Password)
item := &models.Team{}
err := rows.Scan(&item.ID, &item.Name, &item.Password)
if err != nil {
return nil, err
}
teams = append(teams, team)
teams = append(teams, item)
}
return teams, nil
}
@@ -76,12 +80,12 @@ func (r *Repository) GetActions(ctx context.Context, teamId int64) ([]*models.Ac
actions := []*models.Action{}
for rows.Next() {
team := &models.Action{}
err := rows.Scan(&team.ID, &team.Place)
item := &models.Action{}
err := rows.Scan(&item.ID, &item.Place)
if err != nil {
return nil, err
}
actions = append(actions, team)
actions = append(actions, item)
}
return actions, nil
}
@@ -97,7 +101,7 @@ func (r *Repository) AddActions(ctx context.Context, actions []*models.Action) e
}
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)
rows, err := r.db.Query("select id, name from teams where LOWER(name) = LOWER($1) and password = $2", teamId, password)
if err != nil {
return nil, err
}
@@ -105,15 +109,46 @@ func (r *Repository) GetTeam(ctx context.Context, teamId any, password any) (*mo
teams := []*models.Team{}
for rows.Next() {
team := &models.Team{}
err := rows.Scan(&team.ID, &team.Name)
item := &models.Team{}
err := rows.Scan(&item.ID, &item.Name)
if err != nil {
return nil, err
}
teams = append(teams, team)
teams = append(teams, item)
}
if len(teams) != 1 {
return nil, errors.New("bad result")
}
return teams[0], nil
}
func (r *Repository) AddApplications(ctx context.Context, actions []*models.Action) error {
for _, action := range actions {
for _, application := range action.Applications {
_, err := r.db.Exec("insert into applications (name, teamId, state) values ($1, $2, $3)", application.Name, action.TeamID, application.State)
if err != nil {
return err
}
}
}
return nil
}
func (r *Repository) GetApplications(ctx context.Context, teamId int64, state string) ([]*models.Application, error) {
rows, err := r.db.Query("select id, name from applications where teamId = $1 and state = $2", teamId, state)
if err != nil {
panic(err)
}
defer rows.Close()
applications := []*models.Application{}
for rows.Next() {
item := &models.Application{}
err := rows.Scan(&item.ID, &item.Name)
if err != nil {
return nil, err
}
applications = append(applications, item)
}
return applications, nil
}