Skip to content

Commit 3da399e

Browse files
committed
Add current-balance API
1 parent 8ab2860 commit 3da399e

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

account/views.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
from django.shortcuts import render
1+
from django.http import HttpResponse
2+
from account.models import Transaction
23

3-
# Create your views here.
4+
5+
def current_balance_view(request):
6+
balance = 0
7+
for transaction in Transaction.objects.all():
8+
if transaction.type == Transaction.Type.INCOME:
9+
balance += transaction.amount
10+
else:
11+
balance -= transaction.amount
12+
13+
return HttpResponse(f"{balance} บาท")

personal_finance/urls.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
"""personal_finance URL Configuration
2-
3-
The `urlpatterns` list routes URLs to views. For more information please see:
4-
https://docs.djangoproject.com/en/3.1/topics/http/urls/
5-
Examples:
6-
Function views
7-
1. Add an import: from my_app import views
8-
2. Add a URL to urlpatterns: path('', views.home, name='home')
9-
Class-based views
10-
1. Add an import: from other_app.views import Home
11-
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12-
Including another URLconf
13-
1. Import the include() function: from django.urls import include, path
14-
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15-
"""
161
from django.contrib import admin
172
from django.urls import path
3+
from account.views import current_balance_view
184

195
urlpatterns = [
206
path('admin/', admin.site.urls),
7+
path('account/current-balance/', current_balance_view)
218
]

0 commit comments

Comments
 (0)