diff --git a/README.md b/README.md deleted file mode 100644 index ff689d5..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# RestAPI diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..05c7e94 --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.1.5 + + + com.example + resttempl + 0.0.1-SNAPSHOT + resttempl + resttempl + + 17 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/src/main/java/com/evil/rest/RestApplication.java b/src/main/java/com/evil/rest/RestApplication.java new file mode 100644 index 0000000..9209125 --- /dev/null +++ b/src/main/java/com/evil/rest/RestApplication.java @@ -0,0 +1,17 @@ +package com.evil.rest; + +import com.evil.rest.controller.UserController; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; + + +@SpringBootApplication +public class RestApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext context = SpringApplication.run(RestApplication.class, args); + UserController communication = context.getBean("userController", UserController.class); + System.out.println("Answer: " + communication.getCode()); + } +} \ No newline at end of file diff --git a/src/main/java/com/evil/rest/controller/UserController.java b/src/main/java/com/evil/rest/controller/UserController.java new file mode 100644 index 0000000..5c43e13 --- /dev/null +++ b/src/main/java/com/evil/rest/controller/UserController.java @@ -0,0 +1,56 @@ +package com.evil.rest.controller; + +import com.evil.rest.model.User; +import com.evil.rest.repository.UserRepository; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; + +@Controller +public class UserController { + private UserRepository userRepository; + private String code = ""; + + public UserController(UserRepository userRepository) { + this.userRepository = userRepository; + } + + + @GetMapping("/execute") + public void executeOperations() { + List users = userRepository.getAllUsers(); + + if (users != null && !users.isEmpty()) { + User user = new User(); + user.setId(3); + user.setName("James"); + user.setLastName("Brown"); + user.setAge((byte) 30); + + User createdUser = userRepository.createUser(user); + if (createdUser != null) { + code += createdUser.getId(); + + User updatedUser = new User(); + updatedUser.setName("Thomas"); + updatedUser.setLastName("Shelby"); + + User modifiedUser = userRepository.updateUser(createdUser.getId(), updatedUser); + if (modifiedUser != null) { + code += modifiedUser.getName().substring(1, 3); + + userRepository.deleteUser(modifiedUser.getId()); + code += modifiedUser.getName().substring(4); + } + } + } + } + + @GetMapping("/code") + public String getCode() { + return code; + } + + +} \ No newline at end of file diff --git a/src/main/java/com/evil/rest/model/User.java b/src/main/java/com/evil/rest/model/User.java new file mode 100644 index 0000000..d9c9cfb --- /dev/null +++ b/src/main/java/com/evil/rest/model/User.java @@ -0,0 +1,40 @@ +package com.evil.rest.model; +public class User { + private int id; + private String name; + private String lastName; + private byte age; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(byte age) { + this.age = age; + } +} + diff --git a/src/main/java/com/evil/rest/repository/UserRepository.java b/src/main/java/com/evil/rest/repository/UserRepository.java new file mode 100644 index 0000000..5d066b6 --- /dev/null +++ b/src/main/java/com/evil/rest/repository/UserRepository.java @@ -0,0 +1,17 @@ +package com.evil.rest.repository; + +import com.evil.rest.model.User; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface UserRepository { + List getAllUsers(); + + User createUser(User user); + + User updateUser(int id, User user); + + void deleteUser(int id); +} diff --git a/src/main/java/com/evil/rest/repository/UserRepositoryImpl.java b/src/main/java/com/evil/rest/repository/UserRepositoryImpl.java new file mode 100644 index 0000000..a454d51 --- /dev/null +++ b/src/main/java/com/evil/rest/repository/UserRepositoryImpl.java @@ -0,0 +1,82 @@ +package com.evil.rest.repository; + + +import com.evil.rest.model.User; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.stereotype.Repository; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +@Repository +public class UserRepositoryImpl implements UserRepository { + private RestTemplate restTemplate; + private String sessionId; + + public UserRepositoryImpl() { + this.restTemplate = new RestTemplate(); + } + + @Override + public List getAllUsers() { + HttpHeaders headers = new HttpHeaders(); + if (sessionId != null) { + headers.add("Cookie", sessionId); + } + HttpEntity entity = new HttpEntity<>(headers); + ResponseEntity> response = restTemplate.exchange("http://94.198.50.185:7081/api/users", HttpMethod.GET, entity, new ParameterizedTypeReference>() {}); + List users = response.getBody(); + + if (sessionId == null) { + List cookies = response.getHeaders().get("Set-Cookie"); + if (cookies != null) { + for (String cookie : cookies) { + if (cookie.startsWith("session=")) { + sessionId = cookie.split(";")[0]; + } + } + } + } + + return users; + } + + @Override + public User createUser(User user) { + HttpHeaders headers = new HttpHeaders(); + if (sessionId != null) { + headers.add("Cookie", sessionId); + } + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(user, headers); + ResponseEntity response = restTemplate.postForEntity("http://94.198.50.185:7081/api/users", request, User.class); + User createdUser = response.getBody(); + + return createdUser; + } + + @Override + public User updateUser(int id, User user) { + HttpHeaders headers = new HttpHeaders(); + if (sessionId != null) { + headers.add("Cookie", sessionId); + } + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(user, headers); + ResponseEntity response = restTemplate.exchange("http://94.198.50.185:7081/api/users/" + id, HttpMethod.PUT, request, User.class); + User updatedUser = response.getBody(); + + return updatedUser; + } + + @Override + public void deleteUser(int id) { + HttpHeaders headers = new HttpHeaders(); + if (sessionId != null) { + headers.add("Cookie", sessionId); + } + HttpEntity entity = new HttpEntity<>(headers); + restTemplate.exchange("http://94.198.50.185:7081/api/users/" + id, HttpMethod.DELETE, entity, Void.class); + } +}