Skip to content
Snippets Groups Projects
Commit 515634db authored by thomas.blanc.2@etu.univ-amu.fr's avatar thomas.blanc.2@etu.univ-amu.fr
Browse files

Refactored API calls

parent 8e3cd089
Branches
No related tags found
No related merge requests found
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, {
......
......@@ -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
}
}
}
}
......
......@@ -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 + ' ?',
......
......@@ -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
}
}
}
}
......
......@@ -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()
......
<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>
......@@ -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)
},
......
......@@ -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,31 +115,17 @@ 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
}
this.modelList = await this.getModelList()
this.userList = await this.getUserList()
this.tagList = await this.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
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.$buefy.toast.open(name + ' deleted')
} catch (error) {
this.$buefy.toast.open('Delete error ' + error)
this.modelList = await api.getModelList()
this.userList = await api.getUserList()
this.tagList = await api.getTags()
}
})
},
addNewTagDialog (categoryId) {
this.$buefy.dialog.prompt({
......@@ -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
}
}
}
}
......
......@@ -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) {
......
......@@ -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)
}
}
}
}
......
......@@ -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)
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment