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>
{{ error.message }}
<template v-if="error.response">
{{ error.response.Message }}
<br/>{{ error.response.data.Message }}
</template>
</div>
@ -20,7 +20,7 @@
<form class="ui form" v-bind:class="{ loading: loading }" v-if="!success">
<div class="field">
<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 class="three fields">
<div class="field">
@ -100,7 +100,7 @@
</div>
</div>
<a class="ui button" type="submit" @click="insertNewBook()">Submit</a>
<a class="ui blue button" type="submit" @click="insertNewBook()">Submit</a>
</form>
</div>
</template>
@ -218,64 +218,70 @@
this.addAuthorForm.splice(i, 1)
},
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
let as = this.newAuthors
// Add all newly created authors to it
let as = this.newAuthors
for (const i in as) {
this.book.Authors.push({
ID: 0,
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
for (const i in as) {
this.book.Authors.push({
ID: 0,
Name: as[i]
})
}
// Put it all together
this.book.Authors[i] = {
ID: this.book.Authors[i].ID,
Forename: firstname,
Lastname: lastname
}
}
// 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 = ''
// Finally Send it
// If we want to newly insert it, make a different request
if (this.edit) {
HTTP.post('books/' + this.book.ID, {book: this.book})
.then(response => {
this.loading = false
this.success = 'The book was successfully updated!'
})
.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!'
})
.catch(e => {
this.loading = false
this.error = e
})
// 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
this.book.Authors[i] = {
ID: this.book.Authors[i].ID,
Forename: firstname,
Lastname: lastname
}
}
// Finally Send it
// If we want to newly insert it, make a different request
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
newBook, err := models.AddOrUpdateBook(bookToInsert)