Added methods to add or update an author
This commit is contained in:
parent
76dd5807f6
commit
0d63ab8826
|
@ -31,7 +31,7 @@
|
|||
Add a author
|
||||
</router-link>
|
||||
|
||||
<button @click="loadauthors()" class="ui teal labeled icon button" style="float: right;">
|
||||
<button @click="loadAuthors()" class="ui teal labeled icon button" style="float: right;">
|
||||
<i class="refresh icon"></i>
|
||||
Refresh
|
||||
</button>
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
<template>
|
||||
<div>
|
||||
<div class="ui negative message" v-if="error">
|
||||
<div class="header">
|
||||
An error occured.
|
||||
</div>
|
||||
{{ error.message }}
|
||||
<template v-if="error.response">
|
||||
<br/>{{ error.response.data.Message }}
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div class="ui positive message" v-if="success">
|
||||
<div class="header">
|
||||
Success
|
||||
</div>
|
||||
{{ success }}
|
||||
</div>
|
||||
|
||||
<form class="ui form" v-bind:class="{ loading: loading }" v-if="!success" @submit.prevent="insertOrUpdateAuthor">
|
||||
<div class="field">
|
||||
<label>Forename</label>
|
||||
<input name="forename" placeholder="Forename" type="text" v-model="author.Forename">
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>Lastname</label>
|
||||
<input name="lastname" placeholder="Lastname" type="text" v-model="author.Lastname" required>
|
||||
</div>
|
||||
|
||||
<button class="ui blue button" type="submit">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {HTTP} from '../http-common'
|
||||
|
||||
export default {
|
||||
name: 'AuthorsAddEdit',
|
||||
data () {
|
||||
return {
|
||||
error: '',
|
||||
success: '',
|
||||
loading: false,
|
||||
authorID: this.$route.params.id,
|
||||
edit: false,
|
||||
author: {
|
||||
Forename: '',
|
||||
Lastname: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this.loading = true
|
||||
|
||||
// Look if we're in edit mode and get the author infos if nessesary
|
||||
if (this.authorID) {
|
||||
HTTP.get('authors/' + this.authorID)
|
||||
.then(response => {
|
||||
this.author = response.data
|
||||
this.edit = true
|
||||
})
|
||||
.catch(e => {
|
||||
this.error = e
|
||||
})
|
||||
}
|
||||
this.loading = false
|
||||
},
|
||||
methods: {
|
||||
insertOrUpdateAuthor: function () {
|
||||
if (this.author.Lastname === '') {
|
||||
this.error = {message: 'Please provide at least a lastname.'}
|
||||
} else {
|
||||
this.loading = true
|
||||
|
||||
// Finally Send it
|
||||
// If we want to newly insert it, make a different request
|
||||
if (this.edit) {
|
||||
HTTP.post('authors/' + this.author.ID, {author: this.author})
|
||||
.then(response => {
|
||||
this.loading = false
|
||||
this.success = 'The author was successfully updated!'
|
||||
this.error = ''
|
||||
})
|
||||
.catch(e => {
|
||||
this.loading = false
|
||||
this.error = e
|
||||
})
|
||||
} else { // insert a new author
|
||||
HTTP.put('authors', {author: this.author})
|
||||
.then(response => {
|
||||
this.loading = false
|
||||
this.success = 'The author was successfully inserted!'
|
||||
this.error = ''
|
||||
})
|
||||
.catch(e => {
|
||||
this.loading = false
|
||||
this.error = e
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
|
@ -5,6 +5,7 @@ import Login from '@/components/Login'
|
|||
import Books from '@/components/Books'
|
||||
import BooksAddEdit from '@/components/BooksAddEdit'
|
||||
import Authors from '@/components/Authors'
|
||||
import AuthorsAddEdit from '@/components/AuthorsAddEdit'
|
||||
|
||||
Vue.use(Router)
|
||||
|
||||
|
@ -39,6 +40,16 @@ export default new Router({
|
|||
path: '/authors',
|
||||
name: 'authors',
|
||||
component: Authors
|
||||
},
|
||||
{
|
||||
path: '/authors/add',
|
||||
name: 'authors-add',
|
||||
component: AuthorsAddEdit
|
||||
},
|
||||
{
|
||||
path: '/authors/:id/edit',
|
||||
name: 'author-edit',
|
||||
component: AuthorsAddEdit
|
||||
}
|
||||
],
|
||||
linkActiveClass: 'active'
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package models
|
||||
|
||||
// AddAuthor adds a new author based on an author struct
|
||||
func AddAuthor(author Author) (newAuthor Author, err error) {
|
||||
_, err = x.Insert(&author)
|
||||
|
||||
if err != nil {
|
||||
return Author{}, err
|
||||
}
|
||||
|
||||
// Get the newly inserted author
|
||||
newAuthor = author
|
||||
|
||||
return newAuthor, err
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package models
|
||||
|
||||
// AddOrUpdateAuthor adds a new author based on an author struct
|
||||
func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
|
||||
|
||||
// If the ID is 0, insert the author, otherwise update it
|
||||
if author.ID == 0 {
|
||||
_, err = x.Insert(&author)
|
||||
|
||||
if err != nil {
|
||||
return Author{}, err
|
||||
}
|
||||
} else {
|
||||
_, err = x.Where("id = ?", author.ID).Update(&author)
|
||||
|
||||
if err != nil {
|
||||
return Author{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// Get the newly inserted author
|
||||
newAuthor = author
|
||||
|
||||
return newAuthor, err
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package models
|
||||
|
||||
// UpdateAuthor updates an author by its ID and a new author struct
|
||||
func UpdateAuthor(author Author, id int64) (newAuthor Author, err error) {
|
||||
_, err = x.Where("id = ?", id).Update(&author)
|
||||
|
||||
if err != nil {
|
||||
return Author{}, err
|
||||
}
|
||||
|
||||
// Get the newly updated author
|
||||
newAuthor, _, err = GetAuthorByID(id)
|
||||
|
||||
return newAuthor, err
|
||||
}
|
|
@ -85,7 +85,7 @@ func AddOrUpdateBook(book Book) (newBook Book, err error) {
|
|||
if !exists {
|
||||
|
||||
// We have to insert authors on this inperformant way, because we need the ne ids afterwards
|
||||
insertedAuthor, err := AddAuthor(author)
|
||||
insertedAuthor, err := AddOrUpdateAuthor(author)
|
||||
|
||||
if err != nil {
|
||||
return Book{}, err
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AuthorAdd is the handler to add an author
|
||||
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
|
||||
newAuthor, err := models.AddAuthor(authorstruct)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, newAuthor)
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type authorPayload struct {
|
||||
Author models.Author `json:"author" form:"author"`
|
||||
}
|
||||
|
||||
// AuthorAddOrUpdate is the handler to add or update an author
|
||||
func AuthorAddOrUpdate(c echo.Context) error {
|
||||
// Check for Request Content
|
||||
authorFromString := c.FormValue("author")
|
||||
var authorToInsert models.Author
|
||||
|
||||
if authorFromString == "" {
|
||||
b := new(authorPayload)
|
||||
if err := c.Bind(b); err != nil {
|
||||
fmt.Println(err)
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"})
|
||||
}
|
||||
authorToInsert = b.Author
|
||||
} else {
|
||||
// Decode the JSON
|
||||
dec := json.NewDecoder(strings.NewReader(authorFromString))
|
||||
err := dec.Decode(&authorToInsert)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding author: " + err.Error()})
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we have at least a Lastname
|
||||
if authorToInsert.Lastname == "" {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least a lastame."})
|
||||
}
|
||||
|
||||
// Insert the author
|
||||
newAuthor, err := models.AddOrUpdateAuthor(authorToInsert)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, newAuthor)
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AuthorUpdate is the handler to update an author
|
||||
func AuthorUpdate(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"})
|
||||
}
|
||||
|
||||
// Look for the authors id
|
||||
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 not get author infos"})
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The author does not exist."})
|
||||
}
|
||||
|
||||
// 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
|
||||
newAuthor, err := models.UpdateAuthor(authorstruct, authorID)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, newAuthor)
|
||||
}
|
|
@ -13,8 +13,8 @@ type bookPayload struct {
|
|||
Book models.Book `json:"book" form:"book"`
|
||||
}
|
||||
|
||||
// BookAdd is the handler to add a book
|
||||
func BookAdd(c echo.Context) error {
|
||||
// BookAddOrUpdate is the handler to add a book
|
||||
func BookAddOrUpdate(c echo.Context) error {
|
||||
// Check for Request Content
|
||||
bookFromString := c.FormValue("book")
|
||||
var bookToInsert models.Book
|
|
@ -84,14 +84,14 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.POST("/tokenTest", apiv1.CheckToken)
|
||||
|
||||
// Manage Books
|
||||
a.PUT("/books", apiv1.BookAdd)
|
||||
a.PUT("/books", apiv1.BookAddOrUpdate)
|
||||
a.DELETE("/books/:id", apiv1.BookDelete)
|
||||
a.POST("/books/:id", apiv1.BookAdd)
|
||||
a.POST("/books/:id", apiv1.BookAddOrUpdate)
|
||||
|
||||
// Manage Authors
|
||||
a.PUT("/authors", apiv1.AuthorAdd)
|
||||
a.PUT("/authors", apiv1.AuthorAddOrUpdate)
|
||||
a.DELETE("/authors/:id", apiv1.AuthorDelete)
|
||||
a.POST("/authors/:id", apiv1.AuthorUpdate)
|
||||
a.POST("/authors/:id", apiv1.AuthorAddOrUpdate)
|
||||
|
||||
// Manage Publishers
|
||||
a.PUT("/publishers", apiv1.PublisherAdd)
|
||||
|
|
Loading…
Reference in New Issue