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

Commit

Permalink
fix #73: add GET /api/v1/license/{license}/workplaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 25, 2018
1 parent 16da310 commit 0ec10d0
Show file tree
Hide file tree
Showing 15 changed files with 554 additions and 108 deletions.
29 changes: 24 additions & 5 deletions env/api/license.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ message Rate {
Unit unit = 2;
}

message Workplace {
string id = 1;
google.protobuf.Timestamp created_at = 2;
google.protobuf.Timestamp updated_at = 3;
}

message RegisterLicenseRequest {
string id = 1;
Contract contract = 2;
Expand Down Expand Up @@ -147,33 +153,46 @@ service License {
put: "/api/v1/license/{id}/workplace/{workplace}"
};
}
rpc Workplaces (WorkplaceListRequest) returns (WorkplaceListResponse) {
option (google.api.http) = {
get: "/api/v1/license/{license}/workplaces"
};
}
}

message EmptyResponse {}

message AddEmployeeRequest {
string id = 1;
string id = 1; // TODO issue#refactoring rename to license
string employee = 2;
}

message DeleteEmployeeRequest {
string id = 1;
string id = 1; // TODO issue#refactoring rename to license
string employee = 2;
}

message AddWorkplaceRequest {
string id = 1;
string id = 1; // TODO issue#refactoring rename to license
string workplace = 2;
}

message DeleteWorkplaceRequest {
string id = 1;
string id = 1; // TODO issue#refactoring rename to license
string workplace = 2;
}

message PushWorkplaceRequest {
string id = 1;
string id = 1; // TODO issue#refactoring rename to license
string workplace = 2;
}

message WorkplaceListRequest {
string license = 1;
}

message WorkplaceListResponse {
repeated Workplace workplaces = 1;
}

// issue#draft }
51 changes: 51 additions & 0 deletions env/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,30 @@
"License"
]
}
},
"/api/v1/license/{license}/workplaces": {
"get": {
"operationId": "Workplaces",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/protobufWorkplaceListResponse"
}
}
},
"parameters": [
{
"name": "license",
"in": "path",
"required": true,
"type": "string"
}
],
"tags": [
"License"
]
}
}
},
"definitions": {
Expand Down Expand Up @@ -648,6 +672,33 @@
"format": "date-time"
}
}
},
"protobufWorkplace": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"created_at": {
"type": "string",
"format": "date-time"
},
"updated_at": {
"type": "string",
"format": "date-time"
}
}
},
"protobufWorkplaceListResponse": {
"type": "object",
"properties": {
"workplaces": {
"type": "array",
"items": {
"$ref": "#/definitions/protobufWorkplace"
}
}
}
}
},
"securityDefinitions": {
Expand Down
6 changes: 6 additions & 0 deletions env/client/rest.http
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ X-Request-ID: 10000000-2000-4000-8000-160000000000

###

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

###

DELETE 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
2 changes: 2 additions & 0 deletions pkg/storage/internal/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type LicenseManager interface {
DeleteWorkplace(*types.Token, query.LicenseWorkplace) error
// PushWorkplace TODO issue#docs
PushWorkplace(*types.Token, query.LicenseWorkplace) error
// Workplaces TODO issue#docs
Workplaces(*types.Token, query.WorkplaceList) ([]types.Workplace, error)

// issue#draft }
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/storage/internal/postgres/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,31 @@ func (scope licenseManager) PushWorkplace(token *types.Token, data query.License
return nil
}

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

// issue#draft }
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 @@ -258,4 +258,23 @@ func (storage *Storage) PushWorkplace(ctx context.Context, id domain.Token, data
return storage.exec.LicenseManager(ctx, conn).PushWorkplace(token, data)
}

// LicenseWorkplaces TODO issue#docs
func (storage *Storage) LicenseWorkplaces(ctx context.Context, id domain.Token, data query.WorkplaceList) (
[]types.Workplace,
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).Workplaces(token, data)
}

// issue#draft }
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
}

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

// issue#draft }

/*
Expand Down
8 changes: 8 additions & 0 deletions pkg/storage/types/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ type License struct {
DeletedAt *time.Time `db:"deleted_at"`
Account *Account `db:"-"`
}

// Workplace TODO issue#docs
type Workplace struct {
ID domain.ID `db:"workplace"`
License domain.ID `db:"license"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt *time.Time `db:"updated_at"`
}
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.

37 changes: 18 additions & 19 deletions pkg/transport/grpc/protobuf/common.pb.go

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

Loading

0 comments on commit 0ec10d0

Please sign in to comment.