85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type GetNewsEventsListParams struct {
|
|
Limit int32
|
|
Offset int32
|
|
IsWithApproved string
|
|
}
|
|
|
|
type NewsEventRow struct {
|
|
ID int32 `json:"id"`
|
|
Title string `json:"title"`
|
|
Url string `json:"url"`
|
|
Source string `json:"source"`
|
|
Thumbnail string `json:"thumbnail"`
|
|
Description string `json:"description"`
|
|
IsDeleted bool `json:"is_deleted"`
|
|
SubmittedBy string `json:"submitted_by"`
|
|
ApprovedBy int32 `json:"approved_by"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) GetNewsEventsList(ctx context.Context, arg GetNewsEventsListParams) ([]NewsEventRow, error) {
|
|
getNewsEventsListQ := fmt.Sprintf(`
|
|
SELECT
|
|
n.id,
|
|
n.title,
|
|
n.url,
|
|
n.source,
|
|
COALESCE(n.thumbnail, '') as thumbnail,
|
|
COALESCE(n.description, '') as description,
|
|
n.is_deleted,
|
|
u.username as submitted_by,
|
|
COALESCE(n.approved_by, 0) as approved_by,
|
|
n.created_at,
|
|
n.updated_at
|
|
FROM news_events n
|
|
JOIN users u ON n.submitted_by = u.id
|
|
%s
|
|
ORDER BY n.created_at DESC
|
|
LIMIT $1
|
|
OFFSET $2
|
|
`, arg.IsWithApproved)
|
|
|
|
rows, err := q.db.Query(ctx, getNewsEventsListQ, arg.Limit, arg.Offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
items := []NewsEventRow{}
|
|
|
|
for rows.Next() {
|
|
var i NewsEventRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Title,
|
|
&i.Url,
|
|
&i.Source,
|
|
&i.Thumbnail,
|
|
&i.Description,
|
|
&i.IsDeleted,
|
|
&i.SubmittedBy,
|
|
&i.ApprovedBy,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
items = append(items, i)
|
|
}
|
|
rows.Close()
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|