<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Webtutsplus.com</title>
    <description>The latest articles on DEV Community by Webtutsplus.com (@webtutsplus).</description>
    <link>https://dev.to/webtutsplus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F3574%2Fe68b8acc-4c2d-4f79-a6ef-3215950c572e.jpg</url>
      <title>DEV Community: Webtutsplus.com</title>
      <link>https://dev.to/webtutsplus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/webtutsplus"/>
    <language>en</language>
    <item>
      <title>Let’s Add Products for our eCommerce App with Vue.js</title>
      <dc:creator>KunalShekhar</dc:creator>
      <pubDate>Thu, 14 Jan 2021 19:53:09 +0000</pubDate>
      <link>https://dev.to/webtutsplus/let-s-add-products-for-our-ecommerce-app-with-vue-js-m2k</link>
      <guid>https://dev.to/webtutsplus/let-s-add-products-for-our-ecommerce-app-with-vue-js-m2k</guid>
      <description>&lt;p&gt;We are building a complete E-commerce application from scratch. Today we are going to add the Products feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7tY4CBv2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4sfog7jzvwveb0v0rau5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7tY4CBv2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4sfog7jzvwveb0v0rau5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As part of building an E-Commerce application, previously we built the backend for UserProfiles and created android UI for the same.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/technology-hits/lets-develop-an-ecommerce-application-from-scratch-using-java-and-spring-6dfac6ce5a9f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vm73w5AQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AhNjwft_dNBxvTr6g6TOVGg.jpeg" alt="Nil Madhab"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/technology-hits/lets-develop-an-ecommerce-application-from-scratch-using-java-and-spring-6dfac6ce5a9f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Let’s Develop an E-Commerce Application From Scratch Using Java and Spring | by Nil Madhab | Javarevisited | Dec, 2020 | Medium&lt;/h2&gt;
      &lt;h3&gt;Nil Madhab ・ &lt;time&gt;Jan 10, 2021&lt;/time&gt; ・ 5 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBvj_QRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/technology-hits/android-ui-for-e-commerce-user-profile-backend-4053ab122a13" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vm73w5AQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AhNjwft_dNBxvTr6g6TOVGg.jpeg" alt="Nil Madhab"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/technology-hits/android-ui-for-e-commerce-user-profile-backend-4053ab122a13" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Let’s Build User Profile UI With Android for our E-Commerce App | by Nil Madhab | Javarevisited | Dec, 2020 | Medium&lt;/h2&gt;
      &lt;h3&gt;Nil Madhab ・ &lt;time&gt;Jan 8, 2021&lt;/time&gt; ・ 10 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBvj_QRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;The most important part of an online shop is products. We are going to create product backend APIs and consume those APIs in our web front-end built using the shiny Vue.js framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://remotedevs.org:8000/"&gt;Webtutsplus E-commerce App&lt;/a&gt;&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/oh8mse1eD3U"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Database Design
&lt;/h2&gt;

&lt;p&gt;This is a sample JSON of a product, we will link it to a category and add other fields in later tutorials.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
products.sql




&lt;p&gt;Each product has a unique ID, name, image URL, price, description. We can model it in Springboot as&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  API Design
&lt;/h2&gt;

&lt;p&gt;In the real-world, we need to see the products, create new products, and update the products. So we will create 3 APIs. Later we will create many other real-world features like deleting, stocks, linking to categories, and adding tags. After this, we will create the UI.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
Product controller to create, add and update products




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mvbgzd_G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j0msr8zwd1z2dot51w5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mvbgzd_G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j0msr8zwd1z2dot51w5o.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
Swagger listing of APIs



&lt;p&gt;You can find the backend code in this &lt;a href="https://bitbucket.org/ecommerce-webtutsplus/ecommerce/src/596dd222404adcdd5541ddd04e48f9b75dfa3205/?at=nmadhab%2Fproducts-v1-first-tutorial"&gt;branch&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Vue.js introduction
&lt;/h2&gt;

&lt;p&gt;We will use Vue.js 3 to design the front end for displaying products of our E-commerce app.&lt;/p&gt;

&lt;p&gt;Vue is a JavaScript framework that is used to build user interfaces. The official page of Vue describes it as a “progressive framework” which is “incrementally adoptable”. Both of these terms roughly relate to the same meaning. Vue can be used in two ways. We can either create a full Vue website, commonly known as a Single-page application or we can create stand-alone widgets (also known as components) which we can then use in our HTML as an addition. This way, the adoption of Vue in our application is totally in our hands. In this tutorial, we will be creating a Vue component to display products of our E-Commerce App.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vue.js Requirements
&lt;/h2&gt;

&lt;p&gt;As we are just creating a single widget for displaying the product and its details, we can make use of the Vue CDN from its installation page and carry on. Using Vue CDN is a beginner-friendly way to start our journey. For bigger projects, a different setup using Vue CLI is recommended. As a code editor, I will be using VS Code, but you can use any editor of your choice. First, we will create the following three files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PoCQvmOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vwa59mux0ut5urlxxpy0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PoCQvmOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vwa59mux0ut5urlxxpy0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s get started
&lt;/h2&gt;

&lt;p&gt;First, let us set up the boilerplate for index.html. Our very first requirement is Vue CDN. Apart from this, I am also including Bootstrap 4 CDN and some Google fonts which I will be using. And for my custom styles, I am also including the link to my external CSS file. Also, include the script tag to link our JavaScript file app.js in the body tag. After all these things are done, our index.html will look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We will revisit this file once we are done with our app.js file.&lt;/p&gt;

&lt;p&gt;You can find the complete code &lt;a href="https://bitbucket.org/ecommerce-webtutsplus/ecommerce-ui/src/68a63aa70194f9064e8be0115f86e95293fe1a05/?at=chitresh%2Fvue.js-product-edit"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Vue Component
&lt;/h2&gt;

&lt;p&gt;Now it's time for our app.js file, where we will be writing the logic for creating our Vue Component. To create a component, Vue provides us with a method createApp(). This method takes as a parameter a component instance. This instance is made up of various component options and Lifecycle hooks that add user-defined behaviour to the component.&lt;br&gt;
&lt;strong&gt;Component Options&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will be using two of the component options — data and methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The data option lists all the data fields which are exposed by our component instance.&lt;/li&gt;
&lt;li&gt;The methods option lists all the functions which we might need to use. For example, here we can define a function to execute whenever some button in our component template is clicked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lifecycle Hooks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Apart from these component options, our component instance also consists of Lifecycle hooks. Lifecycle hooks are the functions that our component automatically runs at different stages of its initialization. These stages include when a component is created, added to the DOM, updated, or destroyed. We will be using the lifecycle hook, mounted. This function is called when our component is added to the DOM. In this function, we can make HTTP requests to fetch data for our component.&lt;/p&gt;
&lt;h2&gt;
  
  
  Our Component
&lt;/h2&gt;

&lt;p&gt;So in our case, the data option consists of six fields. A product array to store our products, showAddForm(a boolean value) to decide whether to display form for adding a new product and four-string data fields which we will use to get input from the user for product edit functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data() {
    return {
        products : null,
        showAddForm : false,
        tempName : "",
        tempDescription : "",
        tempPrice : "",
        tempImageURL : "" 
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In methods option, we have five methods,&lt;strong&gt;resetInputFields, addProductButtonPressed, addProduct, editButtonPressed and editProduct.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;resetInputFields -&lt;/strong&gt; it sets the values of all data fields bound to the input form to an empty string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;addProductButtonPressed -&lt;/strong&gt; it toggles the display of form used for adding a new product. It gets executed whenever the user clicks the “Add a New Product” button.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;addProduct -&lt;/strong&gt; it creates a new product as per the user input and makes a POST request to backend API to add this product. It gets executed when the user submits the form for adding a new product.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;editButtonPressed -&lt;/strong&gt; it toggles the form for editing a product based on the product’s showEditForm property and sets the values of input fields as per the invoking product details. It gets executed when the user clicks the “Edit” button. For distinguishing the product, this method takes the index of the product as a method argument.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;editProduct-&lt;/strong&gt; it creates a new product as per the user input and makes a POST request to backend API to update the product details. This method also takes the index of the product as a method argument. It gets executed when the user submits the form for editing a product. The backend API makes use of the product’s unique id to update its details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In mounted life cycle hook, we fetch all the products from the E-Commerce API and store them in the products array. In addition to this, we also add a property showEditForm to all products and set it to false.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mounted : async function mounted(){
    const response  = await fetch(urlGetProduct);
    this.products = await response.json();
    for(product of this.products) {
        product.showEditForm = false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now that our component is created we have to add this component to our DOM. For this, we make use of the mount method.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.mount("#app");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This statement mounts our component to the HTML element with id “app”. Now our Vue component controls everything inside this element. We can make this HTML element to render dynamic data, add interactivity and handle events occurring inside it. This completes the creation of our Vue component in app.js.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Designing the Component’s Template
&lt;/h2&gt;

&lt;p&gt;We are now back to our index.html file.&lt;/p&gt;

&lt;p&gt;We have created an h1 tag for displaying the heading at the top of the web page. Then we have to create a div element with id as “app”. This will be the playground of our Vue component. All the data fields declared in our component’s data option are accessible here. First, we will design a form for adding a new product. Then a section to display product details. Finally, a form for editing products. This will be very similar to the first form.&lt;/p&gt;

&lt;p&gt;For achieving all this, Vue gives us the power of directives.&lt;br&gt;
&lt;strong&gt;Vue Directives&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Directives are like instructions to Vue to do something. Vue provides us with many directives in our armoury. These directives can be used for listening to events and conditional rendering of HTML elements to the DOM.&lt;/p&gt;
&lt;h2&gt;
  
  
  Add Form
&lt;/h2&gt;

&lt;p&gt;Let’s start with the add form. Here we will use the directives &lt;strong&gt;“v-on”&lt;/strong&gt; and “v-show”. The &lt;strong&gt;“v-on”&lt;/strong&gt; directive is used for listening to DOM events. This includes events like clicking, hovering, scrolling and many more. We will use this directive to listen to click events on the “Add a new product” button. On this click event, we can either perform some JavaScript logic or we can call some method of our component declared earlier. While calling the method, we can also pass arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button v-on:click=”addProductButtonPressed”&amp;gt; Add Product &amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Vue also provides shortcuts for some directives. For example, &lt;strong&gt;“v-on”&lt;/strong&gt; can be replaced with @ symbol.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button @click=”addProductButtonPressed”&amp;gt; Add Product &amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We will use the “v-show” directive to show the form for adding products. The “v-show” directive can be used to toggle the display of some HTML element based on some boolean condition. In the form, we will make use of the boolean variable showAddForm to decide whether to display the form or not.&lt;/p&gt;

&lt;p&gt;Now in designing the form, we make use of the “v-model” directive. We can use this directive in the input fields of the form. This creates a two-way binding between the input fields and the data fields of our component. This way we can easily read the input values and even reset them when required.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" placeholder="Enter name" v-model="tempName"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;At the bottom of the form will be a submit button. Here also we will make use of the “v-on” directive to listen to the click event.&lt;br&gt;
Product Details&lt;/p&gt;

&lt;p&gt;In a div element with class “products-box”, we will use the directive “v-for” for looping through the products array. In the loop, along with the individual product, we will also keep a count of the index.&lt;/p&gt;

&lt;p&gt;Now the interesting part is, inside this div, all the HTML elements will be rendered one after the other for all the individual products. And for different products, we will be displaying dynamic content based on the product details.&lt;/p&gt;

&lt;p&gt;We can use “v-bind” for binding the attributes of HTML element with dynamic content.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img v-bind:src=”product.imageURL” alt=”product-image”&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Along with “v-for” directive we also have access to “v-if” and “v-else” directive which we can use in an if-else situation. But, it is beneficial to use &lt;strong&gt;“v-show”&lt;/strong&gt;, instead of “v-if” anywhere possible because &lt;strong&gt;“v-show”&lt;/strong&gt; only displays or hides the content. In case the condition is false, it does not remove any part from the DOM, it just hides it. For displaying dynamic text content, we have to wrap data fields inside double braces.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2 class="product_name"&amp;gt;{{product.name}}&amp;lt;/h2&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;** Edit Product&lt;/p&gt;

&lt;p&gt;Finally, we will design the form for edit product functionality. The layout of this form will be similar to the earlier form. We will be using the same directives &lt;strong&gt;“v-show”&lt;/strong&gt;, &lt;strong&gt;“v-on”&lt;/strong&gt; and &lt;strong&gt;“v-model”&lt;/strong&gt;. The only difference will be the logic for handling the submission of the form.&lt;/p&gt;

&lt;p&gt;So after designing the component template, our index.html will look something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;In this HTML file, the div element with class “product-item” encapsulates displaying the product details and its edit form. It is this div element, which is rendered for all the products.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0_jsAM95--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nvr0okxvlj5en22pp7bh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0_jsAM95--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nvr0okxvlj5en22pp7bh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CSS&lt;/p&gt;

&lt;p&gt;Let’s also add some custom CSS :&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So now we have a front-end to display all the products and their details. We have also got the functionality to add new products and edit the existing ones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iXtzscxy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v832dnkpywb8jcbgum6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iXtzscxy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v832dnkpywb8jcbgum6h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use a framework?
&lt;/h2&gt;

&lt;p&gt;Front-end frameworks have made the lives of developers a lot easier. The biggest advantage of using a framework over traditional JavaScript is the link between our data and its presentation. Using frameworks, developers do not need to worry about writing the code for updating DOM when some variables change. Frameworks achieve this very easily using two-way binding. Along with easing our work, some frameworks also provide additional features like routing, which can be used to create Single Page Applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  But why Vue.js?
&lt;/h2&gt;

&lt;p&gt;Vue.js is getting backed by a lot of developers. The Vue.js community is rapidly growing. The recent updates to Vue have made it a lot more robust. Many prefer Vue.js over other frameworks because of its ease-of-use, its performance, and its compact size. The official documentation of Vue is a great resource to get a deep dive into the framework. Many also consider Vue as a combination of all good parts of React and Angular. Hence, Vue.js is worth giving a try.&lt;br&gt;
Follow the next tutorial in the series, where we will create categories and link products with categories.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/javascript-in-plain-english/lets-link-product-with-category-for-our-ecommerce-app-28100657a848" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vm73w5AQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AhNjwft_dNBxvTr6g6TOVGg.jpeg" alt="Nil Madhab"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/javascript-in-plain-english/lets-link-product-with-category-for-our-ecommerce-app-28100657a848" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Let’s Link Products With Category for our E-Commerce App | by Nil Madhab | JavaScript In Plain English | Dec, 2020 | Medium&lt;/h2&gt;
      &lt;h3&gt;Nil Madhab ・ &lt;time&gt;Jan 2, 2021&lt;/time&gt; ・ 10 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBvj_QRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/webtutsplus/creating-a-simple-vue-js-website-for-our-backend-1d1ef8839c27" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vm73w5AQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AhNjwft_dNBxvTr6g6TOVGg.jpeg" alt="Nil Madhab"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/webtutsplus/creating-a-simple-vue-js-website-for-our-backend-1d1ef8839c27" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Vue.js UI for our Java Springboot backend | JavaScript In Plain English&lt;/h2&gt;
      &lt;h3&gt;Nil Madhab ・ &lt;time&gt;Jan 3, 2021&lt;/time&gt; ・ 13 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBvj_QRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;a href="https://getbootstrap.com/docs/4.0/getting-started/introduction/"&gt;Bootstrap 4 CDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitbucket.org/ecommerce-webtutsplus/ecommerce-ui/src/68a63aa70194f9064e8be0115f86e95293fe1a05/?at=chitresh%2Fvue.js-product-edit"&gt;Frontend Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitbucket.org/ecommerce-webtutsplus/ecommerce/src/596dd222404adcdd5541ddd04e48f9b75dfa3205/?at=nmadhab%2Fproducts-v1-first-tutorial"&gt;Backend code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v3.vuejs.org/guide/introduction.html"&gt;Vue.js 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://remotedevs.org:8080/api/swagger-ui.html"&gt;Swagger-UI for the API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fonts.google.com/"&gt;Google Fonts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitbucket.org/ecommerce-webtutsplus/ecommerce/src/master/"&gt;Backend API Repository&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thank You for Reading!&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Let’s Build User Profile UI With Android for our E-Commerce App</title>
      <dc:creator>KunalShekhar</dc:creator>
      <pubDate>Tue, 12 Jan 2021 16:20:08 +0000</pubDate>
      <link>https://dev.to/webtutsplus/let-s-build-user-profile-ui-with-android-for-our-e-commerce-app-nmg</link>
      <guid>https://dev.to/webtutsplus/let-s-build-user-profile-ui-with-android-for-our-e-commerce-app-nmg</guid>
      <description>&lt;p&gt;We are going to create an android UI for our UserProfile backend API we are creating for our demo E-commerce tutorial&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m_WluMeh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/om7nw0ur5fqqz0bjoao5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m_WluMeh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/om7nw0ur5fqqz0bjoao5.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We are building an e-commerce platform from scratch. In the last tutorial, we build the backend for the UserProfile.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/technology-hits/lets-develop-an-ecommerce-application-from-scratch-using-java-and-spring-6dfac6ce5a9f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vm73w5AQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AhNjwft_dNBxvTr6g6TOVGg.jpeg" alt="Nil Madhab"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/technology-hits/lets-develop-an-ecommerce-application-from-scratch-using-java-and-spring-6dfac6ce5a9f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Let’s Develop an E-Commerce Application From Scratch Using Java and Spring | by Nil Madhab | Javarevisited | Dec, 2020 | Medium&lt;/h2&gt;
      &lt;h3&gt;Nil Madhab ・ &lt;time&gt;Jan 10, 2021&lt;/time&gt; ・ 5 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBvj_QRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this article, we will build the android UI for the UserProfile backend, which can be a base for many other applications.&lt;/p&gt;

&lt;p&gt;Not long before was the time when the user profile consisted of a hard copy form. From the advent of digital technology and sophisticated software, profiles have been becoming more intuitive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backend API description
&lt;/h2&gt;

&lt;p&gt;Here we describe the most important component of our user profile application: the REST API. A REST API is an application that uses the existing protocol stack for data communication between different applications on the web. Here, we are using a web API to communicate between our android application and a remote database for user data storage. Our API handles the GET and POST request which basically allows the android application to create and fetch data from the database.&lt;/p&gt;

&lt;p&gt;The API used here is created using Java and Spring Boot framework. Spring is a popular application development framework, developed for the enterprise edition of the Java programming language. You can find detailed project files for the REST API over here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X8VzxT1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ves5ozdti5pm8k9n1ikk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X8VzxT1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ves5ozdti5pm8k9n1ikk.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iB8musS4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ns8cl374mog2s1kdubcy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iB8musS4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ns8cl374mog2s1kdubcy.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Android Studio is the official Integrated Development Environment (IDE) for &lt;a href="https://medium.com/hackernoon/top-5-courses-to-learn-android-for-java-programmers-667e03d995b4"&gt;Android app development&lt;/a&gt;, based on IntelliJ IDEA. On top of IntelliJ’s powerful code editor and developer tools, Android Studio offers even more features that enhance your productivity when building Android apps, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A flexible Gradle-based build system.&lt;/li&gt;
&lt;li&gt;A fast and feature-rich emulator.&lt;/li&gt;
&lt;li&gt;A unified environment where you can develop for all Android devices.&lt;/li&gt;
&lt;li&gt;Apply Changes to push code and resource changes to your running app without restarting your app.&lt;/li&gt;
&lt;li&gt;Code templates and GitHub integration to help you build common app features and import sample code.&lt;/li&gt;
&lt;li&gt;Extensive testing tools and frameworks.&lt;/li&gt;
&lt;li&gt;Lint tools to catch performance, usability, version compatibility, and other problems.&lt;/li&gt;
&lt;li&gt;C++ and NDK support.&lt;/li&gt;
&lt;li&gt;Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benefit of using Android Studio over any other IDE is we get a rich set of features built-in which enable us to focus more on the development aspect while the back-end of this IDE handles the requirements and dependencies. For this project, an existing API endpoint has been used for database connectivity.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite:
&lt;/h2&gt;

&lt;p&gt;Before we start developing, we need to check off a few requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You will need to have a Java JDK installed on your system. You can find the instructions for installing the JDK here.&lt;/li&gt;
&lt;li&gt;You will need to install Android Studio on your system.&lt;/li&gt;
&lt;li&gt;Lastly, we can use our Android smartphone with USB debugging enabled for testing the android app or we can use the built-in Android emulator from Android Studio (I have used my smartphone for testing and debugging).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Project Setup and Structure:
&lt;/h2&gt;

&lt;p&gt;Once you install Android Studio on your system, you can start with creating a new project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SCv96Jrv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kwha5j5mk052n6yuatua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SCv96Jrv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kwha5j5mk052n6yuatua.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
Android Studio Welcome Screen



&lt;p&gt;To create your new Android project, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the &lt;strong&gt;Welcome to Android Studio&lt;/strong&gt; window, click &lt;strong&gt;Start a new Android Studio project.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;In the Select a &lt;strong&gt;Project Template&lt;/strong&gt; window, select &lt;strong&gt;Empty Activity&lt;/strong&gt; and click &lt;strong&gt;Next.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;In the &lt;strong&gt;Configure your project&lt;/strong&gt; window, complete the following:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Enter “User Authentication” in the &lt;strong&gt;Name&lt;/strong&gt; field.&lt;/li&gt;
&lt;li&gt;Enter “com.example.userprofile” in the &lt;strong&gt;Package name&lt;/strong&gt; field.&lt;/li&gt;
&lt;li&gt;If you’d like to place the project in a different folder, change its &lt;strong&gt;Save&lt;/strong&gt; location.&lt;/li&gt;
&lt;li&gt;Select Java from the Language drop-down menu.&lt;/li&gt;
&lt;li&gt;Select the lowest version of Android your app will support in the &lt;strong&gt;Minimum SDK&lt;/strong&gt; field. I have selected API level 22 (Android Lollipop).&lt;/li&gt;
&lt;li&gt;Leave the other options as they are.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Finish.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After some processing time, the Android Studio main window appears.&lt;/p&gt;

&lt;p&gt;Now take a moment to review the most important files. First, be sure the Project window is open (select &lt;strong&gt;View &amp;gt; Tool Windows &amp;gt; Project)&lt;/strong&gt; and the Android view is selected from the drop-down list at the top of that window. You can then see the following files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;app &amp;gt; java &amp;gt; com.example.userprofile &amp;gt; MainActivity&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the main activity. It’s the entry point for your app. When you build and run your app, the system launches an instance of this Activity and loads its layout.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;app &amp;gt; res &amp;gt; layout &amp;gt; activity_main.xml&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This XML file defines the layout for the activity’s user interface (UI). It contains a TextView element with the text “Hello, World!”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;app &amp;gt; manifests &amp;gt; AndroidManifest.xml&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The manifest file describes the fundamental characteristics of the app and defines each of its components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Gradle Scripts &amp;gt; build.gradle&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two files with this name: one for the project, “Project: User Authentication” and one for the app module, “Module: app.” Each module has its own build.gradle file, but this project currently has just one module. Use each module’s build.gradle file to control how the Gradle plugin builds your app. For more information about this file, see Configure your build.&lt;/p&gt;

&lt;p&gt;Before you run your first app, get your smartphone, and enable USB debugging by following the instructions over here. After, enabling USB debugging, connect your smartphone to your PC and you are ready to go.&lt;/p&gt;

&lt;p&gt;In Android Studio, &lt;strong&gt;select Run &amp;gt; Run ‘app’&lt;/strong&gt; or click the Run icon in the toolbar.&lt;/p&gt;

&lt;p&gt;Congratulations on creating your very first app. But you are far from what we have to achieve. Next, we need to create the rest of the application.&lt;br&gt;
Setting up the files:&lt;/p&gt;

&lt;p&gt;Let’s start with &lt;strong&gt;app &amp;gt; res &amp;gt; drawables.&lt;/strong&gt; This is the directory where we will be storing all the graphical elements for our application. We are going to store three different categories of graphic elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image. (filename: background.jpg)&lt;/li&gt;
&lt;li&gt;Icon elements. (filenames: account.xml, email.xml, pencil.xml)&lt;/li&gt;
&lt;li&gt;Custom graphic file. (round_button.xml)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The image used here is for the background of the UI. You can use any image you like but be sure to use the same file name for the next part where we will be coding the back-end and creating activity layouts.&lt;/p&gt;

&lt;p&gt;Icon elements are XML files for vector graphic icons used in our application. You can find and download these files from here.&lt;/p&gt;

&lt;p&gt;The custom graphic file here is used for creating a rich look for our buttons in the application. It’s an XML file containing various tags and attributes specifying the properties that the button will have for its appearance.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Moving on, we have two layout files for our two pages: create user and fetch user data page. These files are contained in &lt;strong&gt;app &amp;gt; res &amp;gt; layout&lt;/strong&gt; having names activity_main.xml, user.xml. To create these files, right-click on the layout directory and select new &amp;gt; layout resource file. A New Resource File pop-up with several fields should appear. In the File name field, we’ll insert the required file names.&lt;/p&gt;

&lt;p&gt;In the Root element field of this same pop-up, make sure to type in RelativeLayout. The remaining two fields can remain the same. Click OK. This will create a new layout for us.&lt;/p&gt;

&lt;p&gt;However, Android Studio will likely default to the visual editor we saw our Constraint Layout in earlier. To edit our layout’s XML directly, we’ll need to navigate out of the visual editor and into the XML editor. Near the top-right of the editor screen, you should see three tabs: code, split, design. Select the code tab to toggle into the XML editor.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
activity_main.xml


 


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
user.xml




&lt;p&gt;If you can notice then all these files contain some common tags having some attributes. These tags are View elements that are going to be displayed in the application. Let’s review them one by one for a general idea.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RelativeLayout:&lt;/strong&gt; RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left, or center).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TextView:&lt;/strong&gt; A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however, the basic class is configured to not allow editing.
EditText: EditText is a standard entry widget in android apps. It is an overlay over TextView that configures itself to be editable. EditText is a subclass of TextView with text editing operations. We often use EditText in our applications to provide an input or text field, especially in forms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Button:&lt;/strong&gt; In Android, Button represents a push button. Push buttons can be clicked, or pressed by the user to perform an action. On a button, we can perform different actions or events like click event, pressed event, touch event, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TableLayout:&lt;/strong&gt; A layout that arranges its children into rows and columns. A TableLayout consists of several TableRow objects, each defining a row. TableLayout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The table has as many columns as the row with the most cells. A table can leave cells empty. Cells can span columns, as they can in HTML.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TableRow:&lt;/strong&gt; For building a row in a table we use the TableRow element. Table row objects are the child's views of a table layout. Each row of the table has zero or more cells and each cell can hold only one view object like ImageView, TextView, or any other view.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wOEMPZwE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kxlpqfoay2qdtoa82i0r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wOEMPZwE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kxlpqfoay2qdtoa82i0r.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
Rows and Columns in TableLayout Android



&lt;p&gt;There are many more tags and have many different attributes for creating various intuitive design elements and UI for an android application.&lt;/p&gt;

&lt;p&gt;Next up, we will create the files necessary for the back-end of our applications: Java files.&lt;/p&gt;

&lt;p&gt;We will be creating two more java class files which are Config.java and User.java.&lt;/p&gt;

&lt;p&gt;To create them, right-click on &lt;strong&gt;app &amp;gt; java &amp;gt; com.example.userprofile directory and click New &amp;gt; Java Class.&lt;/strong&gt; Enter the file name and press enter.&lt;/p&gt;

&lt;p&gt;Before moving ahead, we must configure our AndroidManifest.xml file for the file additions and requirements that we want.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
AndroidManifest.xml




&lt;h2&gt;
  
  
  Adding in the code
&lt;/h2&gt;

&lt;p&gt;To save the hassle, each class for a given activity contains a default piece of code that is essential for the behavior of the application.&lt;/p&gt;

&lt;p&gt;As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different stages in their life-cycle. The Activity class provides several callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.&lt;/p&gt;

&lt;p&gt;Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, if you’re building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app.&lt;/p&gt;

&lt;p&gt;When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot. In other words, each callback allows you to perform specific work that’s appropriate to a given change of state. Doing the right work at the right time and handling transitions properly make your app more robust and performant. For example, good implementation of the lifecycle callbacks can help ensure that your app avoids:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Crashing if the user receives a phone call or switches to another app while using your app.&lt;/li&gt;
&lt;li&gt;Consuming valuable system resources when the user is not actively using it.&lt;/li&gt;
&lt;li&gt;Losing the user’s progress if they leave your app and return to it at a later time.&lt;/li&gt;
&lt;li&gt;Crashing or losing the user’s progress when the screen rotates between landscape and portrait orientation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4EoOCCbI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ceax7k8hwgor529xox2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4EoOCCbI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ceax7k8hwgor529xox2w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
Activity Life Cycle



&lt;p&gt;We have extensively use the &lt;strong&gt;onCreate()&lt;/strong&gt; activity method which defines the behavior of the application on start-up.&lt;/p&gt;

&lt;p&gt;For the first file, MainActivity.java:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
MainActivity.java




&lt;p&gt;Let’s go through the methods defined in this code snippet-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;create():&lt;/strong&gt; This method is at the heart of profile creation where the magic happens. Right from getting the input to processing it; is managed by this method. The most important job of this method is to create an HTTP client tunnel connection to the server hosting the database and send the HTTP POST request. Here, we have provided the existing API endpoint. Also, this method will be handling failed requests and other errors while successfully creating a user profile.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;user():&lt;/strong&gt; This method starts a new activity called user. There we can view the existing user profiles in the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, we have the User.java file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
User.java




&lt;p&gt;Let’s go through the methods defined in this code snippet-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;fetch():&lt;/strong&gt; This method performs the operation of fetching user data from a remote database. It performs this operation by sending an HTTP GET request. After fetching the data, the method dynamically creates the required number of rows for the number of users that will be displayed on the UI. The received data is in the form of a JSON string, which is parsed into a JSON object. From here, we extract the necessary data for display purposes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;createTextView():&lt;/strong&gt; This method creates a TextView object while setting the required parameters to this object and finally attaching it as a child to the TableRow object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we have the Config.java file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
config.java




&lt;p&gt;This file is a java interface file that stores the API endpoint.&lt;/p&gt;

&lt;p&gt;Now we will run our program as previously stated by connecting our android device via USB.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/BFWYc6tQuoc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/webtutsplus/android-app-for-spring-backend-68f39dca8b72" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vm73w5AQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AhNjwft_dNBxvTr6g6TOVGg.jpeg" alt="Nil Madhab"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/webtutsplus/android-app-for-spring-backend-68f39dca8b72" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;User Authentication Android Application for Java and Spring Backend | Javarevisited&lt;/h2&gt;
      &lt;h3&gt;Nil Madhab ・ &lt;time&gt;Jan 8, 2021&lt;/time&gt; ・ 6 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KBvj_QRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/tools/android-studio/docs/"&gt;Google Cloud Platform&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.google.com/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=&amp;amp;cad=rja&amp;amp;uact=8&amp;amp;ved=2ahUKEwio5YXQ2MLtAhX7yjgGHabuC8oQFjAAegQIARAD&amp;amp;url=https%3A%2F%2Fdeveloper.android.com%2Fstudio&amp;amp;usg=AOvVaw3fIlahucURgOEYHHhVdQuW"&gt;Android Studio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/studio/build#module-level"&gt;Configuring your build&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/studio/debug/dev-options"&gt;Enable USB debugging on your phone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.google.com/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=&amp;amp;cad=rja&amp;amp;uact=8&amp;amp;ved=2ahUKEwi23sKz48LtAhW_xDgGHdBfB7kQFjABegQIARAC&amp;amp;url=https%3A%2F%2Fmaterialdesignicons.com%2F&amp;amp;usg=AOvVaw0nKP6M_S9hl0vHqp8CiPth"&gt;Material Design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/guide"&gt;Android App Developer’s guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://restfulapi.net/http-methods/"&gt;HTTP and the REST API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitbucket.org/ecommerce-webtutsplus/android-ui/src/master/"&gt;GitHub Repository for the above code and resources&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bitbucket.org/ecommerce-webtutsplus/ecommerce/src/master/"&gt;GitHub Repository for the REST API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://138.68.64.95:8080/api/swagger-ui.html#/"&gt;Swagger-UI for the API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Let’s Build Signup, SignIn, and Role-Based Access in Our E-Commerce App</title>
      <dc:creator>KunalShekhar</dc:creator>
      <pubDate>Tue, 12 Jan 2021 13:04:43 +0000</pubDate>
      <link>https://dev.to/webtutsplus/let-s-build-signup-signin-and-role-based-access-in-our-e-commerce-app-18bl</link>
      <guid>https://dev.to/webtutsplus/let-s-build-signup-signin-and-role-based-access-in-our-e-commerce-app-18bl</guid>
      <description>&lt;p&gt;An Overview Of The Tutorial!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Introduction

Database Design

API Design

Demo overview

Signup

Dealing with the Android Application

Our Intention

Requirements

Dependencies

Permissions

Creating Models

Updating the API

Creating Activities

The Main Page

Signing Up Users

Signing In Users

Constructing the Dashboard

Running the Application

Resources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;This is a series of tutorials we are building to demonstrate how to build an E-Commerce App, one component at a time.&lt;/p&gt;

&lt;p&gt;We are going to implement a basic authentication which will be extended to role-based access i.e. Admin can change anything, add new Users, a manager can only add/update Category and Products, users can only see the products and Category.&lt;/p&gt;

&lt;p&gt;When users/admin sign in, we will generate an authentication token, which will be used to verify the users, when they are going to access an API later.&lt;/p&gt;

&lt;p&gt;Database Design&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YqAeEQKX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ijakbo2gz4d78ybecaxv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YqAeEQKX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ijakbo2gz4d78ybecaxv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will have a user table and tokens table. For every user, when they signUp and sign in, we will generate a token, which will have an expiry date. After the expiry day has passed, we should generate a new token, although we will not cover it in the tutorial.&lt;/p&gt;

&lt;p&gt;API Design&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
UserController will have two methods, Signup and SignIn, which will be POST requests.&lt;/p&gt;

&lt;p&gt;Demo Overview&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/UGAjCy5Jc4A"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;SignUp&lt;/p&gt;

&lt;p&gt;Let's look at signUp API. It takes SignupDto as Input and returns True or False as output depending upon if SignUp succeeds.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
We follow these steps for signup
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.Encrypt the password
2.Save the User
3.Generate auth token and save it in database
4.Return Success
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We will now look at models that are mapped as a table in the database&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;User and Tokens are linked by user_id field in tokens table, which has one to one relationship, i.e one user can have one token and vice versa.&lt;/p&gt;

&lt;p&gt;User and Tokens are linked by user_id field in tokens table, which has one to one relationship, i.e one user can have one token and vice versa.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Let’s Develop an E-Commerce Application From Scratch Using Java</title>
      <dc:creator>Nil Madhab</dc:creator>
      <pubDate>Mon, 11 Jan 2021 12:52:23 +0000</pubDate>
      <link>https://dev.to/webtutsplus/let-s-develop-an-e-commerce-application-from-scratch-using-java-1gap</link>
      <guid>https://dev.to/webtutsplus/let-s-develop-an-e-commerce-application-from-scratch-using-java-1gap</guid>
      <description>&lt;h2&gt;
  
  
  Let’s Develop an E-Commerce Application From Scratch Using Java and Spring
&lt;/h2&gt;

&lt;p&gt;Project setup, develop category and product APIs&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3788%2F1%2AT44BfYTTAI3Dj7B47YtzHw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3788%2F1%2AT44BfYTTAI3Dj7B47YtzHw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;In my opinion, the best way to learn programming is to create a real life project which has practical use, this way the entire learning experience becomes quite exciting. Also, you can showcase your app in your portfolio, which can help you a lot if you want to land a freelancing gig or in an interview.&lt;/p&gt;

&lt;p&gt;In this series of blogs, you will amplify your development skills by learning how to build an e-commerce platform from scratch. First, you have to be familiar with &lt;a href="https://medium.com/javarevisited/10-best-places-to-learn-java-online-for-free-ce5e713ab5b2" rel="noopener noreferrer"&gt;Java &lt;/a&gt;and Spring Boot, which we will use to build the backend, and &lt;a href="https://medium.com/javarevisited/10-free-vue-js-nuxt-js-online-courses-for-beginners-in-2021-a347ea2ad144" rel="noopener noreferrer"&gt;Vue.js&lt;/a&gt;, which we will use for the frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Video&lt;/strong&gt; tutorial
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/MgOJmWMSTuQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Project
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, go to &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;https://start.spring.io/&lt;/a&gt; where we can create new spring app and add dependencies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select maven, add &lt;a href="https://javarevisited.blogspot.com/2021/08/top-5-spring-data-jpa-courses-for-java.html" rel="noopener noreferrer"&gt;Spring Data JPA&lt;/a&gt; and Spring Web dependencies&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2690%2F1%2A-C_FlCw8WFUmhb_A7FM4_A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2690%2F1%2A-C_FlCw8WFUmhb_A7FM4_A.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click on Generate and download the .zip file, uncompress it and open it using the &lt;a href="https://medium.com/javarevisited/7-best-courses-to-learn-intellij-idea-for-beginners-and-experienced-java-programmers-2e9aa9bb0c05" rel="noopener noreferrer"&gt;IntelliJ Idea&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2184%2F1%2AgO84VoYpy6C1qnzWJb8LBg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2184%2F1%2AgO84VoYpy6C1qnzWJb8LBg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Main class
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;src/main/java&lt;/em&gt; folder of the project contains a class that has a main method. It is the entry point for the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  application.properties
&lt;/h3&gt;

&lt;p&gt;In &lt;em&gt;src/main/resources&lt;/em&gt; folder there will be a file named &lt;strong&gt;‘application.properties’&lt;/strong&gt;. This file will play a major role in conveying the spring the configurations that we make and how it should create the object for us. In other words, it plays a major role in &lt;a href="https://javarevisited.blogspot.com/2012/12/inversion-of-control-dependency-injection-design-pattern-spring-example-tutorial.html#axzz6u4HTHz4Z" rel="noopener noreferrer"&gt;Inversion of Control(IoC)&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  pom.xml
&lt;/h3&gt;

&lt;p&gt;In the project folder, there will be a file called &lt;a href="https://javarevisited.blogspot.com/2017/06/10-maven-tips-java-developer-should-know.html" rel="noopener noreferrer"&gt;**‘pom.xml’&lt;/a&gt;**. This file is where we will be adding all the required dependencies.&lt;/p&gt;

&lt;p&gt;Now, the project structure will be as below-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ArPf7v55UExPK1fsvGGpjgA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ArPf7v55UExPK1fsvGGpjgA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check the project structure of the backend in the GitHub repository branch given below-&lt;br&gt;
&lt;a href="https://github.com/webtutsplus/ecommerce" rel="noopener noreferrer"&gt;&lt;strong&gt;GitHub — webtutsplus/ecommerce&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Overview of our Backend Application
&lt;/h2&gt;

&lt;p&gt;In this Spring Application, following are important packages that you have to know before starting.&lt;/p&gt;

&lt;p&gt;This is spring architecture. The outside world calls the &lt;a href="https://medium.com/javarevisited/10-best-java-web-services-rest-soap-and-api-courses-for-beginners-724a8f51298d" rel="noopener noreferrer"&gt;REST APIs&lt;/a&gt;, which interact with the &lt;a href="https://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html#ixzz6OYNB9oii" rel="noopener noreferrer"&gt;controller&lt;/a&gt;, which interacts with the Service. Service calls the repository.&lt;/p&gt;

&lt;p&gt;The repository interacts with the database. We follow this pattern to make the codebase maintainable, instead of having spaghetti code, which can be a nightmare in long term.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AaCDU4twFx5VuwdIyebYcdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AaCDU4twFx5VuwdIyebYcdw.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Model / Entity
&lt;/h2&gt;

&lt;p&gt;Model is the basic entity that has a direct relationship with the structure of a table in the &lt;a href="https://medium.com/javarevisited/top-10-free-courses-to-learn-microsoft-sql-server-and-oracle-database-in-2020-6708afcf4ad7" rel="noopener noreferrer"&gt;database&lt;/a&gt;. In other words, these models serve as containers that hold similar and relative data that are used to transport these data from clients to the database. User Profile, Product, and Category are some models in our backend application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Repository
&lt;/h2&gt;

&lt;p&gt;Repository is an interface that acts as a bridge between the database and the application. It carries the model data to and from the database. Every model will have a unique repository for the data transportation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Service
&lt;/h2&gt;

&lt;p&gt;Service is the part of the architecture where the repository is instantiated, and business logic is applied. The data from the client reaching here is manipulated and sent through the repository to the database.&lt;/p&gt;
&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://javarevisited.blogspot.com/2017/11/difference-between-component-service.html" rel="noopener noreferrer"&gt;controller &lt;/a&gt;is the part of the architecture where the requests from the clients are first handled. It controls the processes that should run on the backend and the response that has to be elicited to the clients. It interacts with the service which in turn interacts with the repository which in turn interacts with the database using models.&lt;/p&gt;
&lt;h2&gt;
  
  
  Journey of Data
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3594%2F1%2Aid0h3W4lWbFo8i-9BPGkCg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3594%2F1%2Aid0h3W4lWbFo8i-9BPGkCg.png" alt="How the data moves"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Designing the Category API
&lt;/h2&gt;

&lt;p&gt;Once we have the basic structure ready, it is time to add some product and categories for our ecommerce store.&lt;/p&gt;

&lt;p&gt;Take as an example, we can have a category of shoe and have different types of shoes as product. So one category can have many products, but each product will belong to one category.&lt;/p&gt;
&lt;h2&gt;
  
  
  Model
&lt;/h2&gt;

&lt;p&gt;First we will create a model, Category It will have four fields.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;id&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;categoryName&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;description&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;imageUrl&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will also create a setter and getter for the four fields.&lt;/p&gt;

&lt;p&gt;It will have a corresponding table categories in the &lt;a href="https://medium.com/javarevisited/8-free-oracle-database-and-sql-courses-for-beginners-f4e9b25b33c4" rel="noopener noreferrer"&gt;database&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
We are using @NotBlank annotation for the category. For that, we have to include the following dependency in pom.xml file.
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;

  &amp;lt;groupId&amp;gt;javax.validation&amp;lt;/groupId&amp;gt;

  &amp;lt;artifactId&amp;gt;validation-api&amp;lt;/artifactId&amp;gt;

&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Repository
&lt;/h2&gt;

&lt;p&gt;Now we will create a repository Categoryrepository.java that will extend JpaRepository.&lt;/p&gt;

&lt;p&gt;It will have a method findByCategoryName.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Service
&lt;/h2&gt;

&lt;p&gt;Now we will create a CategoryService file that will be responsible to create, update or fetching repositories.&lt;/p&gt;

&lt;p&gt;The Categoryrepository has inbuilt methods findAll(), save() as it is extending JpaRepository&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;We will create a helper class ApiResponse.java, which will be used to return a response for the APIs.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Now we will create the controller which will contain all the APIs

&lt;p&gt;We will create 3 APIs for category&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;create&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;update&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;list all category&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

We will also add swagger for easy testing of the code.
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

We need to also add these dependencies in pom.xml file for swagger and h2 in memory database. But you are free to choose any other database of your choice.



&lt;p&gt;io.springfox&lt;/p&gt;

&lt;p&gt;springfox-bean-validators&lt;/p&gt;

&lt;p&gt;2.9.2&lt;/p&gt;





&lt;p&gt;io.springfox&lt;/p&gt;

&lt;p&gt;springfox-swagger2&lt;/p&gt;

&lt;p&gt;2.9.2&lt;/p&gt;





&lt;p&gt;io.springfox&lt;/p&gt;

&lt;p&gt;springfox-swagger-ui&lt;/p&gt;

&lt;p&gt;2.9.2&lt;/p&gt;





&lt;p&gt;com.h2database&lt;/p&gt;

&lt;p&gt;h2&lt;/p&gt;

&lt;p&gt;runtime&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We also have to modify our application.properties file by adding the lines&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.datasource.url=jdbc:h2:mem:testdb

spring.datasource.driverClassName=org.h2.Driver

spring.datasource.username=sa

spring.datasource.password=password

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, run the code and open &lt;a href="http://localhost:8080/swagger-ui.html" rel="noopener noreferrer"&gt;http://localhost:8080/swagger-ui.html&lt;/a&gt; page. We will see this screen&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ADmVIKGkSSW14pNJOcLe9-g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ADmVIKGkSSW14pNJOcLe9-g.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;Let’s create a category watch, with this request body. (Note: we do not need to pass id here, it will be auto created.)&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "categoryName": "watches",
  "description": "best watches",
  "imageUrl": "https://images.unsplash.com/photo-1524805444758-089113d48a6d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;ixlib=rb-1.2.1&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=634&amp;amp;q=80"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ARRebmprvGLgUnrr53Fqr8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ARRebmprvGLgUnrr53Fqr8g.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will get the response as below-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2Aw91nIxx7X0dIU_g5QQPibA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2Aw91nIxx7X0dIU_g5QQPibA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us hit the get API&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2018%2F1%2A7rtYwUNeqt37NzAr-CyRYQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2018%2F1%2A7rtYwUNeqt37NzAr-CyRYQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will get the following response-&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "id": 1,
    "categoryName": "watches",
    "description": "best watches",
    "imageUrl": "https://images.unsplash.com/photo-1524805444758-089113d48a6d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;ixlib=rb-1.2.1&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=634&amp;amp;q=80"
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=wh3mDFB7OFg" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=wh3mDFB7OFg&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Enable CORS
&lt;/h2&gt;

&lt;p&gt;We will add the webconfig.java file, so that our &lt;a href="https://medium.com/javarevisited/6-best-frontend-development-courses-for-beginners-to-learn-in-2021-f2772157864" rel="noopener noreferrer"&gt;front end&lt;/a&gt; can hit the API.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Hurray! We can now play with the APIs and can create some new category, update and fetch all the categories.&lt;/p&gt;

&lt;h2&gt;
  
  
  Designing the Product API
&lt;/h2&gt;

&lt;p&gt;Now we have some categories, it is time to make the products APIs. First we will create the model, then we will create the repository, then we will make the service and at the end, we will create the controller and test it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model
&lt;/h2&gt;

&lt;p&gt;Product will have id, name, imageURL, price, description as well as a foreign key to category, as every product belong to a category.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
@Table(name = "products")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private @NotNull String name;
    private @NotNull String imageURL;
    private @NotNull double price;
    private @NotNull String description;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "category_id", nullable = false)
    Category category;


    public Product(String name, String imageURL, double price, String description, Category category) {
        super();
        this.name = name;
        this.imageURL = imageURL;
        this.price = price;
        this.description = description;
        this.category = category;
    }
// setters and getters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Repository
&lt;/h2&gt;

&lt;p&gt;Next we will create a file, ProductRepository.java in repository package, which will just extend &lt;a href="https://www.java67.com/2021/01/spring-data-jpa-interview-questions-answers-java.html" rel="noopener noreferrer"&gt;JpaRepository&lt;/a&gt;. If we need some methods we will add it later&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.educative.ecommerce.repository;

import com.educative.ecommerce.model.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository&amp;lt;Product, Integer&amp;gt; {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Service
&lt;/h2&gt;

&lt;p&gt;Now we are ready to create the service class. Create a file ProductService.java in service directory. It will have an autowired ProductRepository.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class ProductService {

@Autowired
    private ProductRepository productRepository;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  DTO concept&lt;a href="https://www.educative.io/pageeditor/5948990638522368/5499519291097088/5793128230944768#DTO-concept" rel="noopener noreferrer"&gt;#&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Before creating a product, we need to understand, what is a DTO (data transfer object)&lt;/p&gt;

&lt;p&gt;Martin Fowler introduced the concept of a Data Transfer Object (DTO) as an object that carries data between processes.&lt;/p&gt;

&lt;p&gt;In category controller, we directly used the model as request body, but that is not practical in many cases. We need to create a different object because&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;sometimes we might have to change the model, and we do not want to change the API for backward compatibility&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can’t use the model as request body if it has relationship with other model.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So quickly, we will create a package &lt;em&gt;dto&lt;/em&gt; and inside the package we will create another package product, and there we will create our ProductDto.java class, which will have the following attributes&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private Integer id;
private @NotNull String name;
private @NotNull String imageURL;
private @NotNull double price;
private @NotNull String description;
private @NotNull Integer categoryId;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are also passing categoryId, because we need this to link a product with a category.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;Now as we have the productDto ready, now time to create ProductController.java class&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@RequestMapping("/product")
public class ProductController {

@Autowired
    ProductService productService;
    @Autowired
    CategoryService categoryService;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It will autowire ProductService and CategoryService&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a new product API
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@PostMapping("/add")
    public ResponseEntity&amp;lt;ApiResponse&amp;gt; addProduct(@RequestBody ProductDto productDto) {
        Optional&amp;lt;Category&amp;gt; optionalCategory = categoryService.readCategory(productDto.getCategoryId());
        if (!optionalCategory.isPresent()) {
            return new ResponseEntity&amp;lt;ApiResponse&amp;gt;(new ApiResponse(false, "category is invalid"), HttpStatus.CONFLICT);
        }
        Category category = optionalCategory.get();
        productService.addProduct(productDto, category);
        return new ResponseEntity&amp;lt;&amp;gt;(new ApiResponse(true, "Product has been added"), HttpStatus.CREATED);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We will receive categoryId and product details from the request body.&lt;/p&gt;

&lt;p&gt;First we will check if the categoryId is valid or return “category is invalid” error.&lt;/p&gt;

&lt;p&gt;Then we will create a product by calling method, productService.addProduct which takes productDto and category as arguments.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void addProduct(ProductDto productDto, Category category) {
        Product product = getProductFromDto(productDto, category);
        productRepository.save(product);
    }

public static Product getProductFromDto(ProductDto productDto, Category category) {
        Product product = new Product();
        product.setCategory(category);
        product.setDescription(productDto.getDescription());
        product.setImageURL(productDto.getImageURL());
        product.setPrice(productDto.getPrice());
        product.setName(productDto.getName());
        return product;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The complete code can be found in the GitHub repository given below-&lt;br&gt;
&lt;a href="https://github.com/webtutsplus/ecommerce/tree/product-apis" rel="noopener noreferrer"&gt;&lt;strong&gt;GitHub — webtutsplus/ecommerce at product-apis&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this marks the end of this tutorial. But wait! The tutorial series will continue for building the UI using &lt;a href="https://medium.com/javarevisited/top-5-online-courses-to-learn-vue-js-in-2021-249e66b60646" rel="noopener noreferrer"&gt;Vue.js&lt;/a&gt; for the above-developed backend application. Till that, stay tuned!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Happy Learning&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Continue to the next tutorial, where we will use the API to make a frontend using vue.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/lets-develop-an-e-commerce-application-from-scratch-using-spring-boot-and-vue-js-aca33bd76517" rel="noopener noreferrer"&gt;https://javascript.plainenglish.io/lets-develop-an-e-commerce-application-from-scratch-using-spring-boot-and-vue-js-aca33bd76517&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>java</category>
      <category>career</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
