Skip to content

Commit

Permalink
Merge pull request #297 from LerianStudio/refactor/MIDAZ-294
Browse files Browse the repository at this point in the history
Refactor/MIDAZ-294
  • Loading branch information
MartinezAvellan authored Nov 25, 2024
2 parents fae91af + 7f1c7e3 commit a24def6
Show file tree
Hide file tree
Showing 83 changed files with 1,078 additions and 1,253 deletions.
2 changes: 1 addition & 1 deletion components/ledger/.air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cmd = "go build -o ./cmd/tmp/main ./cmd/app"
# Binary file yields from `cmd`.
bin = "cmd/tmp/main"
# Customize binary, can setup environment variables when run your app.
full_bin = "APP_ENV=develop APP_USER=air ./cmd/tmp/main"
full_bin = "APP_ENV=local APP_USER=air ./cmd/tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
Expand Down
Empty file added components/ledger/pkg/.keep
Empty file.
55 changes: 55 additions & 0 deletions components/transaction/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format

# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "cmd/tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./cmd/tmp/main ./cmd/app"
# Binary file yields from `cmd`.
bin = "cmd/tmp/main"
# Customize binary, can setup environment variables when run your app.
full_bin = "APP_ENV=local APP_USER=air ./cmd/tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# Exclude specific regular expressions.
exclude_regex = ["_test\\.go"]
# Exclude unchanged files.
exclude_unchanged = true
# Follow symlink for directories
follow_symlink = true
# This log file places in your tmp_dir.
log = "air.log"
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop running old binary when build errors occur.
stop_on_error = true
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 500 # ms
# Add additional arguments when running binary (bin/full_bin). Will run './tmp/main hello world'.
args_bin = ["hello", "world"]

[log]
# Show log time
time = false

[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# Delete tmp directory on exit
clean_on_exit = true
2 changes: 1 addition & 1 deletion components/transaction/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# ENV_NAME=production

# APP
VERSION=v1.29.0
VERSION=1.21.0
APP_CONTEXT=/transaction/v1
SERVER_PORT=3002
SERVER_ADDRESS=:${SERVER_PORT}
Expand Down
5 changes: 3 additions & 2 deletions components/transaction/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ WORKDIR /transaction-app

COPY . .

RUN CGO_ENABLE=0 GOOS=linux go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o /app components/transaction/cmd/app/main.go

# Add make, install swag and generate docs
RUN apk add --no-cache make \
&& go install github.com/swaggo/swag/cmd/swag@latest \
&& make -C components/ledger generate-docs
&& make -C components/transaction generate-docs

RUN CGO_ENABLE=0 GOOS=linux go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o /app components/transaction/internal/main.go

FROM alpine:latest

Expand Down
8 changes: 2 additions & 6 deletions components/transaction/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ antlr:
rm -f antlr-$(ANTLR4_VERSION)-complete.jar

# COMMONS

wire:
@go generate ./...

run:
@go run internal/main.go .env
@go run cmd/app/main.go .env

test:
@go test -v ./...
Expand All @@ -71,7 +67,7 @@ tidy:
@go mod tidy

generate-docs:
@swag init -g ./main.go -d ./internal -o ./api --parseDependency --parseInternal
@swag init -g ../../../cmd/app/main.go -d ./internal/bootstrap/http -o ./api --parseDependency --parseInternal

# Docker Compose Commands
build:
Expand Down
44 changes: 44 additions & 0 deletions components/transaction/api/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Package api Code generated by swaggo/swag. DO NOT EDIT
package api

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Discord community",
"url": "https://discord.gg/DnhqKwkGv3"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "localhost:3002",
BasePath: "/",
Schemes: []string{},
Title: "Midaz Transaction API",
Description: "This is a swagger documentation for the Midaz Transaction API",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
20 changes: 20 additions & 0 deletions components/transaction/api/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"swagger": "2.0",
"info": {
"description": "This is a swagger documentation for the Midaz Transaction API",
"title": "Midaz Transaction API",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Discord community",
"url": "https://discord.gg/DnhqKwkGv3"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
},
"host": "localhost:3002",
"basePath": "/",
"paths": {}
}
15 changes: 15 additions & 0 deletions components/transaction/api/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
basePath: /
host: localhost:3002
info:
contact:
name: Discord community
url: https://discord.gg/DnhqKwkGv3
description: This is a swagger documentation for the Midaz Transaction API
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: http://swagger.io/terms/
title: Midaz Transaction API
version: "1.0"
paths: {}
swagger: "2.0"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/components/transaction/internal/gen"
"github.com/LerianStudio/midaz/components/transaction/internal/bootstrap"
)

// @title Midaz Transaction API
Expand All @@ -17,5 +17,5 @@ import (
// @BasePath /
func main() {
common.InitLocalEnvConfig()
gen.InitializeService().Run()
bootstrap.InitServers().Run()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metadata
package mongodb

import (
"database/sql/driver"
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@ package mongodb
import (
"context"
"errors"
cn "github.com/LerianStudio/midaz/common/constant"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"strings"
"time"

"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/constant"
"github.com/LerianStudio/midaz/common/mmongo"
m "github.com/LerianStudio/midaz/components/transaction/internal/domain/metadata"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Repository provides an interface for operations related on mongodb a metadata entities.
//
//go:generate mockgen --destination=metadata.mock.go --package=mongodb . Repository
type Repository interface {
Create(ctx context.Context, collection string, metadata *Metadata) error
FindList(ctx context.Context, collection string, filter any) ([]*Metadata, error)
FindByEntity(ctx context.Context, collection, id string) (*Metadata, error)
Update(ctx context.Context, collection, id string, metadata map[string]any) error
Delete(ctx context.Context, collection, id string) error
}

// MetadataMongoDBRepository is a MongoDD-specific implementation of the MetadataRepository.
type MetadataMongoDBRepository struct {
connection *mmongo.MongoConnection
Expand All @@ -36,7 +46,7 @@ func NewMetadataMongoDBRepository(mc *mmongo.MongoConnection) *MetadataMongoDBRe
}

// Create inserts a new metadata entity into mongodb.
func (mmr *MetadataMongoDBRepository) Create(ctx context.Context, collection string, metadata *m.Metadata) error {
func (mmr *MetadataMongoDBRepository) Create(ctx context.Context, collection string, metadata *Metadata) error {
tracer := common.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "mongodb.create_metadata")
Expand All @@ -52,7 +62,7 @@ func (mmr *MetadataMongoDBRepository) Create(ctx context.Context, collection str
logger := common.NewLoggerFromContext(ctx)

coll := db.Database(strings.ToLower(mmr.Database)).Collection(strings.ToLower(collection))
record := &m.MetadataMongoDBModel{}
record := &MetadataMongoDBModel{}

if err := record.FromEntity(metadata); err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to convert metadata to model", err)
Expand All @@ -77,7 +87,7 @@ func (mmr *MetadataMongoDBRepository) Create(ctx context.Context, collection str
}

// FindList retrieves metadata from the mongodb all metadata or a list by specify metadata.
func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection string, filter any) ([]*m.Metadata, error) {
func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection string, filter any) ([]*Metadata, error) {
tracer := common.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "mongodb.find_list")
Expand Down Expand Up @@ -105,10 +115,10 @@ func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection s

spanFind.End()

var meta []*m.MetadataMongoDBModel
var meta []*MetadataMongoDBModel

for cur.Next(ctx) {
var record m.MetadataMongoDBModel
var record MetadataMongoDBModel
if err := cur.Decode(&record); err != nil {
mopentelemetry.HandleSpanError(&spanFind, "Failed to decode metadata", err)

Expand All @@ -130,7 +140,7 @@ func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection s
return nil, err
}

metadata := make([]*m.Metadata, 0, len(meta))
metadata := make([]*Metadata, 0, len(meta))
for i := range meta {
metadata = append(metadata, meta[i].ToEntity())
}
Expand All @@ -139,7 +149,7 @@ func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection s
}

// FindByEntity retrieves a metadata from the mongodb using the provided entity_id.
func (mmr *MetadataMongoDBRepository) FindByEntity(ctx context.Context, collection, id string) (*m.Metadata, error) {
func (mmr *MetadataMongoDBRepository) FindByEntity(ctx context.Context, collection, id string) (*Metadata, error) {
tracer := common.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "mongodb.find_by_entity")
Expand All @@ -154,7 +164,7 @@ func (mmr *MetadataMongoDBRepository) FindByEntity(ctx context.Context, collecti

coll := db.Database(strings.ToLower(mmr.Database)).Collection(strings.ToLower(collection))

var record m.MetadataMongoDBModel
var record MetadataMongoDBModel

ctx, spanFindOne := tracer.Start(ctx, "mongodb.find_by_entity.find_one")

Expand Down Expand Up @@ -201,7 +211,7 @@ func (mmr *MetadataMongoDBRepository) Update(ctx context.Context, collection, id
mopentelemetry.HandleSpanError(&spanUpdate, "Failed to update metadata", err)

if errors.Is(err, mongo.ErrNoDocuments) {
return common.ValidateBusinessError(cn.ErrEntityNotFound, collection)
return common.ValidateBusinessError(constant.ErrEntityNotFound, collection)
}

return err
Expand Down
Loading

0 comments on commit a24def6

Please sign in to comment.