Skip to content

Commit 26e216e

Browse files
committed
1/4 endpoint
1 parent 0c64d2a commit 26e216e

File tree

23 files changed

+813
-631
lines changed

23 files changed

+813
-631
lines changed

api/ozonmp/srv_verification_api/v1/srv_verification_api.proto

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package ozonmp.srv_verification_api.v1;
44

55
import "validate/validate.proto";
66
import "google/api/annotations.proto";
7+
import "google/protobuf/timestamp.proto";
8+
79

810
option go_package = "github.com/ozonmp/srv-verification-api/pkg/srv-verification-api;srv_verification_api";
911

@@ -36,6 +38,8 @@ service SrvVerificationApiService {
3638
message Verification {
3739
uint64 id = 1;
3840
string name = 2;
41+
google.protobuf.Timestamp created_at = 3;
42+
google.protobuf.Timestamp updated_at = 4;
3943
}
4044

4145
message DescribeVerificationV1Request {

cmd/grpc-server/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func main() {
5858
}
5959
defer db.Close()
6060

61-
*migration = false // todo: need to delete this line for homework-4
6261
if *migration {
6362
if err = goose.Up(db.DB, cfg.Database.Migrations); err != nil {
6463
log.Error().Err(err).Msg("Migration failed")

config.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ status:
3232
versionPath: /version
3333

3434
database:
35-
host: postgres
35+
host: localhost
3636
port: 5432
37-
user: docker
38-
password: docker
37+
user: postgres
38+
password: postgres
3939
name: srv_verification_api
4040
sslmode: disable
4141
migrations: migrations

go.mod

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ go 1.16
44

55
require (
66
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
7+
github.com/Masterminds/squirrel v1.5.1
78
github.com/gammazero/workerpool v1.1.2
89
github.com/golang/mock v1.4.4
910
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
1011
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1112
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0
1213
github.com/jackc/pgx/v4 v4.13.0
1314
github.com/jmoiron/sqlx v1.3.4
14-
github.com/lib/pq v1.10.3
15+
github.com/lib/pq v1.10.4
16+
github.com/mattn/go-sqlite3 v1.14.9 // indirect
1517
github.com/opentracing/opentracing-go v1.2.0
1618
github.com/ozonmp/srv-verification-api/pkg/srv-verification-api v0.0.0-00010101000000-000000000000
17-
github.com/pressly/goose/v3 v3.1.0
19+
github.com/pressly/goose/v3 v3.3.1
1820
github.com/prometheus/client_golang v1.11.0
1921
github.com/rs/zerolog v1.24.0
2022
github.com/stretchr/testify v1.7.0
2123
github.com/uber/jaeger-client-go v2.29.1+incompatible
2224
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
25+
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
2326
golang.org/x/tools v0.1.7 // indirect
2427
google.golang.org/grpc v1.41.0
28+
google.golang.org/protobuf v1.27.1
2529
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2630
)
2731

go.sum

+132-1
Large diffs are not rendered by default.

internal/api/api_create_verification.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/ozonmp/srv-verification-api/internal/model"
78
pb "github.com/ozonmp/srv-verification-api/pkg/srv-verification-api"
@@ -21,7 +22,7 @@ func (o *verificationAPI) CreateVerificationV1(
2122
return nil, status.Error(codes.InvalidArgument, err.Error())
2223
}
2324

24-
verification := model.Verification{Name: req.VerificationName}
25+
verification := model.Verification{Name: req.VerificationName, CreatedAt: time.Now(), UpdatedAt: time.Now()}
2526
if err := o.repo.AddVerification(ctx, &verification); err != nil {
2627
log.Error().Err(err).Msg("DescribeVerificationV1 -- failed")
2728

internal/api/api_list_verification.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package api
22

33
import (
44
"context"
5-
65
"github.com/ozonmp/srv-verification-api/internal/model"
76
pb "github.com/ozonmp/srv-verification-api/pkg/srv-verification-api"
87
"github.com/rs/zerolog/log"
98
"google.golang.org/grpc/codes"
109
"google.golang.org/grpc/status"
10+
"google.golang.org/protobuf/types/known/timestamppb"
1111
)
1212

1313
func (o *verificationAPI) ListVerificationV1(
@@ -45,5 +45,7 @@ func convertVerificationToPb(verification *model.Verification) *pb.Verification
4545
return &pb.Verification{
4646
Id: verification.ID,
4747
Name: verification.Name,
48+
CreatedAt: timestamppb.New(verification.CreatedAt),
49+
UpdatedAt: timestamppb.New(verification.UpdatedAt),
4850
}
4951
}

internal/model/verification.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package model
22

3+
import "time"
4+
35
type Verification struct {
46
ID uint64 `db:"id"`
57
Name string `db:"name"`
8+
CreatedAt time.Time `db:"created_at"`
9+
UpdatedAt time.Time `db:"updated_at"`
610
}
711

812
type EventType uint8

internal/repo/repo.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package repo
22

33
import (
44
"context"
5+
"database/sql"
56
"errors"
67

8+
sq "github.com/Masterminds/squirrel"
79
"github.com/jmoiron/sqlx"
810

911
"github.com/ozonmp/srv-verification-api/internal/model"
1012
)
1113

12-
var errNotImplementedMethod = errors.New("method is not implemented")
14+
var ErrNotFound = errors.New("no rows in database for the given query")
15+
var errInternalMethod = errors.New("internal error")
1316

1417
// Repo is DAO for Verification
1518
type Repo interface {
@@ -30,17 +33,33 @@ func NewRepo(db *sqlx.DB, batchSize uint) Repo {
3033
}
3134

3235
func (r *repo) DescribeVerification(ctx context.Context, verificationID uint64) (*model.Verification, error) {
33-
return nil, errNotImplementedMethod
36+
query, args, err := sq.Select("*").PlaceholderFormat(sq.Dollar).From("verification").Where(sq.Eq{"id": verificationID}).ToSql()
37+
38+
if err != nil{
39+
return nil, err
40+
}
41+
verification := model.Verification{}
42+
err = r.db.GetContext(ctx, &verification, query, args...)
43+
44+
switch err {
45+
case nil:
46+
return &verification, nil
47+
case sql.ErrNoRows:
48+
return nil, ErrNotFound
49+
default:
50+
return nil, errInternalMethod
51+
}
52+
3453
}
3554

3655
func (r *repo) AddVerification(ctx context.Context, verification *model.Verification) error {
37-
return errNotImplementedMethod
56+
return errInternalMethod
3857
}
3958

4059
func (r *repo) ListVerification(ctx context.Context) ([]*model.Verification, error) {
41-
return nil, errNotImplementedMethod
60+
return nil, errInternalMethod
4261
}
4362

4463
func (r *repo) RemoveVerification(ctx context.Context, verificationID uint64) (status bool, err error) {
45-
return false, errNotImplementedMethod
64+
return false, errInternalMethod
4665
}

migrations/00001_init_db.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- +goose Up
2+
CREATE TABLE verification (
3+
id BIGSERIAL PRIMARY KEY,
4+
name VARCHAR NOT NULL,
5+
created_at TIMESTAMP NOT NULL,
6+
updated_at TIMESTAMP NOT NULL,
7+
is_removed BOOLEAN NOT NULL DEFAULT FALSE
8+
);
9+
10+
-- +goose Down
11+
DROP TABLE verification;

migrations/20210820172707_init_db.sql

-8
This file was deleted.

0 commit comments

Comments
 (0)