Skip to content

Commit

Permalink
schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Jul 8, 2024
1 parent bab19f1 commit cbf450c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
27 changes: 25 additions & 2 deletions models/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,40 @@ func (m *Schedule) Delete(id, userID int) error {
return nil
}

func (m *Schedule) WithQueryAll() []*Schedule {
return m.queryPrepare(config.App().QUERY["SCHEDULE_MAPS"])
}

func (m *Schedule) WithQuery(userID, offset, limit int, search string) []*Schedule {
schedules := []*Schedule{}

query := strings.TrimSuffix(config.App().QUERY["SCHEDULE_MAPS"], ";")

if userID == 0 {
return schedules
}
query += fmt.Sprintf(" AND user_id=%d", userID)
if search != "" {
query += fmt.Sprintf(` AND (timing ilike %s OR "group"->>'name' ilike %s OR request->>'url' ilike %s OR notification->>'title' ilike %s)`, search, search, search, search)
}
if limit > 0 {
query += fmt.Sprintf(" ORDER BY id DESC offset %d LIMIT %d;", offset, limit)
}

return m.queryPrepare(query)
}

func (m *Schedule) queryPrepare(query string) []*Schedule {
schedules := []*Schedule{}

// prepare schedules paginate
stmt, err := config.App().DB.Prepare(config.App().QUERY["SCHEDULE_MAPS"])
stmt, err := config.App().DB.Prepare(query)
if err != nil {
return schedules
}

// query
rows, err := stmt.Query(userID, "%"+search+"%", offset, limit)
rows, err := stmt.Query()
if err != nil {
return schedules
}
Expand Down
5 changes: 1 addition & 4 deletions query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,4 @@ WITH schedule_lists AS (
LEFT JOIN webhooks w on w.schedule_id=s.id
GROUP BY s.id, u.id, g.id, r.id, n.id
)
SELECT * FROM schedule_lists
WHERE user_id=$1 AND deleted_at isnull
AND (timing ilike $2 OR "group"->>'name' ilike $2 OR request->>'url' ilike $2 OR notification->>'title' ilike $2)
ORDER BY id DESC offset $3 LIMIT $4;
SELECT * FROM schedule_lists WHERE deleted_at isnull;

0 comments on commit cbf450c

Please sign in to comment.