Introduction
In my last
article, we discussed the basics of ASP.NET Core APIs and how to create an API using Entity Framework. Finally, we ended up with creating a Get API which is used to fetch the details of all the authors from the back-end as a list. Now, in this article, we are going to see how to create a CRUD (Create, Read, Update, Delete) API in ASP.NET Core.
This is a continuation of my last
article (create RESTful API using ASP.NET Core with Entity Framework Core), so I highly recommend you to go through my previous article for the basic set up of an ASP.NET Core application.
CREATE
I’m using my existing application that we created in my last article for a demo. Open the LibraryRepository.cs file from the application and write the below code.
- public Author PostAuthor(Author author)
- {
- try
- {
- if(_libraryContext!=null)
- {
- _libraryContext.Add(author);
- _libraryContext.SaveChanges();
- return author;
- }
- else
- {
- return null;
- }
- }
- catch(Exception ex)
- {
-
- return null;
- }
- }
_libraryContext is our database context, the Add method is used to add the records in the entity, the SaveChanges method is used to save the changes in the database.
Open LibrariesController.cs file from the application and write this code.
- [HttpPost]
- [Route("AddAuthor")]
- public IActionResult AddAuthor([FromBody]Author authorparam)
- {
- try
- {
- if (ModelState.IsValid)
- {
- Author author = _libraryRepository.PostAuthor(authorparam);
- return Ok(author);
- }
- else
- {
- return BadRequest();
- }
- }
- catch
- {
-
- return BadRequest();
- }
- }
The AddAuthor action will fetch the data from the body of request. Once the modelstate is validated, the data will be added to the database by calling the PostAuthor method in repository.
Let’s test the API in POSTMAN.
https://localhost:44348/api/Libraries/AddAuthor
From the above image, you can notice that the data is successfully added and returned.
READ
We already discussed about GetAllAuthor() read method in my last article. Now, I’m giving a demo of the method which is used to read the data from the database based on Id.
Open the LibraryReporsitory.cs file from the application and write the following code into it.
- public Author GetAuthor(Guid authorId)
- {
- try
- {
- return _libraryContext.Authors.Where(e => e.AuthorId == authorId).FirstOrDefault();
- }
- catch(Exception ex)
- {
-
- return null;
- }
- }
Open the LibrariesController.cs file from the application to add the below code.
- [HttpGet]
- [Route("GetAuthor")]
-
- public IActionResult GetAuthor(Guid authorId)
- {
- try
- {
- Author author = _libraryRepository.GetAuthor(authorId);
- return Ok(author);
- }
- catch(Exception ex)
- {
-
- return BadRequest();
- }
- }
Let’s test the API in POSTMAN.
https://localhost:44348/api/Libraries/DeleteAuthor?authorId=04dcc177-9a0a-4444-4aff-08d694b67230
From the above image, you can notice the author data is returned based on the author id.
UPDATE
Open LibraryRepository.cs file and update it with the given code.
- public Author UpdateAuthor(Author author)
- {
- try
- {
- if (_libraryContext != null)
- {
- _libraryContext.Update(author);
- _libraryContext.SaveChanges();
- return author;
- }
- else
- {
- return null;
- }
- }
- catch (Exception ex)
- {
-
- return null;
- }
- }
_libraryContext is our database context, the Update method is used to update the records in the entity, the SaveChanges method is used to save the changes in the database.
Open LibrariesController.cs file from the application and write the below code.
- [HttpPut]
- [Route("UpdateAuthor")]
- public IActionResult UpdateAuthor([FromBody]Author authorparam)
- {
- try
- {
- if (ModelState.IsValid)
- {
- Author author = _libraryRepository.UpdateAuthor(authorparam);
- return Ok(author);
- }
- else
- {
-
- return BadRequest();
- }
- }
- catch(Exception ex)
- {
-
- return BadRequest();
- }
- }
The UpdateAuthor action will fetch the data from the body of request. Once the modelstate is validated, the data will be updated in database by calling the UpdateAuthor method in the repository.
Let’s test the API in POSTMAN.
https://localhost:44348/api/Libraries/UpdateAuthor
From the above image, you can notice that the author data is updated based on the request.
Changes in the database
DELETE
Open the LibraryRepository.cs file from the application and add the following code.
- public int DeleteAuthor(Guid authorId)
- {
- try
- {
- if (_libraryContext != null)
- {
- var author = _libraryContext.Authors.FirstOrDefault(x => x.AuthorId== authorId);
- if(author!=null)
- {
- _libraryContext.Remove(author);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 0;
- }
-
-
- }
- catch(Exception ex)
- {
-
- return 0;
- }
- }
The Remove method is used to remove the records in the entity, the SaveChanges method is used to save the changes in the database based on update done in data entity.
Open LibrariesController.cs file from the application and write this code.
- [HttpDelete]
- [Route("DeleteAuthor")]
- public IActionResult DeleteAuthor(Guid authorId)
- {
- try
- {
- int result = _libraryRepository.DeleteAuthor(authorId);
- return Ok(result);
- }
- catch(Exception ex)
- {
-
- return BadRequest();
- }
- }
The DeleteAuthor method will delete the record by calling the DeleterAuthor method in the repository based on the author id passed through the request.
Let’s test the API in POSTMAN.
https://localhost:44348/api/Libraries/DeleteAuthor?authorId=04dcc177-9a0a-4444-4aff-08d694b67230
From the above image, you can notice that we got a response as 1 which means the data is deleted successfully based on the request. Click
here to get the source code.
Conclusion
Today, we saw how to create a CRUD API in ASP.NET Core with Entity Framework. We will see more about ASP.NET Core in my future articles.