view page-index.c @ 78:9bfe5ce3cc45

pasterd: rework themes
author David Demelier <markand@malikania.fr>
date Thu, 16 Mar 2023 20:45:59 +0100
parents fe78b16c694d
children 52029a52a385
line wrap: on
line source

/*
 * page-index.c -- page /
 *
 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>

#include "database.h"
#include "json-util.h"
#include "page-index.h"
#include "page.h"
#include "util.h"

#include "html/index.h"

#define LIMIT 16
#define TITLE "paster -- recent pastes"

static void
get(struct kreq *req)
{
	json_t *pastes;

	if (!(pastes = database_recents(LIMIT)))
		page_status(req, KHTTP_500);
	else
		page_index_render(req, pastes);
}

void
page_index_render(struct kreq *req, json_t *pastes)
{
	assert(req);
	assert(pastes);

	page(req, KHTTP_200, html_index, json_pack("{ss so}",
		"title",        TITLE,
		"pastes",       pastes
	));
}

void
page_index(struct kreq *req)
{
	assert(req);

	switch (req->method) {
	case KMETHOD_GET:
		get(req);
		break;
	default:
		page_status(req, KHTTP_400);
		break;
	}
}