চল শুরু করি ✅ Part 6: User Dashboard & API View — যেখানে ইউজার তার ইনকাম, ইনভেস্টমেন্ট, ROI, রেফার ইনকাম, উইকলি বোনাস, উইথড্র স্ট্যাটাস ইত্যাদি দেখতে পারবে একটি কমপ্যাক্ট API রেসপন্সের মাধ্যমে।
🎯 লক্ষ্য
- ড্যাশবোর্ড API: সারাংশ (summary) দেখাবে
- প্রতিটি ইনকাম সোর্স আলাদা ফিল্ডে দেখাবে
- DRF দিয়ে সম্পূর্ণ API build করবো
📦 Install Django REST Framework
requirements.txt এ যুক্ত করো:
djangorestframework>=3.15.1
ইনস্টল:
pip install djangorestframework
⚙️ settings.py এ যুক্ত করো:
INSTALLED_APPS += ['rest_framework']
📁 accounts/serializers.py
from rest_framework import serializers
from django.contrib.auth.models import User
from accounts.models import Profile
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ['sponsor', 'is_entrepreneur']
📁 dashboard/views.py
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.db.models import Sum
from investment.models import UserInvestment, ROITransaction
from referral.models import ReferralIncome
from bonus.models import WeeklyBonus, RankAchievement
from withdrawal.models import WithdrawRequest
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def user_dashboard(request):
user = request.user
total_investment = UserInvestment.objects.filter(user=user).aggregate(
total=Sum('amount'))['total'] or 0
total_roi = ROITransaction.objects.filter(user=user).aggregate(
total=Sum('amount'))['total'] or 0
total_referral_income = ReferralIncome.objects.filter(referrer=user).aggregate(
total=Sum('amount'))['total'] or 0
total_weekly_bonus = WeeklyBonus.objects.filter(user=user).aggregate(
total=Sum('bonus_amount'))['total'] or 0
total_withdrawn = WithdrawRequest.objects.filter(
user=user, status='approved').aggregate(total=Sum('net_amount'))['total'] or 0
latest_rank = RankAchievement.objects.filter(user=user).order_by('-achieved_at').first()
rank_name = latest_rank.rank_name if latest_rank else "Unranked"
return Response({
"username": user.username,
"is_entrepreneur": getattr(user.profile, 'is_entrepreneur', False),
"total_investment": total_investment,
"total_roi": total_roi,
"total_referral_income": total_referral_income,
"total_weekly_bonus": total_weekly_bonus,
"total_withdrawn": total_withdrawn,
"net_balance": total_roi + total_referral_income + total_weekly_bonus - total_withdrawn,
"current_rank": rank_name,
})
📁 dashboard/urls.py
from django.urls import path
from .views import user_dashboard
urlpatterns = [
path('dashboard/', user_dashboard, name='user-dashboard'),
]
📁 infinity_hub/urls.py এ যুক্ত করো
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('dashboard.urls')),
]
🧪 Sample API Response
GET /api/dashboard/ (Authenticated)
{
"username": "alice",
"is_entrepreneur": true,
"total_investment": 1000.0,
"total_roi": 60.0,
"total_referral_income": 30.0,
"total_weekly_bonus": 10.0,
"total_withdrawn": 20.0,
"net_balance": 80.0,
"current_rank": "INFINITY 2"
}
✅ Part 6 Done.
চাইলে পরবর্তী ধাপে তুমি পাবে:
/api/withdraw/POST endpoint/api/invest/endpoint/api/my-investments/,/api/my-referrals/listing API
তুমি কি এগুলো চাও? নাকি এখন এই Django প্রজেক্টটা আমি GitHub-ready বানিয়ে তোমাকে একটা রেপো স্ট্রাকচার সহ সাজিয়ে দিই?