Example #1
0
// 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"))
}
Example #2
0
// 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"))
}