Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix #75: add /api/v1/license/{license}/employees
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 28, 2018
1 parent 8de4cb7 commit 2561102
Show file tree
Hide file tree
Showing 15 changed files with 481 additions and 85 deletions.
18 changes: 18 additions & 0 deletions env/api/license.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ message Contract {
uint32 workplaces = 5;
}

message Employee {
string id = 1;
google.protobuf.Timestamp created_at = 2;
}

message Rate {
uint32 value = 1;
enum Unit {
Expand Down Expand Up @@ -137,6 +142,11 @@ service License {
delete: "/api/v1/license/{id}/employee/{employee}"
};
}
rpc Employees (EmployeeListRequest) returns (EmployeeListResponse) {
option (google.api.http) = {
get: "/api/v1/license/{license}/employees"
};
}

rpc AddWorkplace (AddWorkplaceRequest) returns (EmptyResponse) {
option (google.api.http) = {
Expand Down Expand Up @@ -172,6 +182,14 @@ message DeleteEmployeeRequest {
string employee = 2;
}

message EmployeeListRequest {
string license = 1;
}

message EmployeeListResponse {
repeated Employee employees = 1;
}

message AddWorkplaceRequest {
string id = 1; // TODO issue#refactoring rename to license
string workplace = 2;
Expand Down
47 changes: 47 additions & 0 deletions env/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,30 @@
]
}
},
"/api/v1/license/{license}/employees": {
"get": {
"operationId": "Employees",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/protobufEmployeeListResponse"
}
}
},
"parameters": [
{
"name": "license",
"in": "path",
"required": true,
"type": "string"
}
],
"tags": [
"License"
]
}
},
"/api/v1/license/{license}/workplaces": {
"get": {
"operationId": "Workplaces",
Expand Down Expand Up @@ -437,6 +461,29 @@
}
}
},
"protobufEmployee": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"created_at": {
"type": "string",
"format": "date-time"
}
}
},
"protobufEmployeeListResponse": {
"type": "object",
"properties": {
"employees": {
"type": "array",
"items": {
"$ref": "#/definitions/protobufEmployee"
}
}
}
},
"protobufEmptyResponse": {
"type": "object"
},
Expand Down
6 changes: 6 additions & 0 deletions env/client/rest.http
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ X-Request-ID: 10000000-2000-4000-8000-160000000000

###

GET http://localhost:8093/api/v1/license/10000000-2000-4000-8000-160000000004/employees
Authorization: Bearer 10000000-2000-4000-8000-160000000003
X-Request-ID: 10000000-2000-4000-8000-160000000000

###

POST http://localhost:8093/api/v1/license/10000000-2000-4000-8000-160000000004/workplace/10000000-2000-4000-8000-160000000006
Authorization: Bearer 10000000-2000-4000-8000-160000000003
X-Request-ID: 10000000-2000-4000-8000-160000000000
Expand Down
4 changes: 2 additions & 2 deletions env/client/rpc/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ guardctl license workplace delete -f env/client/rpc/license.workplace.yml --grpc
guardctl license workplace add -f env/client/rpc/license.workplace.yml --grpc-host=${HOST}

guardctl license create -f env/client/rpc/license.create.yml --grpc-host=${HOST} | \
guardctl license delete --grpc-host=${HOST} | \
guardctl license read --grpc-host=${HOST}
guardctl license delete --grpc-host=${HOST} | \
guardctl license read --grpc-host=${HOST}
2 changes: 2 additions & 0 deletions pkg/storage/internal/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type LicenseManager interface {
AddEmployee(*types.Token, query.LicenseEmployee) error
// DeleteEmployee TODO issue#docs
DeleteEmployee(*types.Token, query.LicenseEmployee) error
// Employees TODO issue#docs
Employees(*types.Token, query.EmployeeList) ([]types.Employee, error)
// AddWorkplace TODO issue#docs
AddWorkplace(*types.Token, query.LicenseWorkplace) error
// DeleteWorkplace TODO issue#docs
Expand Down
26 changes: 26 additions & 0 deletions pkg/storage/internal/postgres/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ func (scope licenseManager) DeleteEmployee(token *types.Token, data query.Licens
return nil
}

// Employees TODO issue#docs
func (scope licenseManager) Employees(token *types.Token, data query.EmployeeList) ([]types.Employee, error) {
license, readErr := scope.Read(token, query.ReadLicense{ID: data.License})
if readErr != nil {
return nil, readErr
}
q := `SELECT "license", "employee", "created_at"
FROM "license_employee"
WHERE "license" = $1`
rows, queryErr := scope.conn.QueryContext(scope.ctx, q, license.ID)
if queryErr != nil {
return nil, queryErr
}
employees := make([]types.Employee, 0, 4)
for rows.Next() {
var employee types.Employee
if scanErr := rows.Scan(&employee.License, &employee.ID, &employee.CreatedAt); scanErr != nil {
return nil, errors.Wrapf(scanErr,
"user %q of account %q with token %q tried to read employees of license %q",
token.UserID, token.User.AccountID, token.ID, license.ID)
}
employees = append(employees, employee)
}
return employees, nil
}

// AddWorkplace TODO issue#docs
func (scope licenseManager) AddWorkplace(token *types.Token, data query.LicenseWorkplace) error {
license, readErr := scope.Read(token, query.ReadLicense{ID: data.ID})
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/migrations/statik.go

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions pkg/storage/protected.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,25 @@ func (storage *Storage) DeleteEmployee(ctx context.Context, id domain.Token, dat
return storage.exec.LicenseManager(ctx, conn).DeleteEmployee(token, data)
}

// LicenseEmployees TODO issue#docs
func (storage *Storage) LicenseEmployees(ctx context.Context, id domain.Token, data query.EmployeeList) (
[]types.Employee,
error,
) {
conn, closer, connErr := storage.connection(ctx)
if connErr != nil {
return nil, connErr
}
defer func() { _ = closer() }()

token, authErr := storage.exec.UserManager(ctx, conn).AccessToken(id)
if authErr != nil {
return nil, authErr
}

return storage.exec.LicenseManager(ctx, conn).Employees(token, data)
}

// AddWorkplace TODO issue#docs
func (storage *Storage) AddWorkplace(ctx context.Context, id domain.Token, data query.LicenseWorkplace) error {
conn, closer, connErr := storage.connection(ctx)
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/query/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type LicenseWorkplace struct {
Workplace domain.ID
}

// EmployeeList TODO issue#docs
type EmployeeList struct {
License domain.ID
}

// WorkplaceList TODO issue#docs
type WorkplaceList struct {
License domain.ID
Expand Down
7 changes: 7 additions & 0 deletions pkg/storage/types/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ type License struct {
Account *Account `db:"-"`
}

// Employee TODO issue#docs
type Employee struct {
ID domain.ID `db:"employee"`
License domain.ID `db:"license"`
CreatedAt time.Time `db:"created_at"`
}

// Workplace TODO issue#docs
type Workplace struct {
ID domain.ID `db:"workplace"`
Expand Down
60 changes: 60 additions & 0 deletions pkg/transport/grpc/gateway/license.pb.gw.go

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

Loading

0 comments on commit 2561102

Please sign in to comment.