Skip to content

Commit

Permalink
Merge pull request #45 from sea-kg/feature/gmd-44/add_role_in_the_team
Browse files Browse the repository at this point in the history
Add role in the team #44
  • Loading branch information
IgorPolyakov authored Jul 12, 2024
2 parents e81d19a + 0467a78 commit 0e8a4b1
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CONCEPT.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

Отдельной таблицей по отношению к пользователям (многие ко многим? - да владеть двумя командами это возможно):
- команда uuid - *уникальный идентификатор*
- Роль в команде - (четыре захаркоженные роли: owner, captain, vice-captain, player)
- Роль в [команде](https://github.com/sea-kg/ctf01d-training-platform/issues/44) - (owner, captain, vice-captain, player, guest)
- Название роли в команде - *роли придуманные капитанами и замами команд*
- публичный комментарий - *виден всем*
- приватный комментарий - *виден только владельцу капитану и зам капитана*
Expand Down
24 changes: 24 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,11 @@ components:
ProfileResponse:
type: object
description: The response schema for a user's profile, including id, timestamps, team name, and team history.
required:
- id
- created_at
- team_name
- team_role
properties:
id:
type: string
Expand All @@ -908,6 +913,15 @@ components:
team_name:
type: string
description: The current name of the user's team.
team_role:
type: string
description: The current role of the user's team.
enum:
- owner
- captain
- vice-captain
- player
- guest
team_history:
type: array
description: The list of teams the user has been part of, including the periods of membership.
Expand All @@ -917,12 +931,22 @@ components:
required:
- name
- join
- role
type: object
description: The schema for recording the history of teams a user has joined and left.
properties:
name:
type: string
description: The name of the team.
role:
type: string
description: Role of the user's team.
enum:
- owner
- captain
- vice-captain
- player
- guest
join:
type: string
format: date-time
Expand Down
4 changes: 2 additions & 2 deletions html/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ function showMyTeams(userId) {
for (var i in data.team_history) {
var team = data.team_history[i];
teamHistoryHtml += '<div class="team">';
teamHistoryHtml += '<div class="team-name">' + team.name + '</div>';
teamHistoryHtml += '<div class="team-name">[' + team.name + '] ' + team.role + '</div>';
teamHistoryHtml += '<div class="team-dates">Joined at: ' + new Date(team.join).toLocaleString() +
(team.left ? ', Left at: ' + new Date(team.left).toLocaleString() : ', ... ') + '</div>';
teamHistoryHtml += '</div>';
}
teamHistoryHtml += '</div>';
var currentTeamHtml = '<div class="current-team"><strong>Current Team: </strong>' + data.team_name + '</div>';
var currentTeamHtml = '<div class="current-team"><strong>Current Team: </strong>[' + data.team_name + '] ' + data.team_role + '</div>';
$('#my_teams_content').html(currentTeamHtml + teamHistoryHtml);
$('#modal_my_teams').modal('show');
});
Expand Down
1 change: 1 addition & 0 deletions internal/app/database/struct_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func RegisterAllUpdates() map[string][]DatabaseUpdateFunc {
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0015_update0016)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0016_update0016testdata)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0016_update0017_no_university)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0017_update0018)
return allUpdates
}

Expand Down
32 changes: 32 additions & 0 deletions internal/app/database/update0016_update0017.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package database

import (
"database/sql"
"log/slog"
"runtime"
)

func DatabaseUpdate_update0016_update0017(db *sql.DB, getInfo bool) (string, string, string, error) {

// WARNING!!!
// Do not change the update if it has already been installed by other developers or in production.
// To correct the database, create a new update and register it in the list of updates.

fromUpdateId, toUpdateId := ParseNameFuncUpdate(runtime.Caller(0))
description := "Add role to profile and team history"
if getInfo {
return fromUpdateId, toUpdateId, description, nil
}
query := `
BEGIN;
ALTER TABLE profiles ADD COLUMN role varchar(50) default 'player' NOT NULL;
ALTER TABLE team_history ADD COLUMN role varchar(50) default 'player' NOT NULL;
COMMIT;
`
_, err := db.Query(query)
if err != nil {
slog.Error("Problem with select, query: " + query + "\n error:" + err.Error())
return fromUpdateId, toUpdateId, description, err
}
return fromUpdateId, toUpdateId, description, nil
}
29 changes: 29 additions & 0 deletions internal/app/database/update0017_update0017testdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package database

import (
"database/sql"
"log/slog"
"runtime"
)

func DatabaseUpdate_update0017_update0017testdata(db *sql.DB, getInfo bool) (string, string, string, error) {

// WARNING!!!
// Do not change the update if it has already been installed by other developers or in production.
// To correct the database, create a new update and register it in the list of updates.

fromUpdateId, toUpdateId := ParseNameFuncUpdate(runtime.Caller(0))
description := "Flush team_history table"
if getInfo {
return fromUpdateId, toUpdateId, description, nil
}

query := `
`
_, err := db.Query(query)
if err != nil {
slog.Error("Problem with select, query: " + query + "\n error:" + err.Error())
return fromUpdateId, toUpdateId, description, err
}
return fromUpdateId, toUpdateId, description, nil
}
32 changes: 32 additions & 0 deletions internal/app/database/update0017_update0018.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package database

import (
"database/sql"
"log/slog"
"runtime"
)

func DatabaseUpdate_update0017_update0018(db *sql.DB, getInfo bool) (string, string, string, error) {

// WARNING!!!
// Do not change the update if it has already been installed by other developers or in production.
// To correct the database, create a new update and register it in the list of updates.

fromUpdateId, toUpdateId := ParseNameFuncUpdate(runtime.Caller(0))
description := "Add role to profile and team history"
if getInfo {
return fromUpdateId, toUpdateId, description, nil
}
query := `
BEGIN;
ALTER TABLE profiles ADD COLUMN role varchar(50) default 'player' NOT NULL;
ALTER TABLE team_history ADD COLUMN role varchar(50) default 'player' NOT NULL;
COMMIT;
`
_, err := db.Query(query)
if err != nil {
slog.Error("Problem with select, query: " + query + "\n error:" + err.Error())
return fromUpdateId, toUpdateId, description, err
}
return fromUpdateId, toUpdateId, description, nil
}
11 changes: 8 additions & 3 deletions internal/app/db/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ package db

import (
"time"

openapi_types "github.com/oapi-codegen/runtime/types"
)

type Profile struct {
CurrentTeam string `db:"name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"created_at"`
Id openapi_types.UUID `db:"id"`
CurrentTeam string `db:"name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"created_at"`
Role string `db:"role"`
}

type ProfileTeams struct {
JoinedAt time.Time `db:"joined_at"`
LeftAt *time.Time `db:"left_at"`
Role string `db:"role"`
Name string `db:"name"`
}

Expand Down
8 changes: 4 additions & 4 deletions internal/app/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ func (r *userRepo) AddUserToTeams(ctx context.Context, userId openapi_types.UUID

func (r *userRepo) GetProfileWithHistory(ctx context.Context, id openapi_types.UUID) (*models.ProfileWithHistory, error) {
query := `
SELECT teams.name, created_at, updated_at
SELECT profiles.id, teams.name, role, created_at, updated_at
FROM profiles JOIN teams on profiles.current_team_id=teams.id
WHERE profiles.user_id = $1
`
profile := models.Profile{}
err := r.db.QueryRowContext(ctx, query, id).Scan(&profile.CurrentTeam, &profile.CreatedAt, &profile.UpdatedAt)
err := r.db.QueryRowContext(ctx, query, id).Scan(&profile.Id, &profile.CurrentTeam, &profile.Role, &profile.CreatedAt, &profile.UpdatedAt)
if err != nil {
return nil, err
}
query = `
SELECT joined_at, left_at, name
SELECT joined_at, left_at, name, role
FROM team_history
JOIN teams ON teams.id = team_history.team_id
WHERE user_id = $1
Expand All @@ -70,7 +70,7 @@ func (r *userRepo) GetProfileWithHistory(ctx context.Context, id openapi_types.U
var history []models.ProfileTeams
for rows.Next() {
var team models.ProfileTeams
err := rows.Scan(&team.JoinedAt, &team.LeftAt, &team.Name)
err := rows.Scan(&team.JoinedAt, &team.LeftAt, &team.Name, &team.Role)
if err != nil {
return nil, err
}
Expand Down
36 changes: 33 additions & 3 deletions internal/app/server/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions internal/app/view/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (

func NewProfileFromModel(p *db.ProfileWithHistory) *server.ProfileResponse {
return &server.ProfileResponse{
CreatedAt: &p.Profile.CreatedAt,
Id: p.Profile.Id,
CreatedAt: p.Profile.CreatedAt,
UpdatedAt: &p.Profile.UpdatedAt,
TeamName: &p.Profile.CurrentTeam,
TeamName: p.Profile.CurrentTeam,
TeamRole: server.ProfileResponseTeamRole(p.Profile.Role),
TeamHistory: makeTeamHistory(p.History),
}
}
Expand All @@ -22,6 +24,7 @@ func makeTeamHistory(tms []db.ProfileTeams) *[]server.TeamHistory {
Join: tm.JoinedAt,
Left: tm.LeftAt,
Name: tm.Name,
Role: server.TeamHistoryRole(tm.Role),
})
}
return &out
Expand Down

0 comments on commit 0e8a4b1

Please sign in to comment.