Web Application Development Tutorial - Part 3: Creating, Updating and Deleting Books
About This Tutorial
In this tutorial series, you will build an ABP based web application named Acme.BookStore
. This application is used to manage a list of books and their authors. It is developed using the following technologies:
- Entity Framework Core as the ORM provider.
- Angular as the UI Framework.
This tutorial is organized as the following parts;
- Part 1: Creating the server side
- Part 2: The book list page
- Part 3: Creating, updating and deleting books (this part)
- Part 4: Integration tests
- Part 5: Authorization
- Part 6: Authors: Domain layer
- Part 7: Authors: Database Integration
- Part 8: Authors: Application Layer
- Part 9: Authors: User Interface
- Part 10: Book to Author Relation
Download the Source Code
This tutorial has multiple versions based on your UI and Database preferences. We've prepared a few combinations of the source code to be downloaded:
If you encounter the "filename too long" or "unzip error" on Windows, it's probably related to the Windows maximum file path limitation. Windows has a maximum file path limitation of 250 characters. To solve this, enable the long path option in Windows 10.
If you face long path errors related to Git, try the following command to enable long paths in Windows. See https://github.com/msysgit/msysgit/wiki/Git-cannot-create-a-file-or-directory-with-a-long-path
git config --system core.longpaths true
Creating a New Book
In this section, you will learn how to create a new modal dialog form to create a new book.
BookComponent
Open /src/app/book/book.component.ts
and replace the content as below:
- We defined a property called
isModalOpen
and a method calledcreateBook
.
Open /src/app/book/book.component.html
and make the following changes:
- Added
New book
button to the card header.. - Added the
abp-modal
which renders a modal to allow user to create a new book.abp-modal
is a pre-built component to show modals. While you could use another approach to show a modal,abp-modal
provides additional benefits.
You can open your browser and click New book button to see the new modal.
Create a Reactive Form
Reactive forms provide a model-driven approach to handling form inputs whose values change over time.
Open /src/app/book/book.component.ts
and replace the content as below:
- Imported
FormGroup
,FormBuilder
andValidators
from@angular/forms
. - Added
form: FormGroup
property. - Added
bookTypes
property as a list ofBookType
enum members. That will be used in form options. - Injected
FormBuilder
into the constructor. FormBuilder provides convenient methods for generating form controls. It reduces the amount of boilerplate needed to build complex forms. - Added
buildForm
method to the end of the file and executed thebuildForm()
in thecreateBook
method. - Added
save
method.
Open /src/app/book/book.component.html
and replace <ng-template #abpBody> </ng-template>
with the following code part:
Also replace <ng-template #abpFooter> </ng-template>
with the following code part:
Datepicker
We've used NgBootstrap datepicker in this component. So, need to arrange dependencies related to this component.
Open /src/app/book/book.module.ts
and replace the content as below:
- We imported
NgbDatepickerModule
to be able to use the date picker.
Open /src/app/book/book.component.ts
and replace the content as below:
- Imported
NgbDateNativeAdapter
andNgbDateAdapter
. - We added a new provider
NgbDateAdapter
that converts Datepicker value toDate
type. See the datepicker adapters for more details.
Now, you can open your browser to see the changes:
Updating a Book
Open /src/app/book/book.component.ts
and replace the content as shown below:
- We declared a variable named
selectedBook
asBookDto
. - We added
editBook
method. This method fetches the book with the givenid
and sets it toselectedBook
object. - We replaced the
buildForm
method so that it creates the form with theselectedBook
data. - We replaced the
createBook
method so it setsselectedBook
to an empty object. - We changed the
save
method to handle both of create and update operations.
Add "Actions" Dropdown to the Table
Open the /src/app/book/book.component.html
and add the following ngx-datatable-column
definition as the first column in the ngx-datatable
:
Added an "Actions" dropdown as the first column of the table that is shown below:
Also, change the ng-template #abpHeader
section as shown below:
This template will show Edit text for edit record operation, New Book for new record operation in the title.
Deleting a Book
Open the /src/app/book/book.component.ts
and inject the ConfirmationService
.
Replace the constructor as below:
- We imported
ConfirmationService
. - We injected
ConfirmationService
to the constructor. - Added a
delete
method.
See the Confirmation Popup documentation for more about this service.
Add a Delete Button
Open /src/app/book/book.component.html
and modify the ngbDropdownMenu
to add the delete button as shown below:
The final actions dropdown UI looks like below:
Clicking the "Delete" action calls the delete
method which then shows a confirmation popup as shown below:
The Next Part
See the next part of this tutorial.