Skip to content

Commit

Permalink
Redirect to new username (#28) (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
IraIvanishak authored Mar 17, 2024
1 parent 7d889d7 commit 60b9c60
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 6 deletions.
7 changes: 7 additions & 0 deletions internal/controllers/web_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (c *WebController) GitHubProfile(ctx *gin.Context) {

currentPageProfile, err := c.getProfileByUsername(ctx, sessionProfile, uri.Username)
if err == sql.ErrNoRows {
username, err := c.userService.GetUsernameByOldUsername(ctx, dbs.SocialProviderGithub, uri.Username)
if err == nil {
ctx.Redirect(http.StatusTemporaryRedirect, "/github/"+username)

return
}

ctx.Data(http.StatusBadRequest, "text/html; charset=utf-8", []byte(fmt.Sprintf("User not found")))

return
Expand Down
18 changes: 18 additions & 0 deletions internal/services/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func (s *UserService) GetBySocialProviderUsername(ctx context.Context, provider
})
}

func (s *UserService) GetUsernameByOldUsername(ctx context.Context, provider dbs.SocialProvider, oldUsername string) (string, error) {
return s.repository.Queries().UsernameHistoryGetByOldUsername(ctx, dbs.UsernameHistoryGetByOldUsernameParams{
Username: oldUsername,
SocialProvider: provider,
})
}

func (s *UserService) GetByID(ctx context.Context, id int64) (dbs.UsersGetByIDRow, error) {
return s.repository.Queries().UsersGetByID(ctx, id)
}
Expand Down Expand Up @@ -119,6 +126,17 @@ func (s *UserService) Upsert(
return err
}

err = queries.UsernameHistoryNew(ctx, dbs.UsernameHistoryNewParams{
UserID: txID,
SocialProvider: provider,
Username: username,
CreatedAt: now,
UpdatedAt: now,
})
if err != nil {
return err
}

err = queries.ProfileTotalViewsNew(ctx, txID)
if err != nil {
return err
Expand Down
22 changes: 21 additions & 1 deletion internal/storage/dbs/db.go

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

11 changes: 10 additions & 1 deletion internal/storage/dbs/models.go

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

2 changes: 1 addition & 1 deletion internal/storage/dbs/profile_hourly_views_stats.sql.go

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

2 changes: 1 addition & 1 deletion internal/storage/dbs/profile_total_views.sql.go

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

2 changes: 1 addition & 1 deletion internal/storage/dbs/referrals.sql.go

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

60 changes: 60 additions & 0 deletions internal/storage/dbs/username_history.sql.go

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

2 changes: 1 addition & 1 deletion internal/storage/dbs/users.sql.go

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

18 changes: 18 additions & 0 deletions internal/storage/migrations/20240306163810_username_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE username_history
(
id BIGSERIAL NOT NULL PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users (id),
social_provider SOCIAL_PROVIDER NOT NULL,
canonical_username VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
UNIQUE (canonical_username, social_provider, user_id)
);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE username_history;
-- +goose StatementEnd
15 changes: 15 additions & 0 deletions internal/storage/queries/username_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- name: UsernameHistoryNew :exec
INSERT INTO username_history (user_id, social_provider, canonical_username, created_at, updated_at)
VALUES (@user_id, @social_provider, LOWER(@username), @created_at, @updated_at)
ON CONFLICT (canonical_username, social_provider, user_id) DO UPDATE
SET updated_at = excluded.updated_at;

-- name: UsernameHistoryGetByOldUsername :one
SELECT u.username
FROM username_history uh
JOIN users u
ON uh.user_id = u.id
WHERE uh.canonical_username = LOWER(@username)
AND uh.social_provider = @social_provider
ORDER BY uh.updated_at DESC
LIMIT 1;

0 comments on commit 60b9c60

Please sign in to comment.