From 1d2dfa487ccd25cf6d75fefd4aa9fac29f6c4a4c Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 21 Nov 2017 12:09:45 +0100 Subject: [PATCH] Implemented list, add, update, delete publisher --- frontend/src/components/Publishers.vue | 232 ++++++++++++++++++ frontend/src/components/PublishersAddEdit.vue | 99 ++++++++ frontend/src/router/index.js | 17 ++ models/books_add_update.go | 2 +- models/publishers_add.go | 18 +- models/publishers_delete.go | 3 - models/publishers_update.go | 15 -- routes/api/v1/publishers_add.go | 36 --- routes/api/v1/publishers_add_update.go | 47 ++++ routes/api/v1/publishers_update.go | 57 ----- routes/routes.go | 4 +- 11 files changed, 411 insertions(+), 119 deletions(-) create mode 100644 frontend/src/components/Publishers.vue create mode 100644 frontend/src/components/PublishersAddEdit.vue delete mode 100644 models/publishers_update.go delete mode 100644 routes/api/v1/publishers_add.go create mode 100644 routes/api/v1/publishers_add_update.go delete mode 100644 routes/api/v1/publishers_update.go diff --git a/frontend/src/components/Publishers.vue b/frontend/src/components/Publishers.vue new file mode 100644 index 0000000..ff44c56 --- /dev/null +++ b/frontend/src/components/Publishers.vue @@ -0,0 +1,232 @@ + + + + + diff --git a/frontend/src/components/PublishersAddEdit.vue b/frontend/src/components/PublishersAddEdit.vue new file mode 100644 index 0000000..68e42e6 --- /dev/null +++ b/frontend/src/components/PublishersAddEdit.vue @@ -0,0 +1,99 @@ + + + diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index e65ae39..6f136b5 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -6,6 +6,8 @@ import Books from '@/components/Books' import BooksAddEdit from '@/components/BooksAddEdit' import Authors from '@/components/Authors' import AuthorsAddEdit from '@/components/AuthorsAddEdit' +import Publishers from '@/components/Publishers' +import PublishersAddEdit from '@/components/PublishersAddEdit' Vue.use(Router) @@ -50,6 +52,21 @@ export default new Router({ path: '/authors/:id/edit', name: 'author-edit', component: AuthorsAddEdit + }, + { + path: '/publishers', + name: 'publishers', + component: Publishers + }, + { + path: '/publishers/add', + name: 'publishers-add', + component: PublishersAddEdit + }, + { + path: '/publishers/:id/edit', + name: 'publisher-edit', + component: PublishersAddEdit } ], linkActiveClass: 'active' diff --git a/models/books_add_update.go b/models/books_add_update.go index 5850e96..199a772 100644 --- a/models/books_add_update.go +++ b/models/books_add_update.go @@ -36,7 +36,7 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) { } if !exists { - newPublisher, err := AddPublisher(Publisher{Name: book.PublisherFull.Name}) + newPublisher, err := AddOrUpdatePublisher(Publisher{Name: book.PublisherFull.Name}) if err != nil { return Book{}, err } diff --git a/models/publishers_add.go b/models/publishers_add.go index 94d4711..b356999 100644 --- a/models/publishers_add.go +++ b/models/publishers_add.go @@ -1,11 +1,19 @@ package models -// AddPublisher adds a publisher from a publisher struct -func AddPublisher(publisher Publisher) (newPublisher Publisher, err error) { - _, err = x.Insert(&publisher) +// AddOrUpdatePublisher adds or updates a publisher from a publisher struct +func AddOrUpdatePublisher(publisher Publisher) (newPublisher Publisher, err error) { + if publisher.ID == 0 { + _, err = x.Insert(&publisher) - if err != nil { - return Publisher{}, err + if err != nil { + return Publisher{}, err + } + } else { + _, err = x.Where("id = ?", publisher.ID).Update(&publisher) + + if err != nil { + return Publisher{}, err + } } newPublisher, _, err = GetPublisherByID(publisher.ID) diff --git a/models/publishers_delete.go b/models/publishers_delete.go index db25ceb..6ad8c5b 100644 --- a/models/publishers_delete.go +++ b/models/publishers_delete.go @@ -10,9 +10,6 @@ func DeletePublisherByID(id int64) error { } // Set all publisher to 0 on all book with this publisher - //book := Book{Publisher:0} - //book.Publisher = 0 - //_, err = x.Where("publisher = ?", id).Update(book) _, err = x.Table("books").Where("publisher = ?", id).Update(map[string]interface{}{"publisher": 0}) return err diff --git a/models/publishers_update.go b/models/publishers_update.go deleted file mode 100644 index 854eb61..0000000 --- a/models/publishers_update.go +++ /dev/null @@ -1,15 +0,0 @@ -package models - -// UpdatePublisher updates a publisher, takes an ID and a publisher struct with the new publisher infos -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 -} diff --git a/routes/api/v1/publishers_add.go b/routes/api/v1/publishers_add.go deleted file mode 100644 index c926281..0000000 --- a/routes/api/v1/publishers_add.go +++ /dev/null @@ -1,36 +0,0 @@ -package v1 - -import ( - "encoding/json" - "git.mowie.cc/konrad/Library/models" - "github.com/labstack/echo" - "net/http" - "strings" -) - -// PublisherAdd is the handler to add a publisher -func PublisherAdd(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"}) - } - - // 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.AddPublisher(publisherstruct) - - if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) - } - - return c.JSON(http.StatusOK, newPublisher) -} diff --git a/routes/api/v1/publishers_add_update.go b/routes/api/v1/publishers_add_update.go new file mode 100644 index 0000000..d49e3fc --- /dev/null +++ b/routes/api/v1/publishers_add_update.go @@ -0,0 +1,47 @@ +package v1 + +import ( + "encoding/json" + "git.mowie.cc/konrad/Library/models" + "github.com/labstack/echo" + "net/http" + "strings" + "fmt" +) + +type publisherPayload struct { + Publisher models.Publisher `json:"publisher" form:"publisher"` +} + +// PublisherAddOrUpdate is the handler to add a publisher +func PublisherAddOrUpdate(c echo.Context) error { + // Check for Request Content + publisherFromString := c.FormValue("publisher") + var publisherToInsert models.Publisher + + if publisherFromString == "" { + b := new(publisherPayload) + if err := c.Bind(b); err != nil { + fmt.Println(err) + return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"}) + } + publisherToInsert = b.Publisher + } else { + // Decode the JSON + dec := json.NewDecoder(strings.NewReader(publisherFromString)) + err := dec.Decode(&publisherToInsert) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding publisher: " + err.Error()}) + } + } + + // Insert the publisher + newPublisher, err := models.AddOrUpdatePublisher(publisherToInsert) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) + } + + return c.JSON(http.StatusOK, newPublisher) +} diff --git a/routes/api/v1/publishers_update.go b/routes/api/v1/publishers_update.go deleted file mode 100644 index 96ff3cc..0000000 --- a/routes/api/v1/publishers_update.go +++ /dev/null @@ -1,57 +0,0 @@ -package v1 - -import ( - "encoding/json" - "git.mowie.cc/konrad/Library/models" - "github.com/labstack/echo" - "net/http" - "strconv" - "strings" -) - -// PublisherUpdate is the handler to update a publishers information -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 ed988c4..aff5759 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -94,9 +94,9 @@ func RegisterRoutes(e *echo.Echo) { a.POST("/authors/:id", apiv1.AuthorAddOrUpdate) // Manage Publishers - a.PUT("/publishers", apiv1.PublisherAdd) + a.PUT("/publishers", apiv1.PublisherAddOrUpdate) a.DELETE("/publishers/:id", apiv1.PublisherDelete) - a.POST("/publishers/:id", apiv1.PublisherUpdate) + a.POST("/publishers/:id", apiv1.PublisherAddOrUpdate) // Manage Users