diff --git a/models/publishers_update.go b/models/publishers_update.go new file mode 100644 index 0000000..fd5fb7f --- /dev/null +++ b/models/publishers_update.go @@ -0,0 +1,14 @@ +package models + +func UpdatePublisher(publisher Publisher, id int64) (newPublisher Publisher, err error){ + _, err = x.Where("id = ?", id).Update(&publisher) + + if err != nil { + return Publisher{}, err + } + + // Get the newly updated publisher + newPublisher, _, err = GetPublisherByID(id) + + return newPublisher, err +} \ No newline at end of file diff --git a/routes/api/v1/publishers_update.go b/routes/api/v1/publishers_update.go new file mode 100644 index 0000000..f65c0cc --- /dev/null +++ b/routes/api/v1/publishers_update.go @@ -0,0 +1,56 @@ +package v1 + +import ( + "github.com/labstack/echo" + "net/http" + "git.mowie.cc/konrad/Library/models" + "encoding/json" + "strings" + "strconv" +) + +func PublisherUpdate(c echo.Context) error { + // Check for Request Content + publisher := c.FormValue("publisher") + if publisher == "" { + return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"}) + } + + // Look for the publishers id + id := c.Param("id") + + // Make int + publisherID, err := strconv.ParseInt(id, 10, 64) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher infos"}) + } + + // Check if the publisher exists + _, exists, err := models.GetPublisherByID(publisherID) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher infos"}) + } + + if !exists { + return c.JSON(http.StatusBadRequest, models.Message{"The publisher does not exist."}) + } + + // Decode the JSON + var publisherstruct models.Publisher + dec := json.NewDecoder(strings.NewReader(publisher)) + + err = dec.Decode(&publisherstruct) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding publisher: " + err.Error()}) + } + + // Insert the publisher + newPublisher, err := models.UpdatePublisher(publisherstruct, publisherID) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) + } + + return c.JSON(http.StatusOK, newPublisher) +} diff --git a/routes/routes.go b/routes/routes.go index df06f86..07bf244 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -62,6 +62,7 @@ func RegisterRoutes(e *echo.Echo) { // Manage Publishers a.PUT("/publishers", apiv1.PublisherAdd) a.DELETE("/publishers/:id", apiv1.PublisherDelete) + a.POST("/publishers/:id", apiv1.PublisherUpdate) // Manage Users @@ -83,14 +84,14 @@ func RegisterRoutes(e *echo.Echo) { PUT /books/add - ✔ |Hinzufügen GET /authors/:id - ✔ Autor anzeigen - POST /authors/:id - |Autor bearbeiten + POST /authors/:id - ✔ |Autor bearbeiten DELETE /authors/:id - ✔ |Autor löschen (auch mit allem in books_author) GET /authors/list - ✔ Autoren auflisten GET /authors/search?s=d - ✔ Autoren suchen PUT /authors/add - ✔ |Hinzufügen 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) GET /publishers/list - ✔ Verlage auflisten GET /publishers/search?s= - ✔ Verlage suchen