Skip to content

Commit

Permalink
预定义交互接口
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Feb 19, 2024
1 parent 0994e63 commit 9a468b8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/interactive/internal/errs/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package errs

var (
SystemError = ErrorCode{Code: 503001, Msg: "系统错误"}
)

type ErrorCode struct {
Code int
Msg string
}
55 changes: 55 additions & 0 deletions internal/interactive/internal/web/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package web

import (
"github.com/ecodeclub/ginx"
"github.com/ecodeclub/ginx/session"
"github.com/gin-gonic/gin"
)

var _ ginx.Handler = &Handler{}

type Handler struct {
}

// PrivateRoutes 这边我们直接让前端来控制 biz 和 biz_id,简化实现
// 这算是一种反范式的设计和实现方式
func (h *Handler) PrivateRoutes(server *gin.Engine) {
g := server.Group("/intr")
g.POST("/collect", ginx.BS[CollectReq](h.Collect))
g.POST("/like", ginx.BS[LikeReq](h.Like))
// 统一用 POST 请求,懒得去处理不同的
g.POST("/cnt", ginx.BS[GetCntReq](h.GetCnt))
}

func (h *Handler) PublicRoutes(server *gin.Engine) {
//TODO implement me
panic("implement me")
}

func (h *Handler) Collect(ctx *ginx.Context, req CollectReq, sess session.Session) (ginx.Result, error) {
return ginx.Result{Msg: "OK"}, nil
}

func (h *Handler) GetCnt(ctx *ginx.Context, req GetCntReq, sess session.Session) (ginx.Result, error) {
return ginx.Result{
Data: &GetCntResp{},
}, nil
}

func (h *Handler) Like(ctx *ginx.Context, req LikeReq, sess session.Session) (ginx.Result, error) {
return ginx.Result{Msg: "OK"}, nil
}
13 changes: 13 additions & 0 deletions internal/interactive/internal/web/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package web

import (
"github.com/ecodeclub/ginx"
"github.com/ecodeclub/webook/internal/interactive/internal/errs"
)

var (
systemErrorResult = ginx.Result{

Check failure on line 9 in internal/interactive/internal/web/result.go

View workflow job for this annotation

GitHub Actions / lint

var `systemErrorResult` is unused (unused)
Code: errs.SystemError.Code,
Msg: errs.SystemError.Msg,
}
)
38 changes: 38 additions & 0 deletions internal/interactive/internal/web/vo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package web

type CollectReq struct {
Biz string `json:"biz"`
BizId int64 `json:"bizId"`
}

type LikeReq struct {
Biz string `json:"biz"`
BizId int64 `json:"bizId"`
}

type GetCntReq struct {
Biz string `json:"biz"`
BizId int64 `json:"bizId"`
}

type GetCntResp struct {
CollectCnt int `json:"collectCnt"`
LikeCnt int `json:"likeCnt"`
ViewCnt int `json:"viewCnt"`
Collected bool `json:"collected"`
Liked bool `json:"liked"`
}

0 comments on commit 9a468b8

Please sign in to comment.