blob: bcda13ef8300d7d49c5a9fded405b11b1257d3dd [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento11904062022-04-14 22:33:28 -04002/*
Davide Pesavento7df36bd2023-10-28 19:32:43 -04003 * Copyright (c) 2014-2023, Regents of the University of California.
weijia yuan82cf9142018-10-21 12:25:02 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
Davide Pesavento11904062022-04-14 22:33:28 -040019
weijia yuan82cf9142018-10-21 12:25:02 -070020#ifndef REPO_REPO_COMMAND_HPP
21#define REPO_REPO_COMMAND_HPP
22
23#include "common.hpp"
24#include "repo-command-parameter.hpp"
25
weijia yuan82cf9142018-10-21 12:25:02 -070026namespace repo {
27
28class RepoCommand : boost::noncopyable
29{
30public:
Davide Pesavento11904062022-04-14 22:33:28 -040031 /**
Davide Pesavento7df36bd2023-10-28 19:32:43 -040032 * \brief Represents an error in RepoCommandParameter.
weijia yuan82cf9142018-10-21 12:25:02 -070033 */
34 class ArgumentError : public std::invalid_argument
35 {
36 public:
Davide Pesavento11904062022-04-14 22:33:28 -040037 using std::invalid_argument::invalid_argument;
weijia yuan82cf9142018-10-21 12:25:02 -070038 };
39
weijia yuan82cf9142018-10-21 12:25:02 -070040 class FieldValidator
41 {
42 public:
43 FieldValidator();
44
Davide Pesavento7df36bd2023-10-28 19:32:43 -040045 /**
46 * \brief Declare a required field.
weijia yuan82cf9142018-10-21 12:25:02 -070047 */
48 FieldValidator&
49 required(RepoParameterField field)
50 {
51 m_required[field] = true;
52 return *this;
53 }
54
Davide Pesavento7df36bd2023-10-28 19:32:43 -040055 /**
56 * \brief Declare an optional field.
weijia yuan82cf9142018-10-21 12:25:02 -070057 */
58 FieldValidator&
59 optional(RepoParameterField field)
60 {
61 m_optional[field] = true;
62 return *this;
63 }
64
65 /** \brief verify that all required fields are present,
66 * and all present fields are either required or optional
67 * \throw ArgumentError
68 */
69 void
70 validate(const RepoCommandParameter& parameters) const;
71
72 private:
73 std::vector<bool> m_required;
74 std::vector<bool> m_optional;
75 };
76
77 void
78 validateRequest(const RepoCommandParameter& parameters);
79
Davide Pesavento7df36bd2023-10-28 19:32:43 -040080protected:
81 ~RepoCommand() = default;
82
weijia yuan82cf9142018-10-21 12:25:02 -070083private:
84 virtual void
85 check(const RepoCommandParameter& parameters) const
86 {
87 }
88
Davide Pesavento7df36bd2023-10-28 19:32:43 -040089protected:
weijia yuan82cf9142018-10-21 12:25:02 -070090 FieldValidator m_requestValidator;
91};
92
Davide Pesavento7df36bd2023-10-28 19:32:43 -040093class InsertCommand final : public RepoCommand
weijia yuan82cf9142018-10-21 12:25:02 -070094{
95public:
96 InsertCommand();
weijia yuan82cf9142018-10-21 12:25:02 -070097};
98
Davide Pesavento7df36bd2023-10-28 19:32:43 -040099class InsertCheckCommand final : public RepoCommand
weijia yuan82cf9142018-10-21 12:25:02 -0700100{
101public:
102 InsertCheckCommand();
103};
104
Davide Pesavento7df36bd2023-10-28 19:32:43 -0400105class DeleteCommand final : public RepoCommand
weijia yuan82cf9142018-10-21 12:25:02 -0700106{
107public:
108 DeleteCommand();
109
110private:
111 void
Davide Pesavento7df36bd2023-10-28 19:32:43 -0400112 check(const RepoCommandParameter& parameters) const final;
weijia yuan82cf9142018-10-21 12:25:02 -0700113};
114
115} // namespace repo
116
Davide Pesavento11904062022-04-14 22:33:28 -0400117#endif // REPO_REPO_COMMAND_HPP