Properly implemented logging
the build failed Details

This commit is contained in:
konrad 2018-03-06 12:36:49 +01:00 committed by kolaente
parent d553ca743b
commit 0763ae07b0
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
23 changed files with 236 additions and 93 deletions

View File

@ -1,7 +1,7 @@
package models
// AddOrUpdateAuthor adds a new author based on an author struct
func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
func AddOrUpdateAuthor(author Author, doer *User) (newAuthor Author, err error) {
// If the ID is 0, insert the author, otherwise update it
if author.ID == 0 {
@ -14,12 +14,24 @@ func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
if err != nil {
return Author{}, err
}
// Log
err = logAction(ActionTypeAuthorAdded, doer, author.ID)
if err != nil {
return Author{}, err
}
} else {
_, err = x.Where("id = ?", author.ID).Update(&author)
if err != nil {
return Author{}, err
}
// Log
err = logAction(ActionTypeAuthorUpdated, doer, author.ID)
if err != nil {
return Author{}, err
}
}
// Get the newly inserted author

View File

@ -1,7 +1,7 @@
package models
// DeleteAuthorByID deletes an author by its ID
func DeleteAuthorByID(id int64) error {
func DeleteAuthorByID(id int64, doer *User) error {
// Check if the id is 0
if id == 0 {
return ErrIDCannotBeZero{}
@ -16,6 +16,12 @@ func DeleteAuthorByID(id int64) error {
// Delete all book relations associated with that author
_, err = x.Delete(&AuthorBook{AuthorID: id})
if err != nil {
return err
}
// Logging
err = logAction(ActionTypeAuthorDeleted, doer, id)
return err
}

View File

@ -16,7 +16,7 @@ sie in die Datenbank eingetragen und mit dem Buch verknüpft.
*/
// AddOrUpdateBook adds a new book or updates an existing one, it takes a book struct with author and publisher. Inserts them if they don't already exist
func AddOrUpdateBook(book Book) (newBook Book, err error) {
func AddOrUpdateBook(book Book, doer *User) (newBook Book, err error) {
// Check if we have at least a booktitle when we're inserting a new book
if book.Title == "" {
@ -43,7 +43,7 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) {
book.PublisherID = publisherid
} else {
// Otherwise insert it and make it the new publisher afterwards
newPublisher, err := AddOrUpdatePublisher(Publisher{Name: book.Publisher.Name})
newPublisher, err := AddOrUpdatePublisher(Publisher{Name: book.Publisher.Name}, doer)
if err != nil {
return Book{}, err
}
@ -62,12 +62,24 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) {
if err != nil {
return Book{}, err
}
// Log
err = logAction(ActionTypeBookAdded, doer, book.ID)
if err != nil {
return Book{}, err
}
} else {
// Update the book
_, err := x.Id(book.ID).Update(book)
if err != nil {
return Book{}, err
}
// Log
err = logAction(ActionTypeBookUpdated, doer, book.ID)
if err != nil {
return Book{}, err
}
}
// Set the Quantity
@ -92,7 +104,7 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) {
if !exists {
// We have to insert authors on this inperformant way, because we need the new ids afterwards
insertedAuthor, err := AddOrUpdateAuthor(author)
insertedAuthor, err := AddOrUpdateAuthor(author, doer)
if err != nil {
return Book{}, err

View File

@ -1,7 +1,7 @@
package models
// DeleteBookByID deletes a book by its ID
func DeleteBookByID(id int64) error {
func DeleteBookByID(id int64, doer *User) error {
// Check if the id is 0
if id == 0 {
return ErrIDCannotBeZero{}
@ -22,6 +22,12 @@ func DeleteBookByID(id int64) error {
// Delete all quantites for this book
_, err = x.Delete(&Quantity{ItemID: id})
if err != nil {
return err
}
// Logging
err = logAction(ActionTypeBookDeleted, doer, id)
return err
}

View File

@ -1,7 +1,7 @@
package models
// AddOrUpdateItem adds or updates a item from a item struct
func AddOrUpdateItem(item Item) (newItem Item, err error) {
func AddOrUpdateItem(item Item, doer *User) (newItem Item, err error) {
// save the quantity for later use
qty := item.Quantity
@ -15,12 +15,24 @@ func AddOrUpdateItem(item Item) (newItem Item, err error) {
if err != nil {
return Item{}, err
}
// Log
err = logAction(ActionTypeItemAdded, doer, item.ID)
if err != nil {
return Item{}, err
}
} else {
_, err = x.ID(item.ID).Update(&item)
if err != nil {
return Item{}, err
}
// Log
err = logAction(ActionTypeItemUpdated, doer, item.ID)
if err != nil {
return Item{}, err
}
}
// Set the Quantity

View File

@ -1,7 +1,7 @@
package models
// DeleteItemByID deletes a item by its ID
func DeleteItemByID(id int64) error {
func DeleteItemByID(id int64, doer *User) error {
// Check if the id is 0
if id == 0 {
return ErrIDCannotBeZero{}
@ -16,6 +16,12 @@ func DeleteItemByID(id int64) error {
// Delete all quantites for this item
_, err = x.Delete(&Quantity{ItemID: id})
if err != nil {
return err
}
// Logging
err = logAction(ActionTypeItemDeleted, doer, id)
return err
}

31
models/log_action.go Normal file
View File

@ -0,0 +1,31 @@
package models
// ActionType is the action type
type ActionType int
// Define action types
const (
ActionTypeUnknown ActionType = -1
ActionTypeBookAdded ActionType = iota
ActionTypeBookUpdated
ActionTypeBookDeleted
ActionTypeAuthorAdded
ActionTypeAuthorUpdated
ActionTypeAuthorDeleted
ActionTypePublisherAdded
ActionTypePublisherUpdated
ActionTypePublisherDeleted
ActionTypeItemAdded
ActionTypeItemUpdated
ActionTypeItemDeleted
ActionTypeUserAdded
ActionTypeUserUpdated
ActionTypeUserDeleted
ActionTypeChangedUserPassword
)
// LogAction logs a user action
func logAction(actionType ActionType, user *User, itemID int64) (err error) {
_, err = x.Insert(UserLog{Log: actionType, UserID: user.ID, ItemID: itemID})
return
}

View File

@ -63,7 +63,7 @@ func SetEngine() (err error) {
if total < 1 {
Config.FirstUser.IsAdmin = true // Make the first user admin
_, err = CreateUser(Config.FirstUser)
_, err = CreateUser(Config.FirstUser, &User{ID:0})
if err != nil {
return err
}

View File

@ -1,20 +1,30 @@
package models
// AddOrUpdatePublisher adds or updates a publisher from a publisher struct
func AddOrUpdatePublisher(publisher Publisher) (newPublisher Publisher, err error) {
func AddOrUpdatePublisher(publisher Publisher, doer *User) (newPublisher Publisher, err error) {
if publisher.ID == 0 {
if publisher.Name == "" { // Only insert it if the name is not empty
return Publisher{}, ErrNoPublisherName{}
}
_, err = x.Insert(&publisher)
if err != nil {
return Publisher{}, err
}
// Log
err = logAction(ActionTypePublisherAdded, doer, publisher.ID)
if err != nil {
return Publisher{}, err
}
} else {
_, err = x.ID(publisher.ID).Update(&publisher)
if err != nil {
return Publisher{}, err
}
// Log
err = logAction(ActionTypePublisherUpdated, doer, publisher.ID)
if err != nil {
return Publisher{}, err
}

View File

@ -1,7 +1,7 @@
package models
// DeletePublisherByID deletes a publisher by its ID
func DeletePublisherByID(id int64) error {
func DeletePublisherByID(id int64, doer *User) error {
// Check if the id is 0
if id == 0 {
return ErrIDCannotBeZero{}
@ -9,7 +9,6 @@ func DeletePublisherByID(id int64) error {
// Delete the publisher
_, err := x.Id(id).Delete(&Publisher{})
if err != nil {
return err
}
@ -18,6 +17,12 @@ func DeletePublisherByID(id int64) error {
_, err = x.Table("books").
Where("publisher_id = ?", id).
Update(map[string]interface{}{"publisher_id": 0})
if err != nil {
return err
}
// Logging
err = logAction(ActionTypePublisherDeleted, doer, id)
return err
}

View File

@ -28,7 +28,7 @@ type User struct {
type UserLog struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
UserID int64 `xorm:"int(11)" json:"userID"`
Log string `xorm:"varchar(250)" json:"log"`
Log ActionType `xorm:"int(11)" json:"log"`
ItemID int64 `xorm:"int(11)" json:"itemID"`
Time int64 `xorm:"created" json:"time"`
}
@ -98,20 +98,14 @@ func GetCurrentUser(c echo.Context) (user User, err error) {
}
// LogAction logs a user action
func logAction(action string, user User, itemID int64) (err error) {
_, err = x.Insert(UserLog{Log: action, UserID: user.ID, ItemID: itemID})
return
}
// LogAction logs a user action
func LogAction(action string, itemID int64, c echo.Context) (err error) {
func LogAction(actionType ActionType, itemID int64, c echo.Context) (err error) {
// Get the user options
user, err := GetCurrentUser(c)
if err != nil {
return err
}
return logAction(action, user, itemID)
return logAction(actionType, &user, itemID)
}
// IsAdmin checks based on it's JWT token if the user is admin

View File

@ -5,7 +5,7 @@ import (
)
// CreateUser creates a new user and inserts it into the database
func CreateUser(user User) (newUser User, err error) {
func CreateUser(user User, doer *User) (newUser User, err error) {
newUser = user
@ -46,8 +46,14 @@ func CreateUser(user User) (newUser User, err error) {
// Get the full new User
newUserOut, _, err := GetUser(newUser)
if err != nil {
return User{}, err
}
return newUserOut, nil
// Logging
err = logAction(ActionTypeUserAdded, doer, newUser.ID)
return newUserOut, err
}
// HashPassword hashes a password
@ -57,7 +63,7 @@ func hashPassword(password string) (string, error) {
}
// UpdateUser updates a user
func UpdateUser(user User) (updatedUser User, err error) {
func UpdateUser(user User, doer *User) (updatedUser User, err error) {
// Check if it exists
theUser, exists, err := GetUserByID(user.ID)
@ -85,14 +91,18 @@ func UpdateUser(user User) (updatedUser User, err error) {
if err != nil {
return User{}, err
}
return updatedUser, nil
// Logging
err = logAction(ActionTypeUserUpdated, doer, user.ID)
return updatedUser, err
}
return User{}, ErrUserDoesNotExist{user.ID}
}
// UpdateUserPassword updates the password of a user
func UpdateUserPassword(userID int64, newPassword string) (err error) {
func UpdateUserPassword(userID int64, newPassword string, doer *User) (err error) {
// Get all user details
user, exists, err := GetUserByID(userID)
@ -117,5 +127,8 @@ func UpdateUserPassword(userID int64, newPassword string) (err error) {
return err
}
return nil
// Logging
err = logAction(ActionTypeChangedUserPassword, doer, user.ID)
return err
}

View File

@ -1,7 +1,7 @@
package models
// DeleteUserByID deletes a user by its ID
func DeleteUserByID(id int64) error {
func DeleteUserByID(id int64, doer *User) error {
// Check if the id is 0
if id == 0 {
return ErrIDCannotBeZero{}
@ -24,5 +24,8 @@ func DeleteUserByID(id int64) error {
return err
}
// Logging
err = logAction(ActionTypeUserDeleted, doer, id)
return err
}

View File

@ -30,8 +30,14 @@ func AuthorDelete(c echo.Context) error {
return c.JSON(http.StatusNotFound, models.Message{"The author does not exist."})
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Delete it
err = models.DeleteAuthorByID(authorID)
err = models.DeleteAuthorByID(authorID, &doer)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
@ -40,11 +46,5 @@ func AuthorDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author."})
}
// Log the action
err = models.LogAction("Deleted an author", authorID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -29,7 +29,7 @@ func AuthorAddOrUpdate(c echo.Context) error {
}
}
// Check if we have at least a Lastname
// Check if we have a name
if datAuthor.Lastname == "" && datAuthor.Forename == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least one name."})
}
@ -58,8 +58,14 @@ func AuthorAddOrUpdate(c echo.Context) error {
}
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Insert or update the author
newAuthor, err := models.AddOrUpdateAuthor(*datAuthor)
newAuthor, err := models.AddOrUpdateAuthor(*datAuthor, &doer)
if err != nil {
if models.IsErrAuthorCannotBeEmpty(err) {
@ -68,11 +74,5 @@ func AuthorAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated an author", newAuthor.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
return c.JSON(http.StatusOK, newAuthor)
}

View File

@ -30,8 +30,14 @@ func BookDelete(c echo.Context) error {
return c.JSON(http.StatusNotFound, models.Message{"The book does not exist."})
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Delete it
err = models.DeleteBookByID(bookID)
err = models.DeleteBookByID(bookID, &doer)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
@ -40,11 +46,5 @@ func BookDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete book."})
}
// Log the action
err = models.LogAction("Deleted a book", bookID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -58,8 +58,14 @@ func BookAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"})
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Insert or update the book
newBook, err := models.AddOrUpdateBook(*datBook)
newBook, err := models.AddOrUpdateBook(*datBook, &doer)
if err != nil {
if models.IsErrAuthorCannotBeEmpty(err) {
@ -78,10 +84,17 @@ func BookAddOrUpdate(c echo.Context) error {
}
// Log the action
err = models.LogAction("Added or updated a book", newBook.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
/*if datBook.ID == 0 { // If the ID is, the author was added, otherwise updated
err = models.LogAction(models.ActionTypeBookAdded, newBook.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
} else {
err = models.LogAction(models.ActionTypeBookUpdated, newBook.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
}*/
return c.JSON(http.StatusOK, newBook)
}

View File

@ -53,8 +53,14 @@ func ItemAddOrUpdate(c echo.Context) error {
}
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Insert or update the item
newItem, err := models.AddOrUpdateItem(*datItem)
newItem, err := models.AddOrUpdateItem(*datItem, &doer)
if err != nil {
if models.IsErrItemTitleCannotBeEmpty(err) {
@ -63,11 +69,5 @@ func ItemAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated an item", newItem.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
return c.JSON(http.StatusOK, newItem)
}

View File

@ -30,8 +30,14 @@ func ItemDelete(c echo.Context) error {
return c.JSON(http.StatusBadRequest, models.Message{"The item does not exist."})
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Delete it
err = models.DeleteItemByID(itemID)
err = models.DeleteItemByID(itemID, &doer)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
@ -40,11 +46,5 @@ func ItemDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete item."})
}
// Log the action
err = models.LogAction("Deleted an item", itemID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -54,8 +54,14 @@ func PublisherAddOrUpdate(c echo.Context) error {
}
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Insert or update the publisher
newPublisher, err := models.AddOrUpdatePublisher(*datPublisher)
newPublisher, err := models.AddOrUpdatePublisher(*datPublisher, &doer)
if err != nil {
if models.IsErrNoPublisherName(err) {
@ -66,10 +72,18 @@ func PublisherAddOrUpdate(c echo.Context) error {
}
// Log the action
err = models.LogAction("Added or updated a publisher", newPublisher.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
/*if datPublisher.ID == 0 { // If the ID is, the author was added, otherwise updated
err = models.LogAction(models.ActionTypePublisherAdded, newPublisher.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
} else {
err = models.LogAction(models.ActionTypePublisherUpdated, newPublisher.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
}*/
return c.JSON(http.StatusOK, newPublisher)
}

View File

@ -30,8 +30,14 @@ func PublisherDelete(c echo.Context) error {
return c.JSON(http.StatusNotFound, models.Message{"The publisher does not exist."})
}
// Get the user options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Delete it
err = models.DeletePublisherByID(publisherID)
err = models.DeletePublisherByID(publisherID, &doer)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
@ -41,10 +47,10 @@ func PublisherDelete(c echo.Context) error {
}
// Log the action
err = models.LogAction("Deleted a publisher", publisherID, c)
/*err = models.LogAction(models.ActionTypePublisherDeleted, publisherID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
}*/
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -54,12 +54,18 @@ func UserAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the user exists."})
}
// Get the doer options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Insert or update the user
var newUser models.User
if exists {
newUser, err = models.UpdateUser(*datUser)
newUser, err = models.UpdateUser(*datUser, &doer)
} else {
newUser, err = models.CreateUser(*datUser)
newUser, err = models.CreateUser(*datUser, &doer)
}
if err != nil {
@ -91,12 +97,6 @@ func UserAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated a user", newUser.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
// Obfuscate his password
newUser.Password = ""

View File

@ -35,8 +35,14 @@ func UserDelete(c echo.Context) error {
return c.JSON(http.StatusNotFound, models.Message{"The user does not exist."})
}
// Get the doer options
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Delete it
err = models.DeleteUserByID(userID)
err = models.DeleteUserByID(userID, &doer)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
@ -50,11 +56,5 @@ func UserDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete user."})
}
// Log the action
err = models.LogAction("Deleted a user", userID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}