Skip to content

Commit

Permalink
feat: add OrderByDesc method (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamandlou authored Jan 31, 2024
1 parent e503ecf commit 4ce625b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type Query interface {
Omit(columns ...string) Query
// Order specifies the order in which the results should be returned.
Order(value any) Query
// OrderByDesc specifies the order should be descending.
OrderByDesc(column string) Query
// OrWhere add an "or where" clause to the query.
OrWhere(query any, args ...any) Query
// OrWhereIn adds an "or where column in" clause to the query.
Expand Down
4 changes: 4 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ func (r *QueryImpl) Order(value any) ormcontract.Query {
return NewQueryImplByInstance(tx, r)
}

func (r *QueryImpl) OrderByDesc(column string) ormcontract.Query {
return r.Order(fmt.Sprintf("%s DESC", column))
}

func (r *QueryImpl) OrWhere(query any, args ...any) ormcontract.Query {
tx := r.instance.Or(query, args...)

Expand Down
20 changes: 20 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,26 @@ func (s *QueryTestSuite) TestOrder() {
}
}

func (s *QueryTestSuite) TestOrderByDesc() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "order_desc_user", Avatar: "order_desc_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "order_desc_user", Avatar: "order_desc_avatar1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

var users []User
s.Nil(query.Where("name = ?", "order_desc_user").OrderByDesc("id").Get(&users))
usersLength := len(users)
s.True(usersLength == 2)
s.True(users[usersLength-1].ID == user.ID)
})
}
}

func (s *QueryTestSuite) TestPaginate() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
Expand Down
16 changes: 16 additions & 0 deletions mocks/database/orm/Query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions mocks/database/orm/Transaction.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ce625b

Please sign in to comment.