github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/bv/src/pages/admin/passwords/controller.ts (about) 1 import { BaseController, adjustUrlOrigin } from "@/shared/logic/controller"; 2 import Axios, { AxiosResponse } from "axios"; 3 import { GetPasswordsOut, PublicPassword } from "./types"; 4 import Vue from "vue"; 5 6 const BASE_URL = adjustUrlOrigin(); 7 const API_URL = BASE_URL + "/api"; 8 9 class Controller extends BaseController { 10 passwords: NonNullable<GetPasswordsOut> = {}; 11 12 async getPasswords() { 13 try { 14 const r: AxiosResponse<GetPasswordsOut> = await Axios.get(API_URL); 15 this.passwords = r.data || {}; 16 } catch (error) { 17 this.notifications.onAxiosError(error); 18 } 19 } 20 21 async createPassword(password: PublicPassword) { 22 try { 23 const r: AxiosResponse<PublicPassword> = await Axios.put( 24 API_URL, 25 password 26 ); 27 Vue.set(this.passwords, r.data.id, r.data); 28 this.notifications.success = { 29 message: "Mot de passe ajouté avec succès.", 30 title: "Création" 31 }; 32 } catch (error) { 33 this.notifications.onAxiosError(error); 34 } 35 } 36 37 async updatePassword(password: PublicPassword) { 38 try { 39 const r: AxiosResponse<PublicPassword> = await Axios.post( 40 API_URL, 41 password 42 ); 43 Vue.set(this.passwords, r.data.id, r.data); 44 this.notifications.success = { 45 message: "Mot de passe modifié avec succès.", 46 title: "Modification" 47 }; 48 } catch (error) { 49 this.notifications.onAxiosError(error); 50 } 51 } 52 53 async deletePassword(idPassword: number) { 54 try { 55 await Axios.delete(API_URL, { params: { id: idPassword } }); 56 Vue.delete(this.passwords, idPassword); 57 this.notifications.success = { 58 message: "Mot de passe supprimé avec succès.", 59 title: "Suppression" 60 }; 61 } catch (error) { 62 this.notifications.onAxiosError(error); 63 } 64 } 65 } 66 67 export const C = new Controller();