Learn how a request turns into a response behind the screen. In Part 1 you learn the MVC pattern and backend basics that work in any framework. In Part 2 you design a layered architecture that splits API, business rules, DB and external payment by responsibility, using Django as the example.
We connect the docs you made on Day 3 (REQ · DATA · API) to "which folder and which file" they go into in today's backend code.
Use the tabs to switch between Part 1 (backend · MVC) and Part 2 (Django layered architecture). Move with ←/→, and open lecture notes with the N key or the button.
ACE AI SW Startup Training · Day 5 · Part 1
What happens behind one button?
From the moment a user presses "Order" until "Order complete" shows on screen — the backend is responsible for everything in between.
RequestThe browser or app sends a request to the server
ProcessValidate, apply rules and calculate
PersistSave to the DB and connect to outside systems
ResponseSend the result back as JSON or HTML
Client · Server · Database
The frontend shows; the backend protects
Frontend (Day 3 UI spec)
Screens, buttons, input forms, status display
User experience and instant feedback
Users can tamper with it → can't be trusted
Backend (Day 3 DATA · API · REQ)
Final decision on business rules like "sold-out items can't be ordered"
class OrderCreateView(APIView):
def post(self, request):
menu = Menu.objects.get( # DB read
id=request.data["menu_id"])
if not menu.is_available: # business rule
raise ValidationError("Not for sale")
total = menu.price * \
request.data["quantity"] # calculation
order = Order.objects.create( # save
user=request.user, total_price=total)
PaymentAPI.confirm(...) # outside payment
return Response({"order_id": order.id})
As it grows
The same rules get duplicated across many APIs
You can't test business logic without a DB
Several developers edit the same file and clash
Changing payment provider affects the whole service
AI can't decide code locations consistently
HANDS-ON 0515 minTeam · break-apart game
Break up the Fat View and send each piece home
GoalSplit code mixed in one View into layers by responsibility
APIViewTake requests · return responses
Rule / CalculatorbusinessDecide · calculate (no DB)
Career linkDevelopers who keep code in its place per team rules get faster code reviews and fewer team conflicts.
Business layer components
The 6 parts of the Business layer
DTOImmutable objects that carry data between layers (no Models)CreateOrderCommand
RuleJudges just one business conditionOrderQuantityRule, MenuAvailableRule
PolicyCombines several Rules into a final decisionOrderAcceptancePolicy
CalculatorSame input → same result. No DB or outside calls; no float for moneyOrderPriceCalculator
WorkflowCoordinates the use-case order. Doesn't judge itself; it combinesCreateOrderWorkflow
PortProtocol interface for features it needs from outside layersOrderRepository, PaymentGateway
Rule · Policy · Calculator
Day 3 REQs become pure Python rules
Rule · REQ-006
class OrderQuantityRule:
MAX = 20
def evaluate(self, quantity):
if quantity < 1 or quantity > self.MAX:
return RuleResult.reject(
"QUANTITY_OUT_OF_RANGE")
return RuleResult.accept()
Policy
class OrderAcceptancePolicy:
def __init__(self, rules):
self.rules = rules
def evaluate(self, context):
for rule in self.rules:
result = rule.evaluate(context)
if not result.passed:
return result
return RuleResult.accept()
Calculator
class OrderPriceCalculator:
def calculate(self, items):
return sum(
item.unit_price * item.quantity
for item in items
)
# Use integers (smallest currency unit)
# or Decimal for money; no float
HANDS-ON 0710 minSolo · code
Build and test a quantity Rule yourself
GoalTurn a Day 3 requirement into a pure Python Rule and check it with tests
1Read the requirementREQ-006 quantity 1–20
2Write the RuleFill MIN · MAX
3Run testsInstantly, no DB
4Check the edges0 · 1 · 20 · 21
EdgesMost bugs happen at the edges (0 and 1, 20 and 21). Always test at the edges!
class OrderQuantityRule: # REQ-006
MIN = MAX =
def evaluate(self, quantity):
if quantity < self.MIN or quantity > self.MAX:
return reject("QUANTITY_OUT_OF_RANGE")
return accept()
quantity0Expect: reject–
quantity1Expect: pass–
quantity10Expect: pass–
quantity20Expect: pass–
quantity21Expect: reject–
Try entering MIN · MAX
Tick the boxes to check yourself
Career link"A developer who proves it with tests" is welcome on any team. Edge-value testing is a must-know QA interview concept.
class PaymentGatewayRegistry:
def __init__(self, gateways):
self._gateways = {
g.provider_code: g for g in gateways}
def get(self, provider_code):
return self._gateways[provider_code]
/plan-eng-review
Place each REQ in TRACEABILITY.md into a Django layered architecture.
Structure: config / apps / business / infrastructure / tests
- For each REQ, propose in a table which of Rule · Policy · Calculator · Workflow · Port · Repository · Adapter
it goes into, with the file path
- No Django or DRF imports in the business layer
- Mark unclear REQs as "needs human decision"
Don't write any code yet.
② BACKEND_RULES.md draft
Make the AI dev standard doc
Write a draft BACKEND_RULES.md backend development standard for this project.
Include:
1. The 5 main directories and their responsibilities
2. Dependency direction: API → Workflow → Rule/Policy/Calculator → Port ← Adapter
3. The 12-step order for building a new feature
4. 9 things AI must never do
5. File placement table (kind of code → where to write it)
6. 3 test levels (unit / integration / contract): locations and criteria
My project's domain names: [e.g. cafe, billing]
I'll save the file after I review it.
③ Build contract for one feature
Used in the next build session
First read BACKEND_RULES.md and LAYER_MAP.md.
This scope is only [REQ-___].
Follow the 12-step order, but before writing code, first report
1) file paths to create or change 2) DTOs and Ports to define 3) unit tests to write.
Build after approval,
and report only test results you actually ran.
④ Rule-violation review
/review (Codex: gstack-review skill)
/review
Review the current diff against BACKEND_RULES.md.
Check these first:
- django · rest_framework imports inside business/
- Business rules · amount calculations · direct ORM calls inside Views
- Duplicate implementations of existing Rules
- Exposing raw provider responses, hard-coded Secrets
Report only severity · file · line · evidence, and don't change anything.