-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Polina Sheviakova
committed
Jul 26, 2024
1 parent
c720996
commit 2c000e0
Showing
13 changed files
with
902 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package model | ||
|
||
import "github.com/pkg/errors" | ||
|
||
var ErrorUserNotFound = errors.New("user not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package converter | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
model "github.com/polshe-v/microservices_auth/internal/model" | ||
modelRepo "github.com/polshe-v/microservices_auth/internal/repository/cache/model" | ||
) | ||
|
||
// ToUserFromRepo converts repository layer model to structure of service layer. | ||
func ToUserFromRepo(user *modelRepo.User) *model.User { | ||
var updatedAt sql.NullTime | ||
if user.UpdatedAtNs != nil { | ||
updatedAt = sql.NullTime{ | ||
Time: time.Unix(0, *user.UpdatedAtNs), | ||
Valid: true, | ||
} | ||
} | ||
|
||
return &model.User{ | ||
ID: user.ID, | ||
Name: user.Name, | ||
Email: user.Email, | ||
Role: user.Role, | ||
CreatedAt: time.Unix(0, user.CreatedAtNs), | ||
UpdatedAt: updatedAt, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
// User type is the main structure for user. | ||
type User struct { | ||
ID int64 `redis:"id"` | ||
Name string `redis:"name"` | ||
Email string `redis:"email"` | ||
Role string `redis:"role"` | ||
CreatedAtNs int64 `redis:"created_at"` | ||
UpdatedAtNs *int64 `redis:"updated_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
redigo "github.com/gomodule/redigo/redis" | ||
|
||
"github.com/polshe-v/microservices_auth/internal/model" | ||
"github.com/polshe-v/microservices_auth/internal/repository" | ||
"github.com/polshe-v/microservices_auth/internal/repository/cache/converter" | ||
modelRepo "github.com/polshe-v/microservices_auth/internal/repository/cache/model" | ||
"github.com/polshe-v/microservices_common/pkg/cache" | ||
) | ||
|
||
type repo struct { | ||
client cache.Client | ||
} | ||
|
||
// NewRepository creates new object of repository layer. | ||
func NewRepository(client cache.Client) repository.CacheRepository { | ||
return &repo{client: client} | ||
} | ||
|
||
func (r *repo) CreateRecord(ctx context.Context, user *model.User) error { | ||
var updatedAtNs *int64 | ||
|
||
if user.UpdatedAt.Valid { | ||
updatedAt := user.UpdatedAt.Time.UnixNano() | ||
updatedAtNs = &updatedAt | ||
} | ||
|
||
userRecord := modelRepo.User{ | ||
ID: user.ID, | ||
Name: user.Name, | ||
Email: user.Email, | ||
Role: user.Role, | ||
CreatedAtNs: user.CreatedAt.UnixNano(), | ||
UpdatedAtNs: updatedAtNs, | ||
} | ||
|
||
idStr := strconv.FormatInt(user.ID, 10) | ||
err := r.client.HSet(ctx, idStr, userRecord) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *repo) GetRecord(ctx context.Context, id int64) (*model.User, error) { | ||
idStr := strconv.FormatInt(id, 10) | ||
values, err := r.client.HGetAll(ctx, idStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(values) == 0 { | ||
return nil, model.ErrorUserNotFound | ||
} | ||
|
||
var user modelRepo.User | ||
err = redigo.ScanStruct(values, &user) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return converter.ToUserFromRepo(&user), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.