Insert methods now return the inserted object

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-11 18:17:51 +02:00 committed by kolaente
parent a78ac5d7da
commit cfcf4ac365
5 changed files with 29 additions and 13 deletions

View File

@ -1,6 +1,14 @@
package models package models
func AddAuthor(author Author) (err error){ func AddAuthor(author Author) (newAuthor Author, err error){
_, err = x.Insert(&author) _, err = x.Insert(&author)
return err
if err != nil {
return Author{}, err
}
// Get the newly inserted author
newAuthor, _, err = GetAuthorByID(author.ID)
return newAuthor, err
} }

View File

@ -1,6 +1,14 @@
package models package models
func AddBook(book Book) (err error){ func AddBook(book Book) (newBook Book, err error){
_, err = x.Insert(&book) _, err = x.Insert(&book)
return err
if err != nil {
return Book{}, err
}
// Get the newly inserted book
newBook, _, err = GetBookById(book.ID)
return book, err
} }

View File

@ -25,11 +25,11 @@ func AuthorAdd(c echo.Context) error {
} }
// Insert the author // Insert the author
err = models.AddAuthor(authorstruct) newAuthor, err := models.AddAuthor(authorstruct)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
} }
return c.JSON(http.StatusOK, models.Message{"success"}) return c.JSON(http.StatusOK, newAuthor)
} }

View File

@ -25,11 +25,11 @@ func BookAdd(c echo.Context) error {
} }
// Insert the book // Insert the book
err = models.AddBook(bookstruct) newBook, err := models.AddBook(bookstruct)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
} }
return c.JSON(http.StatusOK, models.Message{"success"}) return c.JSON(http.StatusOK, newBook)
} }

View File

@ -27,17 +27,17 @@ func RegisterRoutes(e *echo.Echo) {
// API Routes // API Routes
a := e.Group("/api/v1") a := e.Group("/api/v1")
// Lookup Books // Lookup Books
a.GET("/books/list", apiv1.BookList) a.GET("/books", apiv1.BookList)
a.GET("/books/:id", apiv1.BookShow) a.GET("/books/:id", apiv1.BookShow)
a.GET("/books/search", apiv1.BookSearch) a.GET("/books/search", apiv1.BookSearch)
// Lookup Authors // Lookup Authors
a.GET("/authors/list", apiv1.AuthorsList) a.GET("/authors", apiv1.AuthorsList)
a.GET("/authors/:id", apiv1.AuthorShow) a.GET("/authors/:id", apiv1.AuthorShow)
a.GET("/authors/search", apiv1.AuthorSearch) a.GET("/authors/search", apiv1.AuthorSearch)
// Lookup Publishers // Lookup Publishers
a.GET("/publishers/list", apiv1.PublishersList) a.GET("/publishers", apiv1.PublishersList)
a.GET("/publishers/:id", apiv1.PublisherShow) a.GET("/publishers/:id", apiv1.PublisherShow)
a.GET("/publishers/search", apiv1.PublisherSearch) a.GET("/publishers/search", apiv1.PublisherSearch)
@ -90,10 +90,10 @@ func RegisterRoutes(e *echo.Echo) {
GET /publishers/:id - Verlag anzeigen GET /publishers/:id - Verlag anzeigen
POST /publishers/:id - |Verlag bearbeiten POST /publishers/:id - |Verlag bearbeiten
DELETE /publishers/:id - |Verlag löschen (bei büchern Verlag auf 0 setzen) DELETE /publishers/:id - |Verlag löschen (bei büchern Verlag auf 0 setzen)
GET /publishers/list - Verlage auflisten GET /publishers/list - Verlage auflisten
GET /publishers/search?s= - Verlage suchen GET /publishers/search?s= - Verlage suchen
PUT /publishers/add - |Hinzufügen PUT /publishers/add - |Hinzufügen
GET /settings - |Nutzereinstellungen (Passwort, name etc) GET /settings - |Nutzereinstellungen (Passwort, name etc)
POST /settings - |Nutzereinstellungen (Passwort, name etc) POST /settings - |Nutzereinstellungen (Passwort, name etc)