diff --git a/models/monthlyInfo.go b/models/monthlyInfo.go index e1e42ff..b496f94 100644 --- a/models/monthlyInfo.go +++ b/models/monthlyInfo.go @@ -38,6 +38,7 @@ type AgencyMonthlyInfo struct { Duration float64 `json:"duration,omitempty"` // Crawling duration (seconds) ManualCollection bool `json:"coleta_manual,omitempty"` // If the data was collected manually Inconsistent bool `json:"inconsistent"` // If the data is inconsistent + Notices string `json:"notices"` // If the data has notices } type Meta struct { diff --git a/repo/database/database_mock.go b/repo/database/database_mock.go index 1d99a9c..b9d36ad 100644 --- a/repo/database/database_mock.go +++ b/repo/database/database_mock.go @@ -259,6 +259,21 @@ func (mr *MockInterfaceMockRecorder) GetMonthlyInfo(agencies, year interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMonthlyInfo", reflect.TypeOf((*MockInterface)(nil).GetMonthlyInfo), agencies, year) } +// GetNotices mocks base method. +func (m *MockInterface) GetNotices(agency string, year, month int) ([]*string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNotices", agency, year, month) + ret0, _ := ret[0].([]*string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetNotices indicates an expected call of GetNotices. +func (mr *MockInterfaceMockRecorder) GetNotices(agency, year, month interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNotices", reflect.TypeOf((*MockInterface)(nil).GetNotices), agency, year, month) +} + // GetNumberOfMonthsCollected mocks base method. func (m *MockInterface) GetNumberOfMonthsCollected() (int, error) { m.ctrl.T.Helper() diff --git a/repo/database/interface.go b/repo/database/interface.go index 011ee29..5b991cb 100644 --- a/repo/database/interface.go +++ b/repo/database/interface.go @@ -33,4 +33,5 @@ type Interface interface { GetPaychecks(agency models.Agency, year int) ([]models.Paycheck, error) GetPaycheckItems(agency models.Agency, year int) ([]models.PaycheckItem, error) GetAveragePerCapita(agency string, year int) (*models.PerCapitaData, error) + GetNotices(agency string, year int, month int) ([]*string, error) } diff --git a/repo/database/postgres.go b/repo/database/postgres.go index 8664923..367498e 100644 --- a/repo/database/postgres.go +++ b/repo/database/postgres.go @@ -582,3 +582,32 @@ func (p *PostgresDB) GetAveragePerCapita(agency string, ano int) (*models.PerCap avg := dtoAvg.ConvertToModel() return avg, nil } + +func (p *PostgresDB) GetNotices(agency string, year int, month int) ([]*string, error) { + var notices []*string + params := []interface{}{} + + query := "atual = true AND avisos IS NOT NULL AND id_orgao = ?" + + if agency != "" { + params = append(params, agency) + } else { + return nil, fmt.Errorf("error agency cannot be empty") + } + + if year != 0 { + query = query + " AND ano = ?" + params = append(params, year) + if month != 0 { + query = query + " AND mes = ?" + params = append(params, month) + } + } + + result := p.db.Model(&dto.AgencyMonthlyInfoDTO{}).Distinct("avisos").Where(query, params...) + if err := result.Find(¬ices).Error; err != nil { + return nil, fmt.Errorf("error getting notices: %w", err) + } + + return notices, nil +}