diff --git a/src/api.js b/src/api.js index 402b5844486d6536ee88b6725707a314f27c535d..d47a79c321ef28448e35670d4a6168e960fd08b9 100644 --- a/src/api.js +++ b/src/api.js @@ -1,6 +1,7 @@ +const serverurl = 'http://localhost:8181/' export const api = { async search (name, tags, param, sort, size, page) { - var url = this.$serverurl + 'search' + var url = serverurl + 'search' const params = new URLSearchParams() if (name != null) params.append('name', name) @@ -22,7 +23,7 @@ export const api = { } }, async getTags () { - const url = this.$serverurl + 'tags' + const url = serverurl + 'tags' const response = await fetch(url) try { return await response.json() @@ -32,7 +33,7 @@ export const api = { }, async getUser () { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'user' + const url = serverurl + 'user' try { const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) return await response.json() @@ -41,7 +42,7 @@ export const api = { } }, async addNewCategory (name) { - const url = this.$serverurl + 'tags/category' + const url = serverurl + 'tags/category' const token = await localStorage.getItem('token') try { const response = await fetch( @@ -63,7 +64,7 @@ export const api = { } }, async addNewTag (tag, categoryId) { - const url = this.$serverurl + 'tags' + const url = serverurl + 'tags' const token = await localStorage.getItem('token') try { const response = await fetch( @@ -87,7 +88,7 @@ export const api = { }, async getUserList () { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'user/list' + const url = serverurl + 'user/list' try { const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) return await response.json() @@ -97,7 +98,7 @@ export const api = { }, async getModelList () { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'models/list' + const url = serverurl + 'models/list' try { const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) return await response.json() @@ -107,7 +108,7 @@ export const api = { }, async getUserModels () { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'models' + const url = serverurl + 'models' try { const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) return await response.json() @@ -116,7 +117,7 @@ export const api = { } }, async getModel (id) { - const url = this.$serverurl + 'models' + '?id=' + id + const url = serverurl + 'models' + '?id=' + id const response = await fetch(url) try { return await response.json() @@ -126,7 +127,7 @@ export const api = { }, async removeModel (id) { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'models' + '?id=' + id + const url = serverurl + 'models' + '?id=' + id try { await fetch(url, { method: 'DELETE', @@ -141,7 +142,7 @@ export const api = { }, async removeTag (id) { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'tags' + '?id=' + id + const url = serverurl + 'tags' + '?id=' + id try { await fetch(url, { method: 'DELETE', @@ -156,7 +157,7 @@ export const api = { }, async removeCategory (id) { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'tags/category' + '?id=' + id + const url = serverurl + 'tags/category' + '?id=' + id try { await fetch(url, { method: 'DELETE', @@ -171,7 +172,7 @@ export const api = { }, async removeUser (id) { const token = await localStorage.getItem('token') - const url = this.$serverurl + 'user' + '?id=' + id + const url = serverurl + 'user' + '?id=' + id try { await fetch(url, { method: 'DELETE', @@ -185,7 +186,7 @@ export const api = { } }, async login (username, password) { - const url = this.$serverurl + 'login' + const url = serverurl + 'login' try { const response = await fetch( url, { @@ -206,7 +207,7 @@ export const api = { } }, async register (username, email, password) { - const url = this.$serverurl + 'user/signup' + const url = serverurl + 'user/signup' try { const response = await fetch( url, { diff --git a/src/components/LoginForm.vue b/src/components/LoginForm.vue index b3dc08df24c9b135bbe1db6b50faaf91205cf5fc..ea97e8a7f79afe0f2ccaeb8ad4e8835af3c7551c 100644 --- a/src/components/LoginForm.vue +++ b/src/components/LoginForm.vue @@ -26,6 +26,8 @@ </template> <script> +import { api } from '@/api.js' + export default { name: 'LoginForm', data () { @@ -37,7 +39,7 @@ export default { }, methods: { async onLogin () { - var response = await this.login(this.username, this.password) + var response = await api.login(this.username, this.password) console.log('LOGIN RESPONSE ' + response) if (response !== null) { console.log('LOGIN TOKEN OK') @@ -47,27 +49,6 @@ export default { } else { this.errorMessage = 'Username or password incorect' } - }, - async login (username, password) { - const url = this.$serverurl + 'login' - try { - const response = await fetch( - url, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - username: username, - password: password - }) - } - ) - return await response.json() - } catch (error) { - return null - } } } } diff --git a/src/components/ModelTable.vue b/src/components/ModelTable.vue index a5701f3ea50e1348f36df936d310e0be59bccdfe..b2c3c363a8cf30959dbfc3ac63122d708104b919 100644 --- a/src/components/ModelTable.vue +++ b/src/components/ModelTable.vue @@ -27,6 +27,8 @@ </template> <script> +import { api } from '@/api.js' + export default { name: 'ModelTable', data () { @@ -36,23 +38,10 @@ export default { } }, async mounted () { - this.models = await this.getModels() + this.models = await api.getModels() this.isLoading = false }, methods: { - async getModels () { - const token = await localStorage.getItem('token') - const url = this.$serverurl + 'models' - try { - const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) - return await response.json() - } catch (error) { - return null - } - }, - edit (id) { - - }, removePrompt (id, name) { this.$buefy.dialog.confirm({ message: 'Delete ' + name + ' ?', diff --git a/src/components/RegisterForm.vue b/src/components/RegisterForm.vue index b553034e610834591ceac1ad85a989e58612cd26..4c62020a472cf41378b0e9d6e85d69a1f4bd3aff 100644 --- a/src/components/RegisterForm.vue +++ b/src/components/RegisterForm.vue @@ -31,6 +31,8 @@ </template> <script> +import { api } from '@/api.js' + export default { name: 'RegisterForm', data () { @@ -48,7 +50,7 @@ export default { this.errorMessage = 'Passwords are different' return } - const response = await this.register(this.username, this.email, this.password) + const response = await api.register(this.username, this.email, this.password) if (response.error) { this.errorMessage = response.message } else { @@ -56,28 +58,6 @@ export default { this.$parent.close() this.$router.go() } - }, - async register (username, email, password) { - const url = this.$serverurl + 'user/signup' - try { - const response = await fetch( - url, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - email: email, - username: username, - password: password - }) - } - ) - return await response.json() - } catch (error) { - return null - } } } } diff --git a/src/components/TagEditor.vue b/src/components/TagEditor.vue index f78f2898426cfcf72950fb0fe858eafaa5222e16..efeac0155a554b6d97016b3fd9d84ada23a0280c 100644 --- a/src/components/TagEditor.vue +++ b/src/components/TagEditor.vue @@ -58,6 +58,8 @@ </template> <script> +import { api } from '@/api.js' + export default { name: 'TagEditor', props: { @@ -75,18 +77,9 @@ export default { } }, async mounted () { - this.aviablesTags = await this.getTags() + this.aviablesTags = await api.getTags() }, methods: { - async getTags () { - const url = this.$serverurl + 'models/tags' - const response = await fetch(url) - try { - return await response.json() - } catch (error) { - return null - } - }, addNewTagDialog (categoryId) { this.$buefy.dialog.prompt({ message: 'Tag', @@ -95,34 +88,11 @@ export default { }, confirmText: 'Add', onConfirm: async (value) => { - await this.addNewTag(value, categoryId) - this.aviablesTags = await this.getTags() + await api.addNewTag(value, categoryId) + this.aviablesTags = await api.getTags() } }) }, - async addNewTag (tag, categoryId) { - const url = this.$serverurl + 'models/tags' - const token = await localStorage.getItem('token') - try { - const response = await fetch( - url, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + token - }, - body: JSON.stringify({ - name: tag, - categoryId: categoryId - }) - } - ) - return await response.json() - } catch (error) { - return null - } - }, addTagToModel (tag) { this.modelTags.push(tag) this.syncToParent() diff --git a/src/components/TagEditorOLD.vue b/src/components/TagEditorOLD.vue deleted file mode 100644 index 23a212badeca5d3f85c94d47f2a455814a629221..0000000000000000000000000000000000000000 --- a/src/components/TagEditorOLD.vue +++ /dev/null @@ -1,146 +0,0 @@ -<template> - <div class="tagEditor"> - <b-field position="is-centered" has-addons> - <b-autocomplete - v-model="tagName" - :open-on-focus="true" - :data="filteredTags" - icon-right="close-circle" - icon-right-clickable> - <template slot="header"> - <a @click="addNewTagDialog"> - <span> Add new... </span> - </a> - </template> - </b-autocomplete> - <p class="control"> - <b-select placeholder="Select a category" v-model="selectedCategory"> - <option v-for="category in tagList" v-bind:key="category.name" :value="category.name">{{category.name}}</option> - </b-select> - </p> - <p class="control"> - <b-button class="control is-primary" @click="addTagToModel">Add</b-button> - </p> - </b-field> - - <b-taglist> - <b-tag - v-for="tag in tags" - v-bind:key="tag.name" - type="is-info" - @close="removeTagFromModel(tag)" - closable> - {{tag.name}} - </b-tag> - </b-taglist> - </div> -</template> - -<script> -export default { - name: 'TagEditor', - props: { - tags: { - type: Array, - default: function () { - return [] - } - } - }, - data () { - return { - tagName: '', - selectedCategory: null, - tagList: [] - } - }, - computed: { - filteredTags () { - if (this.selectedCategory === null) return [] - var category = this.tagList.filter((tag) => { - return tag.name === this.selectedCategory - }) - return category[0].tags.filter((tag) => { - return tag - .toString() - .toLowerCase() - .indexOf(this.tagName.toLowerCase()) >= 0 - }) - } - }, - async mounted () { - this.tagList = await this.getTags() - }, - methods: { - async getTags () { - const url = this.$serverurl + 'models/tags' - const response = await fetch(url) - try { - return await response.json() - } catch (error) { - return null - } - }, - addNewTagDialog () { - this.$buefy.dialog.prompt({ - message: 'Tag', - inputAttrs: { - maxlength: 20, - value: this.name - }, - confirmText: 'Add', - onConfirm: (value) => { - this.addNewTag(value) - var category = this.tagList.filter((tag) => { - return tag.name === this.selectedCategory - }) - category[0].types.push(value) - } - }) - }, - async addNewTag (tag) { - const url = this.$serverurl + 'models/tags' - try { - const response = await fetch( - url, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - name: this.selectedCategory, - type: tag - }) - } - ) - return await response.json() - } catch (error) { - return null - } - }, - addTagToModel () { - this.tags.push({ - name: this.selectedCategory, - type: this.tagName - }) - this.tagName = '' - this.syncToParent() - }, - removeTagFromModel (tag) { - const index = this.tags.indexOf(tag) - if (index > -1) { - this.tags.splice(index, 1) - } - this.syncToParent() - }, - syncToParent () { - this.$emit('update-tags', this.tags) - } - } -} -</script> - -<style> - -</style> diff --git a/src/views/Account.vue b/src/views/Account.vue index ce6f3e30774d493d8e951475549acff8c133bc50..1d33f8cf2ee02ff5cbacebb426f004aaff949e56 100644 --- a/src/views/Account.vue +++ b/src/views/Account.vue @@ -35,11 +35,16 @@ <h1 class="title is-4">My models</h1> <ModelTable/> </div> + + <div class="box"> + <h1 class="title is-4">Liked models</h1> + </div> </div> </template> <script> import ModelTable from '@/components/ModelTable.vue' +import { api } from '@/api.js' export default { name: 'Account', @@ -48,11 +53,13 @@ export default { }, data () { return { - account: {} + account: {}, + models: [] } }, async mounted () { - this.account = await this.getAccount() + this.account = await api.getUser() + this.models = await api.getModels() await localStorage.setItem('user_role', this.account.role) await localStorage.setItem('user_id', this.account.id) }, diff --git a/src/views/Admin.vue b/src/views/Admin.vue index 52099b934769786d4ab7732008cb022dbb70ce11..a0639a5fa293084a709e20129987530d4262de1f 100644 --- a/src/views/Admin.vue +++ b/src/views/Admin.vue @@ -87,6 +87,8 @@ </template> <script> +import { api } from '@/api.js' + export default { name: 'Admin', data () { @@ -98,41 +100,12 @@ export default { } }, async mounted () { - this.modelList = await this.getModelList() - this.userList = await this.getUserList() - this.tagList = await this.getTags() + this.modelList = await api.getModelList() + this.userList = await api.getUserList() + this.tagList = await api.getTags() this.isLoading = false }, methods: { - async getModelList () { - const token = await localStorage.getItem('token') - const url = this.$serverurl + 'models/list' - try { - const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) - return await response.json() - } catch (error) { - return null - } - }, - async getUserList () { - const token = await localStorage.getItem('token') - const url = this.$serverurl + 'user/list' - try { - const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) - return await response.json() - } catch (error) { - return null - } - }, - async getTags () { - const url = this.$serverurl + 'models/tags' - const response = await fetch(url) - try { - return await response.json() - } catch (error) { - return null - } - }, removePrompt (id, name, mode) { this.$buefy.dialog.confirm({ message: 'Delete ' + name + ' ?', @@ -142,32 +115,18 @@ export default { hasIcon: true, onConfirm: async () => { switch (mode) { - case 'user' : this.remove(id, name, 'user'); break - case 'model' : this.remove(id, name, 'models'); break - case 'tag' : this.remove(id, name, 'models/tags'); break - case 'category' : this.remove(id, name, 'models/category'); break + case 'user' : api.removeUser(id); break + case 'model' : api.removeModel(id); break + case 'tag' : api.removeTag(id); break + case 'category' : api.removeCategory(id); break } - this.modelList = await this.getModelList() - this.userList = await this.getUserList() - this.tagList = await this.getTags() + this.$buefy.toast.open(name + ' deleted') + this.modelList = await api.getModelList() + this.userList = await api.getUserList() + this.tagList = await api.getTags() } }) }, - async remove (id, name, type) { - const token = await localStorage.getItem('token') - const url = this.$serverurl + type + '?id=' + id - try { - await fetch(url, { - method: 'DELETE', - headers: { - Authorization: 'Bearer ' + token - } - }) - this.$buefy.toast.open(name + ' deleted') - } catch (error) { - this.$buefy.toast.open('Delete error ' + error) - } - }, addNewTagDialog (categoryId) { this.$buefy.dialog.prompt({ message: 'Tag', @@ -176,34 +135,11 @@ export default { }, confirmText: 'Add', onConfirm: async (value) => { - await this.addNewTag(value, categoryId) - this.tagList = await this.getTags() + await api.addNewTag(value, categoryId) + this.tagList = await api.getTags() } }) }, - async addNewTag (tag, categoryId) { - const url = this.$serverurl + 'models/tags' - const token = await localStorage.getItem('token') - try { - const response = await fetch( - url, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + token - }, - body: JSON.stringify({ - name: tag, - categoryId: categoryId - }) - } - ) - return await response.json() - } catch (error) { - return null - } - }, addNewCategoryDialog () { this.$buefy.dialog.prompt({ message: 'Category', @@ -212,32 +148,10 @@ export default { }, confirmText: 'Add', onConfirm: async (value) => { - await this.addNewCategory(value) - this.tagList = await this.getTags() + await api.addNewCategory(value) + this.tagList = await api.getTags() } }) - }, - async addNewCategory (name) { - const url = this.$serverurl + 'models/category' - const token = await localStorage.getItem('token') - try { - const response = await fetch( - url, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + token - }, - body: JSON.stringify({ - name: name - }) - } - ) - return await response.json() - } catch (error) { - return null - } } } } diff --git a/src/views/Model.vue b/src/views/Model.vue index 06aea1ffe4ba348d6c2afcf4fb2771cbd1bf1c0c..fda6e635790aa3ed7b30a4f84995a6fd3220353e 100644 --- a/src/views/Model.vue +++ b/src/views/Model.vue @@ -69,6 +69,7 @@ <script> import Comments from '@/components/Comments.vue' import marked from 'marked' +import { api } from '@/api.js' export default { name: 'Model', @@ -91,7 +92,7 @@ export default { }, async mounted () { const id = this.$route.query.id - this.model = await this.getModel(id) + this.model = await api.getModel(id) if (this.model == null) this.isError = true this.isLoading = false @@ -104,15 +105,6 @@ export default { this.downloadUrl = this.$serverurl + 'models/download?id=' + this.model.id }, methods: { - async getModel (id) { - const url = this.$serverurl + 'models' + '?id=' + id - const response = await fetch(url) - try { - return await response.json() - } catch (error) { - return null - } - }, downloadModel (id) { }, downloadLayer (id) { diff --git a/src/views/ModelEdit.vue b/src/views/ModelEdit.vue index b047404dd4c8c0463c53ffa2cbe1d747a03fda4f..87b39ebf0e9007b54c97d6c08c1d40c02e6d8b28 100644 --- a/src/views/ModelEdit.vue +++ b/src/views/ModelEdit.vue @@ -77,6 +77,7 @@ import markdownEditor from '@/components/MarkdownEditor.vue' import tagEditor from '@/components/TagEditor.vue' import layersEditor from '@/components/LayersEditor.vue' import ModelUpload from '@/components/ModelUpload.vue' +import { api } from '@/api.js' export default { name: 'ModelEdit', @@ -120,24 +121,8 @@ export default { confirmText: 'Delete', type: 'is-danger', hasIcon: true, - onConfirm: () => this.remove() + onConfirm: () => api.removeModel(this.model.id) }) - }, - async remove () { - const token = await localStorage.getItem('token') - const url = this.$serverurl + 'models' + '?id=' + this.model.id - try { - await fetch(url, { - method: 'DELETE', - headers: { - Authorization: 'Bearer ' + token - } - }) - this.$buefy.toast.open(this.model.name + ' deleted') - this.getModels() - } catch (error) { - this.$buefy.toast.open('Delete error ' + error) - } } } } diff --git a/src/views/Search.vue b/src/views/Search.vue index 192d24e0527a5ea2c00fda99db0da8f1cb725922..217c53f02d53db65515309a9702573130e3c7043 100644 --- a/src/views/Search.vue +++ b/src/views/Search.vue @@ -39,6 +39,7 @@ <script> import Filters from '@/components/Filters.vue' import ModelCard from '@/components/ModelCard.vue' +import { api } from '@/api.js' export default { name: 'Search', @@ -53,7 +54,6 @@ export default { paramTags: '', paramSearch: '', paramOrder: '', - paramPerf: '', resultSize: 10, page: 1, isLoading: true @@ -62,57 +62,26 @@ export default { async mounted () { const search = this.$route.query.n if (search != null) this.paramSearch = search - this.tags = await this.getTags() - this.result = await this.getResult() + this.tags = await api.getTags() + this.result = await api.search(this.paramSearch, this.paramTags, null, this.paramOrder, this.resultSize, this.page) this.isLoading = false }, methods: { async changePage (page) { this.page = page - this.result = await this.getResult() + this.result = await api.search(this.paramSearch, this.paramTags, null, this.paramOrder, this.resultSize, this.page) }, async setSearch () { - this.result = await this.getResult() + this.result = await api.search(this.paramSearch, this.paramTags, null, this.paramOrder, this.resultSize, this.page) }, async setOrder (order) { this.paramOrder = order - this.result = await this.getResult() + this.result = await api.search(this.paramSearch, this.paramTags, null, this.paramOrder, this.resultSize, this.page) }, async setTags (tags, perfValue) { this.paramTags = tags this.paramPerf = perfValue - this.result = await this.getResult() - }, - async getTags () { - const url = this.$serverurl + 'models/tags' - const response = await fetch(url) - try { - return await response.json() - } catch (error) { - return null - } - }, - async getResult () { - var url = this.$serverurl + 'search' - const params = new URLSearchParams() - - if (this.paramSearch != null) params.append('name', this.paramSearch) - if (this.paramTags != null) params.append('tags', this.paramTags) - if (this.paramPerf != null) params.append('param', this.paramPerf) - if (this.paramOrder != null) params.append('sort', this.paramOrder) - - params.append('size', this.resultSize) - params.append('page', this.page) - - console.log('get models ' + this.paramSearch + ' + ' + this.paramPerf + ' + ' + this.paramTags) - - url += '?' + params - const response = await fetch(url) - try { - return await response.json() - } catch (error) { - return null - } + this.result = await api.search(this.paramSearch, this.paramTags, null, this.paramOrder, this.resultSize, this.page) } } }