The question and answer App

This is an App example. This is a shorten version of an App back end for a Mobile/Desktop front end.

The App front end blocks the users screen on interval and ask a predefined question to unlock.

This offers parents a way to limit screen time in a way that forces kids to research and learn about subjects that matters to them.

Lets see how to build the back end and API in 10 minutes.

We will be needing a model for Questions, Answers and Categories.

You can get the whole project configuration here.

Questions

Take a look at the table element below:

	question(Question): {
			set_menu_priority: 1000,
			is_wysiwyg_colunms: ["question","explanation"],
			with_child_tables: ["question_answer","question_file"],
			id_question: ["primary()"],
			id_question_category('Category'): ["foreign(question_category)","required","onDelete:cascade"],
			question('Question'): ["longvarchar()","required"],
			explanation('Explanation'): ["longvarchar()","required"],
		}

The table and columns are pretty straight forward. A primary key, a foreign key for categories, and a longvarchar for a question.

To transform a column into a interface field, or to have a menu item for a table, you need to have a description. The description is used as the caption in the model.

If you are new to database relationship, learn the basics here.

Here is the column type documentation if you want to know more.

The behavior element adds visual tweaks to the back end.

set_menu_priority make sure the left hand menu entry for Questions will be on top of the menu. Higher value equals higher menu item.

Questions and explanations need formatting and images to be visually attractive. This is why is_wysiwyg_colunms sets both columns question and answer as WYSIWYG. This will transform the textarea to CkEditor.

with_child_tables sets two models as children. For more info on the parent/child functionalities, check parent/child behavior.

Child model always relate to a parent. set_parent_table will have to be added to those table to work properly.

Answers

	question_answer(Answer): {
			set_parent_table: question,
			id_question_answer: ["primary()"],
			answer('Answer'): ["longvarchar()","required"],
			solution('Solution'): ["enum(No, Yes)"],
			exact('Exact'): ["enum(No, Yes)"],
			order('Order'): ["integer()","required"],
			id_question: ["foreign(question)","required","onDelete:cascade"],
		}

Again, simple table with a primary key and an answer column that will display as a textarea for exact comparisons.

The solution and exact column are set as type enum with a valueSet. This will produce selectboxes.

And there is the set_parent_table and a foreign key to the the question table. This is essential for the parent/child relationship.

Question images

	question_file(Image): {
			set_parent_table: question,
			set_order_list_columns: [["date_modification","ASC"]],
			is_file_upload_table: {"thumbnail":"yes","limit":"5","filters":{"max_file_size":"20mb","mime_types":[{"title":"images","extensions":"jpg,gif,png,jpeg"}]},"image_support":"yes"},
			id_question_file: ["primary()"],
			name('Name'): ["varchar(100)","required"],
			date_modification('Date'): ["date()","required"],
			file: ["varchar(255)","required"],
			id_question: ["foreign(question)","required","onDelete:cascade"],
		}

File uploads are managed via the table element with the is_file_upload_table behavior parameter. The column file needs to be present.

Categories

question_category('Question category'): {
			set_parent_menu: Settings,
			id_question_category: ["primary()"],
			name('Name'): ["varchar(40)","required"],
			description('Description'): ["longvarchar()","required"],
		}

Since the model question has a column id_question_category define as a foreign key to this table, the field in the Question form will display a selectbox with the content of this table.

This model will be displayed in the Settings menu, as a submenu.

Results

Control panel

Here is a couple screenshots of the results. It is a preview. You can easily create a free account and try it yourself.

The last screenshot is the Authy model. It appears in your schema but most functionalities are added by the compiler.

Your can notice that there is a search functionality in the list of users. This is define in your schema as:

<parameter name="with_search_columns" value='{
                    "Name":[["username", "%val", "or"], ["email", "%val"]],
                    "Primary group":[["id_authy_group", "%val"]]
                    }' />

Try adding a search to the Question model.

API

If you set the with_api parameter at the database level, a full REST API will be built with it. Look at the documentation.

<?xml version="1.0" encoding="UTF-8"?>
<database name="test_1" defaultIdMethod="native">
    <behavior name="GoatCheese">
        <parameter name="with_api" value='true'/>
    </behavior>
  ...

Leave a Reply