Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ install-minimock:
GOBIN=${LOCAL_BIN} go install github.com/gojuno/minimock/v3/cmd/minimock@v3.4.1

install-golangci-lint:
$(LOCAL_BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
GOBIN=${LOCAL_BIN} go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

lint:
$(LOCAL_BIN)/golangci-lint cache clean
$(LOCAL_BIN)/golangci-lint run ./... --config .golangci.pipeline.yaml
${LOCAL_BIN}/golangci-lint cache clean
${LOCAL_BIN}/golangci-lint run ./... --config .golangci.pipeline.yaml



Expand Down Expand Up @@ -44,25 +44,25 @@ generate-user:
mkdir -p pkg/user_v1
protoc --proto_path=./api/proto/user_v1 --proto_path vendor.protogen \
--go_out=pkg/user_v1 --go_opt=paths=source_relative \
--plugin=protoc-gen-go=bin/protoc-gen-go \
--plugin=protoc-gen-go=$(LOCAL_BIN)/protoc-gen-go \
--go-grpc_out=pkg/user_v1 --go-grpc_opt=paths=source_relative \
--plugin=protoc-gen-go-grpc=bin/protoc-gen-go-grpc \
--plugin=protoc-gen-go-grpc=$(LOCAL_BIN)/protoc-gen-go-grpc \
--grpc-gateway_out=pkg/user_v1 --grpc-gateway_opt=paths=source_relative \
--plugin=protoc-gen-grpc-gateway=bin/protoc-gen-grpc-gateway \
--plugin=protoc-gen-grpc-gateway=$(LOCAL_BIN)/protoc-gen-grpc-gateway \
--validate_out lang=go:pkg/user_v1 --validate_opt=paths=source_relative \
--plugin=protoc-gen-validate=bin/protoc-gen-validate \
--plugin=protoc-gen-validate=$(LOCAL_BIN)/protoc-gen-validate \
--openapiv2_out=allow_merge=true,merge_file_name=api:pkg/swagger \
--plugin=protoc-gen-openapiv2=bin/protoc-gen-openapiv2 \
--plugin=protoc-gen-openapiv2=$(LOCAL_BIN)/protoc-gen-openapiv2 \
./api/proto/user_v1/user.proto ./api/proto/user_v1/models.proto

generate-auth:
mkdir -p pkg/auth_v1
protoc --proto_path=./api/proto/auth_v1 --proto_path vendor.protogen \
--go_out=pkg/auth_v1 --go_opt=paths=source_relative \
--plugin=protoc-gen-go=bin/protoc-gen-go \
--plugin=protoc-gen-go=$(LOCAL_BIN)/protoc-gen-go \
--go-grpc_out=pkg/auth_v1 --go-grpc_opt=paths=source_relative \
--validate_out lang=go:pkg/auth_v1 --validate_opt=paths=source_relative \
--plugin=protoc-gen-validate=bin/protoc-gen-validate \
--plugin=protoc-gen-validate=$(LOCAL_BIN)/protoc-gen-validate \
./api/proto/auth_v1/auth.proto


Expand Down
33 changes: 24 additions & 9 deletions api/proto/user_v1/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ service UserApi {
};
}

rpc Get(GetRequest) returns (GetResponse) {
rpc GetById(GetByIdRequest) returns (GetByResponse) {
option (google.api.http) = {
get: "/user/get"
get: "/user/get-by-id"
};
}

rpc GetByName(GetByNameRequest) returns (GetByResponse) {
option (google.api.http) = {
get: "/user/get-by-name"
};
}


rpc Update(UpdateRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
put: "/user/update"
Expand All @@ -65,15 +72,23 @@ message CreateResponse {
int64 id = 1;
}

message GetRequest {
oneof nameOrId {
option (validate.required) = true;
int64 id = 1;
string username = 2 [(validate.rules).string = {ignore_empty: true, min_len: 1, max_len: 255}];
}
message GetByIdRequest {
int64 id = 1;
}

message GetByNameRequest {
string name = 1;
}

message GetByNameResponse {
int64 id = 1;
User user = 2;
google.protobuf.Timestamp created_at = 3;
google.protobuf.Timestamp updated_at = 4;
}

message GetResponse {

message GetByResponse {
int64 id = 1;
User user = 2;
google.protobuf.Timestamp created_at = 3;
Expand Down
1 change: 1 addition & 0 deletions deploy/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ services:
env_file:
- "../auth.env"


migrator:
build:
context: ..
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/stretchr/testify v1.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20241021214115-324edc3d5d38
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.35.1
google.golang.org/protobuf v1.36.5
)

require (
Expand Down Expand Up @@ -53,5 +53,6 @@ require (
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ const (
strAdmin = "ADMIN"
)

func mappingUserParams(userParams *userv1.GetRequest) models.GetUserParams {
var params models.GetUserParams
switch nameOrID := userParams.NameOrId.(type) {
case *userv1.GetRequest_Id:
params.ID = &nameOrID.Id
case *userv1.GetRequest_Username:
params.Username = &nameOrID.Username
}

return params
}

func toModelRole(role userv1.Role) string {
switch role {
case userv1.Role_ROLE_UNSPECIFIED:
Expand All @@ -49,10 +37,10 @@ func toModelUser(user *userv1.CreateRequest) models.User {
Password: user.Password}
}

func toProtoUserProfile(user models.User) *userv1.GetResponse {
func toProtoUserProfile(user models.User) *userv1.GetByResponse {
userRole := role2String(user.Role)

return &userv1.GetResponse{
return &userv1.GetByResponse{
Id: user.ID,
User: &userv1.User{
Name: user.Name,
Expand Down
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions internal/api/handler/user/get_by_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package user

import (
"context"
"fmt"

"github.com/Dnlbb/auth/internal/api/request"
userv1 "github.com/Dnlbb/auth/pkg/user_v1"
)

// GetById конвертация grpc структуры в сервисную модель и дальнейшая передача запроса в сервисный слой Get.
func (c *Controller) GetById(ctx context.Context, req *userv1.GetByIdRequest) (*userv1.GetByResponse, error) {
id := request.ID(req)

userProfile, err := c.authService.GetById(ctx, id)
if err != nil {
return nil, fmt.Errorf("error when getUser request: %w", err)
}

return toProtoUserProfile(*userProfile), nil
}
24 changes: 24 additions & 0 deletions internal/api/handler/user/get_by_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package user

import (
"context"
"fmt"

"github.com/Dnlbb/auth/internal/api/request"
userv1 "github.com/Dnlbb/auth/pkg/user_v1"
)

// GetByName конвертация grpc структуры в сервисную модель и дальнейшая передача запроса в сервисный слой Get.
func (c *Controller) GetByName(ctx context.Context, req *userv1.GetByNameRequest) (*userv1.GetByResponse, error) {
name, err := request.Name(req)
if err != nil {
return nil, err
}

userProfile, err := c.authService.GetByName(ctx, name)
if err != nil {
return nil, fmt.Errorf("error when getUser request: %w", err)
}

return toProtoUserProfile(*userProfile), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"fmt"
"testing"

"github.com/Dnlbb/auth/internal/api/user"
"github.com/Dnlbb/auth/internal/api/handler/user"
"github.com/Dnlbb/auth/internal/models"
serviceMocks "github.com/Dnlbb/auth/internal/service/mocks"
"github.com/Dnlbb/auth/internal/service/servinterfaces"
Expand Down Expand Up @@ -72,7 +72,7 @@
Password: password,
Role: "USER",
}).Return(&id, nil)
return mock

Check failure on line 75 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / linter

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)

Check failure on line 75 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)
},
},
{
Expand Down Expand Up @@ -100,7 +100,7 @@
Password: password,
Role: "ADMIN",
}).Return(&id, nil)
return mock

Check failure on line 103 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / linter

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)

Check failure on line 103 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)
},
},
{
Expand Down Expand Up @@ -128,7 +128,7 @@
Password: password,
Role: "ROLE_UNSPECIFIED",
}).Return(&id, nil)
return mock

Check failure on line 131 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / linter

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)

Check failure on line 131 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)
},
},
{
Expand All @@ -155,7 +155,7 @@
Password: password,
Role: "USER",
}).Return(nil, errorCreate)
return mock

Check failure on line 158 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / linter

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)

Check failure on line 158 in internal/api/handler/user/tests/create_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"fmt"
"testing"

"github.com/Dnlbb/auth/internal/api/user"
"github.com/Dnlbb/auth/internal/api/handler/user"
"github.com/Dnlbb/auth/internal/models"
serviceMocks "github.com/Dnlbb/auth/internal/service/mocks"
"github.com/Dnlbb/auth/internal/service/servinterfaces"
Expand Down Expand Up @@ -54,7 +54,7 @@
authServiceMock: func(mc *minimock.Controller) servinterfaces.UserService {
mock := serviceMocks.NewUserServiceMock(mc)
mock.DeleteMock.Expect(ctx, models.DeleteID(idToDelete)).Return(nil)
return mock

Check failure on line 57 in internal/api/handler/user/tests/delete_test.go

View workflow job for this annotation

GitHub Actions / linter

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)

Check failure on line 57 in internal/api/handler/user/tests/delete_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)
},
},
{
Expand All @@ -70,7 +70,7 @@
authServiceMock: func(mc *minimock.Controller) servinterfaces.UserService {
mock := serviceMocks.NewUserServiceMock(mc)
mock.DeleteMock.Expect(ctx, models.DeleteID(idToDelete)).Return(errorDelete)
return mock

Check failure on line 73 in internal/api/handler/user/tests/delete_test.go

View workflow job for this annotation

GitHub Actions / linter

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)

Check failure on line 73 in internal/api/handler/user/tests/delete_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use mock (variable of type *mocks.UserServiceMock) as servinterfaces.UserService value in return statement: *mocks.UserServiceMock does not implement servinterfaces.UserService (missing method GetById)
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"testing"
"time"

"github.com/Dnlbb/auth/internal/api/user"
"github.com/Dnlbb/auth/internal/api/handler/user"
"github.com/Dnlbb/auth/internal/models"
serviceMocks "github.com/Dnlbb/auth/internal/service/mocks"
"github.com/Dnlbb/auth/internal/service/servinterfaces"
Expand All @@ -22,7 +22,7 @@
authServiceMockFunc func(mc *minimock.Controller) servinterfaces.UserService
args struct {
ctx context.Context
req *user_v1.GetRequest

Check failure on line 25 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / linter

undefined: user_v1.GetRequest

Check failure on line 25 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

undefined: user_v1.GetRequest
}
)

Expand All @@ -43,7 +43,7 @@
tests := []struct {
name string
args args
want *user_v1.GetResponse

Check failure on line 46 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / linter

undefined: user_v1.GetResponse

Check failure on line 46 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

undefined: user_v1.GetResponse
err error
authServiceMock authServiceMockFunc
}{
Expand All @@ -51,8 +51,8 @@
name: "success case: get by id",
args: args{
ctx: ctx,
req: &user_v1.GetRequest{

Check failure on line 54 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / linter

undefined: user_v1.GetRequest

Check failure on line 54 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

undefined: user_v1.GetRequest
NameOrId: &user_v1.GetRequest_Id{Id: id},

Check failure on line 55 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / linter

undefined: user_v1.GetRequest_Id

Check failure on line 55 in internal/api/handler/user/tests/get_test.go

View workflow job for this annotation

GitHub Actions / build-and-test

undefined: user_v1.GetRequest_Id
},
},
want: &user_v1.GetResponse{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/Dnlbb/auth/internal/api/user"
"github.com/Dnlbb/auth/internal/api/handler/user"
"github.com/Dnlbb/auth/internal/models"
serviceMocks "github.com/Dnlbb/auth/internal/service/mocks"
"github.com/Dnlbb/auth/internal/service/servinterfaces"
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions internal/api/request/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package request

import (
"errors"

userv1 "github.com/Dnlbb/auth/pkg/user_v1"
)

var (
ErrBadRequest = errors.New("empty name")
)

func ID(req *userv1.GetByIdRequest) int {
return int(req.GetId())
}

func Name(req *userv1.GetByNameRequest) (string, error) {
name := req.GetName()
if name == "" {
return "", ErrBadRequest
}

return name, nil
}
19 changes: 0 additions & 19 deletions internal/api/user/get.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/app/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"log"

"github.com/Dnlbb/auth/internal/api/auth"
"github.com/Dnlbb/auth/internal/api/user"
"github.com/Dnlbb/auth/internal/api/handler/auth"
"github.com/Dnlbb/auth/internal/api/handler/user"
"github.com/Dnlbb/auth/internal/client/cache/redis"
"github.com/Dnlbb/auth/internal/config"
"github.com/Dnlbb/auth/internal/repository/AccessPolicies"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/jackc/pgx/v5"
)

// GetUser получаем пользователя из базы postgresql
func (s *storage) GetUser(ctx context.Context, params models.GetUserParams) (*models.User, error) {
// GetUserById получаем пользователя из базы postgresql
func (s *storage) GetUserById(ctx context.Context, id int) (*models.User, error) {
var (
user models.User
err error
Expand All @@ -27,12 +27,7 @@ func (s *storage) GetUser(ctx context.Context, params models.GetUserParams) (*mo
"updated_at",
).From("users")

switch {
case params.ID != nil:
query = query.Where(sq.Eq{"id": *params.ID})
case params.Username != nil:
query = query.Where(sq.Eq{"name": *params.Username})
}
query = query.Where(sq.Eq{"id": id})

query = query.PlaceholderFormat(sq.Dollar)

Expand Down
53 changes: 53 additions & 0 deletions internal/repository/postgres/storage/get_user_by_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package storage

import (
"context"
"errors"
"fmt"

"github.com/Dnlbb/auth/internal/models"
"github.com/Dnlbb/platform_common/pkg/db"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
)

// GetUserByName получаем пользователя из базы postgresql
func (s *storage) GetUserByName(ctx context.Context, name string) (*models.User, error) {
var (
user models.User
err error
)

query := sq.Select("id",
"name",
"email",
"role",
"password",
"created_at",
"updated_at",
).From("users")

query = query.Where(sq.Eq{"name": name})

query = query.PlaceholderFormat(sq.Dollar)

sqlQuery, args, err := query.ToSql()
if err != nil {
return nil, fmt.Errorf("error building sql query: %w", err)
}

q := db.Query{
Name: "Get user",
QueryRow: sqlQuery,
}

err = s.db.DB().ScanOneContext(ctx, &user, q, args...)

if errors.Is(err, pgx.ErrNoRows) {
return nil, fmt.Errorf("error: the user does not exist %w", err)
} else if err != nil {
return nil, fmt.Errorf("error when accessing the database to get a user profile: %w", err)
}

return &user, nil
}
Loading