<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Thirdy Gayares]]></title><description><![CDATA[Explore the portfolio of Thirdy Gayares, a Software Engineer, Mobile Developer, and Data Scientist. Discover projects, expertise, and services offered.]]></description><link>https://software-engineer.thirdygayares.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1726124292518/a2d22b15-36a9-41a0-a013-6059babb8fae.png</url><title>Thirdy Gayares</title><link>https://software-engineer.thirdygayares.com</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 22:35:35 GMT</lastBuildDate><atom:link href="https://software-engineer.thirdygayares.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Set Up Python fast API]]></title><description><![CDATA[To start a project with FastAPI in Python, follow these steps:
1. Install Python & Dependencies
Make sure you have Python 3.7 or later installed. You can check your Python version with:
python --version


Create a virtual environment:

 python -m ven...]]></description><link>https://software-engineer.thirdygayares.com/set-up-python-fast-api</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/set-up-python-fast-api</guid><category><![CDATA[set up fast api]]></category><category><![CDATA[FastAPI]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Wed, 09 Apr 2025 13:12:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744204250505/7aad825b-8bbf-445e-a52d-000debc4e20d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>To start a project with FastAPI in Python, follow these steps:</p>
<h3 id="heading-1-install-python-amp-dependencies">1. Install Python &amp; Dependencies</h3>
<p>Make sure you have Python 3.7 or later installed. You can check your Python version with:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">python</span> <span class="hljs-selector-tag">--version</span>
</code></pre>
<ol start="2">
<li><h2 id="heading-create-a-virtual-environment">Create a virtual environment:</h2>
</li>
<li><pre><code class="lang-css"> <span class="hljs-selector-tag">python</span> <span class="hljs-selector-tag">-m</span> <span class="hljs-selector-tag">venv</span> <span class="hljs-selector-class">.venv</span>
</code></pre>
</li>
<li><p>Activate the virtual environment:</p>
<ul>
<li><p>On Windows:</p>
<pre><code class="lang-css">  <span class="hljs-selector-class">.venv</span>\<span class="hljs-selector-tag">Scripts</span>\<span class="hljs-selector-tag">activate</span>
</code></pre>
</li>
<li><p>On macOS/Linux:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">source</span> <span class="hljs-selector-class">.venv</span>/<span class="hljs-selector-tag">bin</span>/<span class="hljs-selector-tag">activate</span>
</code></pre>
</li>
</ul>
</li>
</ol>
<p>install them using <code>pip</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">pip</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">fastapi</span> <span class="hljs-selector-tag">uvicorn</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1744204220219/da68308b-4db4-4e91-896a-2193ce18cc19.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-2-create-mainpy">2. Create main.py</h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> fastapi <span class="hljs-keyword">import</span> FastAPI

app = FastAPI()

<span class="hljs-meta">@app.get("/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_root</span>():</span>
    <span class="hljs-keyword">return</span> {<span class="hljs-string">"message"</span>: <span class="hljs-string">"Hello, FastAPI!"</span>}
</code></pre>
<h3 id="heading-4-run-the-fastapi-application">4. Run the FastAPI Application</h3>
<p>Now, you can run the FastAPI app using <code>uvicorn</code>. From your project directory, run:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">uvicorn</span> <span class="hljs-selector-tag">main</span><span class="hljs-selector-pseudo">:app</span> <span class="hljs-selector-tag">--reload</span>
</code></pre>
<ul>
<li><p><code>main</code>: This refers to the Python file <a target="_blank" href="http://main.py"><code>main.py</code></a> (without the <code>.py</code> extension).</p>
</li>
<li><p><code>app</code>: This is the FastAPI app instance.</p>
</li>
<li><p><code>--reload</code>: This flag enables auto-reloading during development, so the server restarts when you make changes to your code.</p>
</li>
</ul>
<p>You should see output similar to this:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">INFO</span>:     <span class="hljs-selector-tag">Will</span> <span class="hljs-selector-tag">watch</span> <span class="hljs-selector-tag">for</span> <span class="hljs-selector-tag">changes</span> <span class="hljs-selector-tag">in</span> <span class="hljs-selector-tag">these</span> <span class="hljs-selector-tag">directories</span>: <span class="hljs-selector-attr">[<span class="hljs-string">'&lt;your_project_directory&gt;'</span>]</span>
<span class="hljs-selector-tag">INFO</span>:     <span class="hljs-selector-tag">Uvicorn</span> <span class="hljs-selector-tag">running</span> <span class="hljs-selector-tag">on</span> <span class="hljs-selector-tag">http</span>://127<span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.1</span><span class="hljs-selector-pseudo">:8000</span> (<span class="hljs-selector-tag">Press</span> <span class="hljs-selector-tag">CTRL</span>+<span class="hljs-selector-tag">C</span> <span class="hljs-selector-tag">to</span> <span class="hljs-selector-tag">quit</span>)
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1744204271185/1bac3b2b-8245-4c00-a9c0-fb70fcbd6118.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-5-access-the-fastapi-app">5. Access the FastAPI App</h3>
<p>Open a browser and go to:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">http</span>://127<span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.1</span><span class="hljs-selector-pseudo">:8000</span>
</code></pre>
<p>You should see a JSON response:</p>
<pre><code class="lang-css">{
  "message": "Hello, FastAPI!"
}
</code></pre>
<h3 id="heading-6-explore-the-documentation">6. Explore the Documentation</h3>
<p>FastAPI automatically generates interactive documentation for your API. To view it:</p>
<ul>
<li><p><strong>Swagger UI</strong>: Go to <a target="_blank" href="http://127.0.0.1:8000/docs"><code>http://127.0.0.1:8000/docs</code></a></p>
</li>
<li><p><strong>ReDoc</strong>: Go to <a target="_blank" href="http://127.0.0.1:8000/redoc"><code>http://127.0.0.1:8000/redoc</code></a></p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733500425036/772b9bba-9376-4154-9bb2-3dccc3dcb74a.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h3 id="heading-7-add-more-routes">7. Add More Routes</h3>
<p>You can add more endpoints (routes) to your app by defining more functions with FastAPI decorators. For example:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">from</span> <span class="hljs-selector-tag">fastapi</span> <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">FastAPI</span>

<span class="hljs-selector-tag">app</span> = <span class="hljs-selector-tag">FastAPI</span>()

<span class="hljs-keyword">@app</span>.get("/")
def read_root():
    return {"<span class="hljs-selector-tag">message</span>": "<span class="hljs-selector-tag">Hello</span>, <span class="hljs-selector-tag">FastAPI</span>!"}

<span class="hljs-keyword">@app</span>.get("/items/{<span class="hljs-selector-tag">item_id</span>}")
<span class="hljs-selector-tag">def</span> <span class="hljs-selector-tag">read_item</span>(<span class="hljs-selector-tag">item_id</span>: <span class="hljs-selector-tag">int</span>, <span class="hljs-selector-tag">q</span>: <span class="hljs-selector-tag">str</span> = <span class="hljs-selector-tag">None</span>):
    <span class="hljs-selector-tag">return</span> {"item_id": item_id, "q": q}
</code></pre>
<p>In this case, the endpoint <code>/items/{item_id}</code> will accept an <code>item_id</code> as a URL parameter, and an optional query parameter <code>q</code>.</p>
<h3 id="heading-8-test-your-api">8. Test Your API</h3>
<p>You can test the new <code>/items/{item_id}</code> endpoint by navigating to:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">http</span>://127<span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.1</span><span class="hljs-selector-pseudo">:8000</span>/<span class="hljs-selector-tag">items</span>/42?<span class="hljs-selector-tag">q</span>=<span class="hljs-selector-tag">test</span>
</code></pre>
<p>This should return:</p>
<pre><code class="lang-css">{
  "item_id": 42,
  "q": "test"
}
</code></pre>
<h3 id="heading-10-optional-create-a-requirementstxt">10. Optional: Create a <code>requirements.txt</code></h3>
<p>To share the project with others, you can generate a <code>requirements.txt</code> file that lists all your dependencies:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">pip</span> <span class="hljs-selector-tag">freeze</span> &gt; <span class="hljs-selector-tag">requirements</span><span class="hljs-selector-class">.txt</span>
</code></pre>
<p>Others can install the dependencies by running:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">pip</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">-r</span> <span class="hljs-selector-tag">requirements</span><span class="hljs-selector-class">.txt</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[Set Up React Typescript with Vite  | Thirdy Gayares]]></title><description><![CDATA[Original Content → https://thirdygayares.com/react/set-up-react-vite-typescript
In this article, I will show you how to set up a React project using Vite and TypeScript step by step. This guide is perfect for beginners who want to start using React w...]]></description><link>https://software-engineer.thirdygayares.com/set-up-react-typescript-with-vite-thirdy-gayares</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/set-up-react-typescript-with-vite-thirdy-gayares</guid><category><![CDATA[React]]></category><category><![CDATA[react-vite]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Wed, 02 Apr 2025 12:58:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1743598700331/38e0e70a-b27a-474a-8ac4-bc264679d548.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Original Content → <a target="_blank" href="https://thirdygayares.com/react/set-up-react-vite-typescript">https://thirdygayares.com/react/set-up-react-vite-typescript</a></p>
<p>In this article, I will show you how to set up a <strong>React</strong> project using <strong>Vite</strong> and <strong>TypeScript</strong> step by step. This guide is perfect for beginners who want to start using React with TypeScript in a fast and efficient way. By the end, you will have a working project that is ready for development.</p>
<p>Important Note</p>
<p>Before starting, make sure you have Node.js installed on your machine. Vite requires Node.js to run. You can download and install it from the official website: <a target="_blank" href="https://nodejs.org/">https://nodejs.org/</a>.</p>
<h3 id="heading-1-create-a-vite-react-typescript-project">1. Create a Vite + React + TypeScript Project</h3>
<p>Run the following command in your terminal:</p>
<pre><code class="lang-plaintext">npm create vite@latest my-new-app --template react-ts
</code></pre>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F1-create-vite.png&amp;w=1920&amp;q=75" alt="1. Create a Vite + React + TypeScript Project" /></p>
<p>Choose React:</p>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F2-choose-react.png&amp;w=1920&amp;q=75" alt="2. Choose React" /></p>
<p>Choose Typescript:</p>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F3-choose-typescript.png&amp;w=1920&amp;q=75" alt="3. Choose TypeScript" /></p>
<p>Then, go into the project folder:</p>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F4-go-into-the-project-folder.png&amp;w=1920&amp;q=75" alt="4. Go to the Project" /></p>
<hr />
<h3 id="heading-2-install-dependencies">2. Install Dependencies</h3>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F5-install-dependencies.png&amp;w=1920&amp;q=75" alt="5. Install Dependencies" /></p>
<h3 id="heading-3-view-source-code-on-your-favorite-ide">3. View Source Code on your favorite IDE</h3>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F6-view-source-code.png&amp;w=1920&amp;q=75" alt="View Source Code on your favorite IDE " /></p>
<h3 id="heading-4-run-your-code">4: Run your code</h3>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F7-run-your-code.png&amp;w=1920&amp;q=75" alt="Run your code " /></p>
<h3 id="heading-your-react-vite-typescript-project-is-now-ready">Your <strong>React + Vite + TypeScript</strong> project is now ready!</h3>
<p><img src="https://thirdygayares.com/_next/image?url=%2Fimage%2Fblog%2Freact%2F1-set-up-react-vite-typescript%2F8-react-run-output.png&amp;w=1920&amp;q=75" alt="React Setup Output" /></p>
]]></content:encoded></item><item><title><![CDATA[Angular API]]></title><description><![CDATA[In this documentation, we demonstrate how to integrate the API into an Angular application
POSTMAN DOCS
https://documenter.getpostman.com/view/24626304/2sAYBUDXqC
Output Link
https://angular-api-activity.web.app/user
Github:
https://github.com/thirdy...]]></description><link>https://software-engineer.thirdygayares.com/angular-api</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/angular-api</guid><category><![CDATA[angular-api]]></category><category><![CDATA[Angular]]></category><category><![CDATA[APIs]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Mon, 25 Nov 2024 02:21:27 GMT</pubDate><content:encoded><![CDATA[<p>In this documentation, we demonstrate how to integrate the API into an Angular application</p>
<p><strong>POSTMAN DOCS</strong></p>
<p><a target="_blank" href="https://documenter.getpostman.com/view/24626304/2sAYBUDXqC">https://documenter.getpostman.com/view/24626304/2sAYBUDXqC</a></p>
<p><strong>Output Link</strong></p>
<p><a target="_blank" href="https://angular-api-activity.web.app/user">https://angular-api-activity.web.app/user</a></p>
<p><strong>Github:</strong></p>
<p><a target="_blank" href="https://github.com/thirdygayares/angular-api">https://github.com/thirdygayares/angular-api</a></p>
<p><strong>API endpoint:</strong></p>
<p><a target="_blank" href="https://testsvfcb.pythonanywhere.com">https://testsvfcb.pythonanywhere.com</a></p>
<p><strong>Project Directory</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732462850551/1f393be9-d4ad-406c-a61f-962f9dea68af.png" alt class="image--center mx-auto" /></p>
<h1 id="heading-step-1-setup-angular-project">Step 1: Setup Angular Project</h1>
<p><a target="_blank" href="https://software-engineer.thirdygayares.com/angular-create-components-modules-and-routes">https://software-engineer.thirdygayares.com/angular-create-components-modules-and-routes</a></p>
<h1 id="heading-step-2-create-components-and-services"><strong>Step 2: Create Components and Services</strong></h1>
<p><a target="_blank" href="https://software-engineer.thirdygayares.com/angular-create-components-modules-and-routes">https://software-engineer.thirdygayares.com/angular-create-components-modules-and-routes</a></p>
<p><a target="_blank" href="https://software-engineer.thirdygayares.com/angular-services">https://software-engineer.thirdygayares.com/angular-services</a></p>
<h1 id="heading-step-3-create-user-model">Step 3: Create User Model</h1>
<p><code>user.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> User {
  user_id?: <span class="hljs-built_in">number</span>;
  user_uuid?: <span class="hljs-built_in">string</span>;
  email?: <span class="hljs-built_in">string</span>;
  username?: <span class="hljs-built_in">string</span>;
  password?: <span class="hljs-built_in">string</span>;
  role?: <span class="hljs-built_in">string</span>;
  created_at?: <span class="hljs-built_in">string</span>;
  updated_at?: <span class="hljs-built_in">string</span>;
}
</code></pre>
<h1 id="heading-step-4-set-up-the-api-service"><strong>Step 4: Set Up the API Service</strong></h1>
<p><code>user.service.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> {HttpClient} <span class="hljs-keyword">from</span> <span class="hljs-string">"@angular/common/http"</span>;
<span class="hljs-keyword">import</span> {User} <span class="hljs-keyword">from</span> <span class="hljs-string">"../model/user"</span>;
<span class="hljs-keyword">import</span> {Injectable} <span class="hljs-keyword">from</span> <span class="hljs-string">"@angular/core"</span>;

<span class="hljs-meta">@Injectable</span>({
  providedIn: <span class="hljs-string">'root'</span>
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> UserService {
  <span class="hljs-keyword">private</span> apiUrl = <span class="hljs-string">'https://testsvfcb.pythonanywhere.com/users/'</span>;

  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> http: HttpClient</span>) {}

  getUsers() {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.http.get&lt;User[]&gt;(<span class="hljs-built_in">this</span>.apiUrl);
  }

  getUserById(userUUId: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.http.get&lt;User&gt;(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.apiUrl}</span>/<span class="hljs-subst">${userUUId}</span>`</span>);
  }

  createUser(user: User) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.http.post&lt;User&gt;(<span class="hljs-built_in">this</span>.apiUrl, user);
  }

  updateUser(userUUId: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>, user: User) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.http.put&lt;User&gt;(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.apiUrl}</span>/<span class="hljs-subst">${userUUId}</span>`</span>, user);
  }

  deleteUser(userUUId: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.http.delete(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.apiUrl}</span>/<span class="hljs-subst">${userUUId}</span>`</span>);
  }
}
</code></pre>
<h1 id="heading-step-5-implement-user-component"><strong>Step 5: Implement User Component</strong></h1>
<p><code>user.component.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> {UserService} <span class="hljs-keyword">from</span> <span class="hljs-string">"../../services/user.service"</span>;
<span class="hljs-keyword">import</span> {Component, OnInit} <span class="hljs-keyword">from</span> <span class="hljs-string">"@angular/core"</span>;
<span class="hljs-keyword">import</span> {User} <span class="hljs-keyword">from</span> <span class="hljs-string">"../../model/user"</span>;
<span class="hljs-keyword">import</span> {NgForm} <span class="hljs-keyword">from</span> <span class="hljs-string">"@angular/forms"</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'app-user'</span>,
  templateUrl: <span class="hljs-string">'./user.component.html'</span>,
  styleUrls: [<span class="hljs-string">'./user.component.css'</span>]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> UserComponent <span class="hljs-keyword">implements</span> OnInit {
  users: User[] = [];
  selectedUser: User | <span class="hljs-literal">null</span> = <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> userService: UserService</span>) {}

  ngOnInit(): <span class="hljs-built_in">void</span> {
    <span class="hljs-built_in">this</span>.getUsers();
  }

  getUsers() {
    <span class="hljs-built_in">this</span>.userService.getUsers().subscribe({
      next: <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> <span class="hljs-built_in">this</span>.users = data,
      error: <span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> <span class="hljs-built_in">console</span>.error(err)
    });
  }

  createUser(form: NgForm) {
    <span class="hljs-keyword">if</span> (form.invalid) {
      alert(<span class="hljs-string">'Please fill out all the fields correctly.'</span>);
      <span class="hljs-keyword">return</span>;
    }

    <span class="hljs-keyword">const</span> user: User = {
      ...form.value,
      role : <span class="hljs-string">'user'</span>
    };

    <span class="hljs-built_in">this</span>.userService.createUser(user).subscribe({
      next: <span class="hljs-function">() =&gt;</span> {
        <span class="hljs-built_in">this</span>.getUsers();
        form.reset();
        alert(<span class="hljs-string">'User created successfully'</span>);
      },
      error: <span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
        <span class="hljs-built_in">console</span>.error(err);
        alert(<span class="hljs-string">'There was an error creating the user. Please check the console.'</span>);
      },
    });
  }

  updateUser(userUUId: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>, user: User) {
    <span class="hljs-keyword">if</span> (!userUUId) {
      alert(<span class="hljs-string">'User ID is required'</span>);
      <span class="hljs-keyword">return</span>;
    }

    <span class="hljs-keyword">const</span> newName = prompt(<span class="hljs-string">'Enter new username:'</span>, user.username || <span class="hljs-string">''</span>);
    <span class="hljs-keyword">const</span> newEmail = prompt(<span class="hljs-string">'Enter new email:'</span>, user.email || <span class="hljs-string">''</span>);
    <span class="hljs-keyword">const</span> newPassword = prompt(<span class="hljs-string">'Enter new password:'</span>, user.password || <span class="hljs-string">''</span>);
    <span class="hljs-keyword">const</span> newRole = prompt(<span class="hljs-string">'Enter new role:'</span>, user.role || <span class="hljs-string">''</span>);

    <span class="hljs-keyword">if</span> (!newName || !newEmail || !newPassword || !newRole) {
      alert(<span class="hljs-string">'Update canceled. All fields are required.'</span>);
      <span class="hljs-keyword">return</span>;
    }

    <span class="hljs-keyword">const</span> updatedUser: User = {
      username: newName,
      email: newEmail,
      password: newPassword,
      role: newRole,
    };

    <span class="hljs-built_in">this</span>.userService.updateUser(userUUId, updatedUser).subscribe({
      next: <span class="hljs-function">() =&gt;</span> {
        alert(<span class="hljs-string">'User updated successfully'</span>);
        <span class="hljs-built_in">this</span>.getUsers();
      },
      error: <span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
        <span class="hljs-built_in">console</span>.error(err);
        alert(<span class="hljs-string">'Failed to update user'</span>);
      },
    });
  }

  deleteUser(userUUId: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>) {
    <span class="hljs-built_in">this</span>.userService.deleteUser(userUUId).subscribe({
      next: <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">this</span>.getUsers(),
      error: <span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> <span class="hljs-built_in">console</span>.error(err)
    });
  }

  viewUserDetails(userUUId: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>) {
    <span class="hljs-built_in">this</span>.userService.getUserById(userUUId).subscribe({
      next: <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> userDetails = <span class="hljs-string">`
        Details for <span class="hljs-subst">${data.username}</span>:
        User ID: <span class="hljs-subst">${data.user_id}</span>
        User UUID: <span class="hljs-subst">${data.user_uuid}</span>
        Email: <span class="hljs-subst">${data.email}</span>
        Role: <span class="hljs-subst">${data.role}</span>
        Created At: <span class="hljs-subst">${data.created_at}</span>
        Updated At: <span class="hljs-subst">${data.updated_at}</span>

      `</span>;
        alert(userDetails); <span class="hljs-comment">// Show the details in a browser alert</span>
      },
      error: <span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> <span class="hljs-built_in">console</span>.error(err)
    });
  }

}
</code></pre>
<h1 id="heading-step-6-create-html-template"><strong>Step 6: Create HTML Template</strong></h1>
<p><code>user.component.html</code></p>
<pre><code class="lang-typescript">&lt;p&gt;Manage Users&lt;/p&gt;
&lt;section&gt;
  &lt;form #userForm=<span class="hljs-string">"ngForm"</span> (ngSubmit)=<span class="hljs-string">"createUser(userForm)"</span>&gt;
    &lt;input
      <span class="hljs-keyword">type</span>=<span class="hljs-string">"text"</span>
      name=<span class="hljs-string">"email"</span>
      ngModel
      placeholder=<span class="hljs-string">"Email"</span>
      required
      #email=<span class="hljs-string">"ngModel"</span>
    /&gt;
    &lt;div *ngIf=<span class="hljs-string">"email.invalid &amp;&amp; email.touched"</span>&gt;Email is required&lt;/div&gt;

    &lt;input
      <span class="hljs-keyword">type</span>=<span class="hljs-string">"text"</span>
      name=<span class="hljs-string">"username"</span>
      ngModel
      placeholder=<span class="hljs-string">"Username"</span>
      required
      #username=<span class="hljs-string">"ngModel"</span>
    /&gt;
    &lt;div *ngIf=<span class="hljs-string">"username.invalid &amp;&amp; username.touched"</span>&gt;Username is required&lt;/div&gt;

    &lt;input
      <span class="hljs-keyword">type</span>=<span class="hljs-string">"password"</span>
      name=<span class="hljs-string">"password"</span>
      ngModel
      placeholder=<span class="hljs-string">"Password"</span>
      required
      minlength=<span class="hljs-string">"6"</span>
      #password=<span class="hljs-string">"ngModel"</span>
    /&gt;
    &lt;div *ngIf=<span class="hljs-string">"password.invalid &amp;&amp; password.touched"</span>&gt;
      Password is required and must be at least <span class="hljs-number">6</span> characters
    &lt;/div&gt;

    &lt;button <span class="hljs-keyword">type</span>=<span class="hljs-string">"submit"</span> [disabled]=<span class="hljs-string">"userForm.invalid"</span>&gt;Create User&lt;/button&gt;
  &lt;/form&gt;

&lt;/section&gt;

&lt;section&gt;
  &lt;table border=<span class="hljs-string">"1"</span> <span class="hljs-keyword">class</span>=<span class="hljs-string">"table table-striped"</span>&gt;
    &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Id&lt;/th&gt;
      &lt;th&gt;Username&lt;/th&gt;
      &lt;th&gt;Email&lt;/th&gt;
      &lt;th&gt;Actions&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    &lt;tr *ngFor=<span class="hljs-string">"let user of users"</span>&gt;
      &lt;td&gt;{{ user.user_uuid }}&lt;/td&gt;
      &lt;td&gt;{{ user.username }}&lt;/td&gt;
      &lt;td&gt;{{ user.email }}&lt;/td&gt;
      &lt;td&gt;
        &lt;button (click)=<span class="hljs-string">"viewUserDetails(user.user_uuid)"</span>&gt;View Details&lt;/button&gt;
        &lt;button (click)=<span class="hljs-string">"updateUser(user.user_uuid, { email: 'new-email@example.com', username: 'new-username', role: 'new-role' })"</span>&gt;Update&lt;/button&gt;
        &lt;button (click)=<span class="hljs-string">"deleteUser(user.user_uuid)"</span>&gt;Delete&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/section&gt;
</code></pre>
<h1 id="heading-step-7-add-global-style">STEP 7: Add Global Style</h1>
<p><code>styles.css</code></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: Arial, sans-serif;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">1200px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
}

<span class="hljs-selector-tag">table</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">border-collapse</span>: collapse;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0px</span>;
}

<span class="hljs-selector-tag">table</span>, <span class="hljs-selector-tag">th</span>, <span class="hljs-selector-tag">td</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid black;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span>;
}

<span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">th</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f2f2f2</span>;
}

<span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">td</span> <span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#980000</span>;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span> <span class="hljs-number">10px</span>;
}

<span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">td</span> <span class="hljs-selector-tag">button</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0c92ff</span>;
}

<span class="hljs-selector-tag">nav</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0px</span>;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: blue;
  <span class="hljs-attribute">text-decoration</span>: none;
  <span class="hljs-attribute">font-weight</span>: bold;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">color</span>: darkblue;
}


<span class="hljs-selector-tag">form</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">input</span>, <span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">select</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ccc</span>;
}

<span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#980000</span>;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">border</span>: none;
}

<span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">button</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0c92ff</span>;
}
</code></pre>
<h1 id="heading-output">output:</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732501200066/97c76db4-5354-44ab-ab40-ebd6263b6a89.gif" alt class="image--center mx-auto" /></p>
<p><strong>POSTMAN DOCS</strong></p>
<p><a target="_blank" href="https://documenter.getpostman.com/view/24626304/2sAYBUDXqC">https://documenter.getpostman.com/view/24626304/2sAYBUDXqC</a></p>
<p><strong>Output Link</strong></p>
<p><a target="_blank" href="https://angular-api-activity.web.app/user">https://angular-api-activity.web.app/user</a></p>
<p>Github:</p>
<p><a target="_blank" href="https://github.com/thirdygayares/angular-api">https://github.com/thirdygayares/angular-api</a></p>
<p>Output:</p>
]]></content:encoded></item><item><title><![CDATA[Yarn vs Npm]]></title><description><![CDATA[Yarn and npm are two popular package managers for JavaScript that help manage project dependencies, but each has unique features that may suit different project needs.
1. Speed and Performance

Yarn generally offers faster installations because it do...]]></description><link>https://software-engineer.thirdygayares.com/yarn-vs-npm</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/yarn-vs-npm</guid><category><![CDATA[yarn vs npm]]></category><category><![CDATA[Yarn]]></category><category><![CDATA[npm]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Tue, 29 Oct 2024 15:35:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/0TvE4CQ33_I/upload/2357af220363ec310fbd6d6f710594ee.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Yarn and npm are two popular package managers for JavaScript that help manage project dependencies, but each has unique features that may suit different project needs.</p>
<h3 id="heading-1-speed-and-performance">1. <strong>Speed and Performance</strong></h3>
<ul>
<li><p><strong>Yarn</strong> generally offers faster installations because it downloads packages in parallel and uses offline caching, meaning it can pull packages from a local cache instead of redownloading them each time. This caching can save significant time on large projects.</p>
</li>
<li><p><strong>npm</strong> has made strides in recent versions (npm 5+), incorporating caching and improving speed, but it still installs packages sequentially, making it typically slower than Yarn, especially for complex projects with many dependencies.</p>
</li>
</ul>
<h3 id="heading-2-dependency-consistency-and-lock-files">2. <strong>Dependency Consistency and Lock Files</strong></h3>
<ul>
<li><p><strong>Yarn</strong> uses <code>yarn.lock</code> to lock package versions, ensuring that installations are consistent across different environments. This file is particularly useful for projects requiring strict version control.</p>
</li>
<li><p><strong>npm</strong> introduced a similar <code>package-lock.json</code> file in version 5 to tackle the same issue, allowing for more dependable version management across development environments.</p>
</li>
</ul>
<h3 id="heading-3-workspaces-and-monorepo-support">3. <strong>Workspaces and Monorepo Support</strong></h3>
<ul>
<li><p><strong>Yarn</strong> includes robust workspace support, making it ideal for monorepo projects where multiple packages need centralized dependency management. This feature helps teams manage large codebases with shared dependencies.</p>
</li>
<li><p><strong>npm</strong> introduced workspace support in version 7, narrowing the gap, though Yarn's workspaces remain a preferred choice for larger-scale monorepos due to its longer-established support.</p>
</li>
</ul>
<h3 id="heading-4-security-checks">4. <strong>Security Checks</strong></h3>
<ul>
<li><p><strong>npm</strong> includes <code>npm audit</code>, which scans for vulnerabilities in dependencies and provides recommendations for fixes. This feature is popular among developers prioritizing security.</p>
</li>
<li><p><strong>Yarn</strong> also offers a security check but doesn't provide as detailed insights as <code>npm audit</code>, although both package managers use encryption to verify package integrity.</p>
</li>
</ul>
<h3 id="heading-5-ease-of-use-and-commands">5. <strong>Ease of Use and Commands</strong></h3>
<ul>
<li><p><strong>Yarn</strong> and <strong>npm</strong> share many similar commands, making it easy to switch between them. For instance, <code>npm install</code> is equivalent to <code>yarn add</code>, and <code>npm uninstall</code> aligns with <code>yarn remove</code>. However, Yarn includes some unique commands, such as <code>yarn why</code> to show package dependencies, which can be helpful for debugging.</p>
</li>
<li><p>For those switching from npm to Yarn, Yarn is compatible with <code>package-lock.json</code> files, allowing easy migration without losing version data.</p>
</li>
</ul>
<h3 id="heading-6-zero-installs-yarn-only">6. <strong>Zero Installs (Yarn Only)</strong></h3>
<ul>
<li>Yarn has a unique "Zero Installs" feature, storing dependencies directly in the project’s directory. This approach allows nearly instant access without an internet connection, which can be especially valuable in distributed environments.</li>
</ul>
<p>If you prioritize <strong>speed, consistency, and monorepo support</strong>, Yarn may be the better option, especially for large projects. However, <strong>npm</strong> offers broader community support and excellent security auditing tools, making it a solid choice for projects needing these features.</p>
<p>Both Yarn and npm are robust and capable. Experimenting with both on different projects might help you decide which one fits your workflow best.</p>
<p>(<a target="_blank" href="https://www.sitepoint.com/yarn-vs-npm/">https://www.sitepoint.com/yarn-vs-npm/</a>), <a target="_blank" href="https://phoenixnap.com/kb/yarn-vs-npm">PhoenixNAP</a>, and <a target="_blank" href="https://toxigon.com/yarn-vs-npm-comparison">Toxigon</a>.</p>
]]></content:encoded></item><item><title><![CDATA[How to Create MYSQL Table with Foreign Key Constraint and add sample data]]></title><description><![CDATA[ERD:

ERD Credit from Group OnlineQuizPlatformSVFC


💡
SHOW DATABASES




💡
CREATE quiz_db database




💡
Show database again



as you can see, we are successfully created the quiz_db  

💡
use quiz_db;




💡
SHOW TABLES;




Step 1: MySQL Table...]]></description><link>https://software-engineer.thirdygayares.com/how-to-create-mysql-table-with-foreign-key-constraint-and-add-sample-data</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/how-to-create-mysql-table-with-foreign-key-constraint-and-add-sample-data</guid><category><![CDATA[insert data mysql]]></category><category><![CDATA[MySQL]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Fri, 25 Oct 2024 17:59:31 GMT</pubDate><content:encoded><![CDATA[<p>ERD:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728660669973/1233aef0-0b71-4d95-a45c-acb38872fcd8.png" alt /></p>
<p><a target="_blank" href="https://github.com/OnlineQuizPlatformSVFC/OnlineQuizPlatform_Documentation/blob/main/OnlineQuizPlatform_Docu.md">ERD Credit from Group OnlineQuizPlatformSVFC</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877062514/9ce4dd41-b372-4989-82a8-45b8afff0573.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">SHOW DATABASES</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729876591577/274399f2-6390-465c-ab00-71c84924518c.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">CREATE quiz_db database</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729876632107/ac7e2a05-30d9-45e0-8a2c-9d193be42cb4.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Show database again</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729876699604/952fb87d-dde7-4660-a5a5-1054c2927c4c.png" alt class="image--center mx-auto" /></p>
<p>as you can see, we are successfully created the quiz_db  </p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">use quiz_db;</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729876732547/b92bf4a3-486d-494e-8942-821a4c694ac6.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">SHOW TABLES;</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729876757752/fe98e132-b23e-4f02-9594-be33aea26109.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-step-1-mysql-table-creation">Step 1: MySQL Table Creation</h3>
<p>Create the required tables using MySQL <code>CREATE TABLE</code> statements. Here is the SQL to create these tables:</p>
<h3 id="heading-1-user-table">1. <strong>User Table</strong></h3>
<pre><code class="lang-typescript">CREATE TABLE User (
    user_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(<span class="hljs-number">50</span>) NOT NULL,
    email VARCHAR(<span class="hljs-number">100</span>) UNIQUE NOT NULL,
    password VARCHAR(<span class="hljs-number">255</span>) NOT NULL,
    role ENUM(<span class="hljs-string">'teacher'</span>, <span class="hljs-string">'student'</span>) NOT NULL,
    createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt DATETIME NULL ON UPDATE CURRENT_TIMESTAMP
);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877254324/890f34df-6759-41a2-a343-1273c73daa46.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">we can show the tables and describe it</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877368039/943116b8-cc8e-47be-b7d4-280bcfcc1fa0.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-2-quiz-table">2. <strong>Quiz Table</strong></h3>
<pre><code class="lang-typescript">CREATE TABLE Quiz (
    quiz_id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(<span class="hljs-number">100</span>) NOT NULL,
    description TEXT NOT NULL,
    quiz_code VARCHAR(<span class="hljs-number">20</span>) UNIQUE NOT NULL,
    teacher_id INT,
    duration INT NOT NULL,
    createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (teacher_id) REFERENCES User(user_id)
);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877497592/b8e65d67-7839-48ae-b66b-4f39641ac2d9.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-3-question-table">3. <strong>Question Table</strong></h3>
<pre><code class="lang-typescript">CREATE TABLE Question (
    question_id INT PRIMARY KEY AUTO_INCREMENT,
    quiz_id INT NOT NULL,
    question_text TEXT NOT NULL,
    question_type ENUM(<span class="hljs-string">'MCQ'</span>, <span class="hljs-string">'True/False'</span>, <span class="hljs-string">'Short Answer'</span>, <span class="hljs-string">'Multimedia'</span>) NOT NULL,
    createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (quiz_id) REFERENCES Quiz(quiz_id)
);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877614929/5695bbf3-f497-45b7-8873-edec9a17d239.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-4-option-table-for-mcq-questions">4. <strong>Option Table</strong> (For MCQ Questions)</h3>
<pre><code class="lang-typescript">CREATE TABLE OptionTable (
    option_id INT PRIMARY KEY AUTO_INCREMENT,
    question_id INT NOT NULL,
    option_text VARCHAR(<span class="hljs-number">255</span>) NOT NULL,
    is_correct BOOLEAN NOT NULL DEFAULT <span class="hljs-number">0</span>,
    FOREIGN KEY (question_id) REFERENCES Question(question_id)
);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877770287/50b179e5-6835-4a89-ba12-5ad1739f2cd6.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-5-answer-table">5. <strong>Answer Table</strong></h3>
<pre><code class="lang-typescript">CREATE TABLE Answer (
    answer_id INT PRIMARY KEY AUTO_INCREMENT,
    question_id INT NOT NULL,
    student_id INT NOT NULL,
    answer_text TEXT,
    submitted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (question_id) REFERENCES Question(question_id),
    FOREIGN KEY (student_id) REFERENCES User(user_id)
);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729877969882/ed6525ae-a874-4876-86a0-95e08ea203b7.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-6-result-table">6. <strong>Result Table</strong></h3>
<pre><code class="lang-typescript">CREATE TABLE Result (
    result_id INT PRIMARY KEY AUTO_INCREMENT,
    quiz_id INT NOT NULL,
    student_id INT NOT NULL,
    score DECIMAL(<span class="hljs-number">5</span>, <span class="hljs-number">2</span>) NOT NULL,
    submitted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (quiz_id) REFERENCES Quiz(quiz_id),
    FOREIGN KEY (student_id) REFERENCES User(user_id)
);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729878035219/c778f5fa-aa50-4089-acc8-740241b4e07a.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-adding-data-with-foreign-key-rule">Adding Data with Foreign Key Rule</h3>
<p>To add data with foreign key constraints, let's add some data into the <strong>User</strong> and <strong>Quiz</strong> tables, making sure the foreign key rules are respected.</p>
<pre><code class="lang-sql"><span class="hljs-comment">-- Insert into User table</span>
<span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-keyword">User</span> (<span class="hljs-keyword">name</span>, email, <span class="hljs-keyword">password</span>, <span class="hljs-keyword">role</span>) <span class="hljs-keyword">VALUES</span> 
(<span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'john.doe@example.com'</span>, <span class="hljs-string">'password123'</span>, <span class="hljs-string">'teacher'</span>),
(<span class="hljs-string">'Jane Smith'</span>, <span class="hljs-string">'jane.smith@example.com'</span>, <span class="hljs-string">'pass456'</span>, <span class="hljs-string">'student'</span>);

<span class="hljs-comment">-- Insert into Quiz table </span>
<span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Quiz (title, description, quiz_code, teacher_id, <span class="hljs-keyword">duration</span>) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-string">'Math Quiz'</span>, <span class="hljs-string">'Basic Algebra'</span>, <span class="hljs-string">'MATH101'</span>, <span class="hljs-number">10</span>, <span class="hljs-number">60</span>),
(<span class="hljs-string">'Science Quiz'</span>, <span class="hljs-string">'General Science'</span>, <span class="hljs-string">'SCI201'</span>, <span class="hljs-number">10</span>S, <span class="hljs-number">45</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729878324621/e1dd9c6c-603e-4f67-b08c-2b311585405f.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">In this example we received an error cause a <code>Quiz</code> table that references a <code>User</code> table via <code>teacher_id</code>, the value in <code>teacher_id</code> must correspond to an existing <code>user_id</code> in the <code>User</code> table. This helps maintain consistency and prevent invalid data from being entered.</div>
</div>

<hr />
<h3 id="heading-tip-we-must-first-define-the-tables-with-relationships">TIP: we must first define the <strong>tables</strong> with relationships.</h3>
<h3 id="heading-1-add-data-on-user-table">1. Add data on <strong>User Table</strong>:</h3>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-keyword">User</span> (<span class="hljs-keyword">name</span>, email, <span class="hljs-keyword">password</span>, <span class="hljs-keyword">role</span>) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'john.doe@example.com'</span>, <span class="hljs-string">'password123'</span>, <span class="hljs-string">'teacher'</span>),
(<span class="hljs-string">'Jane Smith'</span>, <span class="hljs-string">'jane.smith@example.com'</span>, <span class="hljs-string">'pass456'</span>, <span class="hljs-string">'student'</span>),
(<span class="hljs-string">'Emily Clark'</span>, <span class="hljs-string">'emily.clark@example.com'</span>, <span class="hljs-string">'teacherpass'</span>, <span class="hljs-string">'teacher'</span>),
(<span class="hljs-string">'Michael Brown'</span>, <span class="hljs-string">'michael.brown@example.com'</span>, <span class="hljs-string">'mike123'</span>, <span class="hljs-string">'student'</span>),
(<span class="hljs-string">'Sarah White'</span>, <span class="hljs-string">'sarah.white@example.com'</span>, <span class="hljs-string">'sarahpass'</span>, <span class="hljs-string">'student'</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729878744679/7dde13d8-750a-4bdb-b5d0-ba64b117b5e9.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-2-add-data-on-quiz-table">2. Add data on <strong>Quiz Table</strong>:</h3>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Quiz (title, description, quiz_code, teacher_id, <span class="hljs-keyword">duration</span>) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-string">'Math Quiz'</span>, <span class="hljs-string">'Basic Algebra'</span>, <span class="hljs-string">'MATH101'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">60</span>),
(<span class="hljs-string">'Science Quiz'</span>, <span class="hljs-string">'General Science'</span>, <span class="hljs-string">'SCI201'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">45</span>),
(<span class="hljs-string">'English Quiz'</span>, <span class="hljs-string">'Grammar Basics'</span>, <span class="hljs-string">'ENG301'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">30</span>),
(<span class="hljs-string">'History Quiz'</span>, <span class="hljs-string">'World War II'</span>, <span class="hljs-string">'HIST401'</span>, <span class="hljs-number">8</span>, <span class="hljs-number">50</span>),
(<span class="hljs-string">'Physics Quiz'</span>, <span class="hljs-string">'Mechanics'</span>, <span class="hljs-string">'PHY501'</span>, <span class="hljs-number">8</span>, <span class="hljs-number">40</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729878826887/5f389284-4eb5-4de3-8a59-b0c2c05afcc2.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-3-add-data-on-question-table">3. Add data on <strong>Question Table</strong>:</h3>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Question (quiz_id, question_text, question_type) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-number">8</span>, <span class="hljs-string">'What is 2 + 2?'</span>, <span class="hljs-string">'MCQ'</span>),
(<span class="hljs-number">9</span>, <span class="hljs-string">'What is the chemical symbol for water?'</span>, <span class="hljs-string">'MCQ'</span>),
(<span class="hljs-number">10</span>, <span class="hljs-string">'Identify the verb in the sentence.'</span>, <span class="hljs-string">'True/False'</span>),
(<span class="hljs-number">11</span>, <span class="hljs-string">'When did World War II start?'</span>, <span class="hljs-string">'Short Answer'</span>),
(<span class="hljs-number">12</span>, <span class="hljs-string">'What is Newton’s second law?'</span>, <span class="hljs-string">'MCQ'</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729878918309/b8eebe32-b157-4da2-b707-f20f728cbfca.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-4-add-data-on-option-table-for-mcqs">4. Add data on <strong>Option Table</strong> (For MCQs):</h3>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> OptionTable (question_id, option_text, is_correct) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-number">6</span>, <span class="hljs-string">'4'</span>, <span class="hljs-number">1</span>),
(<span class="hljs-number">6</span>, <span class="hljs-string">'5'</span>, <span class="hljs-number">0</span>),
(<span class="hljs-number">7</span>, <span class="hljs-string">'H2O'</span>, <span class="hljs-number">1</span>),
(<span class="hljs-number">7</span>, <span class="hljs-string">'O2'</span>, <span class="hljs-number">0</span>),
(<span class="hljs-number">8</span>, <span class="hljs-string">'F = ma'</span>, <span class="hljs-number">1</span>);
</code></pre>
<h3 id="heading-5-add-data-on-answer-table">5. Add data on <strong>Answer Table</strong>:</h3>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Answer (question_id, student_id, answer_text) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'4'</span>),
(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'H2O'</span>),
(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-string">'True'</span>),
(<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'1939'</span>),
(<span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-string">'F = ma'</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729878985554/c743bfe8-85cb-488d-b89a-2077c4d387e9.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-6add-data-on-result-table">6.Add data on <strong>Result Table</strong>:</h3>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-keyword">Result</span> (quiz_id, student_id, score) <span class="hljs-keyword">VALUES</span>
(<span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">90.5</span>),
(<span class="hljs-number">9</span>, <span class="hljs-number">2</span>, <span class="hljs-number">85.0</span>),
(<span class="hljs-number">10</span>, <span class="hljs-number">9</span>, <span class="hljs-number">75.0</span>),
(<span class="hljs-number">11</span>, <span class="hljs-number">10</span>, <span class="hljs-number">88.0</span>),
(<span class="hljs-number">12</span>, <span class="hljs-number">9</span>, <span class="hljs-number">95.0</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729879086301/1b92399a-a8f0-40e5-a95f-19866f6d7079.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Foreign keys maintain <strong>data integrity</strong> by ensuring relationships between records. For example in this article, quizzes cannot exist without a valid teacher, and quiz results must reference existing quizzes and students. This structure prevents invalid data from entering your database, helping keep the system <strong>reliable and consistent</strong>.</div>
</div>]]></content:encoded></item><item><title><![CDATA[Set Email and Username in the Specific Project in GIT]]></title><description><![CDATA[To check the global email and username set in Git, you can use the following commands:

Check global username:
 git config --global user.name

 

Check global email:
 git config --global user.email

 
Set a specific email and username
 To set a speci...]]></description><link>https://software-engineer.thirdygayares.com/set-email-and-username-in-the-specific-project-in-git</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/set-email-and-username-in-the-specific-project-in-git</guid><category><![CDATA[GitHub]]></category><category><![CDATA[Git]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Fri, 25 Oct 2024 15:48:51 GMT</pubDate><content:encoded><![CDATA[<p>To check the global email and username set in Git, you can use the following commands:</p>
<ol>
<li><p><strong>Check global username</strong>:</p>
<pre><code class="lang-typescript"> git config --<span class="hljs-built_in">global</span> user.name
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729871009895/8d6d3176-768b-4242-997f-7e8cd0172701.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Check global email</strong>:</p>
<pre><code class="lang-typescript"> git config --<span class="hljs-built_in">global</span> user.email
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729871042466/f5585a51-7b5e-4d16-9a16-867c0ed96467.png" alt class="image--center mx-auto" /></p>
<h1 id="heading-set-a-specific-email-and-username">Set a specific <strong>email</strong> and <strong>username</strong></h1>
<p> To set a specific <strong>email</strong> and <strong>username</strong> for the current project only in Git, you can configure Git at the repository level using the following commands. This allows you to use a different identity for a particular project without affecting your global Git configuration.</p>
<h3 id="heading-step-1-navigate-to-your-project-directory">Step 1: Navigate to Your Project Directory</h3>
<p> Open your terminal or command prompt and navigate to the directory of your project:</p>
<pre><code class="lang-typescript"> cd open_source\keriderya
</code></pre>
<h3 id="heading-step-2-set-local-git-username-and-email">Step 2: Set Local Git Username and Email</h3>
<p> To set the <strong>username</strong> and <strong>email</strong> for this specific project, use the following commands:</p>
<ol>
<li><p><strong>Set the email</strong> for the current project:</p>
<pre><code class="lang-typescript"> git config user.email <span class="hljs-string">"gayaresthird@gmail.com"</span>
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729871131094/5a763cad-dfb8-40fa-be7f-69cfe6b535e1.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Set the username</strong> for the current project:</p>
<pre><code class="lang-typescript"> git config user.name <span class="hljs-string">"Thirdy Gayares"</span>
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729871210614/bcb6e56c-a4b7-4e68-86c2-751ea6809841.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
</li>
</ol>
<h3 id="heading-step-3-verify-the-local-configuration">Step 3: Verify the Local Configuration</h3>
<p>    To verify that the username and email are set correctly for the current project, run:</p>
<pre><code class="lang-typescript">    git config --local --list
</code></pre>
<p>    You should see something like:</p>
<pre><code class="lang-typescript">    user.name=Thirdy Gayares
    user.email=gayaresthird<span class="hljs-meta">@gmail</span>.com
</code></pre>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729871250254/7b73dfaf-31d8-4a30-b0a9-1755aefefc69.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-explanation">Explanation:</h3>
<ul>
<li><p><code>git config --local</code> modifies the <code>.git/config</code> file within the current project. This ensures that the changes only affect the current project and do not alter the global settings.</p>
</li>
<li><p><strong>Global settings</strong> can be checked by running <code>git config --global --list</code>, which contains the default username and email for all repositories unless overridden by local settings.</p>
</li>
</ul>
<p>    This way, Git will use the specified email and username for commits made in this project.</p>
]]></content:encoded></item><item><title><![CDATA[How to enabled choco in windows 10]]></title><description><![CDATA[To enable and install Chocolatey (Choco) on Windows, follow these steps:
1. Open PowerShell as Administrator:

Press the Windows Key, type PowerShell, right-click on Windows PowerShell, and select Run as administrator.

2. Run the installation comman...]]></description><link>https://software-engineer.thirdygayares.com/how-to-enabled-choco-in-windows-10</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/how-to-enabled-choco-in-windows-10</guid><category><![CDATA[windows-choco]]></category><category><![CDATA[Windows]]></category><category><![CDATA[choco]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Fri, 25 Oct 2024 15:29:26 GMT</pubDate><content:encoded><![CDATA[<p>To enable and install <strong>Chocolatey (Choco)</strong> on Windows, follow these steps:</p>
<h3 id="heading-1-open-powershell-as-administrator">1. <strong>Open PowerShell as Administrator</strong>:</h3>
<ul>
<li>Press the <strong>Windows Key</strong>, type <strong>PowerShell</strong>, right-click on <strong>Windows PowerShell</strong>, and select <strong>Run as administrator</strong>.</li>
</ul>
<h3 id="heading-2-run-the-installation-command">2. <strong>Run the installation command</strong>:</h3>
<p>Copy and paste the following command into the PowerShell window, then press <strong>Enter</strong>:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">Set</span>-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor <span class="hljs-number">3072</span>; iex ((New-<span class="hljs-built_in">Object</span> System.Net.WebClient).DownloadString(<span class="hljs-string">'https://community.chocolatey.org/install.ps1'</span>))
</code></pre>
<h3 id="heading-3-wait-for-the-installation">3. <strong>Wait for the installation</strong>:</h3>
<p>The script will download and install Chocolatey on your system. It might take a few moments.</p>
<h3 id="heading-4-verify-the-installation">4. <strong>Verify the installation</strong>:</h3>
<p>Once installed, verify that Chocolatey is working by typing the following command in PowerShell:</p>
<pre><code class="lang-typescript">choco -v
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729870124469/19b88465-1773-4e0e-83bd-18410378a681.png" alt class="image--center mx-auto" /></p>
<p>This should display the version of Chocolatey if the installation was successful.</p>
<p>Now, Chocolatey is enabled, and you can use it to install packages on your Windows system.</p>
]]></content:encoded></item><item><title><![CDATA[Env and .gitgnore]]></title><description><![CDATA[In software development, both the .env and .gitignore files play important in securing sensitive data and organizing your project.
.env File
A .env file stores environment variables such as API keys, database credentials such as HOST , password and u...]]></description><link>https://software-engineer.thirdygayares.com/env-and-gitgnore</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/env-and-gitgnore</guid><category><![CDATA[gitignore]]></category><category><![CDATA[Environment variables]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Sat, 19 Oct 2024 14:23:33 GMT</pubDate><content:encoded><![CDATA[<p>In software development, both the <code>.env</code> and <code>.gitignore</code> files play important in securing sensitive data and organizing your project.</p>
<h3 id="heading-env-file"><code>.env</code> File</h3>
<p>A <code>.env</code> file stores <strong>environment variables</strong> such as API keys, database credentials such as HOST , password and username, and other configuration values that are essential for running your application. Instead of hardcoding these sensitive values directly in your codebase, you store them in the <code>.env</code> file and reference them using environment variables in your code.</p>
<p>For example:</p>
<pre><code class="lang-typescript">DATABASE_URL=mysql:<span class="hljs-comment">//user:password@localhost/db_name</span>
SECRET_KEY=my_secret_key
</code></pre>
<p>Using environment variables ensures that sensitive data isn't exposed directly in the source code. This makes your application more secure and easier to configure across different environments (development, testing, production) by simply updating the <code>.env</code> file without changing the source code itself.</p>
<h3 id="heading-gitignore-file"><code>.gitignore</code> File</h3>
<p>The <code>.gitignore</code> file tells Git which files or directories should <strong>not</strong> be tracked in the version control system. Since <code>.env</code> files contain sensitive data, it’s important to include <code>.env</code> them in <code>.gitignore</code> to prevent it from being committed and pushed to public repositories like GitHub or Gitlab, bitbucket, where it could be exposed to others.</p>
<p>A typical <code>.gitignore</code> file that excludes the <code>.env</code> file looks like this:</p>
<pre><code class="lang-typescript"># Ignore .env file
.env
</code></pre>
<p>If you’ve accidentally committed your <code>.env</code> file to your Git repository, you should remove it from tracking using:</p>
<pre><code class="lang-typescript">git rm --cached .env
</code></pre>
<p>And then push the changes again to make sure the file is removed from the version control history​</p>
<h3 id="heading-why-use-them-together">Why Use Them Together?</h3>
<ol>
<li><p><strong>Security</strong>: By storing sensitive information in <code>.env</code> and ensuring it is ignored via <code>.gitignore</code>, you reduce the risk of exposing your secrets, like API keys and passwords, to public repositories.</p>
</li>
<li><p><strong>Flexibility</strong>: Each developer or environment (development, staging, production) can have its own <code>.env</code> file with different configurations, making it easier to manage different setups without modifying the code.</p>
</li>
<li><p><strong>Best Practices</strong>: It’s a widely accepted best practice to include an <code>.env.example</code> file in the repository that acts as a template for others to create their own <code>.env</code> file. This way, the structure of environment variables is shared, but the sensitive values themselves are kept private.</p>
</li>
</ol>
<h3 id="heading-alternatives-and-best-practices">Alternatives and Best Practices</h3>
<p>While <code>.env</code> files are widely used, as your project scales, it might be beneficial to look into <strong>secret management tools</strong> like <strong>AWS Secrets Manager</strong> or <strong>Hashicorp Vault</strong> for storing and managing sensitive data securely. These tools add an additional layer of protection by encrypting your secrets and controlling access through permissions</p>
<p>In conclusion, using <code>.env</code> and <code>.gitignore</code> files correctly helps to maintain security and proper project structure, protecting sensitive information from being exposed.</p>
<p>REF.</p>
<p><a target="_blank" href="https://dev.to/dangtony98/doing-much-better-than-your-env-file-46bg">DEV Community</a><a target="_blank" href="https://dev.to/khalidk799/environment-variables-its-best-practices-1o1o">DEV Community</a></p>
]]></content:encoded></item><item><title><![CDATA[What is Cookie?]]></title><description><![CDATA[A cookie is a small piece of data that a web server sends to a user's web browser. The browser may store it on the user’s computer and then send it back to the server with subsequent requests. Cookies are primarily used to remember information about ...]]></description><link>https://software-engineer.thirdygayares.com/what-is-cookie</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/what-is-cookie</guid><category><![CDATA[cookies]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Sat, 19 Oct 2024 14:21:50 GMT</pubDate><content:encoded><![CDATA[<p>A <strong>cookie</strong> is a small piece of data that a web server sends to a user's web browser. The browser may store it on the user’s computer and then send it back to the server with subsequent requests. Cookies are primarily used to <mark>remember informatio</mark>n about the user, making interactions with websites smoother and more personalized. They are essential for maintaining session states, storing user preferences, and enabling website functionalities such as user authentication.</p>
<h3 id="heading-expanded-explanation">Expanded Explanation:</h3>
<h4 id="heading-1-purpose-and-functionality">1. <strong>Purpose and Functionality</strong></h4>
<p>Cookies are commonly used to make the user experience better by remembering things like:</p>
<ul>
<li><p><strong>Login information</strong>: Cookies can store session identifiers so users remain logged in to a website even after closing the browser.</p>
</li>
<li><p><strong>Shopping carts</strong>: In e-commerce, cookies allow websites to remember items a user has added to their cart, even if they leave the site and return later.</p>
</li>
<li><p><strong>User preferences</strong>: Cookies help websites remember user preferences, like language settings, theme choices, or regional options, so the user doesn't need to reset them each time they visit.</p>
</li>
</ul>
<h4 id="heading-2-types-of-cookies">2. <strong>Types of Cookies</strong></h4>
<p>There are different types of cookies based on their function and how long they persist:</p>
<ul>
<li><p><strong>Session Cookies</strong>: These cookies are temporary and are deleted once the user closes their browser. They are used to maintain state during a browsing session (e.g., remembering items in a cart).</p>
</li>
<li><p><strong>Persistent Cookies</strong>: These cookies remain on a user's device until they expire or are manually deleted. Websites use them to remember login credentials or preferences across visits.</p>
</li>
<li><p><strong>First-Party Cookies</strong>: Set by the website the user is visiting, these cookies are used to manage essential website functionality, such as remembering login status.</p>
</li>
<li><p><strong>Third-Party Cookies</strong>: Set by external domains (often ad networks), these cookies track users across multiple sites for advertising and analytics purposes. They are often used to serve targeted ads.</p>
</li>
</ul>
<h4 id="heading-3-how-cookies-work">3. <strong>How Cookies Work</strong></h4>
<p>When you visit a website, the server sends a cookie to your browser, which might contain:</p>
<ul>
<li><p>A unique user identifier</p>
</li>
<li><p>Session details</p>
</li>
<li><p>Expiration dates</p>
</li>
<li><p>Flags, such as "secure" (ensuring the cookie is sent only over HTTPS)</p>
</li>
</ul>
<p>Once the cookie is set, every time the browser makes a request to the same server (e.g., clicking on a new link or submitting a form), it sends the stored cookie along with the request. This allows the server to recognize the user and customize the experience accordingly.</p>
<h4 id="heading-4-security-concerns">4. <strong>Security Concerns</strong></h4>
<p>Cookies can present some privacy and security concerns:</p>
<ul>
<li><p><strong>Tracking and Privacy</strong>: Third-party cookies, in particular, are controversial because they enable companies to track users across websites</p>
</li>
<li><p><strong>Cross-Site Scripting (XSS)</strong>: If cookies are not properly secured, they can be vulnerable to attacks where hackers inject malicious scripts to steal user data.</p>
</li>
<li><p><strong>Cross-Site Request Forgery (CSRF)</strong>: Cookies are used in CSRF attacks, where unauthorized commands are transmitted from a user that the website trusts.</p>
</li>
</ul>
<p>To mitigate risks, developers use techniques like <strong>HTTP-only cookies</strong> (which can't be accessed via JavaScript) and <strong>secure cookies</strong> (which are sent only over HTTPS connections).</p>
<h4 id="heading-5-regulations-and-laws">5. <strong>Regulations and Laws</strong></h4>
<p>Due to privacy concerns, many regions have laws governing the use of cookies. For example:</p>
<ul>
<li><p><strong>GDPR (General Data Protection Regulation)</strong>: In Europe, websites must get explicit user consent before storing cookies, particularly if the cookies track user behavior.</p>
</li>
<li><p><strong>CCPA (California Consumer Privacy Act)</strong>: In California, websites must inform users about the data being collected and provide them with the option to opt out.</p>
</li>
</ul>
<p>Cookies are foundational to modern web browsing but must be handled carefully to balance functionality, security, and privacy concerns.</p>
<p>Sources:</p>
<ul>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies">Mozilla MDN Web Docs</a></p>
</li>
<li><p><a target="_blank" href="https://blog.gitguardian.com">GitGuardian Blog on Secure Cookie Handling</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How to Export APK in Flutter]]></title><description><![CDATA[To export an APK (Android Package) file in Flutter, follow this step-by-step guide. This guide will allow you to generate a release version of your Flutter app and prepare it for distribution or installation on Android devices.
Step 1: Set Up Your En...]]></description><link>https://software-engineer.thirdygayares.com/how-to-export-apk-in-flutter</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/how-to-export-apk-in-flutter</guid><category><![CDATA[flutter to apk]]></category><category><![CDATA[Flutter]]></category><category><![CDATA[apk]]></category><category><![CDATA[apk build]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Wed, 16 Oct 2024 22:17:03 GMT</pubDate><content:encoded><![CDATA[<p>To export an APK (Android Package) file in Flutter, follow this step-by-step guide. This guide will allow you to generate a release version of your Flutter app and prepare it for distribution or installation on Android devices.</p>
<h3 id="heading-step-1-set-up-your-environment">Step 1: <strong>Set Up Your Environment</strong></h3>
<p>Make sure you have Flutter and Android Studio installed on your machine. You’ll also need an emulator or a connected Android device to test the APK.</p>
<ul>
<li><p>Make sure Android SDK and Android Studio are properly installed.</p>
</li>
<li><p>Ensure that your Flutter project is working correctly.</p>
</li>
</ul>
<p>You can verify your environment by running:</p>
<pre><code class="lang-typescript">flutter doctor
</code></pre>
<p>Fix any issues reported by <code>flutter doctor</code> before proceeding.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729115056161/0dfd04bd-af3f-4302-b172-036722712290.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Make sure your flutter is running in android emulator</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729115205519/4d7493c2-0741-415b-96ab-edac80452ae2.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-step-2-configure-your-app-for-release">Step 2: <strong>Configure Your App for Release</strong></h3>
<p>Before exporting the APK, you need to configure your Flutter app for release mode.</p>
<ol>
<li><p><strong>Update</strong> <code>android/app/build.gradle</code>:</p>
<ul>
<li><p>Open <code>android/app/build.gradle</code>.</p>
</li>
<li><p>Locate the <code>buildTypes</code> block and ensure the <code>release</code> block is properly set up. Here's a default release configuration:</p>
</li>
</ul>
</li>
</ol>
<pre><code class="lang-typescript">    android {
        compileSdkVersion <span class="hljs-number">34</span>

        defaultConfig {
            applicationId <span class="hljs-string">"com.example.myapp"</span> <span class="hljs-comment">//make sure the name is from your company and prohect name</span>
            minSdkVersion <span class="hljs-number">21</span>
            targetSdkVersion <span class="hljs-number">33</span>
            versionCode <span class="hljs-number">1</span>
            versionName <span class="hljs-string">"1.0"</span>
        }

        buildTypes {
            release {
                <span class="hljs-comment">// Shrinks, optimizes, and obfuscates your code by default</span>
                minifyEnabled <span class="hljs-literal">true</span>
                <span class="hljs-comment">// Enables ProGuard for code obfuscation (optional)</span>
                proguardFiles getDefaultProguardFile(<span class="hljs-string">'proguard-android-optimize.txt'</span>), <span class="hljs-string">'proguard-rules.pro'</span>
            }
        }
    }
</code></pre>
<h3 id="heading-step-4-build-the-apk">Step 4: <strong>Build the APK</strong></h3>
<p>Now that the project is set up for release, you can generate the APK.</p>
<ol>
<li><p><strong>Run the Flutter Build Command</strong>: Run the following command in the terminal from your project’s root directory:</p>
<pre><code class="lang-typescript"> flutter build apk --release
</code></pre>
<p> This command builds the release APK and stores it in the <code>build/app/outputs/flutter-apk</code> directory.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729116053174/16484eaf-f2e2-4ab0-8649-280807205548.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<ol start="2">
<li><p><strong>Check the APK</strong>: The APK will be located at:</p>
<pre><code class="lang-typescript"> build/app/outputs/flutter-apk/app-release.apk
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729116823674/e37422da-ade2-4e5b-abb2-91d7588a8c92.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<h3 id="heading-step-5-test-the-apk-on-an-android-device">Step 5: <strong>Test the APK on an Android Device</strong></h3>
<ol>
<li><p>Transfer the APK to your Android device via USB, email, or cloud storage.</p>
 <div data-node-type="callout">
 <div data-node-type="callout-emoji">💡</div>
 <div data-node-type="callout-text">In my case I use Telegram</div>
 </div>
</li>
<li><p>Open the APK file on your device and install it.  </p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729116610345/90f11d37-0dba-456f-82a7-fd9d024cc1bf.png" alt class="image--center mx-auto" /></p>
 <div data-node-type="callout">
 <div data-node-type="callout-emoji">💡</div>
 <div data-node-type="callout-text">That’s All! I think this is simply exporting Flutter app to APK, If your Flutter app has many features I think you will encounter some issues but you can simply search for the error or fix soulotion by prompting CHAT GPT xD</div>
 </div></li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Step by Step how to deploy flutter on Firebase Hosting]]></title><description><![CDATA[💡
Please Follow me on LinkedIn and GitHub


 
1. Install Firebase CLI
First, you need to install the Firebase Command Line Interface (CLI) to interact with Firebase from the terminal.

If you have Node.js (Make Sure if none you can downloat it from ...]]></description><link>https://software-engineer.thirdygayares.com/step-by-step-how-to-deploy-flutter-on-firebase-hosting</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/step-by-step-how-to-deploy-flutter-on-firebase-hosting</guid><category><![CDATA[flutter-deploy-firebase]]></category><category><![CDATA[Flutter]]></category><category><![CDATA[Firebase hosting]]></category><category><![CDATA[flutter-firebase]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Sat, 12 Oct 2024 16:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729071065171/a531cbcb-2106-43b5-b7bf-c638f42dc72b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Please Follow me on LinkedIn and GitHub</div>
</div>

<div class="hn-embed-widget" id="linkedin"></div><p> </p>
<h3 id="heading-1-install-firebase-cli">1. <strong>Install Firebase CLI</strong></h3>
<p>First, you need to install the Firebase Command Line Interface (CLI) to interact with Firebase from the terminal.</p>
<ul>
<li><p>If you have Node.js (Make Sure if none you can downloat it from their official website) installed, run this command in your terminal:</p>
<pre><code class="lang-typescript">  npm install -g firebase-tools
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068291196/eb4252a3-3e7f-4328-ba56-a90eda6fdd48.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>After installation, verify that Firebase CLI is installed by running:</p>
<pre><code class="lang-typescript">  firebase --version
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068305740/dd004f9b-2bd4-4ac5-9ecd-8bd162e06aa7.png" alt class="image--center mx-auto" /></p>
<p>  if no problem encountered, We are ready to go on to the next step</p>
</li>
</ul>
<h3 id="heading-2-create-a-firebase-project">2. <strong>Create a Firebase Project</strong></h3>
<ul>
<li>If you don't already have a Firebase project, create one in the Firebase Console. You’ll need this project to host your Flutter app.</li>
</ul>
<p>Go to this link and login</p>
<p><a target="_blank" href="https://firebase.google.com/">https://firebase.google.com/</a></p>
<p>Go to Console</p>
<p>Click Create Project</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068394736/fa8af3db-ac46-4ad2-ad52-269f049aae89.png" alt class="image--center mx-auto" /></p>
<p>I named it flutter test deploy</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068426350/a3a4b361-66db-4dd5-8837-ecbcb3ee2b40.png" alt class="image--center mx-auto" /></p>
<p><strong><br />Go to Hosting Tab</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068841588/e72d9f11-aa46-468b-91a8-8d13ad997cf9.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068927618/f9770b55-23d0-49c6-ab6f-a324d706e5b8.png" alt class="image--center mx-auto" /></p>
<p>Just Click continue to console</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068949667/fc0ce428-5bcf-412e-8b11-a1cb876f1a8f.png" alt class="image--center mx-auto" /></p>
<p>and here we have site, If you click the link:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068986591/63e63d2d-69bf-4f4c-9e50-37cb77b981ff.png" alt class="image--center mx-auto" /></p>
<p>Site not found cause we did not yet deploy our flutter app</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Just Follow the instruction and we will proceed to the next step</div>
</div>

<h3 id="heading-3-log-in-to-firebase">3. <strong>Log in to Firebase</strong></h3>
<ul>
<li><p>Log in to your Firebase account by running in your cmd:</p>
<pre><code class="lang-typescript">  firebase login
</code></pre>
</li>
<li><p>Follow the instructions to authenticate your Google account.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068540108/9c8c1907-2773-41be-af7d-1cbdc93fc1e0.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Type Y . Then it will automatically direct to the browser, if not just copy the link and paste it to your browser. Choose your account where your project created in #2</div>
</div>

<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068633892/0724b66d-a3f1-4b01-83d3-7d10bf04e50d.png" alt class="image--center mx-auto" /></p>
<p>If successfully</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068644836/4395aa51-bdff-4e91-90fd-521905c9acc0.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-4firebase-cli-provides-web-framework-support-to-enable-it-call-the-following">4.Firebase CLI provides web framework support. To enable it, call the following:</h3>
<pre><code class="lang-typescript">firebase experiments:enable webframeworks
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068743900/aeec1e34-990b-46c7-b5d5-c16238aacc1b.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-4-set-up-your-flutter-web-app">4. <strong>Set Up Your Flutter Web App</strong></h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069151501/edc4f62a-7502-4ad0-9a99-32571e2f778c.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Ensure your Flutter app is ready for web deployment by switching to the <code>web</code> platform:</p>
<pre><code class="lang-typescript">  flutter config --enable-web
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729068810793/fe397974-70f3-49b6-9cc3-859052bad996.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Make sure the project directory is in your flutter project</div>
</div>



<ul>
<li><p>Build your Flutter web app:</p>
<pre><code class="lang-typescript">  flutter build web
</code></pre>
<p>  This will generate a <code>build/web/</code> directory that contains the compiled files for your web app.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069109947/4657487d-bfa5-4b33-aeff-65069fbd65e2.png" alt class="image--center mx-auto" /></p>
<p>  again this is from bash but it the directory of my flutter project</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069329988/8282a8c2-e7bc-4010-8887-13f3456ce98c.png" alt class="image--center mx-auto" /></p>
  <div data-node-type="callout">
  <div data-node-type="callout-emoji">💡</div>
  <div data-node-type="callout-text">as you can it generates a build folder on your Flutter app. Every time we change or modify our app, if we deploy for the next version we need to <code>flutter build</code> . to rebuild your changes and ready for deployment</div>
  </div>


</li>
</ul>
<h3 id="heading-5-initialize-firebase-hosting">5. <strong>Initialize Firebase Hosting</strong></h3>
<ul>
<li><p>Navigate to your Flutter project directory in the terminal, and then initialize Firebase Hosting:</p>
<pre><code class="lang-typescript">  firebase init hosting
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069292762/bd30f10d-9422-4dd9-886a-618ffe8777c0.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>When prompted:</p>
<ul>
<li><p>Select your Firebase project.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069304613/596a87dd-c2a0-4221-8131-40fa71bd6973.png" alt class="image--center mx-auto" /></p>
<p>  Clieck Enter</p>
</li>
</ul>
</li>
</ul>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069568841/43891cc4-685f-47ef-af3a-0e157f9b6f59.png" alt class="image--center mx-auto" /></p>
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">I choose<code> flutter-test-deploy</code> project.</div>
    </div>

<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069619034/b2e91ab9-2e6e-4cd0-bcc3-4cf72f34238b.png" alt class="image--center mx-auto" /></p>
<p>    type y and enter</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069678096/ccd7beca-06a6-4391-9a38-40446d2a7c75.png" alt class="image--center mx-auto" /></p>
<p>Choose Asia if you are in Asia. Ask me why ?</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069692078/04ea2d47-2aec-4467-8504-9d9b9bfdcf1a.png" alt class="image--center mx-auto" /></p>
<p>type n , as of now</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069731653/197bf5c4-4d36-49e2-81c1-0c81e55a9aa3.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069760628/79d1fbff-261c-4bbd-ac2c-2026244a8365.png" alt class="image--center mx-auto" /></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">as you can see firebase.json and .firebaserc was generated</div>
</div>

<h3 id="heading-6-deploy-to-firebase-hosting">6. <strong>Deploy to Firebase Hosting</strong></h3>
<ul>
<li><p>Deploy your Flutter web app by running:</p>
<pre><code class="lang-typescript">  firebase deploy
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069862977/b193123f-8f05-49b0-93c3-5583e4dc2b85.png" alt class="image--center mx-auto" /></p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069871371/cc89208c-47bb-459f-9f82-f89fab3273c5.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<ul>
<li>After deployment, Firebase will give you a URL where your app is hosted.</li>
</ul>
<h3 id="heading-7-access-your-app">7. <strong>Access Your App</strong></h3>
<ul>
<li>Once the deployment is done, Firebase CLI will show the URL where your app is live. You can now access your Flutter app on Firebase Hosting.</li>
</ul>
<p>HERE IS OUR OUTPUT:  </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729069991556/71f54691-5d25-417c-b657-9f65ed0b7f85.png" alt class="image--center mx-auto" /></p>
<p><a target="_blank" href="https://flutter-test-deploy.web.app/">Flutter Demo (flutter-test-deploy.web.app)</a></p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">if you have question , you can message me via LinkedIn,</div>
</div>]]></content:encoded></item><item><title><![CDATA[Basic CRUD Python Flask API]]></title><description><![CDATA[Prerequisites

https://software-engineer.thirdygayares.com/virtual-environment-and-python-package-manager

https://software-engineer.thirdygayares.com/how-to-run-python-flask

https://software-engineer.thirdygayares.com/data-structure-again


To star...]]></description><link>https://software-engineer.thirdygayares.com/basic-crud-python-flask-api</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/basic-crud-python-flask-api</guid><category><![CDATA[basic crud]]></category><category><![CDATA[flask api]]></category><category><![CDATA[crud api]]></category><category><![CDATA[Flask Framework]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Sat, 12 Oct 2024 13:21:34 GMT</pubDate><content:encoded><![CDATA[<p>Prerequisites</p>
<ol>
<li><p><a target="_blank" href="https://software-engineer.thirdygayares.com/virtual-environment-and-python-package-manager">https://software-engineer.thirdygayares.com/virtual-environment-and-python-package-manager</a></p>
</li>
<li><p><a target="_blank" href="https://software-engineer.thirdygayares.com/how-to-run-python-flask">https://software-engineer.thirdygayares.com/how-to-run-python-flask</a></p>
</li>
<li><p><a target="_blank" href="https://software-engineer.thirdygayares.com/data-structure-again">https://software-engineer.thirdygayares.com/data-structure-again</a></p>
</li>
</ol>
<h1 id="heading-to-start">To start,</h1>
<ul>
<li><p>Create a Python Virtual Environment</p>
</li>
<li><p>Activate Virtual Environment</p>
</li>
<li><p>make sure Flask is installed:</p>
</li>
</ul>
<pre><code class="lang-java">pip install Flask
</code></pre>
<p><strong>BLUEPRINT</strong></p>
<h1 id="heading-our-blueprint"><strong>OUR BLUEPRINT</strong></h1>
<pre><code class="lang-python"><span class="hljs-comment"># <span class="hljs-doctag">TODO:</span> import flask LIBRARY</span>
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, jsonify, request

<span class="hljs-comment"># call the flask object</span>
app = Flask(__name__)

<span class="hljs-comment"># create list of dictionaries</span>
students = [{}]

<span class="hljs-comment"># get method</span>
<span class="hljs-meta">@app.route('/students', methods=['GET'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_students</span>():</span>

<span class="hljs-comment"># get method : Get a specific student by ID</span>
<span class="hljs-meta">@app.route('/students/&lt;int:student_id&gt;', methods=['GET'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_student</span>(<span class="hljs-params">student_id</span>):</span>

<span class="hljs-comment"># put method</span>
<span class="hljs-meta">@app.route('/students/&lt;int:student_id&gt;', methods=['PUT'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_student</span>(<span class="hljs-params">student_id</span>):</span>

<span class="hljs-comment"># delete method</span>
<span class="hljs-meta">@app.route('/students/&lt;int:student_id&gt;', methods=['DELETE'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_student</span>(<span class="hljs-params">student_id</span>):</span>

<span class="hljs-comment"># Dont forget this</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
</code></pre>
<h3 id="heading-when-testing-the-api">When Testing the API</h3>
<ul>
<li><p><strong>Run the server</strong> by executing the Python script:</p>
<pre><code class="lang-java">  python app.py
</code></pre>
</li>
</ul>
<h3 id="heading-flask-api-code">Flask API Code</h3>
<h1 id="heading-create-apppy">create app.py</h1>
<h2 id="heading-import-flask"><strong>import flask</strong></h2>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, jsonify, request

app = Flask(__name__)
</code></pre>
<h3 id="heading-create-a-list-of-dictionaries"><strong>Create a List of Dictionaries</strong></h3>
<pre><code class="lang-python"><span class="hljs-comment"># Sample data: list of students</span>
students = [
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Juan Carlos"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-4B"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">1</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"juan@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">22</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Jose Rizal"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-2A"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">2</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"jose@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">21</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Juan Luna"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-3A"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">3</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"juan@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">20</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Andres Bonifacio"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-3A"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">4</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"andres@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">20</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Justin Bieber"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-2A"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">5</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"justin@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">21</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Michael Jordan"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-4A"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">6</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"michael@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">19</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Andrew Jordan"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-3A"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">7</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"andrew@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">20</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Jessa Boe"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-2B"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">8</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"jessa@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">18</span>
    },
    {
        <span class="hljs-string">"name"</span>: <span class="hljs-string">"Ted Talk"</span>,
        <span class="hljs-string">"section"</span>: <span class="hljs-string">"BSIT-3B"</span>,
        <span class="hljs-string">"id"</span>: <span class="hljs-number">9</span>,
        <span class="hljs-string">"email"</span>: <span class="hljs-string">"ted@gmail.com"</span>,
        <span class="hljs-string">"age"</span>: <span class="hljs-number">19</span>
    }
]
</code></pre>
<h3 id="heading-get-all-student"><strong>Get All Student</strong></h3>
<pre><code class="lang-python"><span class="hljs-meta">@app.route('/students', methods=['GET'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_students</span>():</span>
    <span class="hljs-keyword">return</span> jsonify(students)
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728738532945/69d714e3-c4af-4385-9255-7e696cb6b85e.png" alt class="image--center mx-auto" /></p>
<p><strong>Get All Students (</strong><code>GET /students</code>): Returns a list of all students in JSON format.</p>
<hr />
<h3 id="heading-get-student-by-id">GET STUDENT BY ID</h3>
<pre><code class="lang-python"><span class="hljs-comment"># READ: Get a specific student by ID</span>
<span class="hljs-meta">@app.route('/students/&lt;int:student_id&gt;', methods=['GET'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_student</span>(<span class="hljs-params">student_id</span>):</span>
    student = next((student <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students <span class="hljs-keyword">if</span> student[<span class="hljs-string">"id"</span>] == student_id), <span class="hljs-literal">None</span>)
    <span class="hljs-keyword">if</span> student:
        <span class="hljs-keyword">return</span> jsonify(student)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> jsonify({<span class="hljs-string">"message"</span>: <span class="hljs-string">"Student not found"</span>}), <span class="hljs-number">404</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728738772655/e2c69ee1-afd5-413c-b972-8b15cbd09c66.png" alt class="image--center mx-auto" /></p>
<p><strong>Get a Specific Student (</strong><code>GET /students/&lt;id&gt;</code>): Returns details of a student by their <code>id</code>.</p>
<hr />
<h3 id="heading-add-student">ADD STUDENT</h3>
<pre><code class="lang-python"><span class="hljs-meta">@app.route('/students', methods=['POST'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_student</span>():</span>
    new_student = request.get_json()
    new_student[<span class="hljs-string">'id'</span>] = max(student[<span class="hljs-string">'id'</span>] <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students) + <span class="hljs-number">1</span>  <span class="hljs-comment"># Generate new ID</span>
    students.append(new_student)
    <span class="hljs-keyword">return</span> jsonify(new_student), <span class="hljs-number">201</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728738813590/17048f2e-b1b0-4c49-a15c-ee80e3bf63c3.png" alt class="image--center mx-auto" /></p>
<p><strong>Add a New Student (</strong><code>POST /students</code>): Accepts student details in JSON format and adds a new student with a unique <code>id</code>.</p>
<hr />
<h3 id="heading-update-student">UPDATE STUDENT</h3>
<pre><code class="lang-python"><span class="hljs-meta">@app.route('/students/&lt;int:student_id&gt;', methods=['PUT'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_student</span>(<span class="hljs-params">student_id</span>):</span>
    student = next((student <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students <span class="hljs-keyword">if</span> student[<span class="hljs-string">"id"</span>] == student_id), <span class="hljs-literal">None</span>)
    <span class="hljs-keyword">if</span> student:
        data = request.get_json()
        student.update(data)
        <span class="hljs-keyword">return</span> jsonify(student)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> jsonify({<span class="hljs-string">"message"</span>: <span class="hljs-string">"Student not found"</span>}), <span class="hljs-number">404</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728738952551/f769be67-bc36-4d2d-95b8-2078f1499699.png" alt class="image--center mx-auto" /></p>
<p><strong>Update an Existing Student (</strong><code>PUT /students/&lt;id&gt;</code>): Updates the details of a student by their <code>id</code> using JSON data from the request.</p>
<hr />
<pre><code class="lang-python"><span class="hljs-comment"># DELETE: Remove a student by ID</span>
<span class="hljs-meta">@app.route('/students/&lt;int:student_id&gt;', methods=['DELETE'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_student</span>(<span class="hljs-params">student_id</span>):</span>
    student = next((student <span class="hljs-keyword">for</span> student <span class="hljs-keyword">in</span> students <span class="hljs-keyword">if</span> student[<span class="hljs-string">"id"</span>] == student_id), <span class="hljs-literal">None</span>)
    <span class="hljs-keyword">if</span> student:
        students.remove(student)
        <span class="hljs-keyword">return</span> jsonify({<span class="hljs-string">"message"</span>: <span class="hljs-string">"Student deleted"</span>})
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> jsonify({<span class="hljs-string">"message"</span>: <span class="hljs-string">"Student not found"</span>}), <span class="hljs-number">404</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728739118134/66461269-da6e-4950-a812-2b69f6f1ddf1.png" alt class="image--center mx-auto" /></p>
<p><strong>REMEMBER ADD THIS CODE</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
</code></pre>
<p><strong>Delete a Student (</strong><code>DELETE /students/&lt;id&gt;</code>): Deletes a student by their <code>id</code>.</p>
<hr />
<ul>
<li><strong>Using Postman</strong>, you can test each endpoint by sending the appropriate requests.</li>
</ul>
<p>This API provides a simple and clean structure to manage students' data with CRUD operations.</p>
]]></content:encoded></item><item><title><![CDATA[Mysql Database User Role]]></title><description><![CDATA[o create a database and a role in MySQL where the role can view and edit the database but cannot delete any data, follow these steps:
1. Create a New Database
First, log in to MySQL with an admin user (like root) and create the new database.
CREATE D...]]></description><link>https://software-engineer.thirdygayares.com/mysql-database-user-role</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/mysql-database-user-role</guid><category><![CDATA[Mysql database user role]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Sat, 12 Oct 2024 08:56:08 GMT</pubDate><content:encoded><![CDATA[<p>o create a <strong>database</strong> and a <strong>role</strong> in MySQL where the role can view and edit the database but <strong>cannot delete</strong> any data, follow these steps:</p>
<h3 id="heading-1-create-a-new-database">1. <strong>Create a New Database</strong></h3>
<p>First, log in to MySQL with an admin user (like <code>root</code>) and create the new database.</p>
<pre><code class="lang-python">CREATE DATABASE your_database_name;
</code></pre>
<h3 id="heading-2-create-a-new-user">2. <strong>Create a New User</strong></h3>
<p>Next, create a new user (role). This user will have specific permissions later.</p>
<pre><code class="lang-python">CREATE USER <span class="hljs-string">'thirdy'</span>@<span class="hljs-string">'localhost'</span> IDENTIFIED BY <span class="hljs-string">'Test@123456'</span>;
</code></pre>
<ul>
<li><p>Replace <code>'your_username'</code> with the desired username.</p>
</li>
<li><p>Replace <code>'your_password'</code> with a strong password.</p>
</li>
</ul>
<h3 id="heading-3-grant-permissions-to-the-user">3. <strong>Grant Permissions to the User</strong></h3>
<p>Now, give the user permission to <strong>view and edit</strong> the database, but restrict <strong>delete</strong> permissions.</p>
<pre><code class="lang-python">GRANT SELECT, CREATE, INSERT, UPDATE, REFERENCES ON your_database_name.* TO <span class="hljs-string">'thirdy'</span>@<span class="hljs-string">'localhost'</span>;
</code></pre>
<p>This grants:</p>
<ul>
<li><p><strong>SELECT</strong>: The ability to view data.</p>
</li>
<li><p><strong>INSERT</strong>: The ability to add data.</p>
</li>
<li><p><strong>UPDATE</strong>: The ability to modify existing data.</p>
</li>
</ul>
<p>The user <strong>cannot delete</strong> any data because the <code>DELETE</code> privilege is not given.</p>
<h3 id="heading-4-apply-changes">4. <strong>Apply Changes</strong></h3>
<p>After granting the permissions, run the following command to apply the changes:</p>
<pre><code class="lang-python">FLUSH PRIVILEGES;
</code></pre>
<h3 id="heading-5-show-grants">5. <strong>Show Grants</strong></h3>
<pre><code class="lang-python">SHOW GRANTS FOR <span class="hljs-string">'thirdy'</span>@<span class="hljs-string">'localhost'</span>;
</code></pre>
<h3 id="heading-5-test-the-user-permissions">5. <strong>Test the User Permissions</strong></h3>
<p>You can log in as the new user and verify that they have the appropriate permissions.</p>
<pre><code class="lang-python">mysql -u thirdy -p
</code></pre>
<p>Once logged in, try running <code>SELECT</code>, <code>INSERT</code>, and <code>UPDATE</code> queries. The user should not be able to delete records.</p>
<h3 id="heading-example">Example</h3>
<pre><code class="lang-python">SELECT * FROM your_table; -- Should work

INSERT INTO your_table (column1, column2) VALUES (<span class="hljs-string">'value1'</span>, <span class="hljs-string">'value2'</span>); -- Should work

UPDATE your_table SET column1 = <span class="hljs-string">'new_value'</span> WHERE id = <span class="hljs-number">1</span>; -- Should work

DELETE FROM your_table WHERE id = <span class="hljs-number">1</span>; -- Should NOT work
</code></pre>
]]></content:encoded></item><item><title><![CDATA[React Router]]></title><description><![CDATA[1. Install react-router-dom
If you haven't installed React Router yet, run this command in your project directory:
npm install react-router-dom

2. Set Up Routing in Your React App
First, make sure you have components for your pages:

LoginPage.tsx

...]]></description><link>https://software-engineer.thirdygayares.com/react-router</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/react-router</guid><category><![CDATA[react router]]></category><category><![CDATA[React]]></category><category><![CDATA[react basic]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Wed, 09 Oct 2024 13:30:22 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-1-install-react-router-dom">1. Install <code>react-router-dom</code></h3>
<p>If you haven't installed React Router yet, run this command in your project directory:</p>
<pre><code class="lang-plaintext">npm install react-router-dom
</code></pre>
<h3 id="heading-2-set-up-routing-in-your-react-app">2. Set Up Routing in Your React App</h3>
<p>First, make sure you have components for your pages:</p>
<ul>
<li><p><code>LoginPage</code>.tsx</p>
</li>
<li><p><code>ServicesPage.tsx</code></p>
</li>
<li><p><code>AboutPage.tsx</code></p>
</li>
</ul>
<h3 id="heading-example-code">Example Code:</h3>
<h4 id="heading-21-create-components-if-you-havent-already">2.1 Create Components (If you haven’t already)</h4>
<p><strong>LoginPage.tsx</strong></p>
<pre><code class="lang-plaintext">import React from 'react';

const LoginPage: React.FC = () =&gt; {
  return &lt;h2&gt;This is the Login Page&lt;/h2&gt;;
};

export default LoginPage;
</code></pre>
<p><strong>ServicesPage.js</strong></p>
<pre><code class="lang-plaintext">import React from 'react';

function ServicesPage() {
  return &lt;h2&gt;This is the Services Page&lt;/h2&gt;;
}

export default ServicesPage;
</code></pre>
<p><strong>AboutPage.js</strong></p>
<pre><code class="lang-plaintext">import React from 'react';

const AboutPage: React.FC = () =&gt; {
  return &lt;h2&gt;This is the About Page&lt;/h2&gt;;
};

export default AboutPage;
</code></pre>
<h1 id="heading-4-create-a-separate-file-for-routes">4 Create a Separate File for Routes</h1>
<p>Create a new file called <code>AppRoutes.tsx</code>nand define all your routes there.</p>
<h4 id="heading-approutestsx"><strong>app.routes.tsx</strong></h4>
<pre><code class="lang-plaintext">import React from 'react';
import {Route, Routes} from 'react-router-dom';
import LoginPage from "./pages/LoginPage";
import ServicesPage from "./pages/ServicesPage";
import AboutPage from "./pages/AboutPage";

interface RouteProps {
    exact?: boolean;
    path: string;
    component: React.ComponentType&lt;any&gt;;
}


const AppRoutes: React.FC = () =&gt; {
    return (
        &lt;Routes&gt;
            &lt;Route path="/login" element={&lt;LoginPage /&gt;} /&gt;
            &lt;Route path="/services" element={&lt;ServicesPage /&gt;} /&gt;
            &lt;Route path="/about" element={&lt;AboutPage /&gt;} /&gt;
            {/* Default route */}
            &lt;Route path="/" element={&lt;LoginPage /&gt;} /&gt;
        &lt;/Routes&gt;
    );
};

export default AppRoutes;
</code></pre>
<h3 id="heading-4-use-approutes-to-app">4. Use AppRoutes to App</h3>
<p>Now you can import <code>AppRoutes</code> into your main <code>App.tsx</code> file.</p>
<h4 id="heading-apptsx"><strong>App.tsx</strong></h4>
<pre><code class="lang-plaintext">import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import AppRoutes from './app.routes'; // Import your routes
import { Link } from 'react-router-dom';

const App: React.FC = () =&gt; {
  return (
      &lt;div&gt;
        {/* Navigation */}
        &lt;nav&gt;
          &lt;ul&gt;
            &lt;li&gt;&lt;Link to="/login"&gt;Login&lt;/Link&gt;&lt;/li&gt;
            &lt;li&gt;&lt;Link to="/services"&gt;Services&lt;/Link&gt;&lt;/li&gt;
            &lt;li&gt;&lt;Link to="/about"&gt;About&lt;/Link&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/nav&gt;

        {/* Render Routes */}
        &lt;AppRoutes /&gt;
      &lt;/div&gt;
  );
};

export default App;
</code></pre>
<h3 id="heading-3-explanation">3. Explanation:</h3>
<ul>
<li><p><code>app.routes.tsx</code>: Contains all your route definitions using the <code>Switch</code> and <code>Route</code> components.</p>
</li>
<li><p><code>App.tsx</code>: Now imports and uses the <code>AppRoutes</code> component to manage routing in a more organized and modular way.</p>
</li>
</ul>
<h3 id="heading-5-default-route-optional">5. Default Route (Optional)</h3>
<p>You can add a default route to redirect the user to a specific page if they visit the root (<code>/</code>):</p>
<pre><code class="lang-plaintext">&lt;Route exact path="/" component={LoginPage} /&gt;
</code></pre>
<h3 id="heading-6-running-the-app">6. Running the App</h3>
<p>Start your development server:</p>
<pre><code class="lang-plaintext">npm start
</code></pre>
<p>Now, you can navigate to <code>/login</code>, <code>/services</code>, and <code>/about</code> to see each page.</p>
<p>This is a basic routing setup for your React app with <code>login_page</code>, <code>services_page</code>, and <code>about_page</code>.</p>
<p><strong>OUTPUT</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728351670448/8dd74ba4-6f3a-4c29-ad6b-5371bab27215.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728351685618/3cb75f0a-74fa-4a8a-b435-4075f83d03bb.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728351691224/c02b5b55-b7bf-4f3e-81e2-525f4181d227.png" alt class="image--center mx-auto" /></p>
<p><strong>TEST:</strong></p>
<p><a target="_blank" href="https://react-router-thirdy.web.app/login">https://react-router-thirdy.web.app/login</a></p>
<p>GITHUB:</p>
<p><a target="_blank" href="https://github.com/thirdygayares/react-router">thirdygayares/react-router (github.com)</a></p>
]]></content:encoded></item><item><title><![CDATA[How deploy React to Firebase Hosting]]></title><description><![CDATA[To deploy a React app to Firebase Hosting, follow these steps:
Requirements

Install Firebase CLI: Ensure you have Node.js installed, then install Firebase CLI by running:
 npm install -g firebase-tools

 

Login to Firebase: If you haven’t already, ...]]></description><link>https://software-engineer.thirdygayares.com/how-deploy-react-to-firebase-hosting</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/how-deploy-react-to-firebase-hosting</guid><category><![CDATA[react firebase hosting]]></category><category><![CDATA[how to deploy react in firebase hosting]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Wed, 09 Oct 2024 13:27:16 GMT</pubDate><content:encoded><![CDATA[<p>To deploy a React app to Firebase Hosting, follow these steps:</p>
<h3 id="heading-requirements">Requirements</h3>
<ol>
<li><p><strong>Install Firebase CLI</strong>: Ensure you have Node.js installed, then install Firebase CLI by running:</p>
<pre><code class="lang-typescript"> npm install -g firebase-tools
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728479766454/16ad0dc8-beaf-4568-86d2-b0cb04ad225e.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Login to Firebase</strong>: If you haven’t already, log in to your Firebase account using the CLI:</p>
<pre><code class="lang-typescript"> firebase login
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728479835864/296c0d21-fe80-4573-b149-061a3563c05c.png" alt class="image--center mx-auto" /></p>
<p> <code>ITS AUTOMATIC TO REDIRECT TO YOUR BROWSER. IF NOT COPY THE LINK</code></p>
</li>
<li><p><strong>Firebase Project</strong>: Make sure you have a Firebase project set up. You can create one in the Firebase Console: https://console.firebase.google.com/.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728479682306/6dd9b91b-adbe-4c21-81b1-17b82c473b26.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728479699530/2014bcaa-41bf-451e-a6e3-82472f3c408c.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728479717043/c6c85d2f-1fc6-4336-8000-9432d9f7f0fe.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-steps-to-deploy-react-app-to-firebase-hosting">Steps to Deploy React App to Firebase Hosting</h3>
<h4 id="heading-1-create-a-react-app-if-not-already-created">1. Create a React App (if not already created)</h4>
<p>If you don’t already have a React app, create one using <code>create-react-app</code>:</p>
<pre><code class="lang-typescript">npx create-react-app react-router
cd react-router
</code></pre>
<h4 id="heading-2-initialize-firebase-in-your-react-project">2. Initialize Firebase in Your React Project</h4>
<p>In your React app’s root directory, run:</p>
<pre><code class="lang-typescript">firebase init
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728479903187/3279b856-f3d0-40d1-ad52-37b951adb2b9.png" alt class="image--center mx-auto" /></p>
<p>When running <code>firebase init</code>, you'll be prompted with a few questions:</p>
<ul>
<li><p><strong>Select Firebase Hosting</strong>: Use the arrow keys to navigate and space bar to select "Hosting: Configure and deploy Firebase Hosting sites".</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480010860/017b3872-b24b-4754-b3f0-485665449c43.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Choose a Firebase project</strong>: Select the Firebase project you created earlier from the list.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480040239/f432bbd4-9d9e-485f-9d66-c729ddb58074.png" alt class="image--center mx-auto" /></p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480059519/333563bd-b643-435d-a284-cd45e63ed557.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Set up a public directory</strong>: Type <code>build</code> as the public directory (this is where your React app will be built).</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480095646/b46f4428-fcfc-4bfb-a284-7632e88d823a.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Single-page app</strong>: Answer <code>Yes</code> to "Configure as a single-page app (rewrite all URLs to /index.html)?"</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480129570/5be7a4cd-64cf-4d81-b442-8a4ee11fd968.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Overwrite existing</strong> <code>index.html</code>: If prompted, choose <code>No</code> (Firebase will create its own hosting configurations).</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480151741/d8c58c2b-b57b-4b52-86a2-08c08c84f23e.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h4 id="heading-3-build-the-react-app">3. Build the React App</h4>
<p>Before deploying, you need to build your React app. This will create an optimized production build:</p>
<pre><code class="lang-typescript">npm run build
</code></pre>
<p>This command generates a <code>build</code> folder, which contains your entire application optimized for production.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480178697/5f5dfc9c-2fa0-4430-9d43-708edc081878.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480212503/1ae614b4-c2a1-41fc-a1c7-7ed3d0d1c6ee.png" alt class="image--center mx-auto" /></p>
<h4 id="heading-4-deploy-to-firebase-hosting">4. Deploy to Firebase Hosting</h4>
<p>Once the build is ready, deploy your app to Firebase Hosting by running:</p>
<pre><code class="lang-typescript">firebase deploy
</code></pre>
<h4 id="heading-5-firebase-hosting-url">5. Firebase Hosting URL</h4>
<p>After deploying, you will see a specific URL where your app is hosted, like:</p>
<p>Use the <strong>Hosting URL</strong> to access your live React application.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728480276803/4177b9a3-45f1-409d-b785-1c1fb790d128.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-useful-firebase-hosting-commands">Useful Firebase Hosting Commands:</h3>
<ul>
<li><p><code>firebase deploy</code>: Deploys your project to Firebase.</p>
</li>
<li><p><code>firebase serve</code>: Serves your app locally on a development server (useful for testing Firebase Hosting setup).</p>
</li>
</ul>
<hr />
<p>Now your React app should be live on Firebase Hosting! You can access it via the URL provided by Firebase, which will look something like</p>
<p><a target="_blank" href="https://react-router-thirdy.web.app">https://react-router-thirdy.web.app</a>.</p>
<p>Github:</p>
<p><a target="_blank" href="https://github.com/thirdygayares/react-router">https://github.com/thirdygayares/react-router</a></p>
]]></content:encoded></item><item><title><![CDATA[Getting Started with React TypeScript]]></title><description><![CDATA[React (or React.js) is a JavaScript library designed to help developers build dynamic and interactive user interfaces, especially for single-page applications (SPAs). It was developed by Facebook and is widely used for creating reusable UI components...]]></description><link>https://software-engineer.thirdygayares.com/getting-started-with-react-typescript</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/getting-started-with-react-typescript</guid><category><![CDATA[react getting started]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Mon, 07 Oct 2024 23:39:45 GMT</pubDate><content:encoded><![CDATA[<p>React (or React.js) is a <strong>JavaScript library</strong> designed to help developers build dynamic and interactive user interfaces, especially for single-page applications (SPAs). It was developed by Facebook and is widely used for creating reusable UI components that update efficiently based on changes in the application’s data. (<a target="_blank" href="https://blog.hubspot.com/website/react-js">https://blog.hubspot.com/website/react-js</a>)</p>
<p>If you don't have Node.js installed, download it from <a target="_blank" href="https://nodejs.org/">Node.js</a>. You can verify if you have Node and npm installed by running:</p>
<pre><code class="lang-plaintext">node -v
npm -v
</code></pre>
<h3 id="heading-2-create-a-new-react-app-with-typescript">2. <strong>Create a New React App with TypeScript</strong></h3>
<p>Use the following command to create a new React app using TypeScript:</p>
<pre><code class="lang-plaintext">npx create-react-app react-getting-started --template typescript
cd react-getting-started
</code></pre>
<p>This sets up a React project with TypeScript.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728341661890/7e3c107c-4e1f-4106-bc5e-de2bf99d3413.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728341735824/0488b2fc-e12e-4f6e-9ac7-52444fed153c.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-3-start-the-development-server">3. <strong>Start the Development Server</strong></h3>
<p>Navigate to the project directory and start the development server:</p>
<pre><code class="lang-plaintext">npm start
</code></pre>
<p>This will open the app in your browser at <code>http://localhost:3000</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728341980829/41486143-3fd7-4d62-b537-f232c3294e83.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728342007412/34184cb7-ac39-4f34-8af2-2a8524349cd0.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-4-project-structure-with-typescript">4. <strong>Project Structure with TypeScript</strong></h3>
<p>The project structure will look similar to a regular React app, with the main difference being the use of <code>.ts</code> and <code>.tsx</code> files instead of <code>.js</code>:</p>
<pre><code class="lang-plaintext">react-getting-started
├── node_modules
├── public
│   ├── index.html
│   └── ...
├── src
│   ├── App.css
│   ├── App.tsx
│   ├── index.tsx
│   └── ...
├── tsconfig.json
├── package.json
└── ...
</code></pre>
<ul>
<li><p><code>tsconfig.json</code>: This file contains the configuration for TypeScript.</p>
</li>
<li><p><code>src/index.tsx</code>: The main entry point for your app.</p>
</li>
<li><p><code>src/App.tsx</code>: Your main React component in TypeScript.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728341858239/1af79541-b6bf-4065-96b5-7ffe61c0206c.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-5-basic-example-with-typescript">5. <strong>Basic Example with TypeScript</strong></h3>
<p>Open <code>src/App.tsx</code> and modify it to include TypeScript types:</p>
<pre><code class="lang-plaintext">import React from 'react';

interface AppProps {
  message: string;
}

const App: React.FC&lt;AppProps&gt; = ({ message }) =&gt; {
  return (
    &lt;div&gt;
      &lt;h1&gt;{message}&lt;/h1&gt;
      &lt;p&gt;Welcome to your first React app with TypeScript.&lt;/p&gt;
    &lt;/div&gt;
  );
};

export default App;
</code></pre>
<p>In this example:</p>
<ul>
<li><p><code>AppProps</code> is an interface that defines the props that the component accepts (<code>message</code> of type <code>string</code>).</p>
</li>
<li><p><code>React.FC</code> is the type for functional components.</p>
</li>
</ul>
<p>To use this component, modify <code>src/index.tsx</code>:</p>
<pre><code class="lang-plaintext">import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(
  &lt;React.StrictMode&gt;
    &lt;App message="Hello, React with TypeScript!" /&gt;
  &lt;/React.StrictMode&gt;,
  document.getElementById('root')
);
</code></pre>
<h3 id="heading-6-working-with-types">6. <strong>Working with Types</strong></h3>
<p>When using TypeScript with React, you'll often work with interfaces or types to define the structure of your components' props, state, and more. For example:</p>
<ul>
<li><p><strong>Props</strong>: Define the data that components expect.</p>
</li>
<li><p><strong>State</strong>: Define the internal state of a component.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728343532790/580508ec-bf4e-4c61-a0f1-a00e25a888cc.png" alt class="image--center mx-auto" /></p>
<h1 id="heading-github-repository-link"><strong>Github: Repository Link</strong></h1>
<p><a target="_blank" href="https://github.com/thirdygayares/react-getting-started">thirdygayares/react-getting-started (github.com)</a></p>
]]></content:encoded></item><item><title><![CDATA[TypeScript Data Structure]]></title><description><![CDATA[Array

Objects

List Of Objects


Note: Ensure you have typescript globally
npm install -g typescript
How to run?
Example:
tsc array.ts
node array.js


Arrays in TypeScript
An array is a data structure that stores a collection of elements (values), u...]]></description><link>https://software-engineer.thirdygayares.com/typescript-data-structure</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/typescript-data-structure</guid><category><![CDATA[thirdy]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[TypeScript Data Structures]]></category><category><![CDATA[thirdy gayares]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Mon, 07 Oct 2024 16:00:00 GMT</pubDate><content:encoded><![CDATA[<ol>
<li><p>Array</p>
</li>
<li><p>Objects</p>
</li>
<li><p>List Of Objects</p>
</li>
</ol>
<p>Note: Ensure you have typescript globally</p>
<p><code>npm install -g typescript</code></p>
<p><strong>How to run?</strong></p>
<p>Example:</p>
<p><code>tsc array.ts</code></p>
<p><code>node array.js</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728386841844/c6563964-e943-40c6-bb58-e52cc0a84b78.png" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-arrays-in-typescript">Arrays in TypeScript</h1>
<p>An <strong>array</strong> is a data structure that stores a collection of elements (values), usually of the same type, in a single variable. Arrays allow you to store multiple values in an ordered list and provide easy access using an index.</p>
<p>In <strong>TypeScript</strong>, arrays can be declared to hold specific types, ensuring type safety. You can store a list of strings, numbers, or any other data types.</p>
<h3 id="heading-defining-an-array-in-typescript">Defining an Array in TypeScript</h3>
<p>Here's how you can define an array in TypeScript:</p>
<ol>
<li><p><strong>Array of Numbers</strong>:</p>
<pre><code class="lang-plaintext"> let numbers: number[] = [1, 2, 3, 4, 5];
</code></pre>
</li>
<li><p><strong>Array of Strings</strong>:</p>
<pre><code class="lang-plaintext"> let shoppingList: string[] = ["milk", "bread", "eggs"];
</code></pre>
</li>
<li><p><strong>Array of Multiple Types (using</strong> <code>any</code>):</p>
<pre><code class="lang-plaintext"> let mixedArray: any[] = [1, "apple", true];
</code></pre>
</li>
<li><p><strong>Using Generics</strong> (Another way to define arrays):</p>
<pre><code class="lang-plaintext"> let fruits: Array&lt;string&gt; = ["apple", "banana", "orange"];
</code></pre>
<p> <strong>EXAMPLE: SHOPPINGLIST</strong></p>
<pre><code class="lang-plaintext"> let shoppingList: string[] = ["milk", "chocolate" , "shampoo"];

 console.log("List: " + shoppingList);
</code></pre>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728387113986/8dc84f52-5f2c-4a92-92f0-d276567ca0ad.png" alt /></p>
<h2 id="heading-basic-operations-on-arrays-crud-in-typescript">Basic Operations on Arrays (CRUD) in TypeScript</h2>
<h3 id="heading-create-insert-adding-new-items-to-an-array-can-be-done-using-the-push-method"><strong>Create (Insert)</strong>: Adding new items to an array can be done using the <code>push()</code> method:</h3>
<pre><code class="lang-plaintext">shoppingList.push("Safeguard");
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728388187400/284c79f1-81c7-40e9-a082-fee002fe04d5.png" alt /></p>
<h3 id="heading-accessing-array-elements">Accessing Array Elements</h3>
<p>You can access elements in an array using their index. The index starts at <code>0</code>.</p>
<p><code>plaintext let firstItem = shoppingList[0]; let secondItem = shoppingList[1];</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728387593925/f9d15d25-172e-4552-ba75-e68402780995.png" alt /></p>
<h3 id="heading-update-you-can-modify-existing-elements-by-directly-accessing-them-via-their-index"><strong>Update</strong>: You can modify existing elements by directly accessing them via their index:</h3>
<pre><code class="lang-plaintext">shoppingList[1] = "strawberry";
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728388677542/9a16c067-8d00-42b8-8bda-b88591bbf3ce.png" alt /></p>
<h3 id="heading-delete-you-can-remove-elements-using-splice"><strong>Delete</strong>: You can remove elements using <code>splice()</code>:</h3>
<pre><code class="lang-plaintext">shoppingList.splice(2, 1);  // Removes 1 item at index 2
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728388963844/4445298b-4c75-45e7-aa01-79590a345e9d.png" alt /></p>
<p>This is how you use and manipulate arrays in TypeScript for basic operations like creating, reading, updating, and deleting elements.</p>
<hr />
<h1 id="heading-object">OBJECT</h1>
<p>An object in TypeScript is <strong>an instance that contains a set of key-value pairs</strong>. These pairs can hold various data types, including scalar values, functions, or even arrays of other objects. (<a target="_blank" href="https://www.geeksforgeeks.org/typescript-object/#:~:text=An%20object%20in%20TypeScript%20is,even%20arrays%20of%20other%20objects.">GEEKSFORGEEKS</a>)</p>
<h3 id="heading-sample-object"><strong>SAMPLE OBJECT</strong></h3>
<p><code>shoppingObject.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> ShoppingObject{
id: <span class="hljs-built_in">number</span>;
name: <span class="hljs-built_in">string</span>;
price: <span class="hljs-built_in">number</span>;
quantity: <span class="hljs-built_in">number</span>;
}
</code></pre>
<h2 id="heading-sample-operation-in-object"><strong>SAMPLE OPERATION IN OBJECT</strong></h2>
<h3 id="heading-create-data-in-object"><strong>CREATE DATA IN OBJECT</strong></h3>
<p><code>main.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> shoppingObject: ShoppingObject = {
    id: <span class="hljs-number">1</span>,
    name: <span class="hljs-string">"test"</span>,
    price: <span class="hljs-number">20</span>,
    quantity: <span class="hljs-number">2</span>
};

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"List "</span>);
<span class="hljs-built_in">console</span>.log(shoppingObject);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728443810593/61310ca6-d3ee-431d-a396-94ee33d2dc90.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-access-specific-value"><strong>ACCESS SPECIFIC VALUE</strong></h3>
<p><code>main.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-comment">//Access value</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"=====ACCESSSSING====="</span>);
<span class="hljs-keyword">const</span> name: <span class="hljs-built_in">string</span> = shoppingObject.name;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"FETCH_ name: "</span> + name);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728445394633/433644d7-ca16-466d-bc1b-3aa3c835c93c.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-udpating-specific-value"><strong>UDPATING SPECIFIC VALUE</strong></h3>
<p><code>main.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"=====UPDATINGGG====="</span>);

shoppingObject.name = <span class="hljs-string">"thirdy"</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"UPDATED_ now the object: "</span> );
<span class="hljs-built_in">console</span>.log(shoppingObject);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728445637707/368b575e-9861-44df-ae7e-0982dad45f73.png" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-array-of-objects">Array of Objects</h1>
<p>Defining interfaces or type, we will provide the property names and their types inside them and assign them as a explicit type the array. (<a target="_blank" href="https://www.geeksforgeeks.org/how-can-i-define-an-array-of-objects-in-typescript/#using-the-inbuilt-interfaces-and-type">https://www.geeksforgeeks.org/how-can-i-define-an-array-of-objects-in-typescript/#using-the-inbuilt-interfaces-and-type</a>)</p>
<p>sample Array of Object</p>
<p><code>ShoppingListObj.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> ShoppingListObj{
  id: <span class="hljs-built_in">number</span>,
  name: <span class="hljs-built_in">string</span>,
  price: <span class="hljs-built_in">number</span>,
  quantity: <span class="hljs-built_in">number</span>
}
</code></pre>
<h3 id="heading-1-create-operation">1. Create Operation</h3>
<p>This function adds a new product to the <code>products</code> array.</p>
<p><code>main.ts</code></p>
<pre><code class="lang-plaintext">const shoppingList: ShoppingListObj[] = [];

shoppingList.push({id: 1, name: "test", price: 100, quantity: 5});
shoppingList.push({id: 2, name: "second", price: 100, quantity: 5});

console.log("List ");
console.log(shoppingList);
</code></pre>
<h4 id="heading-explanation">Explanation:</h4>
<ul>
<li><p><strong>Parameter</strong>: It accepts a parameter <code>shoppingList</code> of type <code>ShoppingListObj</code>. This represents the new list you want to add.</p>
</li>
<li><p><strong>Operation</strong>: The shopping or cart is added to the <code>shoppingList</code> array using the <code>push()</code> method.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728446986251/4982c29f-4978-412c-ba0c-14670d97f1d7.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<hr />
<h3 id="heading-2-read-product">2. <strong>Read Product</strong></h3>
<p>This function fetches a shoppingList by its <code>id</code>.</p>
<pre><code class="lang-plaintext"> const readMyCart = (id: number): ShoppingListObj | undefined =&gt; {
       return shoppingList.find(p =&gt; p.id === id);
 };
</code></pre>
<h4 id="heading-explanation-1">Explanation:</h4>
<ul>
<li><p><strong>Parameter</strong>: The function accepts <code>productId</code> which is the ID of the product to search for.</p>
</li>
<li><p><strong>Operation</strong>: It uses <code>find()</code> to search through the <code>products</code> array for the product with the matching <code>id</code>.</p>
</li>
<li><p><strong>Conditional Logging</strong>:</p>
<ul>
<li><p>If the product is found, it logs "Product found" and returns the product.</p>
</li>
<li><p>If not found, it logs "Product not found" and returns <code>undefined</code>.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-example-of-usage">Example of usage:</h3>
<pre><code class="lang-plaintext">readProduct(1);  // Tries to find the product with id = 1
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728447997956/ba475cba-7353-475c-908d-362396a1be86.png" alt class="image--center mx-auto" /></p>
<p>This will search for the product with ID <code>1</code>. If it exists, the product will be logged and returned.</p>
<hr />
<h3 id="heading-3-updateproduct-update-operation">3. <strong>updateProduct</strong> (Update Operation)</h3>
<p>This function updates the fields of a product using its <code>id</code>.</p>
<pre><code class="lang-plaintext">const updateMyCart = (id: number, updatedData: Partial&lt;ShoppingListObj&gt;) =&gt; {
        const findIndex = shoppingList.findIndex(p =&gt; p.id === id);
        if (findIndex &gt; -1) {
            shoppingList[findIndex] = { ...shoppingList[findIndex],  ...updatedData };
            console.log("UPDATED DATA: ");
            console.log(updatedData);
            console.log("UPDATED LIST: ");
            console.log(shoppingList);
        } else {
            console.log("Product Not Found");
        }
   };
</code></pre>
<h4 id="heading-explanation-2">Explanation:</h4>
<ul>
<li><p><strong>Parameters</strong>:</p>
<ul>
<li><p><code>id</code>: The ID of the product to be updated.</p>
</li>
<li><p><code>updatedData</code>: A <code>Partial&lt;ShoppingListObj&gt;</code>, meaning it allows you to pass only the fields you want to update, without needing to provide the whole product.</p>
</li>
</ul>
</li>
<li><p><strong>Operation</strong>:</p>
<ul>
<li><p>The function uses <code>findIndex()</code> to locate the product in the <code>products</code> array by its <code>id</code>.</p>
</li>
<li><p>If the product is found, it updates the fields by using object spread (<code>...</code>) to merge the existing product data with the new <code>updatedData</code>.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-example-of-usage-1">Example of usage:</h3>
<pre><code class="lang-plaintext">updateMyCart(2, {name: "shampoo"});
</code></pre>
<p>This will update the product with ID <code>2</code> by setting its <code>name</code> to <code>shampoo</code></p>
<p><strong>OUTPUT:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728461019032/72e4f4a1-516b-4030-b821-f72db49b1bb9.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-4delete-operation">4.Delete Operation</h3>
<p>This function deletes a product by its <code>id</code>.</p>
<pre><code class="lang-plaintext"> const deleteItem = (id: number) =&gt; {
        const findIndex = shoppingList.findIndex(p =&gt; p.id = id);
        if (findIndex !== -1){
            const deleteItem = shoppingList.splice(id, 1);
            console.log("Deleted Item: " + deleteItem[0]);
            console.log("UPDATED LIST: ");
            console.log(shoppingList);
        }
 }
</code></pre>
<h4 id="heading-explanation-3">Explanation:</h4>
<ul>
<li><p><strong>Parameter</strong>: It accepts <code>productId</code>, the ID of the product to be deleted.</p>
</li>
<li><p><strong>Operation</strong>:</p>
<ul>
<li><p>It uses <code>findIndex()</code> to locate the product in the <code>products</code> array.</p>
</li>
<li><p>If the product is found, it is removed using <code>splice()</code> (which removes the product at the found index).</p>
</li>
<li><p>The deleted product is then logged.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-example-of-usage-2">Example of usage:</h3>
<pre><code class="lang-plaintext">deleteItem(1);  // Deletes the product with id = 1
</code></pre>
<p>This will remove the product with ID <code>1</code> from the <code>shoppingList</code> array if it exists.</p>
<p><code>OUTPUT</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728465480233/bafb3e41-40ec-40c5-8b17-b8755a84c6eb.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-again">AGAIN</h3>
<ul>
<li><p><strong>Create</strong>: Adds a new product to the list.</p>
</li>
<li><p><strong>Read</strong>: Retrieves a product by its ID.</p>
</li>
<li><p><strong>Update</strong>: Modifies specific fields of a product.</p>
</li>
<li><p><strong>Delete</strong>: Removes a product by its ID.</p>
</li>
</ul>
<p>Each function uses basic array methods (<code>push</code>, <code>find</code>, <code>findIndex</code>, <code>splice</code>) to manipulate the <code>products</code> array and logs the results for clarity.</p>
<p><strong>GITHUB REPOSITORY:</strong></p>
<p><a target="_blank" href="https://github.com/thirdygayares/typescripts-data-structure.git">https://github.com/thirdygayares/typescripts-data-structure.git</a></p>
]]></content:encoded></item><item><title><![CDATA[Angular Services]]></title><description><![CDATA[In Angular, services are a core feature that allows you to organize and reuse code efficiently. A service in Angular is typically a class that encapsulates business logic, such as fetching data from an API, performing calculations, or managing state ...]]></description><link>https://software-engineer.thirdygayares.com/angular-services</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/angular-services</guid><category><![CDATA[Angular]]></category><category><![CDATA[Angular Services]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Fri, 04 Oct 2024 13:58:18 GMT</pubDate><content:encoded><![CDATA[<p>In Angular, services are a core feature that allows you to organize and reuse code efficiently. A service in Angular is typically a class that encapsulates business logic, such as fetching data from an API, performing calculations, or managing state across different components. This logic is separated from the view or component to ensure that the application remains modular and easier to maintain.</p>
<h4 id="heading-why-use-angular-services">Why Use Angular Services?</h4>
<ol>
<li><p><strong>Separation of Concerns</strong>: Services allow you to offload logic from components. Components are mostly responsible for handling the view, while services deal with application logic, such as retrieving data or handling complex operations. This helps keep your code clean and organized​(<a target="_blank" href="https://angular.io/guide/architecture-services">Angular</a>)​(<a target="_blank" href="https://angular.dev/tutorials/first-app/09-services/">Angular</a>).</p>
</li>
<li><p><strong>Reusability</strong>: Once you define a service, it can be injected and used across multiple components. This avoids repeating the same code in different parts of your application, making it easier to update and maintain​(<a target="_blank" href="https://www.freecodecamp.org/news/angular-services-and-dependency-injection-explained/">FreeCodeCamp</a>)​(<a target="_blank" href="https://dev.to/this-is-angular/introduction-to-angular-services-j55">DEV Community</a>).</p>
</li>
<li><p><strong>Dependency Injection</strong>: Angular has a built-in Dependency Injection (DI) system, making it easy to use services across different parts of your app. You can inject services directly into your components or other services by declaring them in the constructor and marking them with <code>@Injectable</code> decorator​(<a target="_blank" href="https://dev.to/this-is-angular/introduction-to-angular-services-j55">DEV Community</a>)​(<a target="_blank" href="https://www.tektutorialshub.com/angular/angular-services/">TekTutorialsHub</a>).</p>
</li>
</ol>
<h4 id="heading-how-to-create-a-service">How to Create a Service</h4>
<ol>
<li><p><strong>Define the Service</strong>: A service is essentially a class marked with the <code>@Injectable</code> decorator. For example, a simple logging service could look like this:</p>
<pre><code class="lang-bash"> import { Injectable } from <span class="hljs-string">'@angular/core'</span>;

 @Injectable({
   providedIn: <span class="hljs-string">'root'</span>,
 })
 <span class="hljs-built_in">export</span> class LoggingService {
   <span class="hljs-built_in">log</span>(message: string): void {
     console.log(message);
   }
 }
</code></pre>
<p> In this example, the <code>LoggingService</code> logs messages to the console.</p>
</li>
<li><p><strong>Injecting the Service</strong>: Once your service is created, you can inject it into a component by adding it to the constructor:</p>
<pre><code class="lang-bash"> import { Component } from <span class="hljs-string">'@angular/core'</span>;
 import { LoggingService } from <span class="hljs-string">'./logging.service'</span>;

 @Component({
   selector: <span class="hljs-string">'app-root'</span>,
   templateUrl: <span class="hljs-string">'./app.component.html'</span>,
 })
 <span class="hljs-built_in">export</span> class AppComponent {
   constructor(private loggingService: LoggingService) {}

   logMessage(): void {
     this.loggingService.log(<span class="hljs-string">'Hello from AppComponent!'</span>);
   }
 }
</code></pre>
<p> This ensures that the <code>LoggingService</code> can be accessed from within the <code>AppComponent</code>​(<a target="_blank" href="https://angular.dev/tutorials/first-app/09-services/">Angular</a>)​(<a target="_blank" href="https://www.freecodecamp.org/news/angular-services-and-dependency-injection-explained/">FreeCodeCamp</a>).</p>
</li>
<li><p><strong>Using Services with Dependency Injection</strong>: The <code>providedIn: 'root'</code> setting in the <code>@Injectable</code> decorator makes the service available globally, meaning Angular creates a single instance (singleton) of the service across the entire application​(<a target="_blank" href="https://www.freecodecamp.org/news/angular-services-and-dependency-injection-explained/">FreeCodeCamp</a>).</p>
</li>
</ol>
<h4 id="heading-common-use-cases-for-angular-services">Common Use Cases for Angular Services</h4>
<ul>
<li><p><strong>Fetching Data</strong>: Services are often used to make HTTP requests to APIs using Angular’s <code>HttpClient</code>. For instance, you can create a service to fetch data from an external API and then share that data across different components.</p>
</li>
<li><p><strong>State Management</strong>: Services can maintain the state of your application (such as user data or configuration settings), ensuring that the state is consistent across different components.</p>
</li>
<li><p><strong>Logging and Error Handling</strong>: Services can handle centralized logging or error reporting, which simplifies the monitoring and debugging of your application​(<a target="_blank" href="https://angular.io/guide/architecture-services">Angular</a>)​(<a target="_blank" href="https://dev.to/this-is-angular/introduction-to-angular-services-j55">DEV Community</a>).</p>
</li>
</ul>
<p>Angular services allow you to build modular, scalable, and maintainable applications by organizing business logic into reusable units. By leveraging dependency injection, services reduce code duplication and help keep components focused on managing the view.</p>
<h3 id="heading-example"><strong>EXAMPLE:</strong></h3>
<p>Imagine you have a simple app that displays a list of universities. You want to add new universities, edit existing ones, and delete them. While you can handle all the logic in your component, using a <strong>service</strong> allows you to separate concerns and keep your code organized.</p>
<h2 id="heading-creating-the-component">Creating the Component</h2>
<p>Now, let's create our Angular component.</p>
<h3 id="heading-appcomponentts"><code>app.component.ts</code></h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> { Component, OnInit } from <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})</span>
export <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppComponent</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">OnInit</span> </span>{
  universityList: string[] = [];
  newUniversity = <span class="hljs-string">''</span>;

  ngOnInit(): <span class="hljs-keyword">void</span> {
    <span class="hljs-comment">// Initialization logic will go here</span>
  }
}
</code></pre>
<h2 id="heading-setting-up-the-html">Setting Up the HTML</h2>
<pre><code class="lang-java">&lt;h2&gt;Universities&lt;/h2&gt;

&lt;h3&gt;Add a New University&lt;/h3&gt;
&lt;input placeholder=<span class="hljs-string">"Enter university name"</span> /&gt;
&lt;button&gt;Add University&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;table border=<span class="hljs-string">"1"</span> cellpadding=<span class="hljs-string">"5"</span> cellspacing=<span class="hljs-string">"0"</span>&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;University Name&lt;/th&gt;
      &lt;th&gt;Actions&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr *ngFor=<span class="hljs-string">"let university of universityList; let i = index"</span>&gt;
      &lt;td&gt;{{ i + <span class="hljs-number">1</span> }}&lt;/td&gt;
      &lt;td&gt;{{ university }}&lt;/td&gt;
      &lt;td&gt;
        &lt;button&gt;Edit&lt;/button&gt;
        &lt;button&gt;Delete&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>
<p>OUTPUT:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728048486457/2a7e8ded-9fd5-4148-9eef-69d00d97cc9d.png" alt class="image--center mx-auto" /></p>
<p>At this point, the HTML is set up, but the functionality isn't there yet. The buttons don't do anything, and the list doesn't display any data because <code>universityList</code> is not defined.</p>
<p>In this component:</p>
<ul>
<li><p>We declare a <code>universityList</code> array to hold our list of universities.</p>
</li>
<li><p>We have a <code>newUniversity</code> string to bind to our input field.</p>
</li>
<li><p>We implement the <code>OnInit</code> interface to perform initialization logic.</p>
</li>
</ul>
<ol>
<li>Generate Service</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728046674797/9d133b9c-5d97-4b7b-bcf9-1d9ab5fa2d0d.png" alt class="image--center mx-auto" /></p>
<p>in angular UniversityService</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { Injectable } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Injectable</span>({
  providedIn: <span class="hljs-string">'root'</span>
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> UniversityService {
  <span class="hljs-keyword">private</span> universityList: <span class="hljs-built_in">string</span>[] = [<span class="hljs-string">"University of the Philippines"</span>, <span class="hljs-string">"UST"</span>];

  <span class="hljs-comment">// Fetch all universities</span>
  getUniversities(): <span class="hljs-built_in">string</span>[] {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.universityList;
  }

  <span class="hljs-comment">// Add a new university</span>
  addUniversity(university: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">void</span> {
    <span class="hljs-built_in">this</span>.universityList.push(university);
  }

  <span class="hljs-comment">// Update a university by index</span>
  updateUniversity(index: <span class="hljs-built_in">number</span>, newUniversity: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">void</span> {
    <span class="hljs-keyword">if</span> (index &gt;= <span class="hljs-number">0</span> &amp;&amp; index &lt; <span class="hljs-built_in">this</span>.universityList.length) {
      <span class="hljs-built_in">this</span>.universityList[index] = newUniversity;
    }
  }

  <span class="hljs-comment">// Remove a university by index</span>
  deleteUniversity(index: <span class="hljs-built_in">number</span>): <span class="hljs-built_in">void</span> {
    <span class="hljs-keyword">if</span> (index &gt;= <span class="hljs-number">0</span> &amp;&amp; index &lt; <span class="hljs-built_in">this</span>.universityList.length) {
      <span class="hljs-built_in">this</span>.universityList.splice(index, <span class="hljs-number">1</span>);
    }
  }
}
</code></pre>
<p>In this service:</p>
<ul>
<li><p>We have a private <code>universityList</code> array that stores the universities.</p>
</li>
<li><p>Methods to get, add, update, and delete universities.</p>
</li>
<li><p>By injecting this service into our component, we can manage the data efficiently.</p>
</li>
</ul>
<h2 id="heading-connecting-the-service-to-the-component">Connecting the Service to the Component</h2>
<p>Now that we have our service, let's use it in our component.</p>
<h3 id="heading-import-the-service">Import the Service</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> {UniversityService} from <span class="hljs-string">'./services/university-service.service'</span>;
</code></pre>
<h3 id="heading-inject-the-service">Inject the Service</h3>
<p>Modify the constructor to inject the service:</p>
<pre><code class="lang-java">constructor(<span class="hljs-keyword">private</span> universityService: UniversityService) {}
</code></pre>
<h3 id="heading-initialize-the-data">Initialize the Data</h3>
<p>Update the <code>ngOnInit</code> method:</p>
<pre><code class="lang-java">ngOnInit(): <span class="hljs-keyword">void</span> {
  <span class="hljs-keyword">this</span>.universityList = <span class="hljs-keyword">this</span>.universityService.getUniversities();
}
</code></pre>
<h2 id="heading-implementing-the-functions">Implementing the Functions</h2>
<p>Let's make our buttons functional by implementing the methods for adding, editing, and deleting universities.</p>
<h3 id="heading-add-university">Add University</h3>
<p>Update the HTML input and button to bind to <code>newUniversity</code> and call <code>addUniversity()</code>:</p>
<pre><code class="lang-java">&lt;input [(ngModel)]=<span class="hljs-string">"newUniversity"</span> placeholder=<span class="hljs-string">"Enter university name"</span> /&gt;
&lt;button (click)=<span class="hljs-string">"addUniversity()"</span>&gt;Add University&lt;/button&gt;
</code></pre>
<p>In the component:</p>
<pre><code class="lang-java">addUniversity(): <span class="hljs-keyword">void</span> {
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.newUniversity.trim()) {
    <span class="hljs-keyword">this</span>.universityService.addUniversity(<span class="hljs-keyword">this</span>.newUniversity.trim());
    <span class="hljs-keyword">this</span>.newUniversity = <span class="hljs-string">''</span>;
  }
}
</code></pre>
<ul>
<li><p>We check if the input is not empty.</p>
</li>
<li><p>Use the service's <code>addUniversity</code> method to add the new university.</p>
</li>
<li><p>Reset the <code>newUniversity</code> variable to clear the input field.</p>
</li>
</ul>
<h3 id="heading-edit-university">Edit University</h3>
<p>Update the "Edit" button in the HTML:</p>
<pre><code class="lang-java">&lt;button (click)=<span class="hljs-string">"editUniversity(i)"</span>&gt;Edit&lt;/button&gt;
</code></pre>
<p>In the component:</p>
<pre><code class="lang-java">editUniversity(index: number): <span class="hljs-keyword">void</span> {
  <span class="hljs-keyword">const</span> currentName = <span class="hljs-keyword">this</span>.universityList[index];
  <span class="hljs-keyword">const</span> newName = prompt(<span class="hljs-string">'Enter new name for the university:'</span>, currentName);
  <span class="hljs-keyword">if</span> (newName !== <span class="hljs-keyword">null</span> &amp;&amp; newName.trim() !== <span class="hljs-string">''</span>) {
    <span class="hljs-keyword">this</span>.universityService.updateUniversity(index, newName.trim());
  }
}
</code></pre>
<ul>
<li><p>When "Edit" is clicked, we get the current name and prompt the user for a new name.</p>
</li>
<li><p>If the user provides a valid name, we update the university using the service.</p>
</li>
</ul>
<h3 id="heading-delete-university">Delete University</h3>
<p>Update the "Delete" button in the HTML:</p>
<pre><code class="lang-java">&lt;button (click)=<span class="hljs-string">"deleteUniversity(i)"</span>&gt;Delete&lt;/button&gt;
</code></pre>
<p>In the component:</p>
<pre><code class="lang-java">deleteUniversity(index: number): <span class="hljs-keyword">void</span> {
  <span class="hljs-keyword">this</span>.universityService.deleteUniversity(index);
}
</code></pre>
<p><strong>Let's put it all together.</strong></p>
<h4 id="heading-appcomponenthtml"><code>app.component.html</code></h4>
<pre><code class="lang-java">&lt;h2&gt;Universities&lt;/h2&gt;

&lt;h3&gt;Add a New University&lt;/h3&gt;
&lt;input [(ngModel)]=<span class="hljs-string">"newUniversity"</span> placeholder=<span class="hljs-string">"Enter university name"</span> /&gt;
&lt;button (click)=<span class="hljs-string">"addUniversity()"</span>&gt;Add University&lt;/button&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;table border=<span class="hljs-string">"1"</span> cellpadding=<span class="hljs-string">"5"</span> cellspacing=<span class="hljs-string">"0"</span>&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;University Name&lt;/th&gt;
      &lt;th&gt;Actions&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr *ngFor=<span class="hljs-string">"let university of universityList; let i = index"</span>&gt;
      &lt;td&gt;{{ i + <span class="hljs-number">1</span> }}&lt;/td&gt;
      &lt;td&gt;{{ university }}&lt;/td&gt;
      &lt;td&gt;
        &lt;button (click)=<span class="hljs-string">"editUniversity(i)"</span>&gt;Edit&lt;/button&gt;
        &lt;button (click)=<span class="hljs-string">"deleteUniversity(i)"</span>&gt;Delete&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>
<h3 id="heading-appcomponentts-1"><code>app.component.ts</code></h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { Component, OnInit } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { UniversityService } <span class="hljs-keyword">from</span> <span class="hljs-string">'./university.service'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'app-root'</span>,
  templateUrl: <span class="hljs-string">'./app.component.html'</span>,
  styleUrls: [<span class="hljs-string">'./app.component.css'</span>],
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppComponent <span class="hljs-keyword">implements</span> OnInit {
  universityList: <span class="hljs-built_in">string</span>[] = [];
  newUniversity = <span class="hljs-string">''</span>;

  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> universityService: UniversityService</span>) {}

  ngOnInit(): <span class="hljs-built_in">void</span> {
    <span class="hljs-built_in">this</span>.universityList = <span class="hljs-built_in">this</span>.universityService.getUniversities();
  }

  addUniversity(): <span class="hljs-built_in">void</span> {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.newUniversity.trim()) {
      <span class="hljs-built_in">this</span>.universityService.addUniversity(<span class="hljs-built_in">this</span>.newUniversity.trim());
      <span class="hljs-built_in">this</span>.newUniversity = <span class="hljs-string">''</span>;
    }
  }

  deleteUniversity(index: <span class="hljs-built_in">number</span>): <span class="hljs-built_in">void</span> {
    <span class="hljs-built_in">this</span>.universityService.deleteUniversity(index);
  }

  editUniversity(index: <span class="hljs-built_in">number</span>): <span class="hljs-built_in">void</span> {
    <span class="hljs-keyword">const</span> currentName = <span class="hljs-built_in">this</span>.universityList[index];
    <span class="hljs-keyword">const</span> newName = prompt(<span class="hljs-string">'Enter new name for the university:'</span>, currentName);
    <span class="hljs-keyword">if</span> (newName !== <span class="hljs-literal">null</span> &amp;&amp; newName.trim() !== <span class="hljs-string">''</span>) {
      <span class="hljs-built_in">this</span>.universityService.updateUniversity(index, newName.trim());
    }
  }
}
</code></pre>
<h3 id="heading-universityservicets"><code>university.service.ts</code></h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> { Injectable } from <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Injectable({
  providedIn: 'root',
})</span>
export <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UniversityService</span> </span>{
  <span class="hljs-keyword">private</span> universityList: string[] = [<span class="hljs-string">'University of the Philippines'</span>, <span class="hljs-string">'UST'</span>];

  <span class="hljs-comment">// Fetch all universities</span>
  getUniversities(): string[] {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.universityList;
  }

  <span class="hljs-comment">// Add a new university</span>
  addUniversity(university: string): <span class="hljs-keyword">void</span> {
    <span class="hljs-keyword">this</span>.universityList.push(university);
  }

  <span class="hljs-comment">// Update a university by index</span>
  updateUniversity(index: number, newUniversity: string): <span class="hljs-keyword">void</span> {
    <span class="hljs-keyword">if</span> (index &gt;= <span class="hljs-number">0</span> &amp;&amp; index &lt; <span class="hljs-keyword">this</span>.universityList.length) {
      <span class="hljs-keyword">this</span>.universityList[index] = newUniversity;
    }
  }

  <span class="hljs-comment">// Remove a university by index</span>
  deleteUniversity(index: number): <span class="hljs-keyword">void</span> {
    <span class="hljs-keyword">if</span> (index &gt;= <span class="hljs-number">0</span> &amp;&amp; index &lt; <span class="hljs-keyword">this</span>.universityList.length) {
      <span class="hljs-keyword">this</span>.universityList.splice(index, <span class="hljs-number">1</span>);
    }
  }
}
</code></pre>
<p><strong>OUTPUT</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728049309465/460d2b7f-94b6-4d63-a2e2-b03d1e09a129.gif" alt class="image--center mx-auto" /></p>
<p>You've built a simple Angular app that uses a service to manage data. Let's recap what we've learned:</p>
<ul>
<li><p><strong>Angular Services</strong>: A way to encapsulate and share code across components. They help keep your components lean and focused on the view logic.</p>
</li>
<li><p><strong>Data Binding</strong>: Using <code>[(ngModel)]</code> for two-way data binding between the input field and the component property.</p>
</li>
<li><p><strong>Event Binding</strong>: Using <code>(click)</code> to bind button clicks to component methods.</p>
</li>
<li><p><strong>Dependency Injection</strong>: Injecting the <code>UniversityService</code> into the component's constructor to access its methods.</p>
</li>
</ul>
<p>REF:</p>
<p><a target="_blank" href="https://angular.io/guide/architecture-services">Angular.io</a> and <a target="_blank" href="https://www.freecodecamp.org/news/angular-services-and-dependency-injection-explained/">FreeCodeCamp</a>.</p>
<h4 id="heading-website">WEBSITE</h4>
<p><a target="_blank" href="https://thirdy-angular-service.web.app/">AngularServices (thirdy-angular-service.web.app</a></p>
<p><strong>GITHUB REPO:</strong></p>
<p><a target="_blank" href="https://github.com/thirdygayares/angular-services">https://github.com/thirdygayares/angular-services</a></p>
]]></content:encoded></item><item><title><![CDATA[JSON (JavaScript Object Notation)]]></title><description><![CDATA[JSON is a lightweight, text-based format used for data interchange between systems. It was developed as a way to structure data that is easily readable for humans while being efficient for machines to parse and generate. Though JSON originates from J...]]></description><link>https://software-engineer.thirdygayares.com/json-javascript-object-notation</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/json-javascript-object-notation</guid><category><![CDATA[json]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Fri, 04 Oct 2024 11:54:47 GMT</pubDate><content:encoded><![CDATA[<p><strong>JSON</strong> is a lightweight, text-based format used for data interchange between systems. It was developed as a way to structure data that is easily readable for humans while being efficient for machines to parse and generate. Though <strong>JSON</strong> originates from JavaScript syntax, it is language-independent, which means it can be used by a variety of programming languages such as Python, Java, PHP, and Ruby​(<a target="_blank" href="https://www.netguru.com/glossary/javascript-object-notation">Software Development Company | Netguru</a>)​(<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON">MDN Web Docs</a>).</p>
<p>The primary purpose of JSON is to <strong>facilitate the exchange of structured data</strong>. Before JSON became popular, <strong>XML (Extensible Markup Language)</strong> was the dominant format for this purpose. However, JSON’s simpler, more lightweight structure made it more appealing, especially in web development, where speed and efficiency are crucial​(<a target="_blank" href="https://www.hostinger.com/tutorials/what-is-json">Hostinger</a>).</p>
<p>One of JSON’s key strengths is its ability to represent complex data structures such as objects and arrays. These structures make it ideal for transmitting structured data over the internet, especially when dealing with APIs and web services. Its ease of use in web communication has led to its widespread adoption, particularly in <strong>RESTful APIs</strong>​(<a target="_blank" href="https://www.netguru.com/glossary/javascript-object-notation">Software Development Company | Netguru</a>).</p>
<h3 id="heading-json-syntax-and-structure"><strong>JSON Syntax and Structure</strong></h3>
<p>JSON has a minimalistic syntax with strict formatting rules that define how data must be represented. Here’s a breakdown of the key elements of JSON syntax:</p>
<ol>
<li><p><strong>Objects</strong>:</p>
<ul>
<li><p>Represented by curly braces <code>{}</code>.</p>
</li>
<li><p>Consist of key/value pairs separated by a colon <code>:</code>.</p>
</li>
<li><p>Keys must always be strings enclosed in double quotes.</p>
</li>
<li><p>Values can be of various data types, including strings, numbers, booleans, arrays, or even other objects.</p>
</li>
</ul>
</li>
</ol>
<p>    <strong>Example</strong>:</p>
<pre><code class="lang-bash">    {
      <span class="hljs-string">"firstName"</span>: <span class="hljs-string">"Thirdy"</span>,
      <span class="hljs-string">"lastName"</span>: <span class="hljs-string">"Gayares"</span>,
      <span class="hljs-string">"age"</span>: 25,
      <span class="hljs-string">"isStudent"</span>: <span class="hljs-literal">false</span>
    }
</code></pre>
<ol start="2">
<li><p><strong>Arrays</strong>:</p>
<ul>
<li><p>Represented by square brackets <code>[]</code>.</p>
</li>
<li><p>Contain ordered lists of values, which can be of any JSON data type (string, number, object, array, etc.).</p>
</li>
</ul>
</li>
</ol>
<p>    <strong>Example</strong>:</p>
<pre><code class="lang-bash">    {
      <span class="hljs-string">"hobbies"</span>: [<span class="hljs-string">"reading"</span>, <span class="hljs-string">"swimming"</span>, <span class="hljs-string">"gaming"</span>]
    }
</code></pre>
<ol start="3">
<li><p><strong>Data Types in JSON</strong>: JSON supports the following basic data types:</p>
<ul>
<li><p><strong>String</strong>: Enclosed in double quotes, e.g., <code>"name": "Alice"</code>.</p>
</li>
<li><p><strong>Number</strong>: Can be an integer or a floating-point number, e.g., <code>"age": 30</code>.</p>
</li>
<li><p><strong>Boolean</strong>: Represents <code>true</code> or <code>false</code>, e.g., <code>"isStudent": false</code>.</p>
</li>
<li><p><strong>Array</strong>: Ordered lists of values, e.g., <code>"hobbies": ["football", "reading"]</code>.</p>
</li>
<li><p><strong>Object</strong>: A collection of key/value pairs enclosed in curly braces.</p>
</li>
<li><p><strong>Null</strong>: Represents an empty or null value, e.g., <code>"middleName": null</code>​(<a target="_blank" href="https://www.hostinger.com/tutorials/what-is-json">Hostinger</a>)​(<a target="_blank" href="https://www.w3schools.com/js/js_json.asp">W3Schools.com</a>).</p>
</li>
</ul>
</li>
</ol>
<h4 id="heading-key-syntax-rules"><strong>Key Syntax Rules</strong>:</h4>
<ul>
<li><p><strong>Keys</strong> must always be strings enclosed in double quotes.</p>
</li>
<li><p><strong>Values</strong> can be strings, numbers, booleans, arrays, objects, or null.</p>
</li>
<li><p>Key/value pairs are separated by a colon <code>:</code>.</p>
</li>
<li><p>Multiple key/value pairs within an object are separated by commas.</p>
</li>
<li><p>Arrays are ordered lists of values and can contain mixed data types​(<a target="_blank" href="https://dev.to/henriqueleme/what-is-json-javacript-object-notation-and-how-to-use-it-2mhk">DEV Community</a>).</p>
</li>
</ul>
<h3 id="heading-use-cases-in-web-communication"><strong>Use Cases in Web Communication</strong></h3>
<p>The most common application of JSON is in web communication, where it is used to send and receive data between a server and a client (such as a web browser). JSON’s lightweight and straightforward structure makes it ideal for these purposes, as it can easily be parsed by both front-end and back-end systems. Here are some major use cases:</p>
<h4 id="heading-1-apis-and-data-exchange">1. <strong>APIs and Data Exchange</strong>:</h4>
<p>APIs, particularly <strong>RESTful APIs</strong>, frequently use JSON as their data exchange format. JSON is favored because it can be easily generated by servers and quickly parsed by web browsers or mobile applications. For instance, when a web application needs to retrieve user information from a remote server, the server often responds with data formatted as JSON. This data can then be displayed to the user in real time.</p>
<p><strong>Example</strong>: If you use Twitter’s API to fetch the latest tweets, the response is typically in JSON format, containing details like the tweet text, the user who posted it, and timestamps​(<a target="_blank" href="https://www.programiz.com/javascript/json">Programiz</a>)​(<a target="_blank" href="https://www.netguru.com/glossary/javascript-object-notation">Software Development Company | Netguru</a>).</p>
<h4 id="heading-2-asynchronous-data-transfer-ajax">2. <strong>Asynchronous Data Transfer (AJAX)</strong>:</h4>
<p>JSON is widely used in conjunction with <strong>AJAX (Asynchronous JavaScript and XML)</strong> to transfer data between the client and server without reloading the entire page. This enables dynamic content updates, such as refreshing news feeds, submitting forms, or fetching data in the background without affecting the user experience.</p>
<p><strong>Example</strong>: On an e-commerce website, when a user adds an item to their shopping cart, the action may trigger an AJAX request that sends data in JSON format to the server. The server processes the request and returns the updated cart details, also in JSON format, without reloading the page​(<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON">MDN Web Docs</a>)​(<a target="_blank" href="https://www.freecodecamp.org/news/what-is-json-a-json-file-example/">freeCodeCamp</a>).</p>
<h4 id="heading-3-configuration-files">3. <strong>Configuration Files</strong>:</h4>
<p>JSON is also commonly used as a format for configuration files in web and mobile applications. Since JSON is both human-readable and machine-friendly, it’s easy for developers to manually edit configuration files while also allowing the application to parse them for settings during runtime.</p>
<p><strong>Example</strong>: Many modern web applications, particularly those built with frameworks like <strong>React</strong> or <strong>Node.js</strong>, use <strong>JSON</strong> files (like <code>package.json</code>) to manage project dependencies and configurations​(<a target="_blank" href="https://www.hostinger.com/tutorials/what-is-json">Hostinger</a>).</p>
<h4 id="heading-4-data-persistence">4. <strong>Data Persistence</strong>:</h4>
<p>JSON is used in databases such as <strong>NoSQL</strong> systems (e.g., MongoDB) to store and retrieve structured data. In these databases, JSON-like formats (BSON for MongoDB) are used to represent and query data. This makes it easier to work with dynamic or unstructured data models, compared to traditional relational databases​(<a target="_blank" href="https://webflow.com/blog/what-is-json">Webflow</a>).</p>
<h3 id="heading-advantages-of-json-in-web-communication"><strong>Advantages of JSON in Web Communication</strong>:</h3>
<ol>
<li><p><strong>Lightweight</strong>: JSON’s minimal syntax makes it less bulky than other formats like XML, leading to faster data transmission and reduced server load.</p>
</li>
<li><p><strong>Human-Readable</strong>: JSON is easy to read and write, which makes debugging simpler for developers.</p>
</li>
<li><p><strong>Language-Agnostic</strong>: While JSON was derived from JavaScript, it can be parsed and generated in nearly every modern programming language, making it versatile across platforms​(<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON">MDN Web Docs</a>)​(<a target="_blank" href="https://dev.to/henriqueleme/what-is-json-javacript-object-notation-and-how-to-use-it-2mhk">DEV Community</a>).</p>
</li>
<li><p><strong>Faster Parsing</strong>: In comparison to XML, JSON is easier and quicker to parse, especially in browser environments where JavaScript can natively handle it​(<a target="_blank" href="https://www.freecodecamp.org/news/what-is-json-a-json-file-example/">freeCodeCamp</a>)​(<a target="_blank" href="https://www.netguru.com/glossary/javascript-object-notation">Software Development Company | Netguru</a>).</p>
</li>
</ol>
<hr />
<h3 id="heading-references"><strong>References</strong>:</h3>
<ol>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON">MDN Web Docs – Working with JSON</a>​(<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON">MDN Web Docs</a>).</p>
</li>
<li><p>W3Schools – JavaScript JSON​(<a target="_blank" href="https://www.w3schools.com/js/js_json.asp">W3Schools.com</a>).</p>
</li>
<li><p>Hostinger – What Is JSON? Syntax, Examples + Cheat Sheet​(<a target="_blank" href="https://www.hostinger.com/tutorials/what-is-json">Hostinger</a>).</p>
</li>
<li><p><a target="_blank" href="https://dev.to">DEV Community – What is JSON and how is it used?</a>​(<a target="_blank" href="https://dev.to/henriqueleme/what-is-json-javacript-object-notation-and-how-to-use-it-2mhk">DEV Community</a>).</p>
</li>
<li><p><a target="_blank" href="https://www.netguru.com">Netguru – Web Development with JSON</a>​(<a target="_blank" href="https://www.netguru.com/glossary/javascript-object-notation">Software Development Company | Netguru</a>).</p>
</li>
<li><p>FreeCodeCamp – JSON for Beginners​(<a target="_blank" href="https://www.freecodecamp.org/news/what-is-json-a-json-file-example/">freeCodeCamp</a>).</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Why Data Structures Like HashMap and List Are Essential When Studying Android Java]]></title><description><![CDATA[Data structures are important for organizing and managing data efficiently in software development. When studying Android development using Java, understanding key data structures like HashMap and List (specifically ArrayList) is hard. This topic hig...]]></description><link>https://software-engineer.thirdygayares.com/why-data-structures-like-hashmap-and-list-are-essential-when-studying-android-java</link><guid isPermaLink="true">https://software-engineer.thirdygayares.com/why-data-structures-like-hashmap-and-list-are-essential-when-studying-android-java</guid><category><![CDATA[android basic]]></category><category><![CDATA[android data structure]]></category><category><![CDATA[Android]]></category><category><![CDATA[android app development]]></category><dc:creator><![CDATA[Thirdy Gayares]]></dc:creator><pubDate>Fri, 04 Oct 2024 09:02:01 GMT</pubDate><content:encoded><![CDATA[<p>Data structures are important for organizing and managing data efficiently in software development. When studying Android development using Java, understanding key data structures like <code>HashMap</code> and <code>List</code> (specifically <code>ArrayList</code>) is hard. This topic highlights why these structures are important and how they can enhance Android applications.</p>
<h4 id="heading-1-efficient-data-management-with-hashmap">1. <strong>Efficient Data Management with HashMap</strong></h4>
<p><code>HashMap</code> is a key-value pair data structure that allows for quick data retrieval. It’s particularly useful in scenarios where fast access to data is needed.</p>
<ul>
<li><p><strong>Fast Lookups:</strong> <code>HashMap</code> provides average time complexity of O(1) for lookups, meaning you can quickly find data based on a key. This is essential in applications where speed is critical, such as when fetching user details or settings.</p>
</li>
<li><p><strong>Example Scenario:</strong> In a messaging app, you can store user information (like usernames and profile pictures) in a <code>HashMap</code>, where the username serves as the key. This allows quick access to user data without searching through a list.</p>
</li>
</ul>
<h4 id="heading-2-dynamic-data-storage-with-list-arraylist">2. <strong>Dynamic Data Storage with List (ArrayList)</strong></h4>
<p><code>ArrayList</code> is a flexible data structure that allows for dynamic storage of data. It can grow or shrink as needed, making it ideal for managing collections of items.</p>
<ul>
<li><p><strong>Easy Insertion and Deletion:</strong> <code>ArrayList</code> allows for easy addition and removal of items. Though inserting at the end is O(1), inserting or deleting at specific positions can take O(n) time. However, its flexibility makes it suitable for many use cases in Android apps.</p>
</li>
<li><p><strong>Example Scenario:</strong> An e-commerce application can use <code>ArrayList</code> to manage a dynamic shopping cart. Users can add or remove items from their cart, and the <code>ArrayList</code> can easily accommodate these changes.</p>
</li>
</ul>
<h4 id="heading-3-combining-hashmap-and-list-for-enhanced-functionality">3. <strong>Combining HashMap and List for Enhanced Functionality</strong></h4>
<p>In many cases, using both <code>HashMap</code> and <code>ArrayList</code> together can provide powerful solutions:</p>
<ul>
<li><p><strong>Data Organization:</strong> For instance, you could use a <code>HashMap</code> to map categories of products to an <code>ArrayList</code> of products within each category. This combination allows for fast access to all products in a specific category while keeping the product list dynamic.</p>
</li>
<li><p><strong>Example Scenario:</strong> In a social media app, you might store user posts in an <code>ArrayList</code> and use a <code>HashMap</code> to keep track of likes or comments associated with each post. This way, you can efficiently manage and display interactions with posts.</p>
</li>
</ul>
<h4 id="heading-4-problem-solving-and-code-quality">4. <strong>Problem-Solving and Code Quality</strong></h4>
<p>Understanding how to use <code>HashMap</code> and <code>ArrayList</code> improves problem-solving skills and enhances code quality.</p>
<ul>
<li><strong>Clearer Code:</strong> Using appropriate data structures makes code easier to read and maintain. For example, if you use a <code>HashMap</code> to store user settings, other developers can quickly understand how to access and modify those settings.</li>
</ul>
<p>Mastering data structures like <code>HashMap</code> and <code>List</code> (ArrayList) is essential for anyone studying Android development with Java. These data structures enhance data management, improve application performance, and foster better coding practices. By investing time in understanding how to use <code>HashMap</code> and <code>ArrayList</code>, you will be better equipped to build efficient and robust Android applications.</p>
<h2 id="heading-example-of-list-data-structure-in-android">Example of LIST Data Structure in Android:</h2>
<p>XML:</p>
<pre><code class="lang-java">&lt;?xml version=<span class="hljs-string">"1.0"</span> encoding=<span class="hljs-string">"utf-8"</span>?&gt;
&lt;RelativeLayout xmlns:android=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
    xmlns:app=<span class="hljs-string">"http://schemas.android.com/apk/res-auto"</span>
    xmlns:tools=<span class="hljs-string">"http://schemas.android.com/tools"</span>
    android:id=<span class="hljs-string">"@+id/main"</span>
    android:layout_width=<span class="hljs-string">"match_parent"</span>
    android:layout_height=<span class="hljs-string">"match_parent"</span>
    android:background=<span class="hljs-string">"@color/white"</span>
    android:padding=<span class="hljs-string">"20dp"</span>
    tools:context=<span class="hljs-string">".datastructure.list.ImplementList"</span>&gt;


    &lt;LinearLayout
        android:layout_width=<span class="hljs-string">"match_parent"</span>
        android:layout_height=<span class="hljs-string">"match_parent"</span>
        android:orientation=<span class="hljs-string">"vertical"</span>
        &gt;

        &lt;TextView
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"SHOPPING PAGE"</span>
            android:textSize=<span class="hljs-string">"20sp"</span>
            /&gt;


        &lt;EditText
            android:id=<span class="hljs-string">"@+id/et_items"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:padding=<span class="hljs-string">"10dp"</span>
            android:background=<span class="hljs-string">"@android:color/darker_gray"</span>
            android:hint=<span class="hljs-string">"Enter Items"</span>
            android:textColor=<span class="hljs-string">"@color/white"</span>
            android:textColorHint=<span class="hljs-string">"@color/white"</span>
            android:textSize=<span class="hljs-string">"30sp"</span>
            android:layout_marginVertical=<span class="hljs-string">"20dp"</span>
            /&gt;

        &lt;Button
            android:id=<span class="hljs-string">"@+id/btn_submit"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"SUBMIT"</span>
            /&gt;

        &lt;Button
            android:id=<span class="hljs-string">"@+id/btn_all"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"DELETE ALL"</span>
            /&gt;


        &lt;TextView
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"Check logs for output"</span>
            android:textSize=<span class="hljs-string">"20sp"</span>
            android:layout_marginVertical=<span class="hljs-string">"20dp"</span>
            /&gt;

        &lt;TextView
            android:id=<span class="hljs-string">"@+id/txt_output"</span>
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"Output"</span>
            android:textSize=<span class="hljs-string">"30sp"</span>
            android:layout_marginVertical=<span class="hljs-string">"20dp"</span>
            /&gt;



        &lt;TextView
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"Type index to delete"</span>
            android:textSize=<span class="hljs-string">"20sp"</span>
            android:layout_marginVertical=<span class="hljs-string">"20dp"</span>
            /&gt;


        &lt;EditText
            android:id=<span class="hljs-string">"@+id/et_delete_item"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:padding=<span class="hljs-string">"10dp"</span>
            android:background=<span class="hljs-string">"@android:color/darker_gray"</span>
            android:hint=<span class="hljs-string">"Enter Index"</span>
            android:textColor=<span class="hljs-string">"@color/white"</span>
            android:textColorHint=<span class="hljs-string">"@color/white"</span>
            android:textSize=<span class="hljs-string">"30sp"</span>
            android:inputType=<span class="hljs-string">"number"</span>
            android:layout_marginVertical=<span class="hljs-string">"20dp"</span>
            /&gt;

        &lt;Button
        android:id=<span class="hljs-string">"@+id/btn_delete_index"</span>
        android:layout_width=<span class="hljs-string">"match_parent"</span>
        android:layout_height=<span class="hljs-string">"wrap_content"</span>
        android:text=<span class="hljs-string">"SUBMIT"</span>
        /&gt;


    &lt;/LinearLayout&gt;






&lt;/RelativeLayout&gt;
</code></pre>
<h2 id="heading-java">JAVA</h2>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.thirdygayares.android_java_datastructures.datastructure.list;

<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.util.Log;
<span class="hljs-keyword">import</span> android.widget.Button;
<span class="hljs-keyword">import</span> android.widget.EditText;
<span class="hljs-keyword">import</span> android.widget.TextView;
<span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;
<span class="hljs-keyword">import</span> com.thirdygayares.android_java_datastructures.R;

<span class="hljs-keyword">import</span> java.util.ArrayList;
<span class="hljs-keyword">import</span> java.util.List;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImplementList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> </span>{

    EditText etItems, etDeleteItem;
    Button btnSubmit, btnDeleteAll, btnDeleteIndex;
    TextView txt_output;

    List&lt;String&gt; shopping_list = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);

        setContentView(R.layout.activity_implement_list);

        etItems = findViewById(R.id.et_items);
        btnSubmit = findViewById(R.id.btn_submit);
        txt_output = findViewById(R.id.txt_output);
        btnDeleteAll = findViewById(R.id.btn_all);
        etDeleteItem = findViewById(R.id.et_delete_item);
        btnDeleteIndex = findViewById(R.id.btn_delete_index);

        btnSubmit.setOnClickListener(v -&gt; {
            String items = etItems.getText().toString();
            shopping_list.add(items);
            Log.d(<span class="hljs-string">"TAG"</span>, <span class="hljs-string">"My Shoppint list is"</span> + shopping_list);

            txt_output.setText(<span class="hljs-string">"My Shoppint list is"</span> + shopping_list);
        });

        btnDeleteAll.setOnClickListener(v -&gt; {
            shopping_list.clear();
            Log.d(<span class="hljs-string">"TAG"</span>, <span class="hljs-string">"My Shoppint list is"</span> + shopping_list);
            txt_output.setText(<span class="hljs-string">"My Shoppint list is"</span> + shopping_list);
        });

        <span class="hljs-comment">//remove item</span>
        btnDeleteIndex.setOnClickListener(v -&gt; {
            <span class="hljs-keyword">int</span> item = Integer.parseInt(etDeleteItem.getText().toString());
            shopping_list.remove(item);
            Log.d(<span class="hljs-string">"TAG"</span>, <span class="hljs-string">"My shopping List after deleting are"</span> + shopping_list);
            txt_output.setText(<span class="hljs-string">"My Shoppint list is"</span> + shopping_list);
        });

    }
}
</code></pre>
<ul>
<li><p><strong>Use of</strong> <code>ArrayList</code>:</p>
<ul>
<li>The app uses an <code>ArrayList&lt;String&gt;</code> named <code>shopping_list</code> to store the names of items added by the user. An <code>ArrayList</code> is a resizable array implementation that allows dynamic storage of elements.</li>
</ul>
</li>
<li><p><strong>Dynamic Sizing</strong>:</p>
<ul>
<li>As users add items to the shopping list using the <code>etItems</code> input field and the submit button, the <code>ArrayList</code> automatically resizes to accommodate new entries. This is a key advantage of using an <code>ArrayList</code> over a traditional array, which has a fixed size.</li>
</ul>
</li>
<li><p><strong>Efficient Access and Modification</strong>:</p>
<ul>
<li><p>The app allows for efficient access to the list of items. When an item is added, it is appended to the end of the list with <code>shopping_list.add(items);</code>. This operation is generally O(1) (constant time), making it efficient.</p>
</li>
<li><p>The app also enables deletion of items. When a user specifies an index in <code>etDeleteItem</code>, the corresponding item is removed from the list using <code>shopping_list.remove(item);</code>. This operation takes O(n) time in the worst case, as it may involve shifting elements in the array, but it is still manageable for small to moderate lists.</p>
</li>
</ul>
</li>
<li><p><strong>Displaying the List</strong>:</p>
<ul>
<li>The current contents of the <code>ArrayList</code> are displayed in a <code>TextView</code> (<code>txt_output</code>). Whenever the list is modified (an item is added or removed), the output is updated to reflect the current state of the <code>shopping_list</code>. This direct feedback helps users understand the changes they are making.</li>
</ul>
</li>
<li><p><strong>Clear Functionality</strong>:</p>
<ul>
<li>The app provides a method to clear the entire list with the <code>btnDeleteAll</code> button. This is achieved using <code>shopping_list.clear();</code>, which removes all elements efficiently.</li>
</ul>
</li>
</ul>
<p><strong>OUTPUT</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728031807135/e546faed-d915-4b17-abc5-a8c1cc874bad.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-example-of-hashmap-data-structure-in-android">Example of HASHMAP Data Structure in Android:</h2>
<h2 id="heading-xml-code">XML CODE</h2>
<pre><code class="lang-java">&lt;?xml version=<span class="hljs-string">"1.0"</span> encoding=<span class="hljs-string">"utf-8"</span>?&gt;
&lt;RelativeLayout xmlns:android=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
    xmlns:app=<span class="hljs-string">"http://schemas.android.com/apk/res-auto"</span>
    xmlns:tools=<span class="hljs-string">"http://schemas.android.com/tools"</span>
    android:id=<span class="hljs-string">"@+id/main"</span>
    android:layout_width=<span class="hljs-string">"match_parent"</span>
    android:layout_height=<span class="hljs-string">"match_parent"</span>
    tools:context=<span class="hljs-string">".datastructure.hashmap.ImplementHashmap"</span>&gt;

    &lt;LinearLayout
        android:layout_width=<span class="hljs-string">"match_parent"</span>
        android:layout_height=<span class="hljs-string">"match_parent"</span>
        android:orientation=<span class="hljs-string">"vertical"</span>
        &gt;

        &lt;TextView
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"Student Data"</span>
            android:textSize=<span class="hljs-string">"20sp"</span>
          /&gt;

        &lt;EditText
            android:id=<span class="hljs-string">"@+id/et_student_id"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:background=<span class="hljs-string">"@color/white"</span>
            android:padding=<span class="hljs-string">"20dp"</span>
            android:layout_margin=<span class="hljs-string">"10dp"</span>
            android:hint=<span class="hljs-string">"Enter Student ID"</span>
            /&gt;



        &lt;EditText
            android:id=<span class="hljs-string">"@+id/et_student_name"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:background=<span class="hljs-string">"@color/white"</span>
            android:padding=<span class="hljs-string">"20dp"</span>
            android:layout_margin=<span class="hljs-string">"10dp"</span>
            android:hint=<span class="hljs-string">"Enter Student Name"</span>
            /&gt;

        &lt;EditText
            android:id=<span class="hljs-string">"@+id/et_course"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:background=<span class="hljs-string">"@color/white"</span>
            android:padding=<span class="hljs-string">"20dp"</span>
            android:layout_margin=<span class="hljs-string">"10dp"</span>
            android:hint=<span class="hljs-string">"Enter Course"</span>
            /&gt;

        &lt;EditText
            android:id=<span class="hljs-string">"@+id/et_section"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:background=<span class="hljs-string">"@color/white"</span>
            android:padding=<span class="hljs-string">"20dp"</span>
            android:layout_margin=<span class="hljs-string">"10dp"</span>
            android:hint=<span class="hljs-string">"Enter Section"</span>
            /&gt;

        &lt;Button
            android:id=<span class="hljs-string">"@+id/btn_submit"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:padding=<span class="hljs-string">"20dp"</span>
            android:text=<span class="hljs-string">"SUBMIT"</span>
            /&gt;

        &lt;TextView
            android:id=<span class="hljs-string">"@+id/txt_output"</span>
            android:layout_width=<span class="hljs-string">"match_parent"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:text=<span class="hljs-string">"Output"</span>
            android:textSize=<span class="hljs-string">"30sp"</span>
            android:padding=<span class="hljs-string">"20dp"</span>
            android:layout_margin=<span class="hljs-string">"10dp"</span>

            /&gt;


    &lt;/LinearLayout&gt;


&lt;/RelativeLayout&gt;
</code></pre>
<h2 id="heading-ia"> </h2>
<p>JAVA CODE</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.thirdygayares.android_java_datastructures.datastructure.hashmap;

<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.util.Log;
<span class="hljs-keyword">import</span> android.widget.Button;
<span class="hljs-keyword">import</span> android.widget.EditText;
<span class="hljs-keyword">import</span> android.widget.TextView;

<span class="hljs-keyword">import</span> androidx.activity.EdgeToEdge;
<span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;
<span class="hljs-keyword">import</span> androidx.core.graphics.Insets;
<span class="hljs-keyword">import</span> androidx.core.view.ViewCompat;
<span class="hljs-keyword">import</span> androidx.core.view.WindowInsetsCompat;

<span class="hljs-keyword">import</span> com.thirdygayares.android_java_datastructures.R;

<span class="hljs-keyword">import</span> java.util.HashMap;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImplementHashmap</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> </span>{

    EditText etStudentId, etStudentName, etCourse, etSection;
    Button btnSubmit;
    TextView txtOutput;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_implement_hashmap);
        etStudentId= findViewById(R.id.et_student_id);
        etStudentName= findViewById(R.id.et_student_name);
        etCourse = findViewById(R.id.et_course);
        etSection = findViewById(R.id.et_section);
        btnSubmit = findViewById(R.id.btn_submit);
        txtOutput = findViewById(R.id.txt_output);

        HashMap&lt;String, String&gt; student_hashmap = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();

        btnSubmit.setOnClickListener(v-&gt;{

            String studentId = etStudentId.getText().toString();
            String studentName = etStudentName.getText().toString();
            String course = etCourse.getText().toString();
            String section = etSection.getText().toString();

            student_hashmap.put(<span class="hljs-string">"id"</span>, studentId);
            student_hashmap.put(<span class="hljs-string">"name"</span>, studentName);
            student_hashmap.put(<span class="hljs-string">"course"</span>, course);
            student_hashmap.put(<span class="hljs-string">"section"</span>, section);

            Log.d(<span class="hljs-string">"TAG"</span>, <span class="hljs-string">"Student Data"</span> + student_hashmap);
            txtOutput.setText(<span class="hljs-string">"Student Data"</span> + student_hashmap);
        });

    }
}
</code></pre>
<ol>
<li><p><strong>Use of</strong> <code>HashMap</code>:</p>
<ul>
<li>The application utilizes a <code>HashMap&lt;String, String&gt;</code> called <code>student_hashmap</code> to store student details. In this context, the keys are strings that represent different attributes (like "id", "name", "course", and "section"), and the values are the corresponding data entered by the user.</li>
</ul>
</li>
<li><p><strong>Key-Value Pairs</strong>:</p>
<ul>
<li><p>Each student's information is stored as key-value pairs:</p>
<ul>
<li><p><code>"id"</code> → Student's ID</p>
</li>
<li><p><code>"name"</code> → Student's Name</p>
</li>
<li><p><code>"course"</code> → Student's Course</p>
</li>
<li><p><code>"section"</code> → Student's Section</p>
</li>
</ul>
</li>
<li><p>This structure allows for easy retrieval of student information based on specific attributes.</p>
</li>
</ul>
</li>
<li><p><strong>Dynamic Data Storage</strong>:</p>
<ul>
<li>The <code>HashMap</code> allows the application to store multiple students’ data dynamically. Each time the user submits the form, the latest student details are added to the <code>student_hashmap</code>.</li>
</ul>
</li>
<li><p><strong>Efficient Data Access</strong>:</p>
<ul>
<li><code>HashMap</code> provides constant time complexity (O(1)) for retrieval operations on average. This means that once data is stored, it can be accessed very quickly based on its key.</li>
</ul>
</li>
<li><p><strong>Logging and Display</strong>:</p>
<ul>
<li>When the user clicks the submit button, the application logs the current state of the <code>student_hashmap</code> using <code>Log.d</code>, and it updates the <code>TextView</code> (<code>txtOutput</code>) to display the stored student data. This helps in providing instant feedback to the user about the information they just entered.</li>
</ul>
</li>
</ol>
<h3 id="heading-benefits-of-using-hashmap-in-this-app">Benefits of Using <code>HashMap</code> in This App</h3>
<ul>
<li><p><strong>Fast Lookups</strong>: Using a <code>HashMap</code> allows for quick retrieval of student data based on keys. This is particularly useful when you need to access specific information about a student efficiently.</p>
</li>
<li><p><strong>Ease of Use</strong>: The <code>HashMap</code> API is straightforward, allowing developers to easily add and retrieve entries without worrying about the underlying complexities of data storage.</p>
</li>
<li><p><strong>Flexibility</strong>: A <code>HashMap</code> can dynamically resize as needed, making it suitable for applications that require adding or updating data frequently.</p>
</li>
<li><p><strong>Organized Data</strong>: By using keys for different attributes, the data is well-organized and easily understandable. This structure makes it clear what each piece of data represents.</p>
</li>
</ul>
<h3 id="heading-example-scenario">Example Scenario</h3>
<p>If a user enters:</p>
<ul>
<li><p>Student ID: "123"</p>
</li>
<li><p>Student Name: "John Doe"</p>
</li>
<li><p>Course: "Computer Science"</p>
</li>
<li><p>Section: "A"</p>
</li>
</ul>
<p>The <code>student_hashmap</code> would then store:</p>
<pre><code class="lang-java">{
  <span class="hljs-string">"id"</span> : <span class="hljs-string">"1"</span>,
  <span class="hljs-string">"name"</span> : <span class="hljs-string">"Thirdy"</span>,
  <span class="hljs-string">"course"</span> : <span class="hljs-string">"Computer Science"</span>,
  <span class="hljs-string">"section"</span> : <span class="hljs-string">"CDS"</span>
}
</code></pre>
<p>the <code>HashMap</code> is effectively used in this Android application to manage and store student information.</p>
<p>Its ability to handle key-value pairs allows for organized and efficient data management, which enhances the application's functionality and user experience. Understanding how to use <code>HashMap</code> is essential for developing applications that require quick access to various attributes of complex data objects.</p>
<h2 id="heading-hashmap-output"><strong>HASHMAP OUTPUT</strong></h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728032076367/c6dafabe-aeb2-4aa2-870b-fd69af02fef0.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-references-for-further-reading">References for Further Reading</h3>
<ol>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/java-util-hashmap-in-java-with-examples/#:~:text=HashMap%20in%20Java%20stores%20the%20data#:~:text=HashMap%20in%20Java%20stores%20the%20data">HashMap in Java - GeeksforGeeks</a></p>
</li>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/java-util-hashmap-in-java-with-examples/#:~:text=HashMap%20in%20Java%20stores%20the%20data#:~:text=HashMap%20in%20Java%20stores%20the%20data"><strong>GeeksforGeeks: ArrayList in</strong></a> <strong>Java</strong><br /> GeeksforGeeks - ArrayList</p>
</li>
<li><p><strong>Android Developers: Data Storage</strong><br /> <a target="_blank" href="https://developer.android.com/training/data-storage">Android Developers</a></p>
</li>
</ol>
]]></content:encoded></item></channel></rss>