コード例 #1
0
ファイル: logs.go プロジェクト: scanbadge/api
// AddLog adds a new log entry for the current user.
func AddLog(c *gin.Context) {
	var log models.Log
	err := c.Bind(&log)

	if err == nil {
		if log.Type != "" && log.Description != "" && log.Origin != "" && log.Object != "" {
			uid, err := authentication.GetUserID(c)

			if err == nil {
				log.UserID = uid
				err := configuration.Dbmap.Insert(&log)

				if err == nil {
					showResult(c, 201, log)
					return
				}
			}

			showError(c, 400, fmt.Errorf("adding new log entry failed"))
			return
		}

		showError(c, 422, fmt.Errorf("field(s) are empty"))
		return
	}

	showError(c, 400, fmt.Errorf("adding new log entry failed"))
}
コード例 #2
0
ファイル: logs.go プロジェクト: scanbadge/api
// DeleteLog deletes a device based on the identifier.
func DeleteLog(c *gin.Context) {
	id := c.Params.ByName("id")

	if id != "" {
		var log models.Log
		err := c.BindJSON(&log)

		if err == nil {
			uid, err := authentication.GetUserID(c)

			if err == nil {
				log.UserID = uid
				count, err := configuration.Dbmap.Delete(&log)

				if err == nil && count == 1 {
					showSucces(c, fmt.Sprintf("log entry with id %s is deleted", id))
					return
				}
			}
		}

		showError(c, 400, fmt.Errorf("deleting log entry failed"))
		return
	}

	showError(c, 400, fmt.Errorf("deleting log entry failed due to missing identifier"))
}
コード例 #3
0
ファイル: devices.go プロジェクト: scanbadge/api
// AddDevice adds a new device.
func AddDevice(c *gin.Context) {
	var device models.Device

	err := c.Bind(&device)

	if err == nil && device.Description != "" && device.Key != "" && device.Name != "" {
		userID, err := authentication.GetUserID(c)

		if err == nil {
			device.UserID = userID

			err := configuration.Dbmap.Insert(&device)

			if err == nil {
				showResult(c, 201, device)
				return
			}
		}

		showError(c, 400, fmt.Errorf("adding new device failed"))
		return
	}

	showError(c, 422, fmt.Errorf("field(s) are empty"))
}
コード例 #4
0
ファイル: count.go プロジェクト: scanbadge/api
// GetCount gets the count of objects of current user for the provided endpoint.
// For example, when a user has 10 actions, the count will be 10.
// An error will be returned if the endpoint is not found.
func GetCount(c *gin.Context) {
	id := c.Params.ByName("id")
	endpoints := []string{"actions", "conditions", "devices", "logs", "users"}

	if containsString(endpoints, id) {
		uid, err := authentication.GetUserID(c)

		if err == nil {
			query := fmt.Sprintf("select count(*) from %s where user_id=?", id)
			i, err := configuration.Dbmap.SelectInt(query, uid)

			if err == nil {
				var count models.Count
				count.Count = i
				count.Endpoint = id

				showResult(c, 200, count)
				return
			}
		}

		log.Println(err)
	}

	showError(c, 404, fmt.Errorf(fmt.Sprintf("endpoint '%s' not found", id)))
}
コード例 #5
0
ファイル: count.go プロジェクト: scanbadge/api
// GetAllCount gets all the count of objects of current user for all endpoints.
func GetAllCount(c *gin.Context) {
	uid, err := authentication.GetUserID(c)
	endpoints := []string{"actions", "action_error", "action_success", "conditions", "devices", "logs", "users"}
	var counts []models.Count

	if err == nil {
		for _, endpoint := range endpoints {
			var count int64

			if endpoint == "action_error" || endpoint == "action_success" {
				count, err = configuration.Dbmap.SelectInt(fmt.Sprintf("select count(log_type) from logs where log_type='%s' and user_id=?", endpoint), uid)
			} else {
				count, err = configuration.Dbmap.SelectInt(fmt.Sprintf("select count(*) from %s where user_id=?", endpoint), uid)
			}

			if err == nil {
				counts = append(counts, models.Count{Count: count, Endpoint: endpoint})
			} else {
				log.Println(err)
			}
		}

		showResult(c, 200, counts)
		return
	}

	showError(c, 404, fmt.Errorf("no endpoints found to count"))
}
コード例 #6
0
ファイル: logs.go プロジェクト: scanbadge/api
// GetLogs gets all devices.
func GetLogs(c *gin.Context) {
	var logs []models.Log
	uid, err := authentication.GetUserID(c)

	if err == nil {
		_, err := configuration.Dbmap.Select(&logs, "select * from logs where user_id=?", uid)

		if err == nil && len(logs) > 0 {
			showResult(c, 200, logs)
			return
		}
	}

	showError(c, 404, fmt.Errorf("log entries not found"))
}
コード例 #7
0
ファイル: devices.go プロジェクト: scanbadge/api
// GetDevices gets all devices.
func GetDevices(c *gin.Context) {
	var devices []models.Device
	userID, err := authentication.GetUserID(c)

	if err == nil {
		_, err := configuration.Dbmap.Select(&devices, "select devices.* from devices inner join users on devices.user_id=users.id where devices.user_id=?", userID)

		if err == nil && len(devices) > 0 {
			showResult(c, 200, devices)
			return
		}
	}

	log.Println(err)
	showError(c, 404, fmt.Errorf("device(s) not found"))
}
コード例 #8
0
ファイル: logs.go プロジェクト: scanbadge/api
// GetLog gets a device based on the provided identifier.
func GetLog(c *gin.Context) {
	id := c.Params.ByName("id")

	if id != "" {
		var log models.Log
		uid, err := authentication.GetUserID(c)

		if err == nil {
			err := configuration.Dbmap.SelectOne(&log, "select * from logs where id=? and user_id=?", id, uid)

			if err == nil {
				showResult(c, 200, log)
				return
			}
		}

		showError(c, 404, fmt.Errorf("log entry not found"))
		return
	}

	showError(c, 422, fmt.Errorf("log entry identifier not provided"))
}
コード例 #9
0
ファイル: devices.go プロジェクト: scanbadge/api
// GetDevice gets a device based on the provided identifier.
func GetDevice(c *gin.Context) {
	id := c.Params.ByName("id")

	if id != "" {
		var device models.Device
		userID, err := authentication.GetUserID(c)

		if err == nil {
			err := configuration.Dbmap.SelectOne(&device, "select * from devices where id=? and user_id=?", id, userID)

			if err == nil {
				showResult(c, 200, device)
				return
			}
		}

		log.Println(err)
		showError(c, 404, fmt.Errorf("device not found"))
		return
	}

	showError(c, 422, fmt.Errorf("no identifier provided"))
}