Gin API 设计

package main

import (

	"fmt"
	"net/http"
	"reflect"
	"github.com/gin-gonic/gin"
	xutil "hdutil"
	"logic/pkg/dao"



	"logic/pkg/service"

)


func main() {
	r := gin.Default()
	rt := r.Group(`/homeworks`)
	rt.POST(
		``,
		xutil.GRequestBodyObject(reflect.TypeOf(dao.Homework{}), "json"),
		CreateHomework,
	)

	rt.PUT(
		`/:homeworkId`,
		xutil.GPathRequireInt("homeworkId"),
		xutil.GRequestBodyMap,
		UpdateHomework,
	)

	r.GET(
		`/homeworksByFilter`,
		xutil.GQueryRequireString("filter"),
		xutil.GQueryOptionalIntDefault("current", 1),
		xutil.GQueryOptionalIntDefault("pageSize", 10),
		GetHomeworkByFilter,
	)
	rt.POST(
		`/notify`,
		xutil.GRequestBodyObject(reflect.TypeOf([]int64{}), "json"),
		BatchSendRemark,
	)

	rt.GET(
		`/:homeworkId/thumb`,
		xutil.GPathRequireInt("homeworkId"),
		xutil.GQueryRequireString("sessionId"),
		ThumbUp,
	)
	rt.GET(
		`/:homeworkId/sharetimeline`,
		xutil.GPathRequireInt("homeworkId"),
		ShareTimeline,
	)
	rt.GET(
		`/:homeworkId/sharefriend`,
		xutil.GPathRequireInt("homeworkId"),
		ShareFriend,
	)
	r.Run(":5000")
}

func CreateHomework(c *gin.Context) {
	u := c.MustGet("requestBody").(*dao.Homework)
	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, u))
}
func BatchSendRemark(c *gin.Context) {
	ids := c.MustGet("requestBody").(*[]int64)

	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, ids))
}

func UpdateHomework(c *gin.Context) {
	m := c.MustGet("requestBody").(map[string]interface{})
	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, m))
}


func GetHomeworkByFilter(c *gin.Context) {
	current := c.MustGet("current").(int64)
	pagesize := c.MustGet("pageSize").(int64)

	jsonstring := c.MustGet("filter").(string)

	fmt.Println(current,pagesize,jsonstring)
	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, current))
}

func ThumbUp(c *gin.Context) {
	id := c.MustGet("homeworkId").(int64)
	sessionId := c.MustGet("sessionId").(string)
	strId := fmt.Sprintf("%d", id)
	fmt.Println(sessionId,strId)
	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, strId))
}


func ShareTimeline(c *gin.Context) {
	id := c.MustGet("homeworkId").(int64)

	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, id))
}

//分享
func ShareFriend(c *gin.Context) {
	id := c.MustGet("homeworkId").(int64)


	c.Data(http.StatusOK, "", xutil.GR(service.NoneError, id))
}