Skip to content

Commit

Permalink
feat: /api/jewellery GET - add sort, count
Browse files Browse the repository at this point in the history
  • Loading branch information
Nlhmmh committed Aug 4, 2024
1 parent dc3c428 commit 96afb81
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
16 changes: 12 additions & 4 deletions handler/handler.gen.go

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

60 changes: 40 additions & 20 deletions handler/jewellery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"burmese_jewellery/models"
"burmese_jewellery/orm"
"burmese_jewellery/query"
"burmese_jewellery/tx"
"database/sql"
"fmt"

"net/http"

Expand All @@ -13,27 +16,44 @@ import (
)

func (h *Handler) GetApiJewellery(c *gin.Context, params models.GetApiJewelleryParams) {
qList := []qm.QueryMod{
qm.Offset(params.Offset),
qm.Limit(params.Limit),
qm.OrderBy("created_at ASC"),
}
qList = query.EqUUID(qList, params.Id, orm.JewelleryColumns.JewelleryID)
qList = query.EqUUID(qList, params.CategoryId, orm.JewelleryColumns.CategoryID)
qList = query.EqUUID(qList, params.GemId, orm.JewelleryColumns.GemID)
qList = query.EqUUID(qList, params.MaterialId, orm.JewelleryColumns.MaterialID)
qList = query.Like(qList, params.Name, orm.JewelleryColumns.Name)
qList = query.Eq(qList, params.IsPublished, orm.JewelleryColumns.IsPublished)

list, err := orm.Jewelleries(qList...).AllG(c)
if err != nil {
ers.InternalServer.New(err).Abort(c)
return
}
var resp GetResp

resp, err := models.ConvListFromORM(list, models.ConvJewelleryFromORM)
if err != nil {
ers.InternalServer.New(err).Abort(c)
if err := tx.Write(c, func(tx *sql.Tx) *ers.ErrResp {
qList := []qm.QueryMod{}
qList = query.EqUUID(qList, params.Id, orm.JewelleryColumns.JewelleryID)
qList = query.EqUUID(qList, params.CategoryId, orm.JewelleryColumns.CategoryID)
qList = query.EqUUID(qList, params.GemId, orm.JewelleryColumns.GemID)
qList = query.EqUUID(qList, params.MaterialId, orm.JewelleryColumns.MaterialID)
qList = query.Like(qList, params.Name, orm.JewelleryColumns.Name)
qList = query.Eq(qList, params.IsPublished, orm.JewelleryColumns.IsPublished)
count, err := orm.Jewelleries(qList...).Count(c, tx)
if err != nil {
return ers.InternalServer.New(err)
}
resp.Count = count

sort := models.Desc
if v := params.Sort; v != nil {
sort = *v
}
qList = append(qList, []qm.QueryMod{
qm.Offset(params.Offset),
qm.Limit(params.Limit),
qm.OrderBy(fmt.Sprintf("%s %s", orm.AccountColumns.CreatedAt, string(sort))),
}...)
list, err := orm.Jewelleries(qList...).AllG(c)
if err != nil {
return ers.InternalServer.New(err)
}

data, err := models.ConvListFromORM(list, models.ConvJewelleryFromORM)
if err != nil {
return ers.InternalServer.New(err)
}
resp.Data = data

return nil
}); err != nil {
return
}

Expand Down
1 change: 1 addition & 0 deletions models/models.gen.go

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

4 changes: 4 additions & 0 deletions swagger/paths.yml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@
name: is_published
schema:
type: boolean
- in: query
name: sort
schema:
$ref: "#/components/schemas/Sort"
responses:
200:
description: OK
Expand Down
2 changes: 2 additions & 0 deletions swagger/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,8 @@ <h3 class="field-label">Query parameters</h3>

<div class="param-desc"><span class="param-type">Query Parameter</span> &mdash; </div> <div class="param">is_published (optional)</div>

<div class="param-desc"><span class="param-type">Query Parameter</span> &mdash; </div> <div class="param">sort (optional)</div>

<div class="param-desc"><span class="param-type">Query Parameter</span> &mdash; </div> </div> <!-- field-items -->


Expand Down
4 changes: 4 additions & 0 deletions swagger/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ paths:
name: is_published
schema:
type: boolean
- in: query
name: sort
schema:
$ref: '#/components/schemas/Sort'
responses:
200:
description: OK
Expand Down

0 comments on commit 96afb81

Please sign in to comment.