Calling all Qodly Newbies: Build a Simple Pet Contest App

Calling all Qodly Newbies: Build a Simple Pet Contest App

Qodly 05/30/2024

<p><br>Have you been curious about Qodly but unsure where to start? This post is your perfect introduction!</p><p>Today, I'm taking you on a journey through building a fun and engaging application – a pet contest! But this isn't just any pet contest app; it's a chance to explore the exciting capabilities of Qodly, the hybrid low-code platform.&nbsp;</p><p>As someone who's transitioned from software engineering to product and marketing, I'm always on the lookout for tools that bridge efficiency and customization. Qodly's promise of both intrigued me, and I couldn't wait to test it with a simple yet engaging app—a pet contest!</p><h2>&nbsp;</h2><h2>Building our Pet Paradise</h2><p>The application's idea is straightforward, a single-page interface where users can participate in three key actions:</p><p><strong>Add pets:</strong> People can upload a picture and name for their pet, adding them to the contest.&nbsp;</p><p><strong>Vote for pets:</strong> This is the fun part! Users can vote for their favorite pets, making the cutest ones more popular.&nbsp;</p><p><strong>See top 10:</strong> Witness pet popularity come alive as the top ten "cutest" pets are displayed based on the votes.</p><p>This seemingly straightforward app serves as a perfect example to showcase Qodly's basic functionalities.</p><img src="/sites/default/files/petsapp2.png" data-entity-uuid="bcafde82-03d7-471c-8960-88e0a2a8aa8d" data-entity-type="file" alt="" width="1020" height="373"><p>&nbsp;</p><p>Clicking the "Add New Pet" button will open a dedicated dialog, allowing users to upload their pet's picture and name. We'll delve into configuring this pet submission process in the next section.</p><p>&nbsp;</p><img src="/sites/default/files/petsapp1.png" data-entity-uuid="f763f9bc-abcb-4c40-b147-91f5c60c0c7e" data-entity-type="file" alt="" width="1020" height="373"><h2>&nbsp;</h2><h2><em>Building the backend with Qodly</em></h2><p><em>While the user interface may appear simple, there's some magic happening behind the scenes (don't worry, it's friendly magic!).</em></p><p><em>In this blog post, we'll delve into the world of models and webforms, the building blocks of our pet contest app.&nbsp;</em></p><p><em>We'll see how Qodly eases the process with its intuitive tools.</em></p><h3>&nbsp;</h3><h3>Create The model</h3><p><a href="https://developer.qodly.com/docs/studio/model/model-editor-interface">The model</a> defines the data structure for our app. We'll create a dataclass named Pet to hold information about each pet. This dataclass will have attributes like name, picture, and other relevant details.</p><img src="/sites/default/files/Pettable.png" data-entity-uuid="0cfdb38c-df5e-41ac-a94b-d344a4d54fda" data-entity-type="file" alt="" width="498" height="322"><h3>&nbsp;</h3><h3>Create The webform</h3><p>This is where users will interact with the app and provide information about their pets. &nbsp;</p><p>Qodly makes building web forms easy, even for those without coding experience! It allows you to create the form layout visually using drag-and-drop functionality.</p><p><a href="https://developer.qodly.com/docs/studio/design-webforms/create-webform">Create a new webform</a> and call it index. Start dragging your components (see the following section).</p><h4>&nbsp;</h4><h4>Setting Up The Voting Section:</h4><p>For the voting section:</p><p>- Start by dragging a <a href="https://developer.qodly.com/docs/studio/design-webforms/components/stylebox">stylebox</a>: This will act as a container for the voting elements.</p><p>- Inside the stylebox, drag an <a href="https://developer.qodly.com/docs/studio/design-webforms/components/image">image</a> component: Place this inside the style box to display the pet image users will vote on.</p><p>- Drag two <a href="https://developer.qodly.com/docs/studio/design-webforms/components/button">buttons</a> with the labels "Cute" and "Not Cute" inside the stylebox to capture users' votes.</p><img src="/sites/default/files/votingsection.png" data-entity-uuid="5fc1073e-69ba-407a-a659-8580248d4da4" data-entity-type="file" alt="" width="1218" height="430"><h4>&nbsp;</h4><h4>Adding a new pet:</h4><p>Now, let's move to the action of Adding a new pet.&nbsp;</p><p>Drag a button onto your workspace. Clicking this button will later open the pet creation dialog.</p><img src="/sites/default/files/addnewpet.png" data-entity-uuid="379b66ba-e426-4bdf-981d-6db1ffa9883c" data-entity-type="file" alt="" width="1244" height="122"><h4>&nbsp;</h4><h4>Displaying top 10 pets:</h4><p>To show the top ten pets, drag a <a href="https://developer.qodly.com/docs/studio/design-webforms/components/matrix">matrix</a> components inside the stylebox. Think of this matrix as a grid layout that will showcase the top ten pets.</p><img src="/sites/default/files/matrixlistpets.png" data-entity-uuid="dddf0a9a-19ac-4e45-8b8a-983980618786" data-entity-type="file" alt="" width="1796" height="834"><h3>&nbsp;</h3><h3>Creating The Pet Creation Dialog</h3><p>Once users click the "Add New Pet" button, they'll need a way to provide information about their pet. In Qodly, this could be done through a <a href="https://developer.qodly.com/docs/studio/design-webforms/components/dialog">dialog</a>.</p><p>From the Qodly interface, use this button to create a new dialog and name it "addPet".</p><p>&nbsp;</p><img src="/sites/default/files/dialog.png" data-entity-uuid="0fea38f0-3f44-4e2f-888e-9459dd3b7c8f" data-entity-type="file" alt="" width="1376" height="672"><p>Within the dialog, use drag-and-drop to add the following elements:&nbsp;</p><p>- A file upload component where users can select their pet's picture.&nbsp;</p><p>- A text input field for users to enter their pet's name.</p><p>- You can optionally add an image component that displays a default image when the dialog first opens. This can provide a visual cue to users about what kind of image they should upload.</p><img src="/sites/default/files/addnewpetdialog.png" data-entity-uuid="e03cd23e-2463-4b2d-bd1d-4b1192ae0270" data-entity-type="file" alt="" width="1324" height="820"><h2>&nbsp;</h2><h2>Wiring Everything up</h2><p>Here comes the fun part!&nbsp;</p><p>Now that you've built the visual structure of your web form using Qodly's drag-and-drop features, it's time to connect everything and make it functional.&nbsp;</p><p>This will likely involve writing some code (don't worry, Qodly simplifies this process too)!</p><h3>&nbsp;</h3><h3>Adding a new pet</h3><p>The first step is to allow users to submit new pets to the contest. Qodly's <a href="https://developer.qodly.com/docs/studio/design-webforms/events#binding-standard-actions-to-events">standard actions</a> make this process easy—no single line of code is needed!</p><p>Go ahead and open your dialog,&nbsp;</p><img src="/sites/default/files/opendialog.png" data-align="center" data-entity-uuid="163c106d-6ed6-446a-be16-83d25275804b" data-entity-type="file" alt="" width="940" height="314"><p>&nbsp;</p><p><strong>What We Want to Achieve</strong>&nbsp;</p><p>Before diving into the configuration, let's clarify the goal here: When the user uploads a pet's image, enters a name, and clicks "Submit," we want that information to be saved as a new entry in your database.&nbsp;</p><p>But before we get there, there's an important step: <strong>Mapping user input to the database</strong>.</p><p>Before we configure the button to save new pets, let's ensure the user's input is properly mapped to the corresponding fields in your database. Here's how to achieve this:</p><p>1 - <strong>Create a data source of type Entity</strong>: Name it "addPet". Refer to Qodly's documentation for detailed instructions on creating a <a href="https://developer.qodly.com/docs/studio/design-webforms/datasources">data source</a> of type "<a href="https://developer.qodly.com/docs/orda/data-model#entity">Entity</a>" that reflects your pet's data structure (e.g., name, picture, etc.).&nbsp;</p><img src="/sites/default/files/addpetds.png" data-entity-uuid="57357a6d-8d71-48ad-aeb8-2d53aaef3eaf" data-entity-type="file" alt="" width="916" height="482"><p>&nbsp;</p><p>2 - <strong>Map input fields to entity properties:</strong> &nbsp;Once you have the entity data source, use Qodly's interface to map the user input fields (pet name, picture) to the corresponding properties within the entity. This ensures the data is stored correctly in your database.</p><img src="/sites/default/files/mapimage.png" data-entity-uuid="d25433fd-7823-4394-915c-7b602ce1967b" data-entity-type="file" alt="" width="1840" height="488"><img src="/sites/default/files/mapname.png" data-entity-uuid="2bca5836-a703-43a3-9126-e0a8689db141" data-entity-type="file" alt="" width="1836" height="372"><p>In Qodly's interface, navigate to the events panel for the "Submit" button within the pet creation dialog. This panel allows you to define actions triggered by user interactions.</p><img src="/sites/default/files/eventspanel.png" data-entity-uuid="c9a9418b-706c-4789-883f-5c67413812a6" data-entity-type="file" alt="" width="1124" height="614"><p>Within the events panel, choose the "on Click" event and then select "Add a standard action."&nbsp;</p><img src="/sites/default/files/addsa.png" data-entity-uuid="65b5c7ab-7a49-4adc-86a3-dc5843f2c185" data-entity-type="file" alt="" width="594" height="276"><p>From the available options, select the "Save" standard action. This action will save the data entered by the user to your database.</p><img src="/sites/default/files/saveSA.png" data-entity-uuid="94ec003e-08a6-4a19-92ea-29a6ef7c17e9" data-entity-type="file" alt="" width="716" height="622"><p>&nbsp;</p><h3>BUT</h3><p>There's a crucial step before saving new pet information: <strong>pre-populating the entity with an empty record when the dialog loads (on Init)</strong>. Qodly will not automatically create a new entity instance for you to save.</p><p>By pre-populating the entity, you essentially create a blank "container" for the user's input. When the user clicks "Submit," the data entered (pet name, image) will be populated into the existing entity, allowing you to successfully save it to your database using the "Save" standard action.</p><img src="/sites/default/files/createnetity.png" data-entity-uuid="26559d41-406b-49ba-b68e-59b46e3b22a1" data-entity-type="file" alt="" width="1334" height="598"><p>Once you've configured the button action, test the functionality by adding a new pet. The user should be able to enter pet information, and clicking "Submit" should successfully save the new pet entry to your database.</p><p>&nbsp;</p><h3>Bonus</h3><p>Qodly offers a built-in <a href="https://developer.qodly.com/docs/data-explorer/">Data Explorer</a>, a web interface that allows you to:</p><p>- <strong>Browse and Inspect: </strong>Easily view all your entities (including pets) and their data.&nbsp;</p><p>- <strong>Search and Filter:</strong> Quickly find specific pet entries using search and filter options.&nbsp;</p><p>- <strong>Edit on the Fly:</strong> If needed, you can directly edit data within the Data Explorer for troubleshooting or adjustments.</p><p>Locate the Data Explorer within Qodly's interface:</p><img src="/sites/default/files/dataexplorer.png" data-entity-uuid="138ae9b9-47a4-446d-9842-01efab57c129" data-entity-type="file" alt="" width="1568" height="410"><p>If you can find your newly added pet with the corresponding details (name, picture), then congratulations! You've successfully saved the pet entry.</p><h3>Displaying the top ten pet</h3><p>Now that you can add new pets, let's showcase them on the main interface!&nbsp;</p><p>Here, Qodly's configuration shines with its user-friendly approach.</p><p>Start by creating a data source of type "Entity Selection." &nbsp;Name this data source "pets" to clearly represent its purpose. This data source will act as a bridge between your application and the database, specifically retrieving information about the top ten pets.</p><p>&nbsp;</p><img src="/sites/default/files/mapmatrix.png" data-entity-uuid="03469a34-0c71-4258-a4c1-c799e8a59df5" data-entity-type="file" alt="" width="2012" height="1228"><p>With the "pets" data source created, it's time to connect it to the visual element displaying the pet list.&nbsp;</p><p>This element is a "matrix" in Qodly. Use Qodly's interface to map the "pets" data source to this list element.</p><p>Now, it's time to personalize how each pet is displayed on the list. Drag and drop the desired elements you want to show for each pet, such as the pet's name, picture, and any other relevant information. Qodly's visual interface should allow you to easily position these elements for a visually appealing presentation.</p><h4>Adding a CSS touch</h4><p>While we'll delve deeper into CSS styling in a future blog post, feel free to experiment with basic visual adjustments from the right-hand panel within Qodly's interface. You can adjust spacing and add a border radius to your image.</p><p>&nbsp;</p><img src="/sites/default/files/dragndropattributes_0.png" data-entity-uuid="231f78b8-b733-4eb9-a4ef-f7eb6cd52ea3" data-entity-type="file" alt="" width="1806" height="450"><p>For now, you should be able to see the whole list of pets in your database. Let's refine it to see only the top 10.&nbsp;</p><p>Let's create a function that does that.</p><p>Go to your Qodly model and create a new function named topTen. This function will return a selection of pets, specifically the top ten "cutest" ones.</p><img src="/sites/default/files/toptenmodel.png" data-entity-uuid="5c927c47-993c-429c-be23-a7d878cc1441" data-entity-type="file" alt="" width="552" height="558"><p>it will automatically create the function in the Pet class.</p><img src="/sites/default/files/petclass.png" data-entity-uuid="0f22e481-7c07-4d28-9a4d-31e48bffaf6a" data-entity-type="file" alt="" width="928" height="380"><p>&nbsp;</p><p>Simply add this code that displays the top ten pets:</p><pre><code class="language-4d"> exposed Function topTen() -&gt; result: cs.PetSelection result = this.query("totalCutes &gt; totalNotCutes").orderBy("totalCutes desc").slice(0,10)</code></pre><p>&nbsp;</p><p>This code snippet showcases the logic behind the topTen function:&nbsp;</p><ol><li>.query("totalCutes &gt; totalNotCutes"): This filters the pet selection to only include pets where the "totalCutes" score is greater than the "totalNotCutes" score.&nbsp;</li><li>.orderBy("totalCutes desc"): This orders the filtered pet selection by their "totalCutes" score in descending order (showing the most "cutes" first).&nbsp;</li><li>.slice(0,10): This limits the final result to the first ten entries, effectively retrieving the top ten "cutest" pets.</li></ol><p>In the next step, we'll configure Qodly to call this topTen function when the webform loads, ensuring only the top ten pets are initially displayed.</p><img src="/sites/default/files/functioncall.png" data-entity-uuid="eec5ff97-31a3-4648-aa02-d3a2b8c461af" data-entity-type="file" alt="" width="584" height="292"><p>&nbsp;</p><img src="/sites/default/files/functionalconf.png" data-entity-uuid="8720719b-14e1-496e-82c7-417bd1b424e6" data-entity-type="file" alt="" width="1286" height="318"><p>Let's move to the last part.</p><p>&nbsp;</p><h3>Voting functionality</h3><p>Now that you have the cutest contenders showcased, let's allow users to cast their votes!&nbsp;</p><p>We'll implement two functions in Qodly to achieve this:</p><ol><li>randomPet function: This function will retrieve a random pet from the database, ensuring a dynamic voting experience for users.&nbsp;</li><li>vote function: This function will handle user votes, incrementing either the "totalCutes" or "totalNotCutes" score in the database based on the user's choice.</li></ol><h5>randomPet function</h5><p>Go to your Qodly model and create a new function named randomPet. This function will return a single random pet entity.</p><pre><code class="language-4d">exposed Function randomPet() -&gt; result: cs.PetEntity var pets: cs.PetSelection pets = this.all() result = pets.at(random%pets.length)</code></pre><p>&nbsp;</p><p>This function retrieves all the pets using this.all().&nbsp;</p><p>It then uses the random() function to generate a random number within the range of the pet selection's length. Finally, it uses the at(index) method to access and return the pet entity at the randomly chosen index.</p><h5>&nbsp;</h5><h5>Vote function</h5><p>Go to your PetEntity class (where individual pet data is defined) and create a new function named vote. This function will take a boolean argument (cute) indicating the user's vote.</p><pre><code class="language-4d"> exposed Function vote(cute : boolean) if (cute) this.totalCutes+=1 else this.totalNotCutes+=1 end this.save()</code></pre><p>&nbsp;</p><h5>Connecting Functions to User Interactions</h5><p>In Qodly's interface, configure your webform's onLoad event to call the randomPet function. This will ensure a random pet is displayed initially for voting.</p><img src="/sites/default/files/randomPet.png" data-entity-uuid="52fa8bcf-7443-43d7-9f09-2dcb5d4a7dcc" data-entity-type="file" alt="" width="714" height="478"><p>Configure the "Cute" and "Not Cute" buttons with the onClick event. When a user clicks either button, trigger the vote function, passing true for "Cute" and false for "Not Cute" to update the corresponding vote count.</p><img src="/sites/default/files/cute.png" data-entity-uuid="9e4763ef-0337-4cc9-b77f-573f303057c1" data-entity-type="file" alt="" width="752" height="532"><img src="/sites/default/files/notcute.png" data-entity-uuid="692103c4-19b6-4fe7-98fb-75b867a3f980" data-entity-type="file" alt="" width="742" height="524"><p>&nbsp;</p><p>Test the functionality by running your application. You should be able to see a random pet displayed on load. Clicking "Cute" or "Not Cute" should update the pet's respective vote count.&nbsp;</p><p>Congratulations! You've successfully built a core voting system for your pet contest application using Qodly's functionalities.</p><h2>Coming Up Next</h2><p>This is just the first step in our journey!&nbsp;</p><p>In the next part of this series, we'll use the power of CSS to create a visually appealing user interface. We'll also explore how Qodly empowers you to personalize your app beyond the basic functionalities.</p><p>Stay tuned for exciting updates, and get ready to build your own delightful pet contest app!</p><p>Meanwhile, if you have any questions, feel free to connect with us on the <a href="https://community.qodly.com/">Forums</a> or <a href="https://qodly.slack.com/join/shared_invite/zt-20ieeffts-NU57SOXcbakmWgIMnJpStQ#/shared-invite/email">Slack</a>.</p>

By Intissar Elmezrouai, Product Marketing Manager

Share this on :


Have you been curious about Qodly but unsure where to start? This post is your perfect introduction!

Today, I'm taking you on a journey through building a fun and engaging application – a pet contest! But this isn't just any pet contest app; it's a chance to explore the exciting capabilities of Qodly, the hybrid low-code platform. 

As someone who's transitioned from software engineering to product and marketing, I'm always on the lookout for tools that bridge efficiency and customization. Qodly's promise of both intrigued me, and I couldn't wait to test it with a simple yet engaging app—a pet contest!

 

Building our Pet Paradise

The application's idea is straightforward, a single-page interface where users can participate in three key actions:

Add pets: People can upload a picture and name for their pet, adding them to the contest. 

Vote for pets: This is the fun part! Users can vote for their favorite pets, making the cutest ones more popular. 

See top 10: Witness pet popularity come alive as the top ten "cutest" pets are displayed based on the votes.

This seemingly straightforward app serves as a perfect example to showcase Qodly's basic functionalities.

 

Clicking the "Add New Pet" button will open a dedicated dialog, allowing users to upload their pet's picture and name. We'll delve into configuring this pet submission process in the next section.

 

 

Building the backend with Qodly

While the user interface may appear simple, there's some magic happening behind the scenes (don't worry, it's friendly magic!).

In this blog post, we'll delve into the world of models and webforms, the building blocks of our pet contest app. 

We'll see how Qodly eases the process with its intuitive tools.

 

Create The model

The model defines the data structure for our app. We'll create a dataclass named Pet to hold information about each pet. This dataclass will have attributes like name, picture, and other relevant details.

 

Create The webform

This is where users will interact with the app and provide information about their pets.  

Qodly makes building web forms easy, even for those without coding experience! It allows you to create the form layout visually using drag-and-drop functionality.

Create a new webform and call it index. Start dragging your components (see the following section).

 

Setting Up The Voting Section:

For the voting section:

- Start by dragging a stylebox: This will act as a container for the voting elements.

- Inside the stylebox, drag an image component: Place this inside the style box to display the pet image users will vote on.

- Drag two buttons with the labels "Cute" and "Not Cute" inside the stylebox to capture users' votes.

 

Adding a new pet:

Now, let's move to the action of Adding a new pet. 

Drag a button onto your workspace. Clicking this button will later open the pet creation dialog.

 

Displaying top 10 pets:

To show the top ten pets, drag a matrix components inside the stylebox. Think of this matrix as a grid layout that will showcase the top ten pets.

 

Creating The Pet Creation Dialog

Once users click the "Add New Pet" button, they'll need a way to provide information about their pet. In Qodly, this could be done through a dialog.

From the Qodly interface, use this button to create a new dialog and name it "addPet".

 

Within the dialog, use drag-and-drop to add the following elements: 

- A file upload component where users can select their pet's picture. 

- A text input field for users to enter their pet's name.

- You can optionally add an image component that displays a default image when the dialog first opens. This can provide a visual cue to users about what kind of image they should upload.

 

Wiring Everything up

Here comes the fun part! 

Now that you've built the visual structure of your web form using Qodly's drag-and-drop features, it's time to connect everything and make it functional. 

This will likely involve writing some code (don't worry, Qodly simplifies this process too)!

 

Adding a new pet

The first step is to allow users to submit new pets to the contest. Qodly's standard actions make this process easy—no single line of code is needed!

Go ahead and open your dialog, 

 

What We Want to Achieve 

Before diving into the configuration, let's clarify the goal here: When the user uploads a pet's image, enters a name, and clicks "Submit," we want that information to be saved as a new entry in your database. 

But before we get there, there's an important step: Mapping user input to the database.

Before we configure the button to save new pets, let's ensure the user's input is properly mapped to the corresponding fields in your database. Here's how to achieve this:

1 - Create a data source of type Entity: Name it "addPet". Refer to Qodly's documentation for detailed instructions on creating a data source of type "Entity" that reflects your pet's data structure (e.g., name, picture, etc.). 

 

2 - Map input fields to entity properties:  Once you have the entity data source, use Qodly's interface to map the user input fields (pet name, picture) to the corresponding properties within the entity. This ensures the data is stored correctly in your database.

In Qodly's interface, navigate to the events panel for the "Submit" button within the pet creation dialog. This panel allows you to define actions triggered by user interactions.

Within the events panel, choose the "on Click" event and then select "Add a standard action." 

From the available options, select the "Save" standard action. This action will save the data entered by the user to your database.

 

BUT

There's a crucial step before saving new pet information: pre-populating the entity with an empty record when the dialog loads (on Init). Qodly will not automatically create a new entity instance for you to save.

By pre-populating the entity, you essentially create a blank "container" for the user's input. When the user clicks "Submit," the data entered (pet name, image) will be populated into the existing entity, allowing you to successfully save it to your database using the "Save" standard action.

Once you've configured the button action, test the functionality by adding a new pet. The user should be able to enter pet information, and clicking "Submit" should successfully save the new pet entry to your database.

 

Bonus

Qodly offers a built-in Data Explorer, a web interface that allows you to:

- Browse and Inspect: Easily view all your entities (including pets) and their data. 

- Search and Filter: Quickly find specific pet entries using search and filter options. 

- Edit on the Fly: If needed, you can directly edit data within the Data Explorer for troubleshooting or adjustments.

Locate the Data Explorer within Qodly's interface:

If you can find your newly added pet with the corresponding details (name, picture), then congratulations! You've successfully saved the pet entry.

Displaying the top ten pet

Now that you can add new pets, let's showcase them on the main interface! 

Here, Qodly's configuration shines with its user-friendly approach.

Start by creating a data source of type "Entity Selection."  Name this data source "pets" to clearly represent its purpose. This data source will act as a bridge between your application and the database, specifically retrieving information about the top ten pets.

 

With the "pets" data source created, it's time to connect it to the visual element displaying the pet list. 

This element is a "matrix" in Qodly. Use Qodly's interface to map the "pets" data source to this list element.

Now, it's time to personalize how each pet is displayed on the list. Drag and drop the desired elements you want to show for each pet, such as the pet's name, picture, and any other relevant information. Qodly's visual interface should allow you to easily position these elements for a visually appealing presentation.

Adding a CSS touch

While we'll delve deeper into CSS styling in a future blog post, feel free to experiment with basic visual adjustments from the right-hand panel within Qodly's interface. You can adjust spacing and add a border radius to your image.

 

For now, you should be able to see the whole list of pets in your database. Let's refine it to see only the top 10. 

Let's create a function that does that.

Go to your Qodly model and create a new function named topTen. This function will return a selection of pets, specifically the top ten "cutest" ones.

it will automatically create the function in the Pet class.

 

Simply add this code that displays the top ten pets:


exposed Function topTen() -> result: cs.PetSelection 
result = this.query("totalCutes > totalNotCutes").orderBy("totalCutes desc").slice(0,10)

 

This code snippet showcases the logic behind the topTen function: 

  1. .query("totalCutes > totalNotCutes"): This filters the pet selection to only include pets where the "totalCutes" score is greater than the "totalNotCutes" score. 
  2. .orderBy("totalCutes desc"): This orders the filtered pet selection by their "totalCutes" score in descending order (showing the most "cutes" first). 
  3. .slice(0,10): This limits the final result to the first ten entries, effectively retrieving the top ten "cutest" pets.

In the next step, we'll configure Qodly to call this topTen function when the webform loads, ensuring only the top ten pets are initially displayed.

 

Let's move to the last part.

 

Voting functionality

Now that you have the cutest contenders showcased, let's allow users to cast their votes! 

We'll implement two functions in Qodly to achieve this:

  1. randomPet function: This function will retrieve a random pet from the database, ensuring a dynamic voting experience for users. 
  2. vote function: This function will handle user votes, incrementing either the "totalCutes" or "totalNotCutes" score in the database based on the user's choice.
randomPet function

Go to your Qodly model and create a new function named randomPet. This function will return a single random pet entity.

exposed Function randomPet() -> result: cs.PetEntity 
var pets: cs.PetSelection 
pets = this.all() 
result = pets.at(random%pets.length)

 

This function retrieves all the pets using this.all(). 

It then uses the random() function to generate a random number within the range of the pet selection's length. Finally, it uses the at(index) method to access and return the pet entity at the randomly chosen index.

 
Vote function

Go to your PetEntity class (where individual pet data is defined) and create a new function named vote. This function will take a boolean argument (cute) indicating the user's vote.


exposed Function vote(cute : boolean)
 if (cute) 
  this.totalCutes+=1 
 else 
  this.totalNotCutes+=1 
  end 
  this.save()

 

Connecting Functions to User Interactions

In Qodly's interface, configure your webform's onLoad event to call the randomPet function. This will ensure a random pet is displayed initially for voting.

Configure the "Cute" and "Not Cute" buttons with the onClick event. When a user clicks either button, trigger the vote function, passing true for "Cute" and false for "Not Cute" to update the corresponding vote count.

 

Test the functionality by running your application. You should be able to see a random pet displayed on load. Clicking "Cute" or "Not Cute" should update the pet's respective vote count. 

Congratulations! You've successfully built a core voting system for your pet contest application using Qodly's functionalities.

Coming Up Next

This is just the first step in our journey! 

In the next part of this series, we'll use the power of CSS to create a visually appealing user interface. We'll also explore how Qodly empowers you to personalize your app beyond the basic functionalities.

Stay tuned for exciting updates, and get ready to build your own delightful pet contest app!

Meanwhile, if you have any questions, feel free to connect with us on the Forums or Slack.

Subscribe to Qodly Blog 
 

Your source for awesome content and news related to Low Code/No Code trends and challenges, how-to’s, tips and tricks.

By submitting this form, I agree to Qodly's Privacy Policy

Hybrid Low-code Platform For Your Business Apps

Have freedom of customization while avoiding the constraints of No-Code limitations. Try Qodly for Free!

Discover Qodly