Skip to content

Commit 8447335

Browse files
author
Igor Polyakov
committed
Add a user profile and command history
1 parent f5b277b commit 8447335

File tree

7 files changed

+404
-189
lines changed

7 files changed

+404
-189
lines changed

api/openapi.yaml

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ paths:
160160
description: Bad request
161161
'500':
162162
description: Failed response
163-
'/api/v1/users/{uuid}':
163+
'/api/v1/users/{userId}':
164164
get:
165165
tags:
166166
- Users
167167
summary: Get a user by ID
168168
operationId: getUserById
169169
parameters:
170-
- name: uuid
170+
- name: userId
171171
in: path
172172
required: true
173173
style: simple
@@ -193,7 +193,7 @@ paths:
193193
summary: Update a user
194194
operationId: updateUser
195195
parameters:
196-
- name: uuid
196+
- name: userId
197197
in: path
198198
required: true
199199
style: simple
@@ -221,7 +221,7 @@ paths:
221221
summary: Delete a user
222222
operationId: deleteUser
223223
parameters:
224-
- name: uuid
224+
- name: userId
225225
in: path
226226
required: true
227227
style: simple
@@ -276,14 +276,14 @@ paths:
276276
description: Bad request
277277
'500':
278278
description: Failed response
279-
'/api/v1/games/{uuid}':
279+
'/api/v1/games/{gameId}':
280280
get:
281281
tags:
282282
- Games
283283
summary: Get a game by ID
284284
operationId: getGameById
285285
parameters:
286-
- name: uuid
286+
- name: gameId
287287
in: path
288288
required: true
289289
style: simple
@@ -309,7 +309,7 @@ paths:
309309
summary: Update a game
310310
operationId: updateGame
311311
parameters:
312-
- name: uuid
312+
- name: gameId
313313
in: path
314314
required: true
315315
style: simple
@@ -343,7 +343,7 @@ paths:
343343
summary: Delete a game
344344
operationId: deleteGame
345345
parameters:
346-
- name: uuid
346+
- name: gameId
347347
in: path
348348
required: true
349349
style: simple
@@ -411,14 +411,14 @@ paths:
411411
content:
412412
application/json:
413413
schema: {}
414-
'/api/v1/teams/{uuid}':
414+
'/api/v1/teams/{teamId}':
415415
get:
416416
tags:
417417
- Teams
418418
summary: Get a team by ID
419419
operationId: getTeamById
420420
parameters:
421-
- name: uuid
421+
- name: teamId
422422
in: path
423423
required: true
424424
style: simple
@@ -440,7 +440,7 @@ paths:
440440
summary: Update a team
441441
operationId: updateTeam
442442
parameters:
443-
- name: uuid
443+
- name: teamId
444444
in: path
445445
required: true
446446
style: simple
@@ -464,7 +464,7 @@ paths:
464464
summary: Delete a team
465465
operationId: deleteTeam
466466
parameters:
467-
- name: uuid
467+
- name: teamId
468468
in: path
469469
required: true
470470
style: simple
@@ -476,6 +476,66 @@ paths:
476476
responses:
477477
'200':
478478
description: Team deleted successfully
479+
/api/v1/teams/{teamId}/join/{userId}:
480+
post:
481+
tags:
482+
- Teams
483+
summary: Connect user with team
484+
parameters:
485+
- in: path
486+
name: teamId
487+
required: true
488+
schema:
489+
type: string
490+
format: uuid
491+
description: The ID of the team
492+
- in: path
493+
name: userId
494+
required: true
495+
schema:
496+
type: string
497+
format: uuid
498+
description: The ID of the user
499+
responses:
500+
'200':
501+
description: Connect successfully
502+
content:
503+
application/json:
504+
schema:
505+
type: object
506+
properties:
507+
message:
508+
type: string
509+
/api/v1/teams/{teamId}/leave/{userId}:
510+
post:
511+
tags:
512+
- Teams
513+
summary: Leave user from team
514+
parameters:
515+
- in: path
516+
name: teamId
517+
required: true
518+
schema:
519+
type: string
520+
format: uuid
521+
description: The ID of the team
522+
- in: path
523+
name: userId
524+
required: true
525+
schema:
526+
type: string
527+
format: uuid
528+
description: The ID of the user
529+
responses:
530+
'200':
531+
description: Left successfully
532+
content:
533+
application/json:
534+
schema:
535+
type: object
536+
properties:
537+
message:
538+
type: string
479539
/api/v1/results:
480540
get:
481541
tags:
@@ -508,14 +568,14 @@ paths:
508568
responses:
509569
'200':
510570
description: Result created successfully
511-
'/api/v1/results/{uuid}':
571+
'/api/v1/results/{resultId}':
512572
get:
513573
tags:
514574
- Results
515575
summary: Get a result by ID
516576
operationId: getResultById
517577
parameters:
518-
- name: uuid
578+
- name: resultId
519579
in: path
520580
required: true
521581
style: simple
@@ -563,14 +623,14 @@ paths:
563623
responses:
564624
'200':
565625
description: Service created successfully
566-
'/api/v1/services/{uuid}':
626+
'/api/v1/services/{serviceId}':
567627
get:
568628
tags:
569629
- Services
570630
summary: Get a service by ID
571631
operationId: getServiceById
572632
parameters:
573-
- name: uuid
633+
- name: serviceId
574634
in: path
575635
required: true
576636
style: simple
@@ -592,7 +652,7 @@ paths:
592652
summary: Update a service
593653
operationId: updateService
594654
parameters:
595-
- name: uuid
655+
- name: serviceId
596656
in: path
597657
required: true
598658
style: simple
@@ -616,7 +676,7 @@ paths:
616676
summary: Delete a service
617677
operationId: deleteService
618678
parameters:
619-
- name: uuid
679+
- name: serviceId
620680
in: path
621681
required: true
622682
style: simple
@@ -628,7 +688,7 @@ paths:
628688
responses:
629689
'200':
630690
description: Service deleted successfully
631-
/api/v1/services/{uuid}/upload/checker:
691+
/api/v1/services/{serviceId}/upload/checker:
632692
post:
633693
tags:
634694
- Services
@@ -637,7 +697,7 @@ paths:
637697
Handler for upload zip-archive with checker
638698
parameters:
639699
- in: path
640-
name: uuid
700+
name: serviceId
641701
required: true
642702
schema:
643703
type: string
@@ -659,7 +719,7 @@ paths:
659719
content:
660720
application/json:
661721
{}
662-
/api/v1/services/{uuid}/upload/service:
722+
/api/v1/services/{serviceId}/upload/service:
663723
post:
664724
summary: Upload zip service
665725
description: |
@@ -668,7 +728,7 @@ paths:
668728
- Services
669729
parameters:
670730
- in: path
671-
name: uuid
731+
name: serviceId
672732
required: true
673733
schema:
674734
type: string
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package database
2+
3+
import (
4+
"database/sql"
5+
"log/slog"
6+
"runtime"
7+
)
8+
9+
func DatabaseUpdate_update0015_update0016(db *sql.DB, getInfo bool) (string, string, string, error) {
10+
11+
// WARNING!!!
12+
// Do not change the update if it has already been installed by other developers or in production.
13+
// To correct the database, create a new update and register it in the list of updates.
14+
15+
fromUpdateId, toUpdateId := ParseNameFuncUpdate(runtime.Caller(0))
16+
description := "Make profile table"
17+
if getInfo {
18+
return fromUpdateId, toUpdateId, description, nil
19+
}
20+
query := `
21+
BEGIN;
22+
ALTER TABLE team_members RENAME TO profiles;
23+
ALTER TABLE profiles RENAME COLUMN team_id TO current_team_id;
24+
ALTER TABLE profiles ADD COLUMN created_at TIMESTAMP default now();
25+
ALTER TABLE profiles ADD COLUMN updated_at TIMESTAMP default now();
26+
ALTER TABLE profiles ADD COLUMN id UUID DEFAULT gen_random_uuid();
27+
28+
CREATE TABLE team_history (
29+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
30+
user_id UUID REFERENCES users(id),
31+
team_id UUID REFERENCES teams(id),
32+
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
33+
left_at TIMESTAMP
34+
);
35+
COMMIT;
36+
`
37+
_, err := db.Query(query)
38+
if err != nil {
39+
slog.Error("Problem with select, query: " + query + "\n error:" + err.Error())
40+
return fromUpdateId, toUpdateId, description, err
41+
}
42+
return fromUpdateId, toUpdateId, description, nil
43+
}

internal/app/handlers/interface.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,18 @@ func (siw *ServerInterfaceWrapper) UpdateUser(w http.ResponseWriter, r *http.Req
124124
siw.handlers.UpdateUser(w, r, id)
125125
}
126126

127-
func (siw *ServerInterfaceWrapper) PostApiV1ServicesUuidUploadChecker(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
127+
func (siw *ServerInterfaceWrapper) PostApiV1ServicesServiceIdUploadChecker(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
128128
siw.handlers.PostApiV1ServicesUuidUploadChecker(w, r, id)
129129
}
130130

131-
func (siw *ServerInterfaceWrapper) PostApiV1ServicesUuidUploadService(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
131+
func (siw *ServerInterfaceWrapper) PostApiV1ServicesServiceIdUploadService(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
132132
siw.handlers.PostApiV1ServicesUuidUploadService(w, r, id)
133133
}
134+
135+
func (siw *ServerInterfaceWrapper) PostApiV1TeamsTeamIdJoinUserId(w http.ResponseWriter, r *http.Request, teamId openapi_types.UUID, userId openapi_types.UUID) {
136+
siw.handlers.PostApiV1TeamsTeamIdJoinUserId(w, r, teamId, userId)
137+
}
138+
139+
func (siw *ServerInterfaceWrapper) PostApiV1TeamsTeamIdLeaveUserId(w http.ResponseWriter, r *http.Request, teamId openapi_types.UUID, userId openapi_types.UUID) {
140+
siw.handlers.PostApiV1TeamsTeamIdLeaveUserId(w, r, teamId, userId)
141+
}

internal/app/handlers/teams.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,13 @@ func (h *Handlers) UpdateTeam(w http.ResponseWriter, r *http.Request, id openapi
9393
}
9494
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Team updated successfully"})
9595
}
96+
97+
func (h *Handlers) PostApiV1TeamsTeamIdJoinUserId(w http.ResponseWriter, r *http.Request, teamId openapi_types.UUID, userId openapi_types.UUID) {
98+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
99+
w.WriteHeader(http.StatusNotImplemented)
100+
}
101+
102+
func (h *Handlers) PostApiV1TeamsTeamIdLeaveUserId(w http.ResponseWriter, r *http.Request, teamId openapi_types.UUID, userId openapi_types.UUID) {
103+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
104+
w.WriteHeader(http.StatusNotImplemented)
105+
}

internal/app/repository/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *gameRepo) GetGameDetails(ctx context.Context, id openapi_types.UUID) (*
3939
FROM games g
4040
JOIN team_games tg ON g.id = tg.game_id
4141
JOIN teams t ON tg.team_id = t.id
42-
JOIN team_members tm ON t.id = tm.team_id
42+
JOIN profiles tm ON t.id = tm.team_id
4343
JOIN users u ON tm.user_id = u.id
4444
WHERE g.id = $1;
4545
`

internal/app/repository/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (r *userRepo) Create(ctx context.Context, user *models.User) error {
3838

3939
func (r *userRepo) AddUserToTeams(ctx context.Context, userId openapi_types.UUID, teamIds *[]openapi_types.UUID) error {
4040
for _, teamId := range *teamIds {
41-
_, err := r.db.ExecContext(ctx, "INSERT INTO team_members (user_id, team_id) VALUES ($1, $2)", userId, teamId)
41+
_, err := r.db.ExecContext(ctx, "INSERT INTO profiles (user_id, team_id) VALUES ($1, $2)", userId, teamId)
4242
if err != nil {
4343
return err
4444
}
@@ -78,7 +78,7 @@ func (r *userRepo) Delete(ctx context.Context, id openapi_types.UUID) error {
7878
if err != nil {
7979
return err
8080
}
81-
if _, err := tx.ExecContext(ctx, "DELETE FROM team_members WHERE user_id = $1", id); err != nil {
81+
if _, err := tx.ExecContext(ctx, "DELETE FROM profiles WHERE user_id = $1", id); err != nil {
8282
err2 := tx.Rollback()
8383
if err2 != nil {
8484
return err2

0 commit comments

Comments
 (0)