Classes and API flow implementation
Problem Statement
Design and implement the class structure and API flow for a simple User Management System, focusing on object-oriented design principles and clean API layer separation.
This problem assesses your ability to:
📋 Problem Description
You are required to design a small system that manages users in an application.
The system should allow basic operations such as:
You must design the classes, API endpoints, and the flow of control across layers (Controller, Service, Repository).
🧱 Core Requirements
1. API Endpoints
Design REST-like API endpoints:
HTTP MethodEndpointDescription
POST
/users/register
Register a new user
POST
/users/login
Log in with email and password
GET
/users/{id}
Fetch user details
PUT
/users/{id}
Update user information
DELETE
/users/{id}
Delete a user
2. Class Design
You should define the following key classes (expandable as needed):
LayerExample ClassesResponsibility
Model Layer
User
Represents the user entity (id, name, email, password, etc.)
Repository Layer
UserRepository
Handles CRUD operations (can be in-memory or DB simulation)
Service Layer
UserService
Contains business logic (validation, password handling, etc.)
Controller Layer
UserController
Handles API requests and responses
🧩 Example Class Flow
⚙️ Functional Requirements
Requirement and Description
User Registration
Create a new user with unique email and hashed password.
Login
Validate credentials and return a success/failure message.
User Management
Support fetch, update, and delete operations.
Input Validation
Ensure valid data (e.g., non-empty fields, unique email).
Data Persistence
Store user records (simulate using in-memory list or map).
🧠 Design Discussion Points
⚙️ Non-Functional Requirements
Aspect and Requirement
Scalability
Should allow easy addition of new APIs (e.g., password reset).
Modularity
Each layer should have a single responsibility.
Testability
Classes should be unit-testable in isolation.
Maintainability
Code should be readable and extendable.
✅ Deliverables
🚀 Stretch Goals
🧩 Example (Simplified API Flow)
1
2# Controller
3class UserController:
4 def __init__(self, service):
5 self.service = service
6
7 def register_user(self, request):
8 return self.service.register_user(request)
9
10# Service
11class UserService:
12 def __init__(self, repository):
13 self.repository = repository
14
15 def register_user(self, request):
16 user = User(request["name"], request["email"], request["password"])
17 return self.repository.save(user)
18
19# Repository
20class UserRepository:
21 def __init__(self):
22 self.users = {}
23
24 def save(self, user):
25 self.users[user.email] = user
26 return user
27
28# Model
29class User:
30 def __init__(self, name, email, password):
31 self.name = name
32 self.email = email
33 self.password = password