Classes and API flow implementation

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2020-10-09

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:


•Define meaningful classes, relationships, and responsibilities
•Design API endpoints that interact with the service layer
•Implement clean data flow between controller → service → model layers


📋 Problem Description


You are required to design a small system that manages users in an application.


The system should allow basic operations such as:


1.Registering a new user
2.Logging in an existing user
3.Fetching user details
4.Updating or deleting a user


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


1.Controller receives API request (POST /users/register)
2.It calls UserService.registerUser()
3.The service performs validations and calls UserRepository.save()
4.The repository stores the user (in-memory or DB simulation)
5.The result flows back through the service to the controller → API response


⚙️ 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


•How should the classes interact without tight coupling?
•Where should business validation logic reside — in the service or controller?
•How to handle authentication (basic version)?
•How can this design be extended to support roles (admin, customer, etc.)?


⚙️ 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


•UML class diagram showing relationships between Controller, Service, and Repository
•API flow diagram or sequence diagram
•Sample code structure (class definitions and API stubs)


🚀 Stretch Goals


•Add JWT-based authentication for secure login.
•Add role-based access control (e.g., admin vs user).
•Integrate logging and exception handling.
•Add pagination and filtering in the GET API.


🧩 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


Solution

Comments