view scid/crud.h @ 33:1d0ddf9e6efd

misc: general documentation
author David Demelier <markand@malikania.fr>
date Thu, 04 Aug 2022 16:47:10 +0200
parents 081e1c258e64
children 71cd8447e3a4
line wrap: on
line source

/*
 * crud.h -- convenient helpers for page-api-*
 *
 * Copyright (c) 2021-2022 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.
 */

#ifndef SCID_CRUD_H
#define SCID_CRUD_H

/**
 * \file crud.h
 * \brief Convenient helpers for API pages.
 *
 * This module uses the database to fetch or insert data and finally show the
 * result as HTTP response.
 *
 * This module logs message with tag `crud`.
 */

#include <jansson.h>

struct kreq;

/**
 * Decode the JSON input and insert them using the appropriate database
 * function provided.
 *
 * \pre r != NULL
 * \pre saver != NULL
 * \pre topic != NULL
 * \param r the request
 * \param saver the function to save in database
 * \param topic the log topic for diagnostic
 */
void
crud_insert(struct kreq *r, int (*saver)(json_t *), const char *topic);

/**
 * Convert the JSON provided value.
 *
 * For convenience, if the document is NULL (which happen if the database
 * access failed) it will send a HTTP 500 error. Otherwise it is listed and
 * free'd.
 *
 * \pre r != NULL
 * \param r the request
 * \param doc the document to send (maybe NULL)
 * \warning The document will be destroyed using json_decref.
 */
void
crud_list(struct kreq *r, json_t *doc);

#endif /* !SCID_CRUD_H */