Added method to add and delete an author

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
kolaente 2017-10-11 17:20:56 +02:00
parent c82eca84dc
commit 500007bdae
7 changed files with 101 additions and 2 deletions

6
models/authors_add.go Normal file
View File

@ -0,0 +1,6 @@
package models
func AddAuthor(author Author) (err error){
_, err = x.Insert(&author)
return err
}

15
models/authors_delete.go Normal file
View File

@ -0,0 +1,15 @@
package models
func DeleteAuthorByID(id int64) error {
// Delete the author
_, err := x.Id(id).Delete(&Author{})
if err != nil {
return err
}
// Delete all book relations associated with that author
_, err = x.Delete(&AuthorBook{AuthorID:id})
return err
}

View File

@ -0,0 +1,40 @@
package v1
import (
"github.com/labstack/echo"
"strconv"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
func AuthorDelete(c echo.Context) error {
id := c.Param("id")
// Make int
authorID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get author infos"})
}
// Check if the author exists
_, exists, err := models.GetAuthorByID(authorID)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could get author"})
}
if !exists {
return c.JSON(http.StatusBadRequest, models.Message{"The author does not exist."})
}
// Delete it
err = models.DeleteAuthorByID(authorID)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author"})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -0,0 +1,36 @@
package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"encoding/json"
"strings"
"fmt"
)
func AuthorAdd(c echo.Context) error {
// Check for Request Content
author := c.FormValue("author")
if author == "" {
return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"})
}
// Decode the JSON
var authorstruct models.Author
dec := json.NewDecoder(strings.NewReader(author))
err := dec.Decode(&authorstruct)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding author: " + err.Error()})
}
// Insert the author
err = models.AddAuthor(authorstruct)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -21,7 +21,7 @@ func BookAdd(c echo.Context) error {
err := dec.Decode(&bookstruct)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book"})
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()})
}
// Insert the book

View File

@ -55,6 +55,8 @@ func RegisterRoutes(e *echo.Echo) {
a.DELETE("/books/:id", apiv1.BookDelete)
// Manage Authors
a.PUT("/authors", apiv1.AuthorAdd)
a.DELETE("/authors/:id", apiv1.AuthorDelete)
// Manage Publishers
@ -67,7 +69,7 @@ func RegisterRoutes(e *echo.Echo) {
Routes:
GET / - entweder übersicht anzeigen (wenn der nutzer eingeloggt ist) oder auf /login weiterleiten
POST /login - Einloggen
POST /login - Einloggen
POST /logout - ausloggen
GET /books/:id - Buch anzeigen