github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/bv/src/pages/vote/admin/controller.ts (about) 1 import Axios, { AxiosResponse } from "axios"; 2 import { adjustUrlOrigin, BaseController } from "@/shared/logic/controller"; 3 import { 4 Membre, 5 VoteAdmin, 6 VoteCandidats, 7 LockVote 8 } from "@/shared/logic/types"; 9 10 export const BASE_URL = adjustUrlOrigin() + "/api"; 11 12 class Controller extends BaseController { 13 membres: Membre[] = []; 14 15 async getMembres() { 16 try { 17 const r: AxiosResponse<Membre[]> = await Axios.get(BASE_URL + "/membres"); 18 this.membres = r.data; 19 return true; 20 } catch (error) { 21 this.notifications.onAxiosError(error); 22 } 23 } 24 25 async inviteOne(membre: Membre) { 26 try { 27 await Axios.put(BASE_URL + "/membres", membre); 28 return true; 29 } catch (error) { 30 this.notifications.onAxiosError(error); 31 } 32 } 33 34 async inviteAll() { 35 try { 36 await Axios.post(BASE_URL + "/membres"); 37 return true; 38 } catch (error) { 39 this.notifications.onAxiosError(error); 40 } 41 } 42 43 async getVotes() { 44 try { 45 const r: AxiosResponse<VoteAdmin[]> = await Axios.get( 46 BASE_URL + "/votes" 47 ); 48 return r.data; 49 } catch (error) { 50 this.notifications.onAxiosError(error); 51 } 52 } 53 54 async createVote(vote: VoteCandidats) { 55 try { 56 const r: AxiosResponse<VoteAdmin[]> = await Axios.put( 57 BASE_URL + "/votes", 58 vote 59 ); 60 return r.data; 61 } catch (error) { 62 this.notifications.onAxiosError(error); 63 } 64 } 65 66 async updateVote(vote: VoteCandidats) { 67 try { 68 const r: AxiosResponse<VoteAdmin[]> = await Axios.post( 69 BASE_URL + "/votes", 70 vote 71 ); 72 return r.data; 73 } catch (error) { 74 this.notifications.onAxiosError(error); 75 } 76 } 77 78 async lockVote(params: LockVote) { 79 try { 80 const r: AxiosResponse<VoteAdmin[]> = await Axios.post( 81 BASE_URL + "/votes/lock", 82 params 83 ); 84 return r.data; 85 } catch (error) { 86 this.notifications.onAxiosError(error); 87 } 88 } 89 90 async clearVote(idVote: number) { 91 try { 92 const r: AxiosResponse<VoteAdmin[]> = await Axios.get( 93 BASE_URL + "/votes/clear", 94 { params: { id: idVote } } 95 ); 96 return r.data; 97 } catch (error) { 98 this.notifications.onAxiosError(error); 99 } 100 } 101 102 async deleteVote(idVote: number) { 103 try { 104 const r: AxiosResponse<VoteAdmin[]> = await Axios.delete( 105 BASE_URL + "/votes", 106 { params: { id: idVote } } 107 ); 108 return r.data; 109 } catch (error) { 110 this.notifications.onAxiosError(error); 111 } 112 } 113 114 async exportVotes() { 115 try { 116 const r: AxiosResponse<Blob> = await Axios.get( 117 BASE_URL + "/votes/export" 118 ); 119 return r.data; 120 } catch (error) { 121 this.notifications.onAxiosError(error); 122 } 123 } 124 } 125 126 export const C = new Controller();