Skip to content

Commit

Permalink
[FEAT][#9] Refactor proto
Browse files Browse the repository at this point in the history
  • Loading branch information
jacky-htg committed Feb 22, 2021
1 parent 0226321 commit e175435
Show file tree
Hide file tree
Showing 55 changed files with 762 additions and 805 deletions.
2 changes: 1 addition & 1 deletion client/service/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// ViewAccessTree service client
func ViewAccessTree(ctx context.Context, access users.AccessServiceClient) {
response, err := access.List(setMetadata(ctx), &users.Empty{})
response, err := access.List(setMetadata(ctx), &users.MyEmpty{})
if err != nil {
log.Fatalf("Error when calling grpc service: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ChangePassword(ctx context.Context, auth users.AuthServiceClient) {

// IsAuth service client
func IsAuth(ctx context.Context, auth users.AuthServiceClient) {
response, err := auth.IsAuth(setMetadata(ctx), &users.String{String_: "asal"})
response, err := auth.IsAuth(setMetadata(ctx), &users.MyString{String_: "asal"})

if err != nil {
log.Fatalf("Error when calling grpc service: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions client/service/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// ListFeature service client
func ListFeature(ctx context.Context, feature users.FeatureServiceClient) {
stream, err := feature.List(setMetadata(ctx), &users.Empty{})
stream, err := feature.List(setMetadata(ctx), &users.MyEmpty{})
if err != nil {
log.Fatalf("Error when calling grpc service: %s", err)
}
Expand All @@ -29,7 +29,7 @@ func ListFeature(ctx context.Context, feature users.FeatureServiceClient) {

// ListPackageFeature service client
func ListPackageFeature(ctx context.Context, packageFeature users.PackageFeatureServiceClient) {
stream, err := packageFeature.List(setMetadata(ctx), &users.Empty{})
stream, err := packageFeature.List(setMetadata(ctx), &users.MyEmpty{})
if err != nil {
log.Fatalf("Error when calling grpc service: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func ListUser(ctx context.Context, user users.UserServiceClient) {

// GetUserByToken service client
func GetUserByToken(ctx context.Context, user users.UserServiceClient) {
response, err := user.GetByToken(setMetadataToken(ctx), &users.Empty{})
response, err := user.GetByToken(setMetadataToken(ctx), &users.MyEmpty{})

if err != nil {
log.Fatalf("Error when calling grpc service: %s", err)
Expand Down
8 changes: 1 addition & 7 deletions internal/model/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"user-service/internal/pkg/app"
"user-service/pb/users"

"github.com/golang/protobuf/ptypes"

"github.com/google/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -126,11 +124,7 @@ func (u *Branch) Create(ctx context.Context, db *sql.DB, tx *sql.Tx) error {
return status.Errorf(codes.Internal, "Exec insert branch: %v", err)
}

u.Pb.CreatedAt, err = ptypes.TimestampProto(now)
if err != nil {
return status.Errorf(codes.Internal, "convert created by: %v", err)
}

u.Pb.CreatedAt = now.String()
u.Pb.UpdatedAt = u.Pb.CreatedAt

branchesRegion := BranchesRegion{RegionID: u.Pb.GetRegionId(), BranchID: u.Pb.GetId()}
Expand Down
12 changes: 2 additions & 10 deletions internal/model/employee.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"user-service/internal/pkg/app"
"user-service/pb/users"

"github.com/golang/protobuf/ptypes"
"github.com/google/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -129,11 +128,7 @@ func (u *Employee) Create(ctx context.Context, db *sql.DB) error {
return status.Errorf(codes.Internal, "Exec insert employee: %v", err)
}

u.Pb.CreatedAt, err = ptypes.TimestampProto(now)
if err != nil {
return status.Errorf(codes.Internal, "convert created by: %v", err)
}

u.Pb.CreatedAt = now.String()
u.Pb.UpdatedAt = u.Pb.CreatedAt

return nil
Expand Down Expand Up @@ -176,10 +171,7 @@ func (u *Employee) Update(ctx context.Context, db *sql.DB) error {
return status.Errorf(codes.Internal, "Exec update employee: %v", err)
}

u.Pb.UpdatedAt, err = ptypes.TimestampProto(now)
if err != nil {
return status.Errorf(codes.Internal, "convert updated by: %v", err)
}
u.Pb.UpdatedAt = now.String()

return nil
}
Expand Down
14 changes: 2 additions & 12 deletions internal/model/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"user-service/internal/pkg/app"
"user-service/pb/users"

"github.com/golang/protobuf/ptypes"

"github.com/google/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -69,11 +67,7 @@ func (u *Group) Create(ctx context.Context, db *sql.DB) error {
u.Pb.CreatedBy = ctx.Value(app.Ctx("userID")).(string)
u.Pb.UpdatedBy = ctx.Value(app.Ctx("userID")).(string)

u.Pb.CreatedAt, err = ptypes.TimestampProto(now)
if err != nil {
return err
}

u.Pb.CreatedAt = now.String()
u.Pb.UpdatedAt = u.Pb.CreatedAt

query := `
Expand Down Expand Up @@ -107,11 +101,7 @@ func (u *Group) Update(ctx context.Context, db *sql.DB) error {
var err error
now := time.Now().UTC()
u.Pb.UpdatedBy = ctx.Value(app.Ctx("userID")).(string)

u.Pb.UpdatedAt, err = ptypes.TimestampProto(now)
if err != nil {
return err
}
u.Pb.UpdatedAt = now.String()

query := `
UPDATE groups SET
Expand Down
7 changes: 1 addition & 6 deletions internal/model/request_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"time"
"user-service/pb/users"

"github.com/golang/protobuf/ptypes"

"github.com/google/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -55,10 +53,7 @@ func (u *RequestPassword) Get(ctx context.Context, db *sql.DB) error {
return status.Errorf(codes.Internal, "Query Raw: %v", err)
}

u.Pb.CreatedAt, err = ptypes.TimestampProto(createdAt)
if err != nil {
return status.Errorf(codes.Internal, "convert TimestampProto: %v", err)
}
u.Pb.CreatedAt = createdAt.String()

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Access struct {
}

// List access
func (u *Access) List(ctx context.Context, in *users.Empty) (*users.Access, error) {
func (u *Access) List(ctx context.Context, in *users.MyEmpty) (*users.Access, error) {
var accessModel model.Access

tx, err := u.Db.BeginTx(ctx, nil)
Expand Down
9 changes: 4 additions & 5 deletions internal/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -134,9 +133,9 @@ func (u *Auth) ResetPassword(ctx context.Context, in *users.ResetPasswordRequest
return &output, status.Error(codes.PermissionDenied, "token has been used")
}

createdAt, err := ptypes.Timestamp(requestPasswordModel.Pb.GetCreatedAt())
createdAt, err := time.Parse("2006-01-02T15:04:05.000Z", requestPasswordModel.Pb.GetCreatedAt())
if err != nil {
return &output, status.Errorf(codes.Internal, "ptypes timestamp: %v", err)
return &output, status.Errorf(codes.Internal, "parse timestamp: %v", err)
}

if time.Now().UTC().After(createdAt.Add(time.Hour * 2 * 24)) {
Expand Down Expand Up @@ -228,8 +227,8 @@ func (u *Auth) ChangePassword(ctx context.Context, in *users.ChangePasswordReque
}

// IsAuth service
func (u *Auth) IsAuth(ctx context.Context, in *users.String) (*users.Boolean, error) {
output := users.Boolean{Boolean: false}
func (u *Auth) IsAuth(ctx context.Context, in *users.MyString) (*users.MyBoolean, error) {
output := users.MyBoolean{Boolean: false}

ctx, err := getMetadata(ctx)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ func (u *Branch) View(ctx context.Context, in *users.Id) (*users.Branch, error)
}

// Delete branch
func (u *Branch) Delete(ctx context.Context, in *users.Id) (*users.Boolean, error) {
var output users.Boolean
func (u *Branch) Delete(ctx context.Context, in *users.Id) (*users.MyBoolean, error) {
var output users.MyBoolean
output.Boolean = false
var err error
var branchModel model.Branch
Expand Down
4 changes: 2 additions & 2 deletions internal/service/employee.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func (u *Employee) View(ctx context.Context, in *users.Id) (*users.Employee, err
}

// Delete Employee
func (u *Employee) Delete(ctx context.Context, in *users.Id) (*users.Boolean, error) {
var output users.Boolean
func (u *Employee) Delete(ctx context.Context, in *users.Id) (*users.MyBoolean, error) {
var output users.MyBoolean
output.Boolean = false
var err error
var employeeModel model.Employee
Expand Down
2 changes: 1 addition & 1 deletion internal/service/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Feature struct {
}

// List feature
func (u *Feature) List(in *users.Empty, stream users.FeatureService_ListServer) error {
func (u *Feature) List(in *users.MyEmpty, stream users.FeatureService_ListServer) error {
ctx := stream.Context()
rows, err := u.Db.QueryContext(ctx, `SELECT id, name from features`)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func (u *Group) View(ctx context.Context, in *users.Id) (*users.Group, error) {
}

// Delete Group
func (u *Group) Delete(ctx context.Context, in *users.Id) (*users.Boolean, error) {
var output users.Boolean
func (u *Group) Delete(ctx context.Context, in *users.Id) (*users.MyBoolean, error) {
var output users.MyBoolean
output.Boolean = false

var groupModel model.Group
Expand Down
2 changes: 1 addition & 1 deletion internal/service/package_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (u *PackageFeature) View(ctx context.Context, in *users.Id) (*users.Package
}

// List PackageFeature
func (u *PackageFeature) List(in *users.Empty, stream users.PackageFeatureService_ListServer) error {
func (u *PackageFeature) List(in *users.MyEmpty, stream users.PackageFeatureService_ListServer) error {
ctx := stream.Context()
rows, err := u.Db.QueryContext(ctx, `SELECT id, name from package_features`)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func (u *Region) View(ctx context.Context, in *users.Id) (*users.Region, error)
}

// Delete Region
func (u *Region) Delete(ctx context.Context, in *users.Id) (*users.Boolean, error) {
var output users.Boolean
func (u *Region) Delete(ctx context.Context, in *users.Id) (*users.MyBoolean, error) {
var output users.MyBoolean
output.Boolean = false

var err error
Expand Down
6 changes: 3 additions & 3 deletions internal/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ func (u *User) View(ctx context.Context, in *users.Id) (*users.User, error) {
}

// Delete func
func (u *User) Delete(ctx context.Context, in *users.Id) (*users.Boolean, error) {
var output users.Boolean
func (u *User) Delete(ctx context.Context, in *users.Id) (*users.MyBoolean, error) {
var output users.MyBoolean
output.Boolean = false
var err error
var userModel model.User
Expand Down Expand Up @@ -487,7 +487,7 @@ func (u *User) List(in *users.ListUserRequest, stream users.UserService_ListServ
}

// GetByToken func
func (u *User) GetByToken(ctx context.Context, in *users.Empty) (*users.User, error) {
func (u *User) GetByToken(ctx context.Context, in *users.MyEmpty) (*users.User, error) {
var output users.User
var err error
var userModel model.User
Expand Down
Loading

0 comments on commit e175435

Please sign in to comment.