Skip to content

Commit

Permalink
[feat]优化api接口接受方式,除了接收json数据,还可以接收form表单数据
Browse files Browse the repository at this point in the history
  • Loading branch information
凹凸曼 committed Nov 27, 2024
1 parent f2592af commit 91c9fb0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
15 changes: 8 additions & 7 deletions main/api/web/controller/article.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package controller

import (
"github.com/gofiber/fiber/v2"
"moss/api/web/mapper"
appService "moss/application/service"
"moss/domain/core/aggregate"
"moss/domain/core/service"

"github.com/gofiber/fiber/v2"
)

func ArticleList(ctx *fiber.Ctx) error {
Expand Down Expand Up @@ -33,19 +34,19 @@ func ArticleGet(ctx *fiber.Ctx) error {
}

func ArticleCreate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[aggregate.ArticlePost](ctx.Body())
if err != nil {
var obj aggregate.ArticlePost
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResultData(obj, appService.ArticlePost("create", obj)))
return ctx.JSON(mapper.MessageResultData(obj, appService.ArticlePost("create", &obj)))
}

func ArticleUpdate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[aggregate.ArticlePost](ctx.Body())
if err != nil {
var obj aggregate.ArticlePost
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResultData(obj, appService.ArticlePost("update", obj)))
return ctx.JSON(mapper.MessageResultData(obj, appService.ArticlePost("update", &obj)))
}

func ArticleDelete(ctx *fiber.Ctx) error {
Expand Down
15 changes: 8 additions & 7 deletions main/api/web/controller/category.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package controller

import (
"github.com/gofiber/fiber/v2"
"moss/api/web/mapper"
appService "moss/application/service"
"moss/domain/core/entity"
"moss/domain/core/service"

"github.com/gofiber/fiber/v2"
)

func CategoryList(ctx *fiber.Ctx) error {
Expand Down Expand Up @@ -33,19 +34,19 @@ func CategoryGet(ctx *fiber.Ctx) error {
}

func CategoryCreate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Category](ctx.Body())
if err != nil {
var obj entity.Category
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResultData(obj, service.Category.Create(obj)))
return ctx.JSON(mapper.MessageResultData(obj, service.Category.Create(&obj)))
}

func CategoryUpdate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Category](ctx.Body())
if err != nil {
var obj entity.Category
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResult(service.Category.Update(obj)))
return ctx.JSON(mapper.MessageResult(service.Category.Update(&obj)))
}

func CategoryDelete(ctx *fiber.Ctx) error {
Expand Down
15 changes: 8 additions & 7 deletions main/api/web/controller/link.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package controller

import (
"github.com/gofiber/fiber/v2"
"moss/api/web/mapper"
"moss/domain/core/entity"
"moss/domain/core/service"

"github.com/gofiber/fiber/v2"
)

func LinkList(ctx *fiber.Ctx) error {
Expand Down Expand Up @@ -32,19 +33,19 @@ func LinkGet(ctx *fiber.Ctx) error {
}

func LinkCreate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Link](ctx.Body())
if err != nil {
var obj entity.Link
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResultData(obj, service.Link.Create(obj)))
return ctx.JSON(mapper.MessageResultData(obj, service.Link.Create(&obj)))
}

func LinkUpdate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Link](ctx.Body())
if err != nil {
var obj entity.Link
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResult(service.Link.Update(obj)))
return ctx.JSON(mapper.MessageResult(service.Link.Update(&obj)))
}

func LinkDelete(ctx *fiber.Ctx) error {
Expand Down
15 changes: 8 additions & 7 deletions main/api/web/controller/store.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package controller

import (
"github.com/gofiber/fiber/v2"
"moss/api/web/mapper"
appService "moss/application/service"
"moss/domain/core/entity"
"moss/domain/core/service"

"github.com/gofiber/fiber/v2"
)

func StoreList(ctx *fiber.Ctx) error {
Expand Down Expand Up @@ -33,19 +34,19 @@ func StoreGet(ctx *fiber.Ctx) error {
}

func StoreCreate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Store](ctx.Body())
if err != nil {
var obj entity.Store
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResultData(obj, service.Store.Create(obj)))
return ctx.JSON(mapper.MessageResultData(obj, service.Store.Create(&obj)))
}

func StoreUpdate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Store](ctx.Body())
if err != nil {
var obj entity.Store
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResult(service.Store.Update(obj)))
return ctx.JSON(mapper.MessageResult(service.Store.Update(&obj)))
}

func StoreDelete(ctx *fiber.Ctx) error {
Expand Down
15 changes: 8 additions & 7 deletions main/api/web/controller/tag.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package controller

import (
"github.com/gofiber/fiber/v2"
"moss/api/web/mapper"
appService "moss/application/service"
"moss/domain/core/entity"
"moss/domain/core/service"

"github.com/gofiber/fiber/v2"
)

func TagList(ctx *fiber.Ctx) error {
Expand Down Expand Up @@ -33,19 +34,19 @@ func TagGet(ctx *fiber.Ctx) error {
}

func TagCreate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Tag](ctx.Body())
if err != nil {
var obj entity.Tag
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResultData(obj, service.Tag.Create(obj)))
return ctx.JSON(mapper.MessageResultData(obj, service.Tag.Create(&obj)))
}

func TagUpdate(ctx *fiber.Ctx) error {
obj, err := mapper.BodyToCurdModel[entity.Tag](ctx.Body())
if err != nil {
var obj entity.Tag
if err := ctx.BodyParser(&obj); err != nil {
return ctx.JSON(mapper.MessageResult(err))
}
return ctx.JSON(mapper.MessageResult(service.Tag.Update(obj)))
return ctx.JSON(mapper.MessageResult(service.Tag.Update(&obj)))
}

func TagDelete(ctx *fiber.Ctx) error {
Expand Down
6 changes: 0 additions & 6 deletions main/api/web/mapper/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ type curdModel interface {
entity.Article | entity.Category | entity.Tag | entity.Link | aggregate.ArticlePost | entity.Store
}

func BodyToCurdModel[M curdModel](body []byte) (_ *M, err error) {
var obj M
err = BodyParser(body, &obj)
return &obj, err
}

func BodyToStrSet(body []byte) (res []string, err error) {
err = BodyParser(body, &res)
return
Expand Down

0 comments on commit 91c9fb0

Please sign in to comment.