Add gRPC server implementation and database integration for marketplace and products
This commit is contained in:
		
							
								
								
									
										46
									
								
								internal/marketplace/adapter_grpc.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								internal/marketplace/adapter_grpc.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
package marketplace
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/jackc/pgx/v5"
 | 
			
		||||
	"google.golang.org/grpc"
 | 
			
		||||
	"google.golang.org/grpc/codes"
 | 
			
		||||
	"google.golang.org/grpc/status"
 | 
			
		||||
	pb "sipro-mps/api/generated/v1/marketplace"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// AdapterGRPC implements the gRPC server for the Marketplace service.
 | 
			
		||||
type AdapterGRPC struct {
 | 
			
		||||
	pb.UnimplementedMarketplaceServiceServer
 | 
			
		||||
	repo Repository
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewAdapterGRPC(repo Repository) *AdapterGRPC {
 | 
			
		||||
	return &AdapterGRPC{
 | 
			
		||||
		repo: repo,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
func RegisterAdapterGRPC(server *grpc.Server) (*Repository, error) {
 | 
			
		||||
	conn, err := pgx.Connect(context.Background(), "postgresql://postgres:GjitkeYf%5Beq@/sipro?host=/run/postgresql")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	repo := NewDBRepository(conn)
 | 
			
		||||
	adapter := NewAdapterGRPC(repo)
 | 
			
		||||
	pb.RegisterMarketplaceServiceServer(server, adapter)
 | 
			
		||||
	return &repo, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *AdapterGRPC) GetMarketplaceById(ctx context.Context, r *pb.GetMarketplaceByIdRequest) (*pb.Marketplace, error) {
 | 
			
		||||
	mp, err := g.repo.GetMarketplaceByID(ctx, int(r.MarketplaceId))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, status.Errorf(codes.Internal, "failed to get marketplace by ID: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.Marketplace{
 | 
			
		||||
		Id:              uint64(uint32(mp.ID)),
 | 
			
		||||
		BaseMarketplace: uint32(int32(mp.BaseMarketplace)),
 | 
			
		||||
		AuthData:        mp.AuthData,
 | 
			
		||||
		WarehouseId:     mp.WarehouseID,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								internal/marketplace/db/db.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								internal/marketplace/db/db.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
// Code generated by sqlc. DO NOT EDIT.
 | 
			
		||||
// versions:
 | 
			
		||||
//   sqlc v1.28.0
 | 
			
		||||
 | 
			
		||||
package db
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
 | 
			
		||||
	"github.com/jackc/pgx/v5"
 | 
			
		||||
	"github.com/jackc/pgx/v5/pgconn"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DBTX interface {
 | 
			
		||||
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
 | 
			
		||||
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
 | 
			
		||||
	QueryRow(context.Context, string, ...interface{}) pgx.Row
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New(db DBTX) *Queries {
 | 
			
		||||
	return &Queries{db: db}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Queries struct {
 | 
			
		||||
	db DBTX
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
 | 
			
		||||
	return &Queries{
 | 
			
		||||
		db: tx,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								internal/marketplace/db/models.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								internal/marketplace/db/models.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
// Code generated by sqlc. DO NOT EDIT.
 | 
			
		||||
// versions:
 | 
			
		||||
//   sqlc v1.28.0
 | 
			
		||||
 | 
			
		||||
package db
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/jackc/pgx/v5/pgtype"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Marketplace struct {
 | 
			
		||||
	ID              int32
 | 
			
		||||
	BaseMarketplace int32
 | 
			
		||||
	Name            string
 | 
			
		||||
	AuthData        pgtype.Text
 | 
			
		||||
	WarehouseID     pgtype.Text
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								internal/marketplace/db/query.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								internal/marketplace/db/query.sql
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
-- name: GetMarketplaceByID :one
 | 
			
		||||
SELECT * FROM marketplaces
 | 
			
		||||
WHERE id = $1 LIMIT 1;
 | 
			
		||||
							
								
								
									
										28
									
								
								internal/marketplace/db/query.sql.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								internal/marketplace/db/query.sql.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
// Code generated by sqlc. DO NOT EDIT.
 | 
			
		||||
// versions:
 | 
			
		||||
//   sqlc v1.28.0
 | 
			
		||||
// source: query.sql
 | 
			
		||||
 | 
			
		||||
package db
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const getMarketplaceByID = `-- name: GetMarketplaceByID :one
 | 
			
		||||
SELECT id, base_marketplace, name, auth_data, warehouse_id FROM marketplaces
 | 
			
		||||
WHERE id = $1 LIMIT 1
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
func (q *Queries) GetMarketplaceByID(ctx context.Context, id int32) (Marketplace, error) {
 | 
			
		||||
	row := q.db.QueryRow(ctx, getMarketplaceByID, id)
 | 
			
		||||
	var i Marketplace
 | 
			
		||||
	err := row.Scan(
 | 
			
		||||
		&i.ID,
 | 
			
		||||
		&i.BaseMarketplace,
 | 
			
		||||
		&i.Name,
 | 
			
		||||
		&i.AuthData,
 | 
			
		||||
		&i.WarehouseID,
 | 
			
		||||
	)
 | 
			
		||||
	return i, err
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								internal/marketplace/db/schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								internal/marketplace/db/schema.sql
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
create table marketplaces
 | 
			
		||||
(
 | 
			
		||||
    id                           serial
 | 
			
		||||
        primary key,
 | 
			
		||||
    base_marketplace             integer                                        not null,
 | 
			
		||||
    name                         varchar                                        not null,
 | 
			
		||||
    auth_data                    varchar,
 | 
			
		||||
    warehouse_id                 varchar
 | 
			
		||||
);
 | 
			
		||||
							
								
								
									
										10
									
								
								internal/marketplace/db/sqlc.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								internal/marketplace/db/sqlc.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
version: 2
 | 
			
		||||
sql:
 | 
			
		||||
  - engine: "postgresql"
 | 
			
		||||
    queries: "query.sql"
 | 
			
		||||
    schema: "schema.sql"
 | 
			
		||||
    gen:
 | 
			
		||||
      go:
 | 
			
		||||
        package: "db"
 | 
			
		||||
        out: "."
 | 
			
		||||
        sql_package: "pgx/v5"
 | 
			
		||||
							
								
								
									
										8
									
								
								internal/marketplace/entities.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								internal/marketplace/entities.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package marketplace
 | 
			
		||||
 | 
			
		||||
type Marketplace struct {
 | 
			
		||||
	ID              int    `json:"id"`
 | 
			
		||||
	BaseMarketplace int    `json:"base_marketplace"`
 | 
			
		||||
	AuthData        string `json:"auth_data"`
 | 
			
		||||
	WarehouseID     string `json:"warehouse_id"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								internal/marketplace/repository.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								internal/marketplace/repository.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package marketplace
 | 
			
		||||
 | 
			
		||||
import "context"
 | 
			
		||||
 | 
			
		||||
type Repository interface {
 | 
			
		||||
	// GetMarketplaceByID retrieves a marketplace by its ID.
 | 
			
		||||
	GetMarketplaceByID(ctx context.Context, id int) (*Marketplace, error)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								internal/marketplace/repository_db.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								internal/marketplace/repository_db.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
package marketplace
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/jackc/pgx/v5"
 | 
			
		||||
	"sipro-mps/internal/marketplace/db"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type dbRepository struct {
 | 
			
		||||
	conn *pgx.Conn
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewDBRepository(conn *pgx.Conn) Repository {
 | 
			
		||||
	return &dbRepository{conn: conn}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *dbRepository) GetMarketplaceByID(ctx context.Context, id int) (*Marketplace, error) {
 | 
			
		||||
	queries := db.New(r.conn)
 | 
			
		||||
	marketplace, err := queries.GetMarketplaceByID(ctx, int32(id))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return &Marketplace{
 | 
			
		||||
		ID:              int(marketplace.ID),
 | 
			
		||||
		BaseMarketplace: int(marketplace.BaseMarketplace),
 | 
			
		||||
		AuthData:        marketplace.AuthData.String,
 | 
			
		||||
		WarehouseID:     marketplace.WarehouseID.String,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user