Skip to content

A .NET Core (C#) RESTful backend service for CRUD operation using rental app as example

Notifications You must be signed in to change notification settings

aswin130/RentalApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The provided code snippets demonstrate a CRUD (Create, Read, Update, Delete) application for managing vehicles using React. Let's summarize the key aspects, including dependencies, error handling, and the overall functionality within 500 words:

Overview: The CRUD application allows users to perform various operations on a list of vehicles, including adding new vehicles, editing existing ones, and removing vehicles from the list. It utilizes React for building the user interface, React Router for handling navigation, and local storage for data persistence.

Dependencies:
  1. React: The core library for building the user interface.
  2. React Router: Used for client-side routing, enabling navigation between different views.
  3. Bootstrap CSS: Provides styling for the application components.
  4. Lodash: A utility library used for various helper functions, such as checking for empty arrays.
    Components:
  1. AppRouter: Defines the application routes using React Router. It includes routes for displaying the list of vehicles, adding a new vehicle, editing an existing vehicle, and handling redirection.
  2. VehiclesContext: A React context used for managing the state related to vehicles across multiple components. It provides access to the vehicles array and the function to update it.
  3. UseLocalStorage: A custom hook responsible for managing state persistence using local storage. It initializes state from local storage and synchronizes updates to local storage.
  4. VehiclesList: Renders the list of vehicles. It consumes the vehicles context to access the vehicles array and provides functionality to remove vehicles from the list.
  5. EditVehicle: Renders a form for editing a specific vehicle. It consumes the vehicles context to access the vehicles array and updates the corresponding vehicle when the form is submitted.
  6. VehicleForm: A reusable form component used for both adding and editing vehicles. It includes input fields for vehicle details and handles form submission.
    Functionality:
  1. Adding Vehicles: Users can add new vehicles by filling out a form with vehicle details. The submitted vehicle is added to the list of vehicles.
  2. Editing Vehicles: Users can edit existing vehicles by selecting a vehicle from the list and modifying its details in a form. The edited vehicle is updated in the list.
  3. Removing Vehicles: Users can remove vehicles from the list by selecting the delete option associated with each vehicle.
  4. Error Handling: Error handling in the application primarily focuses on handling exceptions during local storage access. If an error occurs while retrieving or parsing data from local storage, the application falls back to using the initial state.
The provided CRUD application demonstrates the use of React and React Router for building a dynamic user interface with client-side routing. It employs local storage for data persistence, allowing users to perform CRUD operations on a list of vehicles. The components are organized efficiently, utilizing React context for state management and reusable form components for adding and editing vehicles. Error handling ensures graceful degradation in case of local storage access errors, maintaining a smooth user experience. Overall, the application provides a robust foundation for managing vehicle data in a web environment.

A A .NET Core (C#) RESTful backend service for CRUD operation using rental app as example

Technologies and Packages:

  1. ASP.NET Core: It is a cross-platform, high-performance framework for building web applications using C#. It provides features like routing, middleware, and dependency injection.
  2. Entity Framework Core: It is an Object-Relational Mapping (ORM) framework provided by Microsoft. It simplifies database access and management by mapping database entities to C# objects.
  3. SQLite: It is a lightweight, file-based database engine that is used as the database provider in this project. SQLite is commonly used in development and testing scenarios due to its simplicity and portability.
  4. ASP.NET Core Identity: It is a membership system that adds authentication and authorization functionalities to ASP.NET Core applications. It provides features like user registration, login, roles, and claims management.
  5. JWT Authentication: JSON Web Token (JWT) authentication is implemented using the Microsoft.AspNetCore.Authentication.JwtBearer package. JWT is a compact, URL-safe means of representing claims to be transferred between two parties.
  6. Swagger/OpenAPI: Swagger/OpenAPI is used for API documentation. It provides a user-friendly interface for exploring and testing APIs. The Swashbuckle.AspNetCore package is used for integrating Swagger with ASP.NET Core applications.
  7. MailKit: It is a cross-platform mail-sending library for .NET applications. It is used for sending emails in this project. MailKit supports SMTP, POP3, and IMAP protocols.
  8. MimeKit: It is a .NET MIME creation and parser library. It is used in conjunction with MailKit for creating MIME messages for email communication.
  9. Microsoft.Extensions.Options: It is a package that provides support for configuring options in an ASP.NET Core application. It is used for configuring email settings in this project.
  10. Microsoft.IdentityModel.Tokens: It is a package that provides support for generating and validating JSON Web Tokens (JWT) in ASP.NET Core applications.
Endpoints

performing CRUD (Create, Read, Update, Delete) operations

Customer controller (http://localhost:5241/api/Customer

Roles Controller http://localhost:5241/api/roles By using [Authorize(Roles = "Admin")] attribute on controller actions or controllers, you ensure that only users with the specified role (in this case, "Admin") can access those endpoints. This helps enforce access control based on user roles.

Assign Roles http://localhost:5241/api/roles/assign-role-to-user

Vehicle Controller http://localhost:5241/api/vehicle

RentalOrderController http://localhost:5241/api/rentalorder

PricingController http://localhost:5241/api/pricing

LocationController http://localhost:5241/api/location)

Account Controller

Controller Setup:

The AccountController is an API controller ([ApiController]) with a base route of "api/[controller]" specified by the [Route] attribute. It injects dependencies such as UserManager, SignInManager, EmailService, and IConfiguration via constructor injection.

Register Action:

The Register action handles user registration. It creates a new IdentityUser based on the provided email and password. Upon successful registration, it generates an email verification token, constructs a verification link, and sends a verification email using the EmailService. It returns an Ok response if registration is successful, otherwise returns a BadRequest response with error details.

VerifyEmail Action:

The VerifyEmail action handles email verification. It confirms the email for the specified user using the verification token. It returns an Ok response if email verification is successful, otherwise returns a BadRequest response.

Login Action:

The Login action handles user login. It validates the user's credentials using SignInManager. Upon successful login, it generates a JWT token containing the user's email and roles, using the GenerateJwtToken method, and returns it as part of the response. It returns an Unauthorized response if login fails.

Logout Action:

The Logout action handles user logout. It signs out the current user using SignInManager and returns an Ok response.

GenerateJwtToken Method:

The GenerateJwtToken method creates a JWT token containing user claims such as email and roles. It uses a symmetric security key and specified token expiration time obtained from the configuration (IConfiguration). The token is signed using JwtSecurityTokenHandler and returned as a string.

Register the User http://localhost:5241/api/account/register

Verify the User http://localhost:5241/api/account/verify-email GET /api/account/verify-email?userId=123&token=verificationToken123 Login http://localhost:5241/api/account/login Logout http://localhost:5241/api/account/logout

DB Context

DbContext Inheritance:

  1. RentalAppContext inherits from IdentityDbContext, which means it extends the functionality provided by Entity Framework Core's DbContext class to support ASP.NET Core Identity's user management features.
  2. The RentalAppContext class includes DbSet properties for each entity in the application: Customers, Locations, Pricings, RentalOrders, and Vehicles. These properties represent collections of entities that correspond to database tables.
  3. OnModelCreating Method method overrides the default behavior for configuring the database model using Fluent API. It calls the base class method and then configures additional aspects of the model.
  4. The configurations include defining primary keys, unique indexes, and relationships between entities.
      Entity Configurations:

      Pricing Entity: Configures a one-to-many relationship between Pricing and Vehicle entities. Specifies that the PriceId property is the primary key.

      Customer Entity: Specifies a unique index on the CustomerId property to enforce uniqueness. RentalOrder Entity: Specifies a composite primary key consisting of OrderId, CustomerId, and VehicleId. Configures one-to-many relationships between RentalOrder and Customer, and between RentalOrder and Vehicle.

      ASP.NET Core controllers responsible for managing various aspects of a rental application, including customers, vehicles, pricing, orders, roles, and user authentication. Each controller implements CRUD operations, handling requests for creating, reading, updating, and deleting data. Additionally, a DbContext named RentalAppContext is discussed, configuring entity relationships, primary keys, and integrating ASP.NET Core Identity for user management. These controllers and DbContext collectively form the backend infrastructure of the rental application, ensuring efficient data management, authentication, and authorization functionalities.