diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index c993e2d..ff1b4e4 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -94,4 +94,18 @@ public class UserService { .encode().toUri(), String.class); return result; } + + @SuppressWarnings("Duplicates") + public String updaaddFriend(String name, String friend) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/setInput") + .queryParam("name", name) + .queryParam("friend",friend); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + String result = this.restTemplate.getForObject(builder.build() + .encode().toUri(), String.class); + return result; + } } diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index bb3a5d6..088f533 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -1,5 +1,6 @@ package greenify.server.data.model; +import greenify.common.ApplicationException; import greenify.server.InputValidator; import lombok.Data; @@ -14,6 +15,7 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import java.util.*; +import java.util.logging.Logger; import javax.persistence.*; import javax.validation.constraints.NotNull; @@ -57,6 +59,7 @@ public class User { this.name = name; this.password = password; this.setFootPrintInputs(InputValidator.getDefaultValues()); + this.friends = new ArrayList(); } /** @@ -115,6 +118,16 @@ public class User { return (ArrayList)this.friends; } + public void addFriend(User user){ + if(user.equals(this)){ + throw new ApplicationException("Cannot add yourself as a friend"); + } + else{ + friends.add(user); + System.out.print("Friend added!"); + } + } + public void setFootPrint(Float footPrint) { this.footPrint = footPrint; } diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 61ff0d9..4087071 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -59,6 +59,21 @@ public class UserService { return new UserDto(user.getId(), user.getName()); } + /** + * Adds a friend to the friendlist of the user. + * @param name the username of the user + * @param friend the name of the friend you want to add. + * @return a userDTO of the logged in user + */ + public void addFriend(String name, String friend) { + User user = userRepository.findByName(name); + User add = userRepository.findByName(friend); + if (add == null) { + throw new ApplicationException("User does not exist"); + } + user.addFriend(add); + } + /** * The method sets input value. * @param name of the user diff --git a/src/Server/src/main/resources/application.properties b/src/Server/src/main/resources/application.properties index 65fce80..3b7b07e 100644 --- a/src/Server/src/main/resources/application.properties +++ b/src/Server/src/main/resources/application.properties @@ -2,5 +2,5 @@ spring.datasource.url=jdbc:h2:file:~/spring-boot-h2.db;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=sa spring.datasource.password= spring.datasource.driver-class-name=org.h2.Driver -spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true \ No newline at end of file diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java index 3a27fb8..b566b40 100644 --- a/src/Server/src/test/java/greenify/server/data/model/UserTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java @@ -6,6 +6,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.Test; +import java.util.ArrayList; + public class UserTest { @Test public void setAndGetTest() { @@ -78,5 +80,25 @@ public class UserTest { assertEquals(first, second); assertEquals(first.hashCode(), second.hashCode()); } + + @Test + public void getFriendEmpty(){ + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "merel", "password"); + assertEquals(first.getFriends(), second.getFriends()); + assertEquals(first.getFriends(), new ArrayList()); + } + + @Test + public void addFriend(){ + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "merel", "password"); + assertEquals(first.getFriends(), second.getFriends()); + System.out.print(second); + first.addFriend(second); + ArrayList test = new ArrayList(); + test.add(second); + assertEquals(first.getFriends(), test); + } }