Added viewing logs via api

This commit is contained in:
konrad 2018-03-06 15:33:18 +01:00 committed by kolaente
parent 91e10eb78b
commit 3de6ecf579
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 36 additions and 0 deletions

View File

@ -29,3 +29,12 @@ func logAction(actionType ActionType, user *User, itemID int64) (err error) {
_, err = x.Insert(UserLog{Log: actionType, UserID: user.ID, ItemID: itemID})
return
}
func GetAllLogs() (logs []UserLog, err error) {
err = x.OrderBy("id DESC").Find(&logs)
if err != nil {
return logs, err
}
return logs, err
}

24
routes/api/v1/logs.go Normal file
View File

@ -0,0 +1,24 @@
package v1
import (
"github.com/labstack/echo"
"git.kolaente.de/konrad/Library/models"
"net/http"
)
func ShowLogs(c echo.Context) error {
// Check if the user is admin
if !models.IsAdmin(c) {
return echo.ErrUnauthorized
}
// Get the logs
logs, err := models.GetAllLogs()
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting logs."})
}
return c.JSON(http.StatusOK, logs)
}

View File

@ -119,6 +119,9 @@ func RegisterRoutes(e *echo.Echo) {
a.DELETE("/users/:id", apiv1.UserDelete)
a.POST("/users/:id/password", apiv1.UserChangePassword)
// View logs
a.GET("/logs", apiv1.ShowLogs)
/*
Alles nur mit Api machen, davor dann einen onepager mit vue.js.