Prerequisites
Before setting up the project, ensure you have the following installed:.NET SDK
.NET 6.0 SDK or later
Node.js
Node.js 14.x or later with npm
SQL Server
SQL Server LocalDB or SQL Server Express
Code Editor
Visual Studio 2022, VS Code, or Rider
Project Structure
The project is organized into two main directories:Backend Setup (API)
1. Navigate to the API Directory
2. Restore NuGet Packages
- Microsoft.EntityFrameworkCore (6.0.8)
- Microsoft.EntityFrameworkCore.SqlServer (6.0.8)
- Microsoft.EntityFrameworkCore.Tools (6.0.8)
- Swashbuckle.AspNetCore (6.2.3)
3. Configure Database Connection
The default connection string is configured inappsettings.json:
appsettings.json
4. Apply Database Migrations
Create or update the database schema using Entity Framework migrations:20220816105919_initial- Creates initial Lockers table20220817035755_updateModels- Adds LockerNo and Size fields20220817035954_addEmployeeModel- Adds Employees table20221001042152_updateLockerModel- Sets LockerNo as primary key
The database will be created automatically if it doesn’t exist.
5. Run the API
Start the backend API server:- HTTPS:
https://localhost:7281 - HTTP:
http://localhost:5281 - Swagger UI:
https://localhost:7281/swagger
Verify the API is running by navigating to the Swagger UI in your browser.
Alternative: Visual Studio
If using Visual Studio 2022:- Open
Locker.API.sln - Press F5 or click “Run”
- The API will launch with Swagger UI
Frontend Setup (UI)
1. Navigate to the UI Directory
2. Install npm Dependencies
- @angular/core (^14.0.0)
- @angular/common (^14.0.0)
- @angular/forms (^14.0.0)
- @angular/router (^14.0.0)
- rxjs (~7.5.0)
3. Configure API Endpoint
The Angular service is configured to connect to the API at:lockers.service.ts
4. Run the Angular Application
Start the development server:- Local:
http://localhost:4200
The Angular app will automatically open in your browser at
http://localhost:4200Running Both Services
To run the full application, you need both the backend API and frontend UI running simultaneously.Option 1: Two Terminal Windows
Terminal 1 - Backend:Option 2: Using VS Code
- Open the workspace root in VS Code
- Use the integrated terminal to run both commands
- Split the terminal to view both outputs
Verifying the Setup
1. Test the Backend API
Open your browser and navigate to:GET /api/lockers- Get all lockersGET /api/lockers/{lockerNo}- Get a specific lockerPOST /api/lockers- Create a new lockerPUT /api/lockers/{lockerNo}- Update a lockerDELETE /api/lockers/{lockerNo}- Delete a locker
2. Test the Frontend
Navigate to:3. Test End-to-End Communication
- Open the Angular app at
http://localhost:4200 - Try creating a new locker
- Verify the data appears in the list
Common Issues and Solutions
Database Connection Failed
Database Connection Failed
Problem: Unable to connect to SQL Server LocalDBSolution:
- Verify SQL Server LocalDB is installed:
sqllocaldb info - Start LocalDB:
sqllocaldb start MSSQLLocalDB - Check connection string in
appsettings.json
CORS Error in Browser
CORS Error in Browser
Problem: Browser shows CORS policy error when calling APISolution:
- Ensure the API is running before starting the Angular app
- Verify CORS policy in
Program.csincludesAllowAnyOrigin() - Check that
app.UseCors("default")is called in the middleware pipeline
Port Already in Use
Port Already in Use
Problem: Port 7281 or 4200 is already in useSolution:
- For API: Edit
launchSettings.jsonand change theapplicationUrlports - For Angular: Run
ng serve --port 4300to use a different port - Update the API URL in
lockers.service.tsif you change the API port
Migration Failed
Migration Failed
Problem:
dotnet ef database update failsSolution:- Ensure EF Core tools are installed:
dotnet tool install --global dotnet-ef - Check that SQL Server is running
- Verify the connection string is correct
- Try creating the database manually first
npm install Errors
npm install Errors
Problem: npm install fails or shows vulnerabilitiesSolution:
- Clear npm cache:
npm cache clean --force - Delete
node_modulesandpackage-lock.json - Run
npm installagain - For vulnerabilities:
npm audit fix
Development Workflow
Once both services are running:-
Make Backend Changes:
- Edit C# files in
API/Locker/Locker.API/ - The API will automatically reload (hot reload in .NET 6)
- Refresh Swagger UI to see changes
- Edit C# files in
-
Make Frontend Changes:
- Edit TypeScript files in
UI/locker/src/ - Angular CLI will automatically recompile
- Browser will auto-refresh
- Edit TypeScript files in
-
Database Changes:
- Modify models in
Models/LockerInfo.cs - Create migration:
dotnet ef migrations add MigrationName - Apply migration:
dotnet ef database update
- Modify models in
Next Steps
Architecture
Learn about the system architecture
Database
Explore the database schema