iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 16
0

查詢今天的執行記錄,回傳群組、任務名稱跟執行時間
[GET] /api/day/record
回傳值

{
    "data": {
        "records": [
            {
                "spend_time": 60,
                "created_timestamp": 1600613426,
                "name": "AA",
                "group_name": "A"
            }
        ]
    },
    "result": true
}

handler/api.go

// DayRecord DayRecord
type DayRecord struct {
	SpendTime        int    `json:"spend_time"`
	CreatedTimestamp int64  `json:"created_timestamp"`
	Name             string `json:"name"`
	GroupName        string `json:"group_name"`
}

// GetDayRecords GetDayRecords
func GetDayRecords(c *gin.Context) {
	userID := c.GetString("user_id")

	nowTime := time.Now()
	date := nowTime.Format(config.Val.TimeFormat)
	records, err := model.RecordsModel.GetByUserIDAndTime(userID, date)
	if err != nil {
		log.WithFields(log.Fields{
			"origin_err": err.Error(),
		}).Error("db error")
		res.SystemError(c, res.ErrSystemCode, gin.H{})
		return
	}

	groups, tasks, err := findGroupsAndTasks(userID)
	if err != nil {
		log.WithFields(log.Fields{
			"user_id":    userID,
			"origin_err": err.Error(),
		}).Error("findGroup error")
		res.SystemError(c, res.ErrSystemCode, gin.H{})
		return
	}

	dayRecordRes := []DayRecord{}
	for _, r := range records {
		d := DayRecord{
			SpendTime:        r.SpendTime,
			CreatedTimestamp: r.CreatedTimestamp,
		}

		groupExist := false
		for _, g := range groups {
			if g.ID == r.ParentID {
				d.GroupName = g.Name
				groupExist = true
				break
			}
		}

		taskExist := false
		for _, t := range tasks {
			if t.ID == r.TaskID {
				d.Name = t.Name
				taskExist = true
				break
			}
		}

		if groupExist && taskExist {
			dayRecordRes = append(dayRecordRes, d)
		}
	}

	res.Success(c, gin.H{
		"records": dayRecordRes,
	})
}

試打一下
https://ithelp.ithome.com.tw/upload/images/20200923/20129767lQuM0eSHC5.png

今天的commit

謝謝大家~


上一篇
Day 15 統計報表API-2
下一篇
Day 17 DB 優化
系列文
Golang & Vue.js 30天從0打造服務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言