import ShortLinkService from "../Services/ShortLinkService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import BadDataException from "../../Types/Exception/BadDataException";
import ShortLink from "../../Models/DatabaseModels/ShortLink";
export default class ShortLinkAPI extends BaseAPI {
    constructor() {
        var _a;
        super(ShortLink, ShortLinkService);
        this.router.get(`${(_a = new this.entityType()
            .getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/redirect-to-shortlink/:id`, async (req, res, next) => {
            try {
                if (!req.params["id"]) {
                    return Response.sendErrorResponse(req, res, new BadDataException("id is required"));
                }
                if (req.params["id"] === "status") {
                    return Response.sendJsonObjectResponse(req, res, {
                        status: "ok",
                    });
                }
                const link = await ShortLinkService.getShortLinkFor(req.params["id"]);
                if (!link || !link.link) {
                    return Response.sendErrorResponse(req, res, new BadDataException("This URL is invalid or expired"));
                }
                return Response.redirect(req, res, link.link);
            }
            catch (err) {
                return next(err);
            }
        });
    }
}
//# sourceMappingURL=ShortLinkAPI.js.map