Skip to content

Commit

Permalink
adicionando a jurisdicao
Browse files Browse the repository at this point in the history
  • Loading branch information
joellensilva committed Nov 2, 2023
1 parent 4a30803 commit c72db4f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
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

0 comments on commit c72db4f

Please sign in to comment.