Retry adding friends. Server down again

This commit is contained in:
Merel Steenbergen
2019-03-20 15:44:47 +01:00
parent 93aaf19f13
commit b3a0ddaa68
5 changed files with 60 additions and 5 deletions

View File

@@ -18,9 +18,9 @@ apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
application {
mainClassName = 'greenify.server.Application'
}
//application {
// mainClassName = 'greenify.server.Application'
//}
repositories {
mavenCentral()

View File

@@ -2,6 +2,8 @@ package greenify.server.data.model;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -27,6 +29,8 @@ public class User {
private int veganMeal;
private ArrayList<User> friends;
public User() {}
/**
@@ -41,6 +45,7 @@ public class User {
this.name = name;
this.password = password;
this.veganMeal = veganMeal;
this.friends = new ArrayList<User>();
}
/**
@@ -91,6 +96,13 @@ public class User {
this.veganMeal = veganMeal;
}
public List<User> getFriends(){
return this.friends;
}
public void addFriend(User user){
friends.add(user);
}
/**
* Returns a human readable object. It's in JSON.

View File

@@ -32,8 +32,6 @@ public class UserController {
return userService.loginUser(name, password);
}
/**
* adds a vegetarian meal to the user.
* @param id the id of the user
@@ -46,4 +44,18 @@ public class UserController {
//addVeganMeal method of the userService
userService.addVeganMeal(id, name);
}
/**
* adds a friend to the user.
* @param id the id of the user
* @param name thr username of the user
*/
@RequestMapping("/addVeganMeal")
public void addVeganMeal(@RequestParam(value = "id") Long id,
@RequestParam(value = "name") String name,
@RequestParam(value = "friend") String friend) {
//here the requestParams are the id and name of the user and the name of the friend,
// because that is needed for the addFriendmethod of the userService
userService.addFriend(id, name, friend);
}
}

View File

@@ -79,6 +79,20 @@ public class UserService {
+ ", name=" + user.getName() + ")");
}
/**
* add vegan meal to the user.
* @param id the id of the user
* @param name the name of the user
*/
public void addFriend(Long id, String name, String friend) {
User user = userRepository.findByName(name);
User add = userRepository.findByName(friend);
user.addFriend(user);
userRepository.save(user);
logger.info("Added friend to user(id=" + user.getId()
+ ", name=" + user.getName() + ")");
}
@GetMapping(path = "/all")
@ResponseBody
public Iterable<User> getAllUsers() {

View File

@@ -18,6 +18,9 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
public class UserServiceTest {
@TestConfiguration
@@ -71,4 +74,18 @@ public class UserServiceTest {
userService.loginUser(null, null);
});
}
@Test
public void addFriendTest() {
User user = new User(1l,"Merel", "password", 0);
User friend = new User(2l, "Ellis", "pass", 0);
userService.registerUser("Merel", "password");
userService.registerUser("Ellis", "pass");
assertEquals(user.getFriends(), new ArrayList<User>());
userService.addFriend(1l,"Merel", "Ëllis");
List<User> ellis = new ArrayList<User>();
((ArrayList) ellis).add(friend);
assertEquals(user.getFriends(), ellis);
}
}