Skip to content

Commit

Permalink
Finder screen is filtering and fetching data.
Browse files Browse the repository at this point in the history
  • Loading branch information
grrlopes committed Aug 9, 2023
1 parent d9c374b commit da84ec0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 48 deletions.
48 changes: 12 additions & 36 deletions repositories/sqlite/sqliterepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ func (sql *SQLiteRepository) Migrate() error {
func (sql *SQLiteRepository) All(limit int) ([]entity.Commands, int, error) {
var commands []entity.Commands

// result, err := sql.db.Query("SELECT * FROM commands limit ?", limit)
result := sql.database.Limit(limit).Find(&commands)

if result.Error != nil {
return commands, limit, result.Error
}
// err = sql.db.QueryRow("SELECT COUNT(*) FROM commands").Scan(&count)

return commands, limit, nil
}
Expand Down Expand Up @@ -99,48 +97,26 @@ func (sql SQLiteRepository) InsertParsed(data string) (int64, error) {
}

func (sql *SQLiteRepository) Search(filter string, limit int, skip int) ([]entity.Commands, int, error) {
var count int

stmt, err := sql.db.Prepare("SELECT * FROM commands WHERE cmd LIKE ? limit ? offset ?")
if err != nil {
return []entity.Commands{}, count, err
}

result, err := stmt.Query("%"+filter+"%", limit, skip)
if err != nil {
return []entity.Commands{}, count, err
}

defer result.Close()

err = sql.db.QueryRow("SELECT COUNT(*) FROM commands WHERE cmd LIKE ?", "%"+filter+"%").Scan(&count)

var data []entity.Commands

for result.Next() {
var command entity.Commands
if err := result.Scan(
&command.ID,
&command.Cmd,
&command.Desc,
); err != nil {
return []entity.Commands{}, count, err
}
var (
count int
commands []entity.Commands
)

data = append(data, command)
result := sql.database.Limit(limit).Offset(skip).Where("cmd LIKE ?", "%"+filter+"%").Find(&commands)
if result.Error != nil {
return commands, count, result.Error
}

return data, count, nil
return commands, count, result.Error
}

func (sql *SQLiteRepository) SearchCount(filter string) (int, error) {
var (
count int64
// command entity.Commands
count int64
command entity.Commands
)

sql.db.QueryRow("SELECT COUNT(*) FROM command WHERE cmd LIKE ?", "%"+filter+"%").Scan(&count)
// sql.db1.Model(&command).Count(&count)
sql.database.Model(&command).Where("cmd LIKE ?", "%"+filter+"%").Count(&count)

return 64, nil
return int(count), nil
}
16 changes: 4 additions & 12 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
usecaseCount count.InputBoundary = count.NewCount(repository)
usecaseHistory fhistory.InputBoundary = fhistory.NewFHistory(frepository, repository)
usecaseHistoryTotal fhistorytotal.InputBoundary = fhistorytotal.NewFHistoryTotal(frepository, repository)
usecaseFinder finder.InputBoundary = finder.NewFinder(repository)
usecaseFinderCount findercount.InputBoundary = findercount.NewFinderCount(repository)
usecaseFinder finder.InputBoundary = finder.NewFinder(repositoryGorm)
usecaseFinderCount findercount.InputBoundary = findercount.NewFinderCount(repositoryGorm)
usecaseAll listall.InputBoundary = listall.NewListAll(repositoryGorm)
)

Expand Down Expand Up @@ -159,13 +159,7 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.Ready = true
}

if m.home.Finder.Focused() {
*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
cmds = append(cmds, cmd)
start, end := m.updatepagination()
m.home.Start = start
m.home.End = end
} else {
if !m.home.Finder.Focused() {
*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
cmds = append(cmds, cmd)
start, end := m.updatepagination()
Expand Down Expand Up @@ -214,11 +208,9 @@ func (m *ModelHome) GetDataView() string {
result []string
)

if m.home.Finder.Focused() {
if !m.home.Finder.Focused() {
m.home.Store, _ = usecasePager.Execute(m.home.Viewport.Height-2, m.home.Start)
m.home.PageTotal = len(m.home.Store)
} else {
m.home.Store, _ = usecasePager.Execute(m.home.Viewport.Height-2, m.home.Start)
}
m.home.Pagination.SetTotalPages(*m.home.Count)

Expand Down

0 comments on commit da84ec0

Please sign in to comment.