A comprehensive ASP.NET Core Web API for managing food donations and reducing waste by connecting food donors with recipients.
- Features
- Technologies Used
- Prerequisites
- Installation
- Configuration
- Database Setup
- Running the Application
- API Documentation
- Project Structure
- Authentication
- Real-time Notifications
- Contributing
- License
- User Management: Registration, login, profile management with JWT authentication
- Google OAuth Integration: Sign in with Google support
- Post Management: Create, update, delete, and browse food donation posts
- Claiming System: Users can claim available food posts
- Bookmarking: Save favorite posts for later
- Real-time Notifications: SignalR-powered instant notifications for claims and updates
- Dashboard Analytics: Track donations, claims, and impact metrics
- Image Upload: Support for food item images with file storage
- Search & Filter: Advanced search by category, location, and expiry date
- JWT token-based authentication
- Refresh token mechanism
- Role-based authorization
- Secure password hashing with ASP.NET Identity
- CORS configuration
- Framework: ASP.NET Core 9.0
- Database: SQL Server with Entity Framework Core 9.0
- Authentication: ASP.NET Core Identity, JWT Bearer tokens
- OAuth: Google Sign-In integration
- Real-time Communication: SignalR
- API Documentation: Swagger/OpenAPI with Scalar UI
- Architecture Patterns: Repository Pattern, Unit of Work Pattern
- Image Storage: File system storage service
- Phone Validation: libphonenumber-csharp
Before you begin, ensure you have the following installed:
- .NET 9.0 SDK
- SQL Server (Express or higher)
- Visual Studio 2022 or VS Code
- Git
git clone https://github.com/yourusername/waster-api.git
cd waster-apidotnet restoreCreate an appsettings.json file in the root directory with the following structure:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Jwt": {
"Key": "YourSuperSecretKeyThatIsAtLeast32CharactersLong!",
"Issuer": "WasterAPI",
"Audience": "WasterClient",
"DurationInDays": 30
},
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=WasterDB;Trusted_Connection=True;TrustServerCertificate=True;"
},
"Authentication": {
"Google": {
"ClientId": "your-google-client-id.apps.googleusercontent.com",
"ClientSecret": "your-google-client-secret"
}
},
"AllowedOrigins": [
"http://localhost:3000",
"http://localhost:4200"
]
}Update the DefaultConnection in appsettings.json with your SQL Server details:
"ConnectionStrings": {
"DefaultConnection": "Server=YOUR_SERVER;Database=WasterDB;User Id=YOUR_USER;Password=YOUR_PASSWORD;TrustServerCertificate=True;"
}To enable Google Sign-In:
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URIs:
https://localhost:5001/signin-google - Copy Client ID and Client Secret to
appsettings.json
dotnet ef database updateYou can add roles and test users manually through the API endpoints or by creating a seed method.
dotnet runOr press F5 in Visual Studio.
The API will be available at:
- HTTPS:
https://localhost:5001 - HTTP:
http://localhost:5000
Navigate to:
- Scalar UI:
https://localhost:5001/scalar/v1 - Swagger UI:
https://localhost:5001/swagger
POST /api/Authentication/Register- Register new userPOST /api/Authentication/Login- Login with credentialsPOST /api/Authentication/RefreshToken- Refresh access tokenPOST /api/Authentication/RevokeToken- Revoke refresh tokenPOST /api/GoogleAuth/google-signin- Sign in with Google
GET /api/Account/me- Get current user profilePUT /api/Account/update-name- Update user namePUT /api/Account/update-Location- Update addressPUT /api/Account/update-PhoneNumber- Update phone numberPOST /api/Account/change-password- Change passwordPOST /api/Account/change-email- Change emailDELETE /api/Account/Delete-Account- Delete account
POST /api/Post/Create-Post- Create new food postPUT /api/Post/Edit-post- Update existing postDELETE /api/Post/Delete-Post- Delete post
GET /api/Browse/feed- Get random feed of available postsGET /api/Browse/expiring-soon- Get posts expiring soonGET /api/Browse/search- Search posts with filtersGET /api/Browse/categories- Get available categories
POST /api/ClaimPost/post/{postId}- Claim a postGET /api/ClaimPost/my-claims- Get user's claimsGET /api/ClaimPost/post/{postId}/claims- Get claims for a post (owner only)PUT /api/ClaimPost/{claimId}/approve- Approve claimPUT /api/ClaimPost/{claimId}/reject- Reject claimPUT /api/ClaimPost/{claimId}/complete- Mark claim as completedDELETE /api/ClaimPost/{claimId}/cancel- Cancel claim
GET /api/BookMarks- Get user's bookmarksPOST /api/BookMarks/{postId}- Bookmark a postDELETE /api/BookMarks/{postId}- Remove bookmarkGET /api/BookMarks/check/{postId}- Check if post is bookmarked
GET /api/Notifications- Get all notificationsGET /api/Notifications/unread-count- Get unread countPUT /api/Notifications/{id}/mark-read- Mark as readPUT /api/Notifications/mark-all-read- Mark all as read
GET /api/Dashboard- Get dashboard statisticsGET /api/Dashboard/my-stats- Get personal statisticsGET /api/Dashboard/categories- Get category statistics
Connection: /notificationHub
Connect to receive real-time notifications for claims and updates.
Waster/
βββ Controllers/ # API Controllers
βββ Models/ # Data models and entities
β βββ DbModels/ # Database models
βββ DTOs/ # Data Transfer Objects
βββ Services/ # Business logic services
βββ Helpers/ # Helper classes and extensions
βββ Hubs/ # SignalR hubs
βββ Migrations/ # EF Core migrations
βββ wwwroot/ # Static files
β βββ uploads/ # Uploaded images
βββ Program.cs # Application entry point
βββ AppDbContext.cs # Database context
βββ appsettings.json # Configuration
The API uses JWT tokens for authentication. Include the token in the Authorization header:
Authorization: Bearer <your-token>
- Login/Register: Receive access token and refresh token
- Access Token: Valid for 30 days (configurable)
- Refresh Token: Valid for 7 days, used to get new access token
- Token Refresh: Use
/api/Authentication/RefreshTokento get new tokens - Logout: Revoke refresh token using
/api/Authentication/RevokeToken
The application uses SignalR for real-time notifications:
- Connect to
/notificationHubwith authentication - Listen for
ReceiveNotificationevents - Receive instant updates for:
- New claims on your posts
- Claim approvals
- Claim rejections
- Abdelrahman Adel
- Abdelrahman Adel