Skip to content

Commit

Permalink
Fix records querying by json query
Browse files Browse the repository at this point in the history
  • Loading branch information
pijng committed Mar 3, 2024
1 parent 6d65446 commit ad109a4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 53 deletions.
6 changes: 0 additions & 6 deletions internal/api/server/controllers/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,6 @@ func GetRecordsByQuery(w http.ResponseWriter, r *http.Request) {
return
}

// count, err := recordUseCase.GetRecordsCountByQuery(recordsToGet, from, to)
// if err != nil {
// response.Return(w, false, http.StatusBadRequest, err, nil, response.Meta{})
// return
// }

pages := int(math.Ceil(float64(count) / float64(limit)))

response.Return(w, true, http.StatusOK, nil, records, response.Meta{Page: page, Count: count, Pages: pages})
Expand Down
2 changes: 1 addition & 1 deletion internal/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.3.18
version: 1.3.19
externalDocs:
description: Find out more about spec
url: ''
Expand Down
49 changes: 8 additions & 41 deletions internal/storage/sqlite_adapter/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@ func (s *RecordStorage) GetRecordsByQuery(record entities.Record, from *time.Tim
queryParams = append(queryParams, qrx.Contains(record.Level))
}

queryBuilder.WriteString(fmt.Sprintf(" AND %s", qrx.MapLike(record.Query)))
queryBuilder.WriteString(fmt.Sprintf(" AND created_at BETWEEN %s", qrx.Between(from, to)))
if len(record.Query) != 0 {
queryBuilder.WriteString(fmt.Sprintf(" AND %s", qrx.MapLike(record.Query)))
}

if from != nil || to != nil {
queryBuilder.WriteString(fmt.Sprintf(" AND created_at BETWEEN %s", qrx.Between(from, to)))
}

countBuilder := queryBuilder
countParams := queryParams

queryBuilder.WriteString(" ORDER BY id DESC LIMIT ? OFFSET ?")
fmt.Println(queryBuilder.String())
queryParams = append(queryParams, limit, offset)

query := fmt.Sprintf(`
Expand Down Expand Up @@ -199,45 +205,6 @@ func (s *RecordStorage) GetRecordsByGroupHash(schemaName string, groupHash strin
return lr, nil
}

func (s *RecordStorage) GetRecordsCountByQuery(record entities.Record, from *time.Time, to *time.Time) (int, error) {
var queryBuilder strings.Builder

queryBuilder.WriteString("SELECT COUNT(*) FROM records WHERE (schema_id = ? OR schema_name = ?)")
queryParams := []interface{}{record.SchemaID, record.SchemaName}

if record.Text != "" {
queryBuilder.WriteString(" AND text LIKE ?")
queryParams = append(queryParams, qrx.Contains(record.Text))
}
if record.Kind != "" {
queryBuilder.WriteString(" AND kind LIKE ?")
queryParams = append(queryParams, qrx.Contains(record.Kind))
}
if record.Level != "" {
queryBuilder.WriteString(" AND level LIKE ?")
queryParams = append(queryParams, qrx.Contains(record.Level))
}

queryBuilder.WriteString(fmt.Sprintf(" AND %s", qrx.MapLike(record.Query)))
queryBuilder.WriteString(fmt.Sprintf(" AND created_at BETWEEN %s;", qrx.Between(from, to)))

stmt, err := s.db.PrepareContext(s.ctx, queryBuilder.String())
if err != nil {
return 0, fmt.Errorf("failed preparing statement: %w", err)
}
defer stmt.Close()

row := stmt.QueryRowContext(s.ctx, queryParams...)

var count int
err = row.Scan(&count)
if err != nil {
return 0, err
}

return count, nil
}

func (s *RecordStorage) GetAllRecordsCount() (int, error) {
query := "SELECT COUNT(*) FROM records;"
stmt, err := s.db.PrepareContext(s.ctx, query)
Expand Down
1 change: 0 additions & 1 deletion internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ type RecordStorage interface {
GetRecordByID(id int) (*entities.Record, error)
GetRecordsByGroupHash(schemaName string, groupHash string) ([]*entities.Record, error)
GetRecordsByQuery(record entities.Record, from *time.Time, to *time.Time, limit int, offset int) ([]*entities.Record, int, error)
GetRecordsCountByQuery(record entities.Record, from *time.Time, to *time.Time) (int, error)
}

func NewRecordStorage(ctx context.Context, storageType string) RecordStorage {
Expand Down
4 changes: 0 additions & 4 deletions internal/usecases/record_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ func (uc *RecordUseCase) GetRecordsByQuery(record entities.Record, from *time.Ti
return uc.recordStorage.GetRecordsByQuery(record, from, to, limit, offset)
}

func (uc *RecordUseCase) GetRecordsCountByQuery(record entities.Record, from *time.Time, to *time.Time) (int, error) {
return uc.recordStorage.GetRecordsCountByQuery(record, from, to)
}

func (uc *RecordUseCase) GetRecordsByGroupHash(schemaName string, groupHash string) ([]*entities.Record, error) {
return uc.recordStorage.GetRecordsByGroupHash(schemaName, groupHash)
}

0 comments on commit ad109a4

Please sign in to comment.