This repository contains the source code for a Book Store application. The application is built with .NET Core for the backend and React for the frontend. It uses JWT for authentication and authorization, and all communications are secured using HTTPS with self-signed certificates.
Prerequisites Setup Backend Setup Frontend Setup Configuration Usage Project Structure API Endpoints JWT Authentication HTTPS Configuration CORS Error Handling Logging
.NET Core SDK Node.js and npm MySQL A Web browser
git clone https://github.com/DouglasHutchful1/Simple-Book-Store.git
cd book-store-app/BookStoreApi Install .NET dependencies: dotnet restore
Ensure you have MySQL running and create a database named BookStore. Update the connection string in appsettings.json as needed.
dotnet run
It will start the backend server on https://localhost:5001.
Navigate to the root directory:
npm install Start the React application:
npm start It will start on http://localhost:3000.
Backend Configuration The backend configuration is primarily located in appsettings.json. Key configurations include:
Update the ConnectionStrings:DefaultConnection with your MySQL database details.
Update the Jwt section with your desired key, issuer, and audience.
Ensure your self-signed certificate details are correctly set up.
Running the Application Backend: Run dotnet run in the BookStoreApi directory. Frontend: Run npm start in the root directory.
To add a book, navigate to the add book page on the frontend and fill out the form. The form will make a POST request to the backend to save the book details in the database.
To update a book, click on the update button next to a book item on the dashboard. This will navigate to an update form pre-filled with the book details.
To delete a book, click on the delete button next to a book item on the dashboard. This will send a DELETE request to the backend to remove the book from the database.
Backend Program.cs: Main entry point for the application. Controllers/: Contains the API controllers. Data/: Contains the DbContext and database models. appsettings.json: Configuration file for the application. Frontend src/: Contains the React source code. src/components/: Contains React components. src/pages/: Contains page components. src/styles/: Contains CSS files. src/App.js: Main React component. API Endpoints Books GET /api/books: Retrieve all books. POST /api/books: Add a new book. PUT /api/books/{id}: Update an existing book. DELETE /api/books/{id}: Delete a book. Authentication POST /api/auth/login: Authenticate a user and return a JWT token. JWT Authentication The application uses JWT (JSON Web Token) for authentication and authorization. The token is generated upon user login and must be included in the Authorization header for protected API requests.
POST /api/auth/login Content-Type: application/json
{ "username": "doug", "password": "doug01" }
GET /api/books Authorization: Bearer
The backend server is configured to use HTTPS with a self-signed certificate. Ensure your local environment trusts the self-signed certificate.
Run the following command to trust the self-signed certificate: dotnet dev-certs https --trust
CORS (Cross-Origin Resource Sharing) is configured to allow requests from http://localhost:3000. This configuration is set in the Program.cs file.
The backend application includes basic error handling mechanisms. Unhandled exceptions are logged and appropriate HTTP responses are returned to the client.