Part 12: Creating Note C.r.u.d. — Create
Last updated
Last updated
Setting up the service is done! Now we need to knock out all of our CRUD (Create, Read, Update, Delete) actions.
Which http verb sends data to our backend server? Post!
In the note.service.ts, we will create a new method called, createNote(). The createNote() method will take in one parameter, the note, then run a http post to send the data.
Don’t forget the Authorization for the header!
Using the Angular CLI, let’s build a note-create component,
Type:
In the controller, create an instance of the NoteService and then inject it to the note-create constructor.
The purpose of this component is for users to build a new note, so they will place information within form field and then submit the data.
We will need to:
Import our form tools
Inject FormBuilder in the constructor
Create a method to create the form
Figure out what fields we need from our API Docs
Create an onSubmit() method
After a successful submission send the users back to the user index.
With our view let’s create the form users will see,
Our form mimics what we’ve created in the login and register pages.
Using the required keyword as an attribute, we can make sure that data is given— and if it’s not— don’t allow the form to be submitted.
That gives us a wall, of protection for our form! On the view side. There are multiple points we can place walls, the first line (or last line, depends on how you write your code) of defense is on the view side.
We need to add the create page to our path in the app.modules.ts file. Let’s first organize our paths for the next incoming components.
Since all of our note components share the same idea ‘notes,’ we can have child paths that will be our components. So, instead of this:
We can have this...
Makes for clearer paths as we scale to larger projects.
Now that we have this path created we can include a routerLink in our note-index.ts file where the pencil currently is located:
We should now have access to the create page:
After submitting the form it should also redirect us back to the index page.