From 0749df43abfacad5d8da705415a6149402b1dc9d Mon Sep 17 00:00:00 2001 From: "thomas.blanc.2@etu.univ-amu.fr" <gltron3000@gmail.com> Date: Fri, 22 May 2020 16:05:47 +0200 Subject: [PATCH] Fixed date & api refactor part1 --- src/api.js | 230 ++++++++++++++++++++++++++++++++++ src/components/Filters.vue | 2 +- src/components/ModelCard.vue | 2 +- src/components/ModelTable.vue | 4 +- src/main.js | 2 +- src/views/Model.vue | 4 +- 6 files changed, 237 insertions(+), 7 deletions(-) create mode 100644 src/api.js diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..402b584 --- /dev/null +++ b/src/api.js @@ -0,0 +1,230 @@ +export const api = { + async search (name, tags, param, sort, size, page) { + var url = this.$serverurl + 'search' + const params = new URLSearchParams() + + if (name != null) params.append('name', name) + if (tags != null) params.append('tags', tags) + if (param != null) params.append('param', param) + if (sort != null) params.append('sort', sort) + + params.append('size', size) + params.append('page', 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 + } + }, + async getTags () { + const url = this.$serverurl + 'tags' + const response = await fetch(url) + try { + return await response.json() + } catch (error) { + return null + } + }, + async getUser () { + const token = await localStorage.getItem('token') + const url = this.$serverurl + 'user' + try { + const response = await fetch(url, { headers: { Authorization: 'Bearer ' + token } }) + return await response.json() + } catch (error) { + return null + } + }, + async addNewCategory (name) { + const url = this.$serverurl + 'tags/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 + } + }, + async addNewTag (tag, categoryId) { + const url = this.$serverurl + '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 + } + }, + 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 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 getUserModels () { + 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 + } + }, + async getModel (id) { + const url = this.$serverurl + 'models' + '?id=' + id + const response = await fetch(url) + try { + return await response.json() + } catch (error) { + return null + } + }, + async removeModel (id) { + const token = await localStorage.getItem('token') + const url = this.$serverurl + 'models' + '?id=' + id + try { + await fetch(url, { + method: 'DELETE', + headers: { + Authorization: 'Bearer ' + token + } + }) + return true + } catch (error) { + return false + } + }, + async removeTag (id) { + const token = await localStorage.getItem('token') + const url = this.$serverurl + 'tags' + '?id=' + id + try { + await fetch(url, { + method: 'DELETE', + headers: { + Authorization: 'Bearer ' + token + } + }) + return true + } catch (error) { + return false + } + }, + async removeCategory (id) { + const token = await localStorage.getItem('token') + const url = this.$serverurl + 'tags/category' + '?id=' + id + try { + await fetch(url, { + method: 'DELETE', + headers: { + Authorization: 'Bearer ' + token + } + }) + return true + } catch (error) { + return false + } + }, + async removeUser (id) { + const token = await localStorage.getItem('token') + const url = this.$serverurl + 'user' + '?id=' + id + try { + await fetch(url, { + method: 'DELETE', + headers: { + Authorization: 'Bearer ' + token + } + }) + return true + } catch (error) { + return false + } + }, + 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 + } + }, + 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/Filters.vue b/src/components/Filters.vue index 07d21b7..4e77197 100644 --- a/src/components/Filters.vue +++ b/src/components/Filters.vue @@ -2,7 +2,7 @@ <b-menu> <div> <b-menu-list v-for="category in tags" v-bind:key="category.name" :label="category.name"> - <div v-for="tag in category.tags" v-bind:key="tag" class="field"> + <div v-for="tag in category.tags" v-bind:key="tag.name" class="field"> <b-checkbox :native-value="tag.name" v-model="checkboxGroup" diff --git a/src/components/ModelCard.vue b/src/components/ModelCard.vue index 26263a2..7c8a1f7 100644 --- a/src/components/ModelCard.vue +++ b/src/components/ModelCard.vue @@ -7,7 +7,7 @@ <p> <strong>{{model.name}}</strong> <br> - <small>{{model.author.username}}</small> - <small>{{new Date(model.modificationDate).toLocaleDateString()}}</small> + <small>{{model.author.username}}</small> - <small>{{new Date(model.lastModified).toLocaleDateString()}}</small> <br> {{model.shortDescription}} </p> diff --git a/src/components/ModelTable.vue b/src/components/ModelTable.vue index f080a5e..a5701f3 100644 --- a/src/components/ModelTable.vue +++ b/src/components/ModelTable.vue @@ -10,11 +10,11 @@ </b-table-column> <b-table-column field="date" label="Last modification" centered sortable> - {{ new Date(props.row.modificationDate).toLocaleDateString() }} + {{ new Date(props.row.lastModified).toLocaleDateString() }} </b-table-column> <b-table-column field="date" label="Added" centered sortable> - {{ new Date(props.row.addedDate).toLocaleDateString() }} + {{ new Date(props.row.added).toLocaleDateString() }} </b-table-column> <b-table-column label="" centered> diff --git a/src/main.js b/src/main.js index 75cf152..aeaa0a3 100644 --- a/src/main.js +++ b/src/main.js @@ -9,7 +9,7 @@ Vue.use(Buefy) Vue.config.productionTip = false // Vue.prototype.$serverurl = process.env.VUE_APP_SERVER_URL -Vue.prototype.$serverurl = '/' +Vue.prototype.$serverurl = 'https://mozen.gltronic.ovh/' new Vue({ router, diff --git a/src/views/Model.vue b/src/views/Model.vue index 7b15081..fec946b 100644 --- a/src/views/Model.vue +++ b/src/views/Model.vue @@ -22,9 +22,9 @@ <h1 class="title is-4">{{model.votes}} votes <b-button size="is-small" icon-left="heart-outline" style="top:-2px"/></h1> <small>Author: </small><strong>{{model.author.username}}</strong> <br> - <small>Added: </small><strong>{{new Date(model.addedDate).toLocaleDateString()}}</strong> + <small>Added: </small><strong>{{new Date(model.added).toLocaleDateString()}}</strong> <br> - <small>Last modification: </small><strong>{{new Date(model.modificationDate).toLocaleDateString()}}</strong> + <small>Last modification: </small><strong>{{new Date(model.lastModified).toLocaleDateString()}}</strong> <br> <br> <b-button type="is-primary" size="is-medium" icon-left="download"> -- GitLab