view libserver/malikania/account_dao.hpp @ 76:858621081b95

Happy new year!
author David Demelier <markand@malikania.fr>
date Sun, 01 Jan 2017 13:35:37 +0100
parents b0593a3e2ca8
children
line wrap: on
line source

/*
 * account_dao.hpp -- database account management
 *
 * Copyright (c) 2013-2017 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 MALIKANIA_SERVER_ACCOUNT_DAO_HPP
#define MALIKANIA_SERVER_ACCOUNT_DAO_HPP

/**
 * \file account_dao.hpp
 * \brief Database account management.
 */

#include <cstdint>
#include <vector>

#include <boost/optional.hpp>

namespace mlk {

class database;
class account;

/**
 * \brief Account DAO.
 */
class account_dao {
private:
    database& m_database;

public:
    /**
     * Constructor.
     *
     * \param database the database
     */
    inline account_dao(database& database) noexcept
        : m_database(database)
    {
    }

    /**
     * Check if the user authentication is valid.
     *
     * \param login the login name
     * \param pass the clear password
     * \return true if authentication was successful
     */
    bool authenticate(const std::string& login, const std::string& pass);

    /**
     * Create the given account.
     *
     * The object will be modified in place.
     *
     * \param account the account to add
     * \throw std::exception on errors
     */
    void create(account& account);

    /**
     * Remove the given account, all data that references this account is
     * deleted too.
     *
     * \param account the account
     * \throw std::exception on errors
     */
    void remove(const account& account);

    /**
     * Update account only, does not recurse into objects that references the
     * account.
     *
     * \param account the account
     * \throw std::exception on errors
     */
    void update(account& account);

    /**
     * Get an account.
     *
     * \param id the account id
     * \return the account
     * \throw std::exception if not found
     */
    boost::optional<account> get(std::uint64_t id);

    /**
     * Find an account by name.
     *
     * \param name the account name
     * \return the account or empty one if not found
     * \throw std::exception on other errors
     */
    boost::optional<account> find_by_name(const std::string& name);

    /**
     * Get the list of account.
     *
     * \throw std::exception on errors
     */
    std::vector<account> list();

    /**
     * Get the number of accounts.
     *
     * \return the number of account.
     */
    std::uint64_t count();

    /**
     * Remove all accounts recursively.
     *
     * \throw std::exception on errors
     * \warning use with care
     */
    void clear();
};

} // !mlk

#endif // !MALIKANIA_SERVER_ACCOUNT_DAO_HPP