2024-09-09 13:13:45 +02:00
|
|
|
import { defineStore } from "pinia";
|
2024-09-15 13:52:54 +02:00
|
|
|
import type { CreateAwardViewModel, UpdateAwardViewModel, AwardViewModel } from "../../viewmodels/admin/award.models";
|
2024-09-09 13:13:45 +02:00
|
|
|
import { http } from "../../serverCom";
|
2024-09-15 13:52:54 +02:00
|
|
|
import type { AxiosResponse } from "axios";
|
2024-09-09 13:13:45 +02:00
|
|
|
|
|
|
|
export const useAwardStore = defineStore("award", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
awards: [] as Array<AwardViewModel>,
|
2024-09-15 13:52:54 +02:00
|
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
2024-09-09 13:13:45 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
fetchAwards() {
|
2024-09-15 13:52:54 +02:00
|
|
|
this.loading = "loading";
|
2024-09-09 13:13:45 +02:00
|
|
|
http
|
|
|
|
.get("/admin/award")
|
|
|
|
.then((result) => {
|
|
|
|
this.awards = result.data;
|
2024-09-15 13:52:54 +02:00
|
|
|
this.loading = "fetched";
|
2024-09-09 13:13:45 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2024-09-15 13:52:54 +02:00
|
|
|
this.loading = "failed";
|
2024-09-09 13:13:45 +02:00
|
|
|
});
|
|
|
|
},
|
2024-09-15 13:52:54 +02:00
|
|
|
fetchAwardById(id: number): Promise<AxiosResponse<any, any>> {
|
|
|
|
return http.get(`/admin/award/${id}`);
|
2024-09-09 13:13:45 +02:00
|
|
|
},
|
2024-09-15 13:52:54 +02:00
|
|
|
async createAward(award: CreateAwardViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.post(`/admin/award`, {
|
|
|
|
award: award.award,
|
|
|
|
});
|
|
|
|
this.fetchAwards();
|
|
|
|
return result;
|
2024-09-09 13:13:45 +02:00
|
|
|
},
|
2024-09-15 13:52:54 +02:00
|
|
|
async updateActiveAward(award: UpdateAwardViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.patch(`/admin/award/${award.id}`, {
|
|
|
|
award: award.award,
|
|
|
|
});
|
|
|
|
this.fetchAwards();
|
|
|
|
return result;
|
2024-09-09 13:13:45 +02:00
|
|
|
},
|
2024-09-15 13:52:54 +02:00
|
|
|
async deleteAward(award: number): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.delete(`/admin/award/${award}`);
|
|
|
|
this.fetchAwards();
|
|
|
|
return result;
|
2024-09-09 13:13:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|