fix: DB pagination
This commit is contained in:
@@ -25,8 +25,7 @@ func NewAlbumRepository(ctx context.Context, o orm.Ormer) model.AlbumRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
||||||
sel := r.selectAlbum(options...)
|
return r.count(Select(), options...)
|
||||||
return r.count(sel, options...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *albumRepository) Exists(id string) (bool, error) {
|
func (r *albumRepository) Exists(id string) (bool, error) {
|
||||||
|
|||||||
@@ -34,8 +34,7 @@ func (r *artistRepository) selectArtist(options ...model.QueryOptions) SelectBui
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *artistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
func (r *artistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
||||||
sel := r.selectArtist(options...)
|
return r.count(Select(), options...)
|
||||||
return r.count(sel, options...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *artistRepository) Exists(id string) (bool, error) {
|
func (r *artistRepository) Exists(id string) (bool, error) {
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ func (r *sqlRepository) newSelectWithAnnotation(itemType, idField string, option
|
|||||||
func (r *sqlRepository) newSelect(options ...model.QueryOptions) SelectBuilder {
|
func (r *sqlRepository) newSelect(options ...model.QueryOptions) SelectBuilder {
|
||||||
sq := Select().From(r.tableName)
|
sq := Select().From(r.tableName)
|
||||||
sq = r.applyOptions(sq, options...)
|
sq = r.applyOptions(sq, options...)
|
||||||
|
sq = r.applyFilters(sq, options...)
|
||||||
return sq
|
return sq
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ func (r *sqlRepository) applyOptions(sq SelectBuilder, options ...model.QueryOpt
|
|||||||
sq = sq.Limit(uint64(options[0].Max))
|
sq = sq.Limit(uint64(options[0].Max))
|
||||||
}
|
}
|
||||||
if options[0].Offset > 0 {
|
if options[0].Offset > 0 {
|
||||||
sq = sq.Offset(uint64(options[0].Max))
|
sq = sq.Offset(uint64(options[0].Offset))
|
||||||
}
|
}
|
||||||
if options[0].Sort != "" {
|
if options[0].Sort != "" {
|
||||||
if options[0].Order == "desc" {
|
if options[0].Order == "desc" {
|
||||||
@@ -61,9 +62,13 @@ func (r *sqlRepository) applyOptions(sq SelectBuilder, options ...model.QueryOpt
|
|||||||
sq = sq.OrderBy(toSnakeCase(options[0].Sort))
|
sq = sq.OrderBy(toSnakeCase(options[0].Sort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if options[0].Filters != nil {
|
|
||||||
sq = sq.Where(options[0].Filters)
|
|
||||||
}
|
}
|
||||||
|
return sq
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *sqlRepository) applyFilters(sq SelectBuilder, options ...model.QueryOptions) SelectBuilder {
|
||||||
|
if len(options) > 0 && options[0].Filters != nil {
|
||||||
|
sq = sq.Where(options[0].Filters)
|
||||||
}
|
}
|
||||||
return sq
|
return sq
|
||||||
}
|
}
|
||||||
@@ -119,7 +124,7 @@ func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
|
|||||||
|
|
||||||
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
|
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
|
||||||
countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
|
countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
|
||||||
countQuery = r.applyOptions(countQuery, options...)
|
countQuery = r.applyFilters(countQuery, options...)
|
||||||
query, args, err := r.toSql(countQuery)
|
query, args, err := r.toSql(countQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|||||||
Reference in New Issue
Block a user