|
| 1 | +import { defineAction } from "astro:actions"; |
| 2 | +import { z } from "astro:schema"; |
| 3 | + |
| 4 | +let bearerToken = import.meta.env.STRAPI_WRITE_TOKEN; |
| 5 | + |
| 6 | +const getDownloadCounts = async () => { |
| 7 | + try { |
| 8 | + const response = await fetch( |
| 9 | + `https://cms.athenaos.org/api/download-counts-list`, |
| 10 | + { |
| 11 | + headers: { |
| 12 | + "Content-Type": "application/json", |
| 13 | + Authorization: `Bearer ${bearerToken}`, |
| 14 | + }, |
| 15 | + } |
| 16 | + ); |
| 17 | + const result = await response.json(); |
| 18 | + return result; |
| 19 | + } catch (error) { |
| 20 | + console.error("Error fetching data:", error); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +const increaseDownloadCount = async (id: string) => { |
| 25 | + let data = [ |
| 26 | + { id: "7", download_type: "docker" }, |
| 27 | + { id: "1", download_type: "iso" }, |
| 28 | + { id: "5", download_type: "virtualbox" }, |
| 29 | + { id: "3", download_type: "vmware" }, |
| 30 | + { id: "9", download_type: "wsl" }, |
| 31 | + ]; |
| 32 | + |
| 33 | + try { |
| 34 | + let countId = data.find((item) => item.download_type === id)?.id; |
| 35 | + |
| 36 | + const response = await fetch( |
| 37 | + `https://cms.athenaos.org/api/download-count/${countId}/increment`, |
| 38 | + { |
| 39 | + method: "POST", |
| 40 | + headers: { |
| 41 | + "Content-Type": "application/json", |
| 42 | + Authorization: `Bearer ${bearerToken}`, |
| 43 | + }, |
| 44 | + } |
| 45 | + ); |
| 46 | + const result = await response.json(); |
| 47 | + return result; |
| 48 | + } catch (error) { |
| 49 | + console.error("Error increasing download count:", error); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +export const server = { |
| 54 | + getDownloadCount: defineAction({ |
| 55 | + handler: async () => { |
| 56 | + return await getDownloadCounts(); |
| 57 | + }, |
| 58 | + }), |
| 59 | + increaseCount: defineAction({ |
| 60 | + input: z.object({ |
| 61 | + id: z.string(), |
| 62 | + }), |
| 63 | + handler: async (input) => { |
| 64 | + return await increaseDownloadCount(input.id); |
| 65 | + }, |
| 66 | + }), |
| 67 | +}; |
0 commit comments