-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
142 lines (116 loc) · 3.15 KB
/
Copy pathbackend.py
File metadata and controls
142 lines (116 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from fastapi import FastAPI
from pydantic import BaseModel
from datetime import datetime
app = FastAPI(
title="Student Management API",
description="A simple FastAPI backend",
version="1.0"
)
# -----------------------------
# In-memory storage
# -----------------------------
students = []
request_count = 0
# -----------------------------
# Data Model
# -----------------------------
class Student(BaseModel):
name: str
department: str
# -----------------------------
# Home Endpoint
# -----------------------------
@app.get("/")
def home():
return {
"status": "success",
"message": "Student Management API is running 🚀",
"documentation": "http://127.0.0.1:8000/docs"
}
# -----------------------------
# Add Student
# -----------------------------
@app.post("/student")
def create_student(data: Student):
global request_count
request_count += 1
student = {
"id": len(students) + 1,
"name": data.name,
"department": data.department,
"created_at": datetime.now().strftime("%d-%m-%Y %H:%M:%S")
}
students.append(student)
return {
"status": "success",
"message": "Student added successfully",
"request_number": request_count,
"student": student
}
# -----------------------------
# Welcome Response
# -----------------------------
@app.post("/response")
def generate_response(data: Student):
global request_count
request_count += 1
return {
"status": "success",
"message": f"Welcome, {data.name}!",
"department": data.department,
"request_number": request_count,
"timestamp": datetime.now().strftime("%d-%m-%Y %H:%M:%S")
}
# -----------------------------
# Get All Students
# -----------------------------
@app.get("/students")
def get_students():
return {
"status": "success",
"total_students": len(students),
"students": students
}
# -----------------------------
# Get Student by ID
# -----------------------------
@app.get("/student/{student_id}")
def get_student(student_id: int):
for student in students:
if student["id"] == student_id:
return {
"status": "success",
"student": student
}
return {
"status": "error",
"message": "Student not found"
}
# -----------------------------
# API Statistics
# -----------------------------
@app.get("/stats")
def stats():
return {
"status": "success",
"total_requests": request_count,
"total_students": len(students),
"server_time": datetime.now().strftime("%d-%m-%Y %H:%M:%S")
}
# -----------------------------
# Delete student by id
# -----------------------------
@app.delete("/student/{student_id}")
def delete_student(student_id: int):
global students
for i, student in enumerate(students):
if student["id"] == student_id:
students.pop(i)
return {
"status": "success",
"message": "Student deleted successfully"
}
return {
"status": "error",
"message": "Student not found"
}