Beego框架 get请求编写

Posted 一只小阿大:)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Beego框架 get请求编写相关的知识,希望对你有一定的参考价值。

目录


根据业务需求,就是需要查询两表的数据,我用了一个笨方法。直接上代码

代码

controllers

// GetAll ...
// @Title Get All
// @Description get TestData
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 object models.TestData
// @Failure 403
// @router / [get]
func (c *TestDeviceDataController) GetAll() 
	//TestDevice variable
	var imei string
	var producer string
	var productName string
	// var productTime  time.Time
	var testResult string
	// var testTime     time.Time
	var tester string
	var testerJobnum string

	//TestData variable
	var testData string
	var testItem string
	var deviceId int

	var limit int = 10
	var offset int = 0

	if v, err := c.GetInt("pageSize"); err == nil 
		limit = v
	
	if v, err := c.GetInt("current"); err == nil 
		offset = (v - 1) * limit
	
	if v := c.GetString("imei"); v != "" 
		imei = v
	
	if v := c.GetString("producer"); v != "" 
		producer = v
	
	if v := c.GetString("productName"); v != "" 
		productName = v
	
	// if v := c.GetString("productTime"); v != "" 
	// 	productTime = v
	// 
	if v := c.GetString("testResult"); v != "" 
		testResult = v
	
	// if v := c.GetString("testTime"); v != "" 
	// 	testTime = v
	// 
	if v := c.GetString("tester"); v != "" 
		tester = v
	
	if v := c.GetString("testerJobnum"); v != "" 
		testerJobnum = v
	
	if v := c.GetString("testData"); v != "" 
		testData = v
	
	if v := c.GetString("testItem"); v != "" 
		testItem = v
	
	if v, err := c.GetInt("deviceId"); err == nil 
		deviceId = v
	
	l, total := models.GetAllTestDeviceData(limit, offset, imei, producer, productName, testResult, tester, testerJobnum, testData, testItem, deviceId)

	c.SuccessData(ListDataData: l, Total: total)

models

func GetAllTestDeviceData(limit int, offset int, imei string, producer string, productName string, testResult string,
	tester string, testerJobnum string, testData string, testItem string, deviceId int) (data []TestDevice, total int64) 
	var testDeviceData []TestDevice
	var ctestData []*TestData
	o := orm.NewOrm()

	qs := o.QueryTable(new(TestDevice).TableName())
	qd := o.QueryTable(new(TestData).TableName())
	if imei != "" 
		qs = qs.Filter("imei", imei)
	
	if producer != "" 
		qs = qs.Filter("producer", producer)
	
	if productName != "" 
		qs = qs.Filter("productName", productName)
	
	if testResult != "" 
		qs = qs.Filter("testResult", testResult)
	
	if tester != "" 
		qs = qs.Filter("tester", tester)
	
	if testerJobnum != "" 
		qs = qs.Filter("testerJobnum", testerJobnum)
	
	if testData != "" 
		qd = qd.Filter("testData", testData)
	
	if testItem != "" 
		qd = qd.Filter("testItem", testItem)
	
	// if deviceId >= 0 
	// 	qs = qs.Filter("deviceId", deviceId)
	// 
	total, _ = qs.Count()

	qs.Limit(limit).Offset(offset).OrderBy("-id").All(&testDeviceData)

	for k, v := range testDeviceData 
		qd.Filter("test_device_id", v.Id).All(&ctestData)
		// v.DevGetJsonData = ctestData
		testDeviceData[k].DevGetJsonData = ctestData
		fmt.Println(v)
	

	return testDeviceData, total

查询结果


  "code": 0,
  "msg": "success",
  "data": 
    "data": [
      
        "Id": 6,
        "dev_imei": "222222222264456",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "ko",
        "dev_test_time": "2022-03-09T08:41:45+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 61,
            "devdata_test_data": "ui,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 6,
            "data": null
          ,
          
            "Id": 62,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 6,
            "data": null
          
        ]
      ,
      
        "Id": 5,
        "dev_imei": "222222222264453",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 49,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 5,
            "data": null
          ,
          
            "Id": 50,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 5,
            "data": null
          
        ]
      ,
      
        "Id": 4,
        "dev_imei": "222222222266442",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 33,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 4,
            "data": null
          ,
          
            "Id": 34,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 4,
            "data": null
          
        ]
      ,
      
        "Id": 3,
        "dev_imei": "222222222266441",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 31,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 3,
            "data": null
          ,
          
            "Id": 32,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 3,
            "data": null
          
        ]
      
    ],
    "total": 4
  

限制页码查询

current 代表页数
pageSize 代表每页显示几个
http://localhost:8080/v1/testDeviceData?pageSize=2&current=1


  "code": 0,
  "msg": "success",
  "data": 
    "data": [
      
        "Id": 6,
        "dev_imei": "222222222264456",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "ko",
        "dev_test_time": "2022-03-09T08:41:45+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 61,
            "devdata_test_data": "ui,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 6,
            "data": null
          ,
          
            "Id": 62,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 6,
            "data": null
          
        ]
      ,
      
        "Id": 5,
        "dev_imei": "222222222264453",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 49,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 5,
            "data": null
          ,
          
            "Id": 50,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 5,
            "data": null
          
        ]
      
    ],
    "total": 4
  

m2m

后面同事跟我说有个多对多模型定义,我摸了半天没摸懂,看了go - 在beego orm中插入具有m2m的模型文章仿写了下

点击进入手册,看多对多关系

开始我一直折腾模型定义的rel_table和rel_through,以及参考文章后面发现没有调用m2m,我也不知道啥问题

func M2mTest() 
	o := orm.NewOrm()
	var data TestData
	data.DataTestData = "CoolDude"
	id, err := o.Insert(&data)
	if err != nil 
		log.Printf(err.Error())
	 else 
		log.Printf("Player ID: %v", id)
	

	var device TestDevice
	id, err = o.Insert(&device)
	if err != nil 
		log.Printf(err.Error())
	 else 
		log.Printf("Game ID: %v", id)
	

	m2m := o.QueryM2M(&device, "DevGetJsonData")
	num, err := m2m.Add(data)
	fmt.Println(m2m.Add(data))
	if err == nil 
		log.Printf("Added nums: %v", num)
	
	// m2m.Remove(63)

以上是关于Beego框架 get请求编写的主要内容,如果未能解决你的问题,请参考以下文章

Beego框架之请求数据处理

Beego框架 POST请求接口编写

Beego框架 POST请求接口编写

beego框架学习 -路由设置

beego框架(golang)学习过滤器(实现restful请求)

golang web框架 beego 学习 beego获取参数