Angular Service Creation: User Data Management
This challenge focuses on creating a fundamental Angular service to manage and retrieve user data. Services are a core concept in Angular for encapsulating reusable logic, especially for data fetching and manipulation, making your application more organized and maintainable.
Problem Description
You need to create an Angular service named UserService that will simulate fetching and managing a list of user objects. This service will be responsible for providing this user data to different components within your Angular application.
Key Requirements:
- Service Creation: Create a new Angular service (
UserService) using the Angular CLI. - Data Structure: Define an interface for your
Userobject, which should at least includeid(number) andname(string). - Data Storage: Internally, the
UserServiceshould maintain an array ofUserobjects. For this challenge, you can initialize this array with some sample user data directly within the service. - Data Retrieval: Implement a method within the service (e.g.,
getUsers()) that returns the entire list of users. This method should return an Observable to simulate asynchronous data fetching, even though the data is hardcoded for this exercise. - Individual User Retrieval: Implement another method (e.g.,
getUserById(id: number)) that takes a user ID and returns a singleUserobject that matches the provided ID. If no user is found, it should returnundefined. - Service Injection: Ensure the
UserServiceis injectable and can be provided at the root level.
Expected Behavior:
- When
getUsers()is called, it should emit a stream containing the array of predefined user objects. - When
getUserById()is called with a valid user ID, it should emit a stream containing the corresponding user object. - When
getUserById()is called with an invalid user ID, it should emit a stream containingundefined.
Edge Cases:
- Handling requests for user IDs that do not exist in the dataset.
Examples
Example 1:
// In a component subscribing to UserService.getUsers()
Observable<User[]> emitted:
[
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" }
]
Explanation: The getUsers() method returns an Observable that emits the array of users.
Example 2:
// In a component subscribing to UserService.getUserById(1)
Observable<User | undefined> emitted:
{ id: 1, name: "Alice" }
Explanation: getUserById(1) successfully finds and returns the user with ID 1.
Example 3:
// In a component subscribing to UserService.getUserById(99)
Observable<User | undefined> emitted:
undefined
Explanation: getUserById(99) does not find a user with ID 99 and returns undefined.
Constraints
- The
UserServiceshould be provided inroot. - The
getUsers()method must return anObservable<User[]>. - The
getUserById(id: number)method must return anObservable<User | undefined>. - The sample user data should include at least 3 users.
Notes
- You'll need to import
Observablefromrxjs. - Consider using
of()fromrxjsto create an Observable from your hardcoded data. - Think about how you would handle this in a real-world scenario where data comes from an API (e.g., using
HttpClient). While this challenge uses hardcoded data, the structure should mimic asynchronous behavior.