Skip to content

Commit

Permalink
aula 05 - integração do kong com todos os sistemas
Browse files Browse the repository at this point in the history
  • Loading branch information
argentinaluiz committed Jun 21, 2024
1 parent 46fe4f5 commit 0a883bd
Show file tree
Hide file tree
Showing 54 changed files with 1,192 additions and 810 deletions.
5 changes: 5 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include:
- golang/docker-compose.yaml
- nestjs-partners-api/docker-compose.yaml
- nextjs-frontend/docker-compose.yaml
- kong-api-gateway/docker-compose.with-dbless.yaml
5 changes: 5 additions & 0 deletions golang/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM golang:1.22.3-alpine3.20

WORKDIR /app

CMD tail -f /dev/null
6 changes: 3 additions & 3 deletions golang/cmd/events/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// @BasePath /
func main() {
// Configuração do banco de dados
db, err := sql.Open("mysql", "test_user:test_password@tcp(localhost:3306)/test_db")
db, err := sql.Open("mysql", "test_user:test_password@tcp(golang-mysql:3306)/test_db")
if err != nil {
log.Fatal(err)
}
Expand All @@ -41,8 +41,8 @@ func main() {

// URLs base específicas para cada parceiro
partnerBaseURLs := map[int]string{
1: "http://localhost:9000/api1",
2: "http://localhost:9000/api2",
1: "http://host.docker.internal:8000/partner1",
2: "http://host.docker.internal:8000/partner2",
}

listEventsUseCase := usecase.NewListEventsUseCase(eventRepo)
Expand Down
36 changes: 29 additions & 7 deletions golang/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
version: '3.8'

services:
mysql:
image: mysql:8.0
container_name: test-mysql

golang:
build: .
ports:
- "8080:8080"
volumes:
- .:/app
extra_hosts:
- "host.docker.internal:host-gateway"

golang-mysql:
image: mysql:8.0.30-debian
#container_name: test-mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
ports:
- "3306:3306"
# ports:
# - "3307:3306"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 3
volumes:
- ./mysql-init:/docker-entrypoint-initdb.d


# C:\Windows\system32\drivers\etc\hosts (bloco de notas em modo administrador)

# 127.0.0.1 host.docker.internal

## Linux ou Mac (Docker Desktop)

# /etc/hosts

# 127.0.0.1 host.docker.internal

# host.docker.internal:8000/partner1
# host.docker.internal:8000/partner2
4 changes: 2 additions & 2 deletions golang/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ const docTemplate = `{
"type": "string"
}
},
"ticket_type": {
"ticket_kind": {
"type": "string"
}
}
Expand Down Expand Up @@ -536,7 +536,7 @@ const docTemplate = `{
"spot_id": {
"type": "string"
},
"ticket_type": {
"ticket_kind": {
"type": "string"
}
}
Expand Down
4 changes: 2 additions & 2 deletions golang/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@
"type": "string"
}
},
"ticket_type": {
"ticket_kind": {
"type": "string"
}
}
Expand Down Expand Up @@ -530,7 +530,7 @@
"spot_id": {
"type": "string"
},
"ticket_type": {
"ticket_kind": {
"type": "string"
}
}
Expand Down
4 changes: 2 additions & 2 deletions golang/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ definitions:
items:
type: string
type: array
ticket_type:
ticket_kind:
type: string
type: object
usecase.BuyTicketsOutputDTO:
Expand Down Expand Up @@ -166,7 +166,7 @@ definitions:
type: number
spot_id:
type: string
ticket_type:
ticket_kind:
type: string
type: object
host: localhost:8080
Expand Down
30 changes: 15 additions & 15 deletions golang/internal/events/domain/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@ import (

// Errors
var (
ErrInvalidTicketType = errors.New("invalid ticket type")
ErrInvalidTicketKind = errors.New("invalid ticket kind")
)

// TicketType represents the type of a ticket.
type TicketType string
// TicketKind represents the kind of a ticket.
type TicketKind string

const (
TicketTypeHalf TicketType = "half" // Half-price ticket
TicketTypeFull TicketType = "full" // Full-price ticket
TicketKindHalf TicketKind = "half" // Half-price ticket
TicketKindFull TicketKind = "full" // Full-price ticket
)

// IsValidTicketType checks if a ticket type is valid.
func IsValidTicketType(ticketType TicketType) bool {
return ticketType == TicketTypeHalf || ticketType == TicketTypeFull
// IsValidTicketKind checks if a ticket kind is valid.
func IsValidTicketKind(ticketKind TicketKind) bool {
return ticketKind == TicketKindHalf || ticketKind == TicketKindFull
}

// Ticket represents a ticket for an event.
type Ticket struct {
ID string
EventID string
Spot *Spot
TicketType TicketType
TicketKind TicketKind
Price float64
}

// NewTicket creates a new ticket with the given parameters.
func NewTicket(event *Event, spot *Spot, ticketType TicketType) (*Ticket, error) {
if !IsValidTicketType(ticketType) {
return nil, ErrInvalidTicketType
func NewTicket(event *Event, spot *Spot, ticketKind TicketKind) (*Ticket, error) {
if !IsValidTicketKind(ticketKind) {
return nil, ErrInvalidTicketKind
}

ticket := &Ticket{
ID: uuid.New().String(),
EventID: event.ID,
Spot: spot,
TicketType: ticketType,
TicketKind: ticketKind,
Price: event.Price,
}
ticket.CalculatePrice()
Expand All @@ -53,9 +53,9 @@ func NewTicket(event *Event, spot *Spot, ticketType TicketType) (*Ticket, error)
return ticket, nil
}

// CalculatePrice calculates the price based on the ticket type.
// CalculatePrice calculates the price based on the ticket kind.
func (t *Ticket) CalculatePrice() {
if t.TicketType == TicketTypeHalf {
if t.TicketKind == TicketKindHalf {
t.Price /= 2
}
}
Expand Down
8 changes: 4 additions & 4 deletions golang/internal/events/domain/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
func TestNewTicket(t *testing.T) {
event, _ := NewEvent("Concert", "Stadium", "Music Inc.", RatingLivre, time.Now().Add(24*time.Hour), 100, 50.0, "http://x.jpg", 1)
spot, _ := NewSpot(event, "A1")
ticket, err := NewTicket(event, spot, TicketTypeFull)
ticket, err := NewTicket(event, spot, TicketKindFull)
assert.Nil(t, err)
assert.NotNil(t, ticket)
assert.Equal(t, TicketTypeFull, ticket.TicketType)
assert.Equal(t, TicketKindFull, ticket.TicketKind)
assert.Equal(t, 50.0, ticket.Price)
assert.Equal(t, event.ID, ticket.EventID)
assert.Equal(t, spot.ID, ticket.Spot.ID)
Expand All @@ -23,10 +23,10 @@ func TestNewTicket(t *testing.T) {
func TestNewTicket_HalfPrice(t *testing.T) {
event, _ := NewEvent("Concert", "Stadium", "Music Inc.", RatingLivre, time.Now().Add(24*time.Hour), 100, 50.0, "http://x.jpg", 1)
spot, _ := NewSpot(event, "A1")
ticket, err := NewTicket(event, spot, TicketTypeHalf)
ticket, err := NewTicket(event, spot, TicketKindHalf)
assert.Nil(t, err)
assert.NotNil(t, ticket)
assert.Equal(t, TicketTypeHalf, ticket.TicketType)
assert.Equal(t, TicketKindHalf, ticket.TicketKind)
assert.Equal(t, 25.0, ticket.Price)
}

Expand Down
36 changes: 18 additions & 18 deletions golang/internal/events/infra/repository/event_repository_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r *mysqlEventRepository) ListEvents() ([]domain.Event, error) {
SELECT
e.id, e.name, e.location, e.organization, e.rating, e.date, e.image_url, e.capacity, e.price, e.partner_id,
s.id, s.event_id, s.name, s.status, s.ticket_id,
t.id, t.event_id, t.spot_id, t.ticket_type, t.price
t.id, t.event_id, t.spot_id, t.ticket_kind, t.price
FROM events e
LEFT JOIN spots s ON e.id = s.event_id
LEFT JOIN tickets t ON s.id = t.spot_id
Expand All @@ -38,7 +38,7 @@ func (r *mysqlEventRepository) ListEvents() ([]domain.Event, error) {
eventMap := make(map[string]*domain.Event)
spotMap := make(map[string]*domain.Spot)
for rows.Next() {
var eventID, eventName, eventLocation, eventOrganization, eventRating, eventImageURL, spotID, spotEventID, spotName, spotStatus, spotTicketID, ticketID, ticketEventID, ticketSpotID, ticketType sql.NullString
var eventID, eventName, eventLocation, eventOrganization, eventRating, eventImageURL, spotID, spotEventID, spotName, spotStatus, spotTicketID, ticketID, ticketEventID, ticketSpotID, ticketKind sql.NullString
var eventDate sql.NullString
var eventCapacity int
var eventPrice, ticketPrice sql.NullFloat64
Expand All @@ -47,7 +47,7 @@ func (r *mysqlEventRepository) ListEvents() ([]domain.Event, error) {
err := rows.Scan(
&eventID, &eventName, &eventLocation, &eventOrganization, &eventRating, &eventDate, &eventImageURL, &eventCapacity, &eventPrice, &partnerID,
&spotID, &spotEventID, &spotName, &spotStatus, &spotTicketID,
&ticketID, &ticketEventID, &ticketSpotID, &ticketType, &ticketPrice,
&ticketID, &ticketEventID, &ticketSpotID, &ticketKind, &ticketPrice,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -99,7 +99,7 @@ func (r *mysqlEventRepository) ListEvents() ([]domain.Event, error) {
ID: ticketID.String,
EventID: ticketEventID.String,
Spot: spot,
TicketType: domain.TicketType(ticketType.String),
TicketKind: domain.TicketKind(ticketKind.String),
Price: ticketPrice.Float64,
}
event.Tickets = append(event.Tickets, ticket)
Expand All @@ -125,7 +125,7 @@ func (r *mysqlEventRepository) FindEventByID(eventID string) (*domain.Event, err
SELECT
e.id, e.name, e.location, e.organization, e.rating, e.date, e.image_url, e.capacity, e.price, e.partner_id,
s.id, s.event_id, s.name, s.status, s.ticket_id,
t.id, t.event_id, t.spot_id, t.ticket_type, t.price
t.id, t.event_id, t.spot_id, t.ticket_kind, t.price
FROM events e
LEFT JOIN spots s ON e.id = s.event_id
LEFT JOIN tickets t ON s.id = t.spot_id
Expand All @@ -139,7 +139,7 @@ func (r *mysqlEventRepository) FindEventByID(eventID string) (*domain.Event, err

var event *domain.Event
for rows.Next() {
var eventIDStr, eventName, eventLocation, eventOrganization, eventRating, eventImageURL, spotID, spotEventID, spotName, spotStatus, spotTicketID, ticketID, ticketEventID, ticketSpotID, ticketType sql.NullString
var eventIDStr, eventName, eventLocation, eventOrganization, eventRating, eventImageURL, spotID, spotEventID, spotName, spotStatus, spotTicketID, ticketID, ticketEventID, ticketSpotID, ticketKind sql.NullString
var eventDate sql.NullString
var eventCapacity int
var eventPrice, ticketPrice sql.NullFloat64
Expand All @@ -148,7 +148,7 @@ func (r *mysqlEventRepository) FindEventByID(eventID string) (*domain.Event, err
err := rows.Scan(
&eventIDStr, &eventName, &eventLocation, &eventOrganization, &eventRating, &eventDate, &eventImageURL, &eventCapacity, &eventPrice, &partnerID,
&spotID, &spotEventID, &spotName, &spotStatus, &spotTicketID,
&ticketID, &ticketEventID, &ticketSpotID, &ticketType, &ticketPrice,
&ticketID, &ticketEventID, &ticketSpotID, &ticketKind, &ticketPrice,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *mysqlEventRepository) FindEventByID(eventID string) (*domain.Event, err
ID: ticketID.String,
EventID: ticketEventID.String,
Spot: &spot,
TicketType: domain.TicketType(ticketType.String),
TicketKind: domain.TicketKind(ticketKind.String),
Price: ticketPrice.Float64,
}
event.Tickets = append(event.Tickets, ticket)
Expand Down Expand Up @@ -231,7 +231,7 @@ func (r *mysqlEventRepository) FindSpotByID(spotID string) (*domain.Spot, error)
query := `
SELECT
s.id, s.event_id, s.name, s.status, s.ticket_id,
t.id, t.event_id, t.spot_id, t.ticket_type, t.price
t.id, t.event_id, t.spot_id, t.ticket_kind, t.price
FROM spots s
LEFT JOIN tickets t ON s.id = t.spot_id
WHERE s.id = ?
Expand All @@ -240,12 +240,12 @@ func (r *mysqlEventRepository) FindSpotByID(spotID string) (*domain.Spot, error)

var spot domain.Spot
var ticket domain.Ticket
var ticketID, ticketEventID, ticketSpotID, ticketType sql.NullString
var ticketID, ticketEventID, ticketSpotID, ticketKind sql.NullString
var ticketPrice sql.NullFloat64

err := row.Scan(
&spot.ID, &spot.EventID, &spot.Name, &spot.Status, &spot.TicketID,
&ticketID, &ticketEventID, &ticketSpotID, &ticketType, &ticketPrice,
&ticketID, &ticketEventID, &ticketSpotID, &ticketKind, &ticketPrice,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand All @@ -258,7 +258,7 @@ func (r *mysqlEventRepository) FindSpotByID(spotID string) (*domain.Spot, error)
ticket.ID = ticketID.String
ticket.EventID = ticketEventID.String
ticket.Spot = &spot
ticket.TicketType = domain.TicketType(ticketType.String)
ticket.TicketKind = domain.TicketKind(ticketKind.String)
ticket.Price = ticketPrice.Float64
spot.TicketID = ticket.ID
}
Expand All @@ -279,10 +279,10 @@ func (r *mysqlEventRepository) CreateSpot(spot *domain.Spot) error {
// CreateTicket inserts a new ticket into the database.
func (r *mysqlEventRepository) CreateTicket(ticket *domain.Ticket) error {
query := `
INSERT INTO tickets (id, event_id, spot_id, ticket_type, price)
INSERT INTO tickets (id, event_id, spot_id, ticket_kind, price)
VALUES (?, ?, ?, ?, ?)
`
_, err := r.db.Exec(query, ticket.ID, ticket.EventID, ticket.Spot.ID, ticket.TicketType, ticket.Price)
_, err := r.db.Exec(query, ticket.ID, ticket.EventID, ticket.Spot.ID, ticket.TicketKind, ticket.Price)
return err
}

Expand Down Expand Up @@ -330,7 +330,7 @@ func (r *mysqlEventRepository) FindSpotByName(eventID, name string) (*domain.Spo
query := `
SELECT
s.id, s.event_id, s.name, s.status, s.ticket_id,
t.id, t.event_id, t.spot_id, t.ticket_type, t.price
t.id, t.event_id, t.spot_id, t.ticket_kind, t.price
FROM spots s
LEFT JOIN tickets t ON s.id = t.spot_id
WHERE s.event_id = ? AND s.name = ?
Expand All @@ -339,12 +339,12 @@ func (r *mysqlEventRepository) FindSpotByName(eventID, name string) (*domain.Spo

var spot domain.Spot
var ticket domain.Ticket
var ticketID, ticketEventID, ticketSpotID, ticketType sql.NullString
var ticketID, ticketEventID, ticketSpotID, ticketKind sql.NullString
var ticketPrice sql.NullFloat64

err := row.Scan(
&spot.ID, &spot.EventID, &spot.Name, &spot.Status, &spot.TicketID,
&ticketID, &ticketEventID, &ticketSpotID, &ticketType, &ticketPrice,
&ticketID, &ticketEventID, &ticketSpotID, &ticketKind, &ticketPrice,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand All @@ -357,7 +357,7 @@ func (r *mysqlEventRepository) FindSpotByName(eventID, name string) (*domain.Spo
ticket.ID = ticketID.String
ticket.EventID = ticketEventID.String
ticket.Spot = &spot
ticket.TicketType = domain.TicketType(ticketType.String)
ticket.TicketKind = domain.TicketKind(ticketKind.String)
ticket.Price = ticketPrice.Float64
spot.TicketID = ticket.ID
}
Expand Down
Loading

0 comments on commit 0a883bd

Please sign in to comment.