From 40f683862bef1fa4e18c863feab1810740d0405d Mon Sep 17 00:00:00 2001 From: IgorPolyakov Date: Sat, 31 Aug 2024 22:39:38 +0300 Subject: [PATCH] fix lint & refactoring --- internal/handler/games.go | 6 +-- internal/handler/results.go | 6 +-- internal/handler/services.go | 4 +- internal/handler/teams.go | 4 +- internal/handler/users.go | 6 +-- .../psql/update0020_update0020testdata.go | 48 +++++++++++++++---- internal/model/game.go | 6 +-- internal/model/profile.go | 2 +- internal/model/result.go | 4 +- internal/model/service.go | 4 +- internal/model/team.go | 4 +- internal/model/university.go | 8 ++-- internal/model/user.go | 8 ++-- internal/repository/game.go | 2 +- internal/repository/result.go | 24 +++++----- internal/repository/service.go | 24 +++++----- internal/repository/team.go | 24 +++++----- internal/repository/team_member_requests.go | 25 ++++++---- internal/repository/university.go | 18 +++---- internal/repository/user.go | 2 +- 20 files changed, 135 insertions(+), 94 deletions(-) diff --git a/internal/handler/games.go b/internal/handler/games.go index 0303389..5aeee07 100644 --- a/internal/handler/games.go +++ b/internal/handler/games.go @@ -38,7 +38,7 @@ func (h *Handler) CreateGame(w http.ResponseWriter, r *http.Request) { api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create game"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewGameFromModel(newGame)) + api_helpers.RespondWithJSON(w, http.StatusOK, newGame.ToResponse()) } func (h *Handler) DeleteGame(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) { @@ -59,7 +59,7 @@ func (h *Handler) GetGameById(w http.ResponseWriter, r *http.Request, id openapi api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch game"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewGameDetailsFromModel(game)) + api_helpers.RespondWithJSON(w, http.StatusOK, game.ToResponseGameDetails()) } func (h *Handler) ListGames(w http.ResponseWriter, r *http.Request) { @@ -72,7 +72,7 @@ func (h *Handler) ListGames(w http.ResponseWriter, r *http.Request) { } gameResponses := make([]*server.GameResponse, 0, len(games)) for _, game := range games { - gameResponses = append(gameResponses, model.NewGameDetailsFromModel(game)) + gameResponses = append(gameResponses, game.ToResponseGameDetails()) } api_helpers.RespondWithJSON(w, http.StatusOK, gameResponses) diff --git a/internal/handler/results.go b/internal/handler/results.go index 2ff53c9..d1b36f0 100644 --- a/internal/handler/results.go +++ b/internal/handler/results.go @@ -32,7 +32,7 @@ func (h *Handler) CreateResult(w http.ResponseWriter, r *http.Request, gameId op api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create result"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewResultFromModel(newResult, 0)) + api_helpers.RespondWithJSON(w, http.StatusOK, newResult.ToResponse(0)) } func (h *Handler) GetResult(w http.ResponseWriter, r *http.Request, gameId openapi_types.UUID, resultId openapi_types.UUID) { @@ -44,7 +44,7 @@ func (h *Handler) GetResult(w http.ResponseWriter, r *http.Request, gameId opena api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to fetch result"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewResultFromModel(result, 0)) + api_helpers.RespondWithJSON(w, http.StatusOK, result.ToResponse(0)) } func (h *Handler) UpdateResult(w http.ResponseWriter, r *http.Request, gameId openapi_types.UUID, resultId openapi_types.UUID) { @@ -69,7 +69,7 @@ func (h *Handler) UpdateResult(w http.ResponseWriter, r *http.Request, gameId op return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewResultFromModel(result, 0)) + api_helpers.RespondWithJSON(w, http.StatusOK, result.ToResponse(0)) } // GetScoreboard retrieves the scoreboard for a given game ID diff --git a/internal/handler/services.go b/internal/handler/services.go index e8cf364..aefd2dd 100644 --- a/internal/handler/services.go +++ b/internal/handler/services.go @@ -34,7 +34,7 @@ func (h *Handler) CreateService(w http.ResponseWriter, r *http.Request) { api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create service"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewServiceFromModel(newService)) + api_helpers.RespondWithJSON(w, http.StatusOK, newService.ToResponse()) } func (h *Handler) DeleteService(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) { @@ -55,7 +55,7 @@ func (h *Handler) GetServiceById(w http.ResponseWriter, r *http.Request, id open api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch service"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewServiceFromModel(service)) + api_helpers.RespondWithJSON(w, http.StatusOK, service.ToResponse()) } func (h *Handler) ListServices(w http.ResponseWriter, r *http.Request) { diff --git a/internal/handler/teams.go b/internal/handler/teams.go index e508562..6cd18f7 100644 --- a/internal/handler/teams.go +++ b/internal/handler/teams.go @@ -36,7 +36,7 @@ func (h *Handler) CreateTeam(w http.ResponseWriter, r *http.Request) { api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create team"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewTeamFromModel(newTeam)) + api_helpers.RespondWithJSON(w, http.StatusOK, newTeam.ToResponse()) } func (h *Handler) DeleteTeam(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) { @@ -57,7 +57,7 @@ func (h *Handler) GetTeamById(w http.ResponseWriter, r *http.Request, id openapi api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch team"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewTeamFromModel(team)) + api_helpers.RespondWithJSON(w, http.StatusOK, team.ToResponse()) } func (h *Handler) ListTeams(w http.ResponseWriter, r *http.Request) { diff --git a/internal/handler/users.go b/internal/handler/users.go index 09967ba..363f93f 100644 --- a/internal/handler/users.go +++ b/internal/handler/users.go @@ -48,7 +48,7 @@ func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request) { return } } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewUserFromModel(newUser)) + api_helpers.RespondWithJSON(w, http.StatusOK, newUser.ToResponse()) } func (h *Handler) DeleteUser(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) { @@ -69,7 +69,7 @@ func (h *Handler) GetUserById(w http.ResponseWriter, r *http.Request, id openapi api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch user"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewUserFromModel(user)) + api_helpers.RespondWithJSON(w, http.StatusOK, user.ToResponse()) } func (h *Handler) GetProfileById(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) { @@ -80,7 +80,7 @@ func (h *Handler) GetProfileById(w http.ResponseWriter, r *http.Request, id open api_helpers.RespondWithJSON(w, http.StatusNotFound, map[string]string{"data": "User have not profile"}) return } - api_helpers.RespondWithJSON(w, http.StatusOK, model.NewProfileFromModel(userProfile)) + api_helpers.RespondWithJSON(w, http.StatusOK, userProfile.ToResponse()) } func (h *Handler) ListUsers(w http.ResponseWriter, r *http.Request) { diff --git a/internal/migrations/psql/update0020_update0020testdata.go b/internal/migrations/psql/update0020_update0020testdata.go index 7114cca..665d2e8 100644 --- a/internal/migrations/psql/update0020_update0020testdata.go +++ b/internal/migrations/psql/update0020_update0020testdata.go @@ -29,7 +29,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str // Очистка таблицы team_games _, err = tx.Exec(`DELETE FROM team_games`) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to clear team_games table: " + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -48,7 +52,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str _, err = tx.Exec(query) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Problem with query execution, query: " + query + "\n error:" + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -56,7 +64,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str // Получение существующих команд из базы данных teamRows, err := tx.Query(`SELECT id, name FROM teams`) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to fetch existing teams: " + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -70,7 +82,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str for teamRows.Next() { var id, name string if err := teamRows.Scan(&id, &name); err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to scan team: " + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -83,7 +99,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str // Получение существующих игр из базы данных gameRows, err := tx.Query(`SELECT id, description FROM games`) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to fetch existing games: " + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -97,7 +117,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str for gameRows.Next() { var id, description string if err := gameRows.Scan(&id, &description); err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to scan game: " + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -113,7 +137,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str _, err = tx.Exec(`INSERT INTO results (score, rank, id, team_id, game_id) VALUES ($1, $2, $3, $4, $5)`, score, rank+1, uuid.New().String(), team.ID, game.ID) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to insert result: " + err.Error()) return fromUpdateId, toUpdateId, description, err } @@ -122,7 +150,11 @@ func DatabaseUpdate_update0020_update0020testdata(db *sql.DB, getInfo bool) (str _, err = tx.Exec(`INSERT INTO team_games (team_id, game_id) VALUES ($1, $2)`, team.ID, game.ID) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + slog.Error("Failed to rollback: " + err.Error()) + return fromUpdateId, toUpdateId, description, err + } slog.Error("Failed to insert team_game: " + err.Error()) return fromUpdateId, toUpdateId, description, err } diff --git a/internal/model/game.go b/internal/model/game.go index 6e6500f..7ac6dfd 100644 --- a/internal/model/game.go +++ b/internal/model/game.go @@ -20,7 +20,7 @@ type GameDetails struct { Teams []*Team } -func NewGameFromModel(m *Game) *server.GameResponse { +func (m *Game) ToResponse() *server.GameResponse { return &server.GameResponse{ Id: m.Id, StartTime: m.StartTime, @@ -29,7 +29,7 @@ func NewGameFromModel(m *Game) *server.GameResponse { } } -func NewGameDetailsFromModel(m *GameDetails) *server.GameResponse { +func (m *GameDetails) ToResponseGameDetails() *server.GameResponse { teams := make([]server.TeamResponse, 0, len(m.Teams)) for _, t := range m.Teams { teams = append(teams, server.TeamResponse{ @@ -51,7 +51,7 @@ func NewGameDetailsFromModel(m *GameDetails) *server.GameResponse { func NewGamesFromModels(ms []*Game) []*server.GameResponse { var games []*server.GameResponse for _, m := range ms { - games = append(games, NewGameFromModel(m)) + games = append(games, m.ToResponse()) } return games } diff --git a/internal/model/profile.go b/internal/model/profile.go index 6888d0b..00fb900 100644 --- a/internal/model/profile.go +++ b/internal/model/profile.go @@ -28,7 +28,7 @@ type ProfileWithHistory struct { History []ProfileTeams } -func NewProfileFromModel(p *ProfileWithHistory) *server.ProfileResponse { +func (p *ProfileWithHistory) ToResponse() *server.ProfileResponse { return &server.ProfileResponse{ Id: p.Profile.Id, CreatedAt: p.Profile.CreatedAt, diff --git a/internal/model/result.go b/internal/model/result.go index 92ccb98..070a4e7 100644 --- a/internal/model/result.go +++ b/internal/model/result.go @@ -13,7 +13,7 @@ type Result struct { Score float64 `db:"score" json:"score"` } -func NewResultFromModel(s *Result, rank int) *server.ResultResponse { +func (s *Result) ToResponse(rank int) *server.ResultResponse { return &server.ResultResponse{ Id: s.Id, GameId: s.GameId, @@ -26,7 +26,7 @@ func NewResultFromModel(s *Result, rank int) *server.ResultResponse { func NewScoreboardFromResults(ms []*Result) []*server.ResultResponse { var results []*server.ResultResponse for i, r := range ms { - results = append(results, NewResultFromModel(r, i+1)) + results = append(results, r.ToResponse(i+1)) } return results } diff --git a/internal/model/service.go b/internal/model/service.go index ee7da54..25d891d 100644 --- a/internal/model/service.go +++ b/internal/model/service.go @@ -18,7 +18,7 @@ type Service struct { IsPublic bool `db:"is_public" json:"is_public"` } -func NewServiceFromModel(s *Service) *server.ServiceResponse { +func (s *Service) ToResponse() *server.ServiceResponse { var logo string if s.LogoUrl.Valid { logo = s.LogoUrl.String @@ -38,7 +38,7 @@ func NewServiceFromModel(s *Service) *server.ServiceResponse { func NewServiceFromModels(ms []*Service) []*server.ServiceResponse { var services []*server.ServiceResponse for _, s := range ms { - services = append(services, NewServiceFromModel(s)) + services = append(services, s.ToResponse()) } return services } diff --git a/internal/model/team.go b/internal/model/team.go index ba01afb..ba54646 100644 --- a/internal/model/team.go +++ b/internal/model/team.go @@ -19,7 +19,7 @@ type Team struct { University *string } -func NewTeamFromModel(t *Team) *server.TeamResponse { +func (t *Team) ToResponse() *server.TeamResponse { var avatarUrl string if t.AvatarUrl.Valid { avatarUrl = t.AvatarUrl.String @@ -39,7 +39,7 @@ func NewTeamFromModel(t *Team) *server.TeamResponse { func NewTeamsFromModels(ts []*Team) []*server.TeamResponse { var teams []*server.TeamResponse for _, t := range ts { - teams = append(teams, NewTeamFromModel(t)) + teams = append(teams, t.ToResponse()) } return teams } diff --git a/internal/model/university.go b/internal/model/university.go index 690a644..b7ef573 100644 --- a/internal/model/university.go +++ b/internal/model/university.go @@ -11,17 +11,17 @@ type University struct { Name string `db:"name" json:"name"` } -func NewUniversityFromModel(u *University) *server.UniversityResponse { +func (u *University) ToResponse() *server.UniversityResponse { return &server.UniversityResponse{ Id: u.Id, Name: u.Name, } } -func NewUniversitiesFromModels(ms []*University) []*server.UniversityResponse { +func NewUniversitiesFromModels(us []*University) []*server.UniversityResponse { var universities []*server.UniversityResponse = []*server.UniversityResponse{} - for _, m := range ms { - universities = append(universities, NewUniversityFromModel(m)) + for _, u := range us { + universities = append(universities, u.ToResponse()) } return universities } diff --git a/internal/model/user.go b/internal/model/user.go index 3216db8..a775389 100644 --- a/internal/model/user.go +++ b/internal/model/user.go @@ -20,7 +20,7 @@ type User struct { PasswordHash string `db:"password_hash" json:"password_hash"` } -func NewUserFromModel(u *User) *server.UserResponse { +func (u *User) ToResponse() *server.UserResponse { userRole := helpers.ConvertUserRequestRoleToUserResponseRole(u.Role) var avatarUrl string if u.AvatarUrl.Valid { @@ -38,10 +38,10 @@ func NewUserFromModel(u *User) *server.UserResponse { } } -func NewUsersFromModels(ms []*User) []*server.UserResponse { +func NewUsersFromModels(us []*User) []*server.UserResponse { var users []*server.UserResponse - for _, m := range ms { - users = append(users, NewUserFromModel(m)) + for _, u := range us { + users = append(users, u.ToResponse()) } return users } diff --git a/internal/repository/game.go b/internal/repository/game.go index 277a72c..64c4024 100644 --- a/internal/repository/game.go +++ b/internal/repository/game.go @@ -5,7 +5,7 @@ import ( "database/sql" "time" - model "ctf01d/internal/model" + "ctf01d/internal/model" "github.com/google/uuid" openapi_types "github.com/oapi-codegen/runtime/types" diff --git a/internal/repository/result.go b/internal/repository/result.go index d68717c..62b5266 100644 --- a/internal/repository/result.go +++ b/internal/repository/result.go @@ -4,17 +4,17 @@ import ( "context" "database/sql" - models "ctf01d/internal/model" + "ctf01d/internal/model" openapi_types "github.com/oapi-codegen/runtime/types" ) type ResultRepository interface { - Create(ctx context.Context, result *models.Result) error - GetById(ctx context.Context, id openapi_types.UUID) (*models.Result, error) - Update(ctx context.Context, result *models.Result) error + Create(ctx context.Context, result *model.Result) error + GetById(ctx context.Context, id openapi_types.UUID) (*model.Result, error) + Update(ctx context.Context, result *model.Result) error Delete(ctx context.Context, id string) error - List(ctx context.Context, gameId openapi_types.UUID) ([]*models.Result, error) + List(ctx context.Context, gameId openapi_types.UUID) ([]*model.Result, error) } type resultRepo struct { @@ -25,7 +25,7 @@ func NewResultRepository(db *sql.DB) ResultRepository { return &resultRepo{db: db} } -func (r *resultRepo) Create(ctx context.Context, result *models.Result) error { +func (r *resultRepo) Create(ctx context.Context, result *model.Result) error { query := `INSERT INTO results (team_id, game_id, score) VALUES ($1, $2, $3) RETURNING id, team_id, game_id, score` @@ -37,9 +37,9 @@ func (r *resultRepo) Create(ctx context.Context, result *models.Result) error { return nil } -func (r *resultRepo) GetById(ctx context.Context, gameId openapi_types.UUID) (*models.Result, error) { +func (r *resultRepo) GetById(ctx context.Context, gameId openapi_types.UUID) (*model.Result, error) { query := `SELECT id, team_id, game_id, score FROM results WHERE game_id = $1 order by score desc` - result := &models.Result{} + result := &model.Result{} err := r.db.QueryRowContext(ctx, query, gameId).Scan(&result.Id, &result.TeamId, &result.GameId, &result.Score) if err != nil { return nil, err @@ -47,7 +47,7 @@ func (r *resultRepo) GetById(ctx context.Context, gameId openapi_types.UUID) (*m return result, nil } -func (r *resultRepo) Update(ctx context.Context, result *models.Result) error { +func (r *resultRepo) Update(ctx context.Context, result *model.Result) error { query := `UPDATE results SET team_id = $1, game_id = $2, score = $3 WHERE id = $4` _, err := r.db.ExecContext(ctx, query, result.TeamId, result.GameId, result.Score, result.Id) return err @@ -59,7 +59,7 @@ func (r *resultRepo) Delete(ctx context.Context, id string) error { return err } -func (r *resultRepo) List(ctx context.Context, gameId openapi_types.UUID) ([]*models.Result, error) { +func (r *resultRepo) List(ctx context.Context, gameId openapi_types.UUID) ([]*model.Result, error) { query := `SELECT id, team_id, game_id, score FROM results WHERE game_id = $1 ORDER BY score DESC` rows, err := r.db.QueryContext(ctx, query, gameId) if err != nil { @@ -67,9 +67,9 @@ func (r *resultRepo) List(ctx context.Context, gameId openapi_types.UUID) ([]*mo } defer rows.Close() - var results []*models.Result + var results []*model.Result for rows.Next() { - var result models.Result + var result model.Result if err := rows.Scan(&result.Id, &result.TeamId, &result.GameId, &result.Score); err != nil { return nil, err } diff --git a/internal/repository/service.go b/internal/repository/service.go index 3266ec0..865b094 100644 --- a/internal/repository/service.go +++ b/internal/repository/service.go @@ -4,17 +4,17 @@ import ( "context" "database/sql" - models "ctf01d/internal/model" + "ctf01d/internal/model" openapi_types "github.com/oapi-codegen/runtime/types" ) type ServiceRepository interface { - Create(ctx context.Context, service *models.Service) error - GetById(ctx context.Context, id openapi_types.UUID) (*models.Service, error) - Update(ctx context.Context, service *models.Service) error + Create(ctx context.Context, service *model.Service) error + GetById(ctx context.Context, id openapi_types.UUID) (*model.Service, error) + Update(ctx context.Context, service *model.Service) error Delete(ctx context.Context, id openapi_types.UUID) error - List(ctx context.Context) ([]*models.Service, error) + List(ctx context.Context) ([]*model.Service, error) } type serviceRepo struct { @@ -25,7 +25,7 @@ func NewServiceRepository(db *sql.DB) ServiceRepository { return &serviceRepo{db: db} } -func (r *serviceRepo) Create(ctx context.Context, service *models.Service) error { +func (r *serviceRepo) Create(ctx context.Context, service *model.Service) error { query := `INSERT INTO services (name, author, logo_url, description, is_public) VALUES ($1, $2, $3, $4, $5) RETURNING id, name, author, logo_url, description, is_public` @@ -37,9 +37,9 @@ func (r *serviceRepo) Create(ctx context.Context, service *models.Service) error return nil } -func (r *serviceRepo) GetById(ctx context.Context, id openapi_types.UUID) (*models.Service, error) { +func (r *serviceRepo) GetById(ctx context.Context, id openapi_types.UUID) (*model.Service, error) { query := `SELECT id, name, author, logo_url, description, is_public FROM services WHERE id = $1` - service := &models.Service{} + service := &model.Service{} err := r.db.QueryRowContext(ctx, query, id).Scan(&service.Id, &service.Name, &service.Author, &service.LogoUrl, &service.Description, &service.IsPublic) if err != nil { return nil, err @@ -47,7 +47,7 @@ func (r *serviceRepo) GetById(ctx context.Context, id openapi_types.UUID) (*mode return service, nil } -func (r *serviceRepo) Update(ctx context.Context, service *models.Service) error { +func (r *serviceRepo) Update(ctx context.Context, service *model.Service) error { query := `UPDATE services SET name = $1, author = $2, logo_url = $3, description = $4, is_public = $5 WHERE id = $6` _, err := r.db.ExecContext(ctx, query, service.Name, service.Author, service.LogoUrl, service.Description, service.IsPublic, service.Id) return err @@ -59,7 +59,7 @@ func (r *serviceRepo) Delete(ctx context.Context, id openapi_types.UUID) error { return err } -func (r *serviceRepo) List(ctx context.Context) ([]*models.Service, error) { +func (r *serviceRepo) List(ctx context.Context) ([]*model.Service, error) { query := `SELECT id, name, author, logo_url, description, is_public FROM services` rows, err := r.db.QueryContext(ctx, query) if err != nil { @@ -67,9 +67,9 @@ func (r *serviceRepo) List(ctx context.Context) ([]*models.Service, error) { } defer rows.Close() - var services []*models.Service + var services []*model.Service for rows.Next() { - var service models.Service + var service model.Service if err := rows.Scan(&service.Id, &service.Name, &service.Author, &service.LogoUrl, &service.Description, &service.IsPublic); err != nil { return nil, err } diff --git a/internal/repository/team.go b/internal/repository/team.go index 76eb805..024c86f 100644 --- a/internal/repository/team.go +++ b/internal/repository/team.go @@ -4,17 +4,17 @@ import ( "context" "database/sql" - models "ctf01d/internal/model" + "ctf01d/internal/model" openapi_types "github.com/oapi-codegen/runtime/types" ) type TeamRepository interface { - Create(ctx context.Context, team *models.Team) error - GetById(ctx context.Context, id openapi_types.UUID) (*models.Team, error) - Update(ctx context.Context, team *models.Team) error + Create(ctx context.Context, team *model.Team) error + GetById(ctx context.Context, id openapi_types.UUID) (*model.Team, error) + Update(ctx context.Context, team *model.Team) error Delete(ctx context.Context, id openapi_types.UUID) error - List(ctx context.Context) ([]*models.Team, error) + List(ctx context.Context) ([]*model.Team, error) } type teamRepo struct { @@ -25,7 +25,7 @@ func NewTeamRepository(db *sql.DB) TeamRepository { return &teamRepo{db: db} } -func (r *teamRepo) Create(ctx context.Context, team *models.Team) error { +func (r *teamRepo) Create(ctx context.Context, team *model.Team) error { query := `INSERT INTO teams (name, description, university_id, social_links, avatar_url) VALUES ($1, $2, $3, $4, $5) RETURNING id, name, description, university_id, social_links, avatar_url` @@ -37,12 +37,12 @@ func (r *teamRepo) Create(ctx context.Context, team *models.Team) error { return nil } -func (r *teamRepo) GetById(ctx context.Context, id openapi_types.UUID) (*models.Team, error) { +func (r *teamRepo) GetById(ctx context.Context, id openapi_types.UUID) (*model.Team, error) { query := `SELECT t.id, t.name, t.description, t.social_links, t.avatar_url, u.name as university_name FROM teams t LEFT JOIN universities u ON t.university_id = u.id WHERE t.id = $1` - team := &models.Team{} + team := &model.Team{} err := r.db.QueryRowContext(ctx, query, id).Scan(&team.Id, &team.Name, &team.Description, &team.SocialLinks, &team.AvatarUrl, &team.University) if err != nil { return nil, err @@ -50,7 +50,7 @@ func (r *teamRepo) GetById(ctx context.Context, id openapi_types.UUID) (*models. return team, nil } -func (r *teamRepo) Update(ctx context.Context, team *models.Team) error { +func (r *teamRepo) Update(ctx context.Context, team *model.Team) error { query := `UPDATE teams SET name = $1, description = $2, university_id = $3, social_links = $4, avatar_url = $5 WHERE id = $6` _, err := r.db.ExecContext(ctx, query, team.Name, team.Description, team.UniversityId, team.SocialLinks, team.AvatarUrl, team.Id) return err @@ -62,7 +62,7 @@ func (r *teamRepo) Delete(ctx context.Context, id openapi_types.UUID) error { return err } -func (r *teamRepo) List(ctx context.Context) ([]*models.Team, error) { +func (r *teamRepo) List(ctx context.Context) ([]*model.Team, error) { query := "SELECT t.*, u.name as university_name FROM teams t LEFT JOIN universities u ON t.university_id = u.id" rows, err := r.db.QueryContext(ctx, query) if err != nil { @@ -70,9 +70,9 @@ func (r *teamRepo) List(ctx context.Context) ([]*models.Team, error) { } defer rows.Close() - var teams []*models.Team + var teams []*model.Team for rows.Next() { - var team models.Team + var team model.Team if err := rows.Scan(&team.Name, &team.Description, &team.SocialLinks, &team.AvatarUrl, &team.Id, &team.UniversityId, &team.University); err != nil { return nil, err } diff --git a/internal/repository/team_member_requests.go b/internal/repository/team_member_requests.go index f0c0e74..20449d3 100644 --- a/internal/repository/team_member_requests.go +++ b/internal/repository/team_member_requests.go @@ -4,7 +4,7 @@ import ( "context" "database/sql" - models "ctf01d/internal/model" + "ctf01d/internal/model" openapi_types "github.com/oapi-codegen/runtime/types" ) @@ -13,7 +13,7 @@ type TeamMemberRequestRepository interface { ConnectUserTeam(ctx context.Context, teamID, userID openapi_types.UUID, role string) error ApproveUserTeam(ctx context.Context, teamID, userID openapi_types.UUID) error LeaveUserFromTeam(ctx context.Context, teamID, userID openapi_types.UUID) error - TeamMembers(ctx context.Context, teamID openapi_types.UUID) ([]*models.User, error) + TeamMembers(ctx context.Context, teamID openapi_types.UUID) ([]*model.User, error) } func NewTeamMemberRequestRepository(db *sql.DB) TeamMemberRequestRepository { @@ -36,7 +36,10 @@ func (r *teamRepo) ApproveUserTeam(ctx context.Context, teamID, userID openapi_t query := `UPDATE team_member_requests SET status = 'approved' WHERE team_id = $1 AND user_id = $2 AND status = 'pending'` _, err = tx.ExecContext(ctx, query, teamID, userID) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + return err + } return err } @@ -44,14 +47,20 @@ func (r *teamRepo) ApproveUserTeam(ctx context.Context, teamID, userID openapi_t query = `SELECT role FROM team_member_requests WHERE team_id = $1 AND user_id = $2 AND status = 'approved'` err = tx.QueryRowContext(ctx, query, teamID, userID).Scan(&role) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + return err + } return err } // fixme - обновить team_history query = `INSERT INTO profiles (current_team_id, user_id, role) VALUES ($1, $2, $3)` _, err = tx.ExecContext(ctx, query, teamID, userID, role) if err != nil { - tx.Rollback() + err = tx.Rollback() + if err != nil { + return err + } return err } @@ -65,7 +74,7 @@ func (r *teamRepo) LeaveUserFromTeam(ctx context.Context, teamID, userID openapi return err } -func (r *teamRepo) TeamMembers(ctx context.Context, teamID openapi_types.UUID) ([]*models.User, error) { +func (r *teamRepo) TeamMembers(ctx context.Context, teamID openapi_types.UUID) ([]*model.User, error) { query := `SELECT u.id, u.display_name, u.user_name, tm.role, u.avatar_url, u.status FROM profiles tm JOIN users u ON tm.user_id = u.id @@ -76,9 +85,9 @@ func (r *teamRepo) TeamMembers(ctx context.Context, teamID openapi_types.UUID) ( } defer rows.Close() - var members []*models.User + var members []*model.User for rows.Next() { - var member models.User + var member model.User if err := rows.Scan(&member.Id, &member.DisplayName, &member.Username, &member.Role, &member.AvatarUrl, &member.Status); err != nil { return nil, err } diff --git a/internal/repository/university.go b/internal/repository/university.go index a610822..19bebae 100644 --- a/internal/repository/university.go +++ b/internal/repository/university.go @@ -4,12 +4,12 @@ import ( "context" "database/sql" - models "ctf01d/internal/model" + "ctf01d/internal/model" ) type UniversityRepository interface { - List(ctx context.Context) ([]*models.University, error) - Search(ctx context.Context, query string) ([]*models.University, error) + List(ctx context.Context) ([]*model.University, error) + Search(ctx context.Context, query string) ([]*model.University, error) } type universityRepo struct { @@ -20,15 +20,15 @@ func NewUniversityRepository(db *sql.DB) UniversityRepository { return &universityRepo{db: db} } -func (repo *universityRepo) Search(ctx context.Context, query string) ([]*models.University, error) { +func (repo *universityRepo) Search(ctx context.Context, query string) ([]*model.University, error) { rows, err := repo.db.QueryContext(ctx, "SELECT id, name FROM universities WHERE name ILIKE '%' || $1 || '%' LIMIT 10", query) if err != nil { return nil, err } defer rows.Close() - var universities []*models.University + var universities []*model.University for rows.Next() { - var u models.University + var u model.University if err := rows.Scan(&u.Id, &u.Name); err != nil { return nil, err } @@ -37,15 +37,15 @@ func (repo *universityRepo) Search(ctx context.Context, query string) ([]*models return universities, nil } -func (repo *universityRepo) List(ctx context.Context) ([]*models.University, error) { +func (repo *universityRepo) List(ctx context.Context) ([]*model.University, error) { rows, err := repo.db.QueryContext(ctx, "SELECT id, name FROM universities LIMIT 10") if err != nil { return nil, err } defer rows.Close() - var universities []*models.University + var universities []*model.University for rows.Next() { - var u models.University + var u model.University if err := rows.Scan(&u.Id, &u.Name); err != nil { return nil, err } diff --git a/internal/repository/user.go b/internal/repository/user.go index 5bb17b7..c32e48d 100644 --- a/internal/repository/user.go +++ b/internal/repository/user.go @@ -4,7 +4,7 @@ import ( "context" "database/sql" - model "ctf01d/internal/model" + "ctf01d/internal/model" openapi_types "github.com/oapi-codegen/runtime/types" )