<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Utkal</title>
    <description>The latest articles on DEV Community by Utkal (@utkal97).</description>
    <link>https://dev.to/utkal97</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F391586%2F0414a550-5c6f-40f1-8d0f-7166f720e69e.jpeg</url>
      <title>DEV Community: Utkal</title>
      <link>https://dev.to/utkal97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/utkal97"/>
    <language>en</language>
    <item>
      <title>Redux: Switching to other case within the same reducer</title>
      <dc:creator>Utkal</dc:creator>
      <pubDate>Sat, 12 Dec 2020 12:55:46 +0000</pubDate>
      <link>https://dev.to/utkal97/redux-switching-to-other-case-within-the-same-reducer-17bd</link>
      <guid>https://dev.to/utkal97/redux-switching-to-other-case-within-the-same-reducer-17bd</guid>
      <description>&lt;p&gt;I am new to Redux. I am building a game with it. I know that reducers are there to react to actions thrown at them, by changing the state. In my reducer, I have 2 cases:&lt;br&gt;
1) a switch case that needs to change the protagonist's state according to the data entry.&lt;br&gt;
2) an error switch case, that sets the state's error attribute.&lt;/p&gt;

&lt;p&gt;`export const movementReducer = (state=initial_state, action) =&amp;gt; {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(action.type) {
    case "moveObject": {
         const new_position = ChangePosition(action.payload);
         return {
             ...state,
             position: new_position
         };
    }
    case "moveObjectError": {
         return {
             ...state,
             error: action.payload
         };
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;What I want:-&lt;br&gt;
When the control comes to "moveObject" case, it must first verify if this state change is feasible. If it is feasible, it must update. If it is not feasible, it must switch to the second case and set the Error.&lt;/p&gt;

&lt;p&gt;What I tried:-&lt;br&gt;
1) I believe this is a hack, makes the code not look good:-&lt;br&gt;
If state change is not feasible for given input, I implicitly set the error and return it from the same case. The code looks like this:-&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
export const movementReducer = (state=initial_state, action) =&amp;gt; {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(action.type) {
    case "moveObject": {
         if( !isMovementFeasible(state, action.payload)) {
              return {
                 ...state,
                 error: "This movement is not possible."
             };
         else {
             const new_position= ChangePosition(action.payload);
             return {
                 ...state,
                 position: new_position
             };
         }
    }
    case "moveObjectError": {
         return {
             ...state,
             error: action.payload
         };
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;This will work fine. But this did not actually trigger the "moveObjectError" action, which will be bad for tracking redux in future.&lt;/p&gt;

&lt;p&gt;2) I can create an action creator, in it I can check for feasibility of movement. But then, I would have to access the state in action creator. I think this is not advised.&lt;/p&gt;

&lt;p&gt;I want a way to trigger the "moveObjectError" case when movement is not feasible.&lt;/p&gt;

</description>
      <category>help</category>
      <category>redux</category>
      <category>react</category>
    </item>
    <item>
      <title>Difference between Controllers, Routes and Services</title>
      <dc:creator>Utkal</dc:creator>
      <pubDate>Thu, 28 May 2020 06:52:27 +0000</pubDate>
      <link>https://dev.to/utkal97/need-help-regarding-file-structure-for-my-node-server-application-33e</link>
      <guid>https://dev.to/utkal97/need-help-regarding-file-structure-for-my-node-server-application-33e</guid>
      <description>&lt;p&gt;I am building an API service using Nodejs, Expressjs and MongoDB. Also, I am using Mongoose library to deal with database. After going through several blogs on file structuring, I am quiet confused about it. I want help with the file structuring for my project. My "src" folder structure looks like this so far :- &lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src
  |
  |__Controllers
  |      |
  |      |__ admin.js
  |      |__ customer.js
  |__Models
  |__Public
  |__Routes
  |      |
  |      |__ admin.js
  |      |__ customer.js
  |__Views
  |__Services
         |
         |__admin.services.js
         |__customer.services.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My concerns are :-&lt;br&gt;
1) Are Controllers the same as Routes? If not, should they be put in Routes folder?&lt;br&gt;&lt;br&gt;
2) How should my Service files be structured (Am I doing this right?)? Or should each API call get a different service file? &lt;br&gt;&lt;br&gt;
3) Since routes are handling request and response objects, what do we use controllers for? Can we just call "services" from routes and respond from each API route, or am I leaving something behind?&lt;br&gt;&lt;/p&gt;

</description>
      <category>help</category>
      <category>express</category>
      <category>node</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
