"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFairs = getFairs;
exports.getFair = getFair;
exports.createFair = createFair;
exports.editFair = editFair;
exports.deleteFair = deleteFair;
const prisma_1 = __importDefault(require("../../utils/prisma"));
// ------------------------------------------------------------------------------------
function getFairs(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const fairs = yield prisma_1.default.fair.findMany({
            orderBy: {
                startDate: "desc",
            },
            select: {
                id: true,
                title: true,
                country: true,
                place: true,
                startDate: true,
                endDate: true,
            },
        });
        return reply.code(200).send(fairs);
    });
}
// ------------------------------------------------------------------------------------
function getFair(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const fair = yield prisma_1.default.fair.findUnique({
            where: {
                id: id,
            },
        });
        if (!fair) {
            return reply.code(404).send({
                message: "Ce foire n'existe pas",
            });
        }
        return reply.code(200).send(fair);
    });
}
// ------------------------------------------------------------------------------------
function createFair(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        const fair = yield prisma_1.default.fair.create({
            data: {
                title: data.title,
                country: data.country,
                place: data.place,
                startDate: data.startDate,
                endDate: data.endDate,
                sector: data.sector,
                organizer: data.organizer,
                phoneNumber: data.phoneNumber,
                fax: data.fax,
                email: data.email.toLowerCase(),
                website: data.website,
            },
            select: {
                id: true,
                title: true,
                country: true,
                place: true,
                startDate: true,
                endDate: true,
            },
        });
        return reply.code(200).send(fair);
    });
}
// ------------------------------------------------------------------------------------
function editFair(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const data = req.body;
        const fair = yield prisma_1.default.fair.findUnique({
            where: {
                id: id,
            },
        });
        if (!fair) {
            return reply.code(404).send({
                message: "Ce foire n'existe pas",
            });
        }
        const newFair = yield prisma_1.default.fair.update({
            where: { id: id },
            data: {
                title: data.title,
                country: data.country,
                place: data.place,
                startDate: data.startDate,
                endDate: data.endDate,
                sector: data.sector,
                organizer: data.organizer,
                phoneNumber: data.phoneNumber,
                fax: data.fax,
                email: data.email.toLowerCase(),
                website: data.website,
            },
            select: {
                id: true,
                title: true,
                country: true,
                place: true,
                startDate: true,
                endDate: true,
            },
        });
        return reply.code(200).send(newFair);
    });
}
// ------------------------------------------------------------------------------------
function deleteFair(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const fair = yield prisma_1.default.fair.findUnique({
            where: {
                id: id,
            },
        });
        if (!fair) {
            return reply.code(404).send({
                message: "Ce foire n'existe pas",
            });
        }
        yield prisma_1.default.fair.delete({
            where: { id: id },
        });
        return reply.code(200).send({});
    });
}
// ------------------------------------------------------------------------------------
