Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adicionando a jurisdicao #123

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions models/indexInformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type IndexInformation struct {
Year int `json:"ano,omitempty"`
Meta *Meta `json:"meta,omitempty"`
Score *Score `json:"score,omitempty"`
Type string `json:"jurisdicao,omitempty"`
}
2 changes: 2 additions & 0 deletions repo/database/dto/indexInformationDTO.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type IndexInformation struct {
Year int `gorm:"column:ano"`
Score
Meta
Type string `gorm:"column:jurisdicao"`
}

func (IndexInformation) TableName() string {
Expand Down Expand Up @@ -37,5 +38,6 @@ func (d *IndexInformation) ConvertToModel() *models.IndexInformation {
BaseRevenue: d.Meta.BaseRevenue,
OtherRecipes: d.Meta.OtherRecipes,
},
Type: d.Type,
}
}
7 changes: 3 additions & 4 deletions repo/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (p *PostgresDB) GetIndexInformation(name string, month, year int) (map[stri

// somente considerar os dados da coleta mais recente de cada órgão.
// lembrar que a gente guarda um histórico de coletas (revisões)
query := "coletas.atual = true"
query := "INNER JOIN orgaos ON coletas.id_orgao = orgaos.id AND coletas.atual = true"

// verificar se devemos considerar o ano como parâmetro e adicionar a query.
if year != 0 {
Expand All @@ -443,17 +443,16 @@ func (p *PostgresDB) GetIndexInformation(name string, month, year int) (map[stri
if porJurisdicao {
// Consultando e mapeando os índices e metadados por jurisdição.
// Para tal, precisamos realizar um join com a tabela de órgãos.
query = fmt.Sprintf("INNER JOIN orgaos ON coletas.id_orgao = orgaos.id AND %s AND orgaos.jurisdicao = ?", query)
query += " AND orgaos.jurisdicao = ?"
params = append(params, name)
d = p.db.Model(&dtoIndex).Joins(query, params...)
} else {
if name != "" {
// Consultando e mapeando os índices e metadados por id do órgão
query += " AND coletas.id_orgao = ?"
params = append(params, name)
}
d = p.db.Model(&dtoIndex).Where(query, params...)
}
d = p.db.Model(&dtoIndex).Select("coletas.*, orgaos.jurisdicao as jurisdicao").Joins(query, params...)
if err := d.Scan(&dtoIndex).Error; err != nil {
return nil, fmt.Errorf("error getting all indexes: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions repo/database/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,7 @@ func (indexInformation) testGetAllIndexInformation(t *testing.T) {
assert.Equal(t, len(agg), 2)
assert.Equal(t, len(agg["tjsp"]), 2)
assert.Equal(t, len(agg["tjba"]), 1)
assert.Equal(t, agg["tjsp"][0].Type, "Estadual")
truncateTables()
}

Expand Down