mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2025-08-16 21:42:53 +02:00
「🏗️」 wip: started doing the api for the contract
This commit is contained in:
13
src/api/scoreStore/default.js
Normal file
13
src/api/scoreStore/default.js
Normal file
@ -0,0 +1,13 @@
|
||||
import { evm } from '@avalabs/avalanchejs';
|
||||
import scoreDB from '../../utils/scoreDB.js';
|
||||
import { getTx } from './getTx.js';
|
||||
|
||||
/**
|
||||
* @param {import('fastify').FastifyInstance} fastify
|
||||
* @param {import('fastify').FastifyPluginOptions} options
|
||||
*/
|
||||
export default async function(fastify, options) {
|
||||
fastify.get("/:id", async (request, reply) => {
|
||||
return getTx(request, reply, fastify);
|
||||
});
|
||||
}
|
19
src/api/scoreStore/getTx.js
Normal file
19
src/api/scoreStore/getTx.js
Normal file
@ -0,0 +1,19 @@
|
||||
import scoreDB from "../../utils/scoreDB.js";
|
||||
|
||||
/**
|
||||
* @async
|
||||
* @param {import("fastify".FastifyRequest)} request
|
||||
* @param {import("fastify").FastifyReply} reply
|
||||
* @param {import("fastify").FastifyInstance} fastify
|
||||
*
|
||||
* @returns {import('fastify').FastifyReply}
|
||||
*/
|
||||
export async function getTx(request, reply, fastify) {
|
||||
try {
|
||||
|
||||
} catch (err) {
|
||||
fastify.log.error(err);
|
||||
return reply.code(500).send({ error: "Internal server error" });
|
||||
}
|
||||
}
|
||||
|
52
src/utils/scoreDB.js
Normal file
52
src/utils/scoreDB.js
Normal file
@ -0,0 +1,52 @@
|
||||
import { Int } from "@avalabs/avalanchejs";
|
||||
import Database from "better-sqlite3";
|
||||
|
||||
var env = process.env.NODE_ENV || 'development';
|
||||
let database;
|
||||
|
||||
if (!env || env === 'development') {
|
||||
database = new Database(":memory:", { verbose: console.log });
|
||||
} else {
|
||||
var dbPath = process.env.DB_PATH || '/db/db.sqlite';
|
||||
database = new Database(dbPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Can be used to prepare the database
|
||||
*/
|
||||
function prepareDB() {
|
||||
database.exec(`
|
||||
CREATE TABLE IF NOT EXISTS scoresTx (
|
||||
id INTEGER PRIMARY KEY,
|
||||
txHash TEXT
|
||||
) STRICT
|
||||
`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Can be used to add a score hash to the DB
|
||||
* @param {Int} The id of the score
|
||||
* @param {String} The hash of the score
|
||||
*/
|
||||
function addTx(id, txHash) {
|
||||
const txAdd = database.prepare('INSERT INTO scoresTx (id, txHash) VALUES (?, ?)');
|
||||
txAdd.run(id, txHash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Can be used to get a tx hash from an id
|
||||
* @param {Int} The id to get
|
||||
* @returns {String} The tx hash
|
||||
*/
|
||||
function getTx(id) {
|
||||
const txGet = database.prepare('SELECT txHash FROM credentials WHERE id = ?;')
|
||||
return txGet.get(id);
|
||||
}
|
||||
|
||||
const scoreDB = {
|
||||
prepareDB,
|
||||
addTx,
|
||||
getTx
|
||||
};
|
||||
|
||||
export default scoreDB;
|
Reference in New Issue
Block a user