Connect server and client for getAllUsers, saveFootprint and getFootprint methods

This commit is contained in:
cugurlu
2019-03-31 00:51:34 +01:00
parent 383d505f52
commit cf1704e872
2 changed files with 72 additions and 24 deletions

View File

@@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RestController
public class UserController { public class UserController {
@Autowired @Autowired
@@ -47,7 +49,6 @@ public class UserController {
public void setInput(@RequestParam(value = "name") String name, public void setInput(@RequestParam(value = "name") String name,
@RequestParam(value = "inputName") String inputName, @RequestParam(value = "inputName") String inputName,
@RequestParam(value = "value") String value) { @RequestParam(value = "value") String value) {
System.out.println("Here is server controller");
userService.setInput(name, inputName, value); userService.setInput(name, inputName, value);
} }
@@ -72,6 +73,35 @@ public class UserController {
return footprint; return footprint;
} }
/**
* This method saves footprint for a user.
* @param name name of the user
*/
@RequestMapping("/saveFootprint")
public Float saveFootprint(@RequestParam(value = "name") String name) {
Float footprint = userService.saveFootprint(name);
return footprint;
}
/**
* This method gets friend list for a user.
* @param name name of the user
*/
@RequestMapping("/getFriends")
public List<String> getFriendNames(@RequestParam(value = "name") String name) {
List<String> friends = userService.getFriends(name);
return friends;
}
/**
* This method gets the list of all users.
*/
@RequestMapping("/getAllUsers")
public List<String> getAllUsers() {
List<String> users = userService.getAllUsers();
return users;
}
/** /**
* This method adds friend for a user. * This method adds friend for a user.
* @param name name of the user * @param name name of the user

View File

@@ -9,8 +9,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList;
import java.util.List;
@Service @Service
public class UserService { public class UserService {
@@ -70,20 +71,11 @@ public class UserService {
public void addFriend(String name, String friend) { public void addFriend(String name, String friend) {
User user = userRepository.findByName(name); User user = userRepository.findByName(name);
User add = userRepository.findByName(friend); User add = userRepository.findByName(friend);
if (add == null) { if (user == null || add == null ) {
throw new ApplicationException("User does not exist"); throw new ApplicationException("User does not exist");
} }
user.addFriend(add); user.addFriend(add);
} userRepository.save(user);
/**
* Returns the friendlist of the user in JSON.
* @param name the username of the user
* @return a userDTO of the logged in user
*/
public String getLeaderboard(String name) {
User user = userRepository.findByName(name);
return user.friendsToString();
} }
/** /**
@@ -101,8 +93,6 @@ public class UserService {
&& InputValidator.isValidItemValue(inputName, value)) { && InputValidator.isValidItemValue(inputName, value)) {
user.getFootPrintInputs().put(inputName, value); user.getFootPrintInputs().put(inputName, value);
userRepository.save(user); userRepository.save(user);
user.setFootPrint(calculatorService.calculateFootprint(user));
userRepository.save(user);
} else { } else {
throw new ApplicationException("Invalid input"); throw new ApplicationException("Invalid input");
} }
@@ -125,11 +115,11 @@ public class UserService {
} }
/** /**
* This method gets the footprint of a user. * This method saves the footprint of a user.
* @param name name of the user * @param name name of the user
* @return footprint of the user * @return footprint of the user
*/ */
public Float getFootprint(String name) { public Float saveFootprint(String name) {
User user = userRepository.findByName(name); User user = userRepository.findByName(name);
user.setFootPrint(calculatorService.calculateFootprint(user)); user.setFootPrint(calculatorService.calculateFootprint(user));
userRepository.save(user); userRepository.save(user);
@@ -137,12 +127,40 @@ public class UserService {
} }
/** /**
* This method gets a JSON of XML with all users. * This method gets the footprint of a user.
* @return JSON/XML of all users * @param name name of the user
* @return footprint of the user
*/ */
@GetMapping(path = "/all") public Float getFootprint(String name) {
@ResponseBody User user = userRepository.findByName(name);
public Iterable<User> getAllUsers() { return user.getFootPrint();
return userRepository.findAll(); }
/**
* This method gets the friends of a user.
* @param name name of the user
* @return list of the friends
*/
public List<String> getFriends(String name) {
List<String> result = new ArrayList<>();
User user = userRepository.findByName(name);
List<User> friends = user.getFriends();
for (User person : friends) {
result.add(person.getName());
}
return result;
}
/**
* This method gets the list of all users.
* @return list of all users
*/
public List<String> getAllUsers() {
List<String> result = new ArrayList<>();
Iterable<User> allUsers = userRepository.findAll();
for (User person : allUsers) {
result.add(person.getName());
}
return result;
} }
} }