At least a title is now required when inserting or updating a book

This commit is contained in:
konrad 2017-11-21 10:33:37 +01:00 committed by kolaente
parent b45ceeb3e5
commit 8c487d17d6
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 67 additions and 56 deletions

View File

@ -6,7 +6,7 @@
</div> </div>
{{ error.message }} {{ error.message }}
<template v-if="error.response"> <template v-if="error.response">
{{ error.response.Message }} <br/>{{ error.response.data.Message }}
</template> </template>
</div> </div>
@ -20,7 +20,7 @@
<form class="ui form" v-bind:class="{ loading: loading }" v-if="!success"> <form class="ui form" v-bind:class="{ loading: loading }" v-if="!success">
<div class="field"> <div class="field">
<label>Title</label> <label>Title</label>
<input name="title" placeholder="Title" type="text" v-model="book.Title"> <input name="title" placeholder="Title" type="text" v-model="book.Title" required>
</div> </div>
<div class="three fields"> <div class="three fields">
<div class="field"> <div class="field">
@ -100,7 +100,7 @@
</div> </div>
</div> </div>
<a class="ui button" type="submit" @click="insertNewBook()">Submit</a> <a class="ui blue button" type="submit" @click="insertNewBook()">Submit</a>
</form> </form>
</div> </div>
</template> </template>
@ -218,64 +218,70 @@
this.addAuthorForm.splice(i, 1) this.addAuthorForm.splice(i, 1)
}, },
insertNewBook: function () { insertNewBook: function () {
this.loading = true if (this.book.Title === '') {
this.error = {message: 'Please provide at least a title.'}
} else {
this.loading = true
// Add all newly created authors to it // Add all newly created authors to it
let as = this.newAuthors let as = this.newAuthors
for (const i in as) { for (const i in as) {
this.book.Authors.push({ this.book.Authors.push({
ID: 0, ID: 0,
Name: as[i] Name: as[i]
}) })
}
// Beautify all Authors aka split the names in forename and lastname
for (const i in this.book.Authors) {
let author = this.book.Authors[i].Name
let firstname = ''
let lastname = ''
// Check if the name contains a space, if so split it up
if ((new RegExp(' ')).test(author)) {
let split = author.indexOf(' ')
let splits = [author.slice(0, split), author.slice(split + 1)]
firstname = splits[0]
lastname = splits[1]
} else {
lastname = author
} }
// Put it all together // Beautify all Authors aka split the names in forename and lastname
this.book.Authors[i] = { for (const i in this.book.Authors) {
ID: this.book.Authors[i].ID, let author = this.book.Authors[i].Name
Forename: firstname, let firstname = ''
Lastname: lastname let lastname = ''
}
}
// Finally Send it // Check if the name contains a space, if so split it up
// If we want to newly insert it, make a different request if ((new RegExp(' ')).test(author)) {
if (this.edit) { let split = author.indexOf(' ')
HTTP.post('books/' + this.book.ID, {book: this.book}) let splits = [author.slice(0, split), author.slice(split + 1)]
.then(response => { firstname = splits[0]
this.loading = false lastname = splits[1]
this.success = 'The book was successfully updated!' } else {
}) lastname = author
.catch(e => { }
this.loading = false
this.error = e // Put it all together
}) this.book.Authors[i] = {
} else { // insert a new book ID: this.book.Authors[i].ID,
HTTP.put('books', {book: this.book}) Forename: firstname,
.then(response => { Lastname: lastname
this.loading = false }
this.success = 'The book was successfully inserted!' }
})
.catch(e => { // Finally Send it
this.loading = false // If we want to newly insert it, make a different request
this.error = e if (this.edit) {
}) HTTP.post('books/' + this.book.ID, {book: this.book})
.then(response => {
this.loading = false
this.success = 'The book was successfully updated!'
this.error = ''
})
.catch(e => {
this.loading = false
this.error = e
})
} else { // insert a new book
HTTP.put('books', {book: this.book})
.then(response => {
this.loading = false
this.success = 'The book was successfully inserted!'
this.error = ''
})
.catch(e => {
this.loading = false
this.error = e
})
}
} }
} }
} }

View File

@ -36,6 +36,11 @@ func BookAdd(c echo.Context) error {
} }
} }
// Check if we have at least a title
if bookToInsert.Title == "" {
return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"})
}
// Insert the book // Insert the book
newBook, err := models.AddOrUpdateBook(bookToInsert) newBook, err := models.AddOrUpdateBook(bookToInsert)