mail@pastecode.io avatar
a year ago
3.6 kB
You are writing an endpoint that returns the total number of items bought in your online
store by a given user. Orders in your system are stored in an external service called
OrdersService. Your task is to:
• write an endpoint in the given UsersController class;
• write a method in the given UsersService class that counts the number of items
bought by the given user;
• configure the UsersController and UsersService classes.
Solving this task requires from you editing the following three files. Please make sure
they are all present in your final solution.
• com.codility. app. AppConfiguration
com.codility. app.UsersController
The UsersService class declares:
• a field ordersService' orivate OrdersService ordersService
• a method getNumber0fItemsBought: public int getNumber@fItemsBought(String
The UsersController class declares:
• a field usersService: private UsersService usersService
• a method totalItemsBought: public Map«String, Integer> totalItensBought ()
They are all present in your initial solution.
Your application is written with the Spring Framework.
Your application is written with the Spring Framework.
Your Spring context is already populated with the OrdersService bean that implements
the following interface:
package com.codility.external;
import java.util.List:
public interface OrdersService {
List<Item> ItemsBought (String username)
Note that ordereService is located in the com.codility.external package, whereas your
application uses the com. codility. app package.
1. Make sure that the AppConfiguration class is treated as a Spring configuration
2. Configure Spring to scan for beans in the com.codility.external package.
3. Prepare the OrdersService bean (please refer to the signatures described above):
• Inject the OrdersService bean into the ordersService field.
• Use it in the getNumber0fItemsBought method to count the number of items
bought by the given user.
4. Inject UsersService into UsersController.
5. Use it in the totalltensBought method to fetch the number of items bought by the
given user.
6. The totalItemsBought method should implement the following contract:
• endpoint URL: /users/(username)/items/total, where username is a path
• response JSON format: (totalitensBought":number), where number is the
number of items bought by the given user,
• status code 200 in case of a successful response.
7. Make sure you pass the usernane variable to the totalltensBought call.
8. For simplicity, you don't have to write any input validation or error handling.
9. You are working with the Spring Framework version 5.1.7 and Java 8.

Service File:--

package com.codility.app;
import com.codility.external.ordersService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
public class UsersService {
private OrdersService ordersService;
public int getNumberofItemsBought(String username) {
recurn 0}}

Controller file:--

package com.codility.app;
import org.springframework. stereotype.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Pathvariable;
import org.springframework.web.bind. annotation. ResponseBody;
import java.til.Collections;
import java.util.Map;
public class Userscontroller /
private UsersService usersService;
public Map<string, Integer> totalItemsBought () {
return null;