diff --git a/pom.xml b/pom.xml index b0a6389..868b718 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,13 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.3 - + com.accenture.codingtest spring-boot-coding-test @@ -27,6 +28,14 @@ spring-boot-starter-test test + + + + org.springframework.boot + spring-boot-starter-web + 3.0.1 + + diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/Controller.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/Controller.java new file mode 100644 index 0000000..bf1926c --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/Controller.java @@ -0,0 +1,204 @@ +package com.accenture.codingtest.springbootcodingtest.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; + +public class Controller { + + + @Autowired + Controller testService; + + enum Role { + ADMIN, PRODUCT_OWNER + } + + enum Status { + NOT_STARTED, IN_PROGRESS, READY_FOR_TEST, COMPLETED + } + + @GetMapping("/users/{role}") + private List getUsers(@PathVariable("role") String role) { + if (Role.ADMIN.name().equalsIgnoreCase(role)) { + return testService.getUsers(); + } else { + return null; + } + } + + @PostMapping("/users/{role}") + private String saveUser(@RequestBody User user, @PathVariable("role") String role) { + if (Role.ADMIN.name().equalsIgnoreCase(role)) { + testService.saveOrUpdate(user); + return user.getId(); + } else { + return null; + } + } + + @PutMapping("/users/{id}/{role}") + private String updateUser(@RequestBody User user, @PathVariable("id") String id, + @PathVariable("role") String role) { + if (Role.ADMIN.name().equalsIgnoreCase(role)) { + testService.updateById(user, id); + return user.getId(); + } else { + return null; + } + } + + @PatchMapping("/users/{id}/{role}") + private String patchUser(@RequestBody User user, @PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.ADMIN.name().equalsIgnoreCase(role)) { + testService.updateById(user, id); + return user.getId(); + } else { + return null; + } + } + + @GetMapping("/user/{id}/{role}") + private User getUserById(@PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.ADMIN.name().equalsIgnoreCase(role)) { + return testService.getUserById(id); + }else { + return null; + } + } + + @DeleteMapping("/user/{id}/{role}") + private void deleteUser(@PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.ADMIN.name().equalsIgnoreCase(role)) { + testService.delete(id); + } + } + + @GetMapping("/projects/{role}") + private List getProjects(@PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + return testService.getProjects(); + }else { + return null; + } + + } + + @PostMapping("/projects/{role}") + private String saveProject(@RequestBody Project project, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.saveOrUpdate(project); + return project.getId(); + }else { + return null; + } + } + + @PutMapping("/projects/{id}/{role}") + private String updateUser(@RequestBody Project project, @PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.updateById(project, id); + return project.getId(); + }else { + return null; + } + } + + @PatchMapping("/projects/{id}/{role}") + private String patchUser(@RequestBody Project project, @PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.updateById(project, id); + return project.getId(); + }else { + return null; + } + } + + @GetMapping("/project/{id}/{role}") + private Project getProjectById(@PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + return testService.getProjectById(id); + }else { + return null; + } + + } + + @DeleteMapping("/project/{id}/{role}") + private void deleteProject(@PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.delete(id); + } + } + + @GetMapping("/tasks/{role}") + private List getTasks(@PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + return testService.getTasks(); + }else { + return null; + } + } + + @GetMapping("/tasks/{role}/{userId}") + private List getTasksByUserId(@PathVariable("role") String role,@PathVariable("userId") String userId) { + return testService.getTasksByUserId(userId); + } + + @PostMapping("/tasks/{role}") + private String saveTask(@RequestBody Task task, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.saveOrUpdate(task); + return task.getId(); + }else { + return null; + } + } + + @PutMapping("/tasks/{id}/{role}") + private String updateTask(@RequestBody Task task, @PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.updateById(task, id); + return task.getId(); + }else { + return null; + } + } + + @PatchMapping("/tasks/{id}/{role}/{userId}") + private String patchTask(@RequestBody Task task, @PathVariable("id") String id, @PathVariable("role") String role,@PathVariable("userId") String userId) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.updateById(task, id); + return task.getId(); + }else { + testService.updateById(task, id,userId); + return task.getId(); + } + } + + @GetMapping("/task/{id}/{role}") + private Task getTaskById(@PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + return testService.getTaskById(id); + }else { + return null; + } + + } + + @DeleteMapping("/task/{id}/{role}") + private void deleteTask(@PathVariable("id") String id, @PathVariable("role") String role) { + if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) { + testService.delete(id); + } + } + +} +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/package-info.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/package-info.java index e69de29..8b2177b 100644 --- a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/package-info.java +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/package-info.java @@ -0,0 +1 @@ +package com.accenture.codingtest.springbootcodingtest.controller; \ No newline at end of file diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/ProjectRepository.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/ProjectRepository.java new file mode 100644 index 0000000..3d961eb --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/ProjectRepository.java @@ -0,0 +1,4 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +public interface ProjectRepository extends CrudRepository { +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/TaskRepository.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/TaskRepository.java new file mode 100644 index 0000000..7786a61 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/TaskRepository.java @@ -0,0 +1,7 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +import java.util.List; + +public interface TaskRepository extends CrudRepository { + List findByUserId(String userId); +} \ No newline at end of file diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/UserRepository.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/UserRepository.java new file mode 100644 index 0000000..7a2620b --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/UserRepository.java @@ -0,0 +1,4 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +public interface UserRepository extends CrudRepository { +} \ No newline at end of file diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/ServiceService.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/ServiceService.java new file mode 100644 index 0000000..7b94781 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/ServiceService.java @@ -0,0 +1,127 @@ +package com.accenture.codingtest.springbootcodingtest.service; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.accenture.codingtest.springbootcodingtest.repository.ProjectRepository; +import com.accenture.codingtest.springbootcodingtest.repository.TaskRepository; +import com.accenture.codingtest.springbootcodingtest.repository.UserRepository; + +@Service +public class ServiceService { + @Autowired + UserRepository userRepository; + + @Autowired + ProjectRepository projectRepository; + + @Autowired + TaskRepository taskRepository; + + public List getUsers() { + List users = new ArrayList(); + userRepository.findAll().forEach(user -> users.add(user)); + return users; + } + + public User getUserById(String id) { + return userRepository.findById(id).get(); + } + + public void saveOrUpdate(User user) { + userRepository.save(user); + } + + public void updateById(User user, String id) { + Optional userRes = userRepository.findById(id); + if (userRes.isPresent()) { + userRes.get().setPassword(user.getPassword()); + userRes.get().setUserName(user.getUserName()); + userRepository.save(userRes.get()); + } + + } + + public void delete(String id) { + userRepository.deleteById(id); + } + + public List getProjects() { + List projects = new ArrayList(); + projectRepository.findAll().forEach(project -> projects.add(project)); + return projects; + } + + public void saveOrUpdate(Project project) { + projectRepository.save(project); + } + + public void updateById(Project project, String id) { + Optional projectRes = projectRepository.findById(id); + if (projectRes.isPresent()) { + projectRes.get().setName(project.getName()); + projectRepository.save(projectRes.get()); + } + } + + public Project getProjectById(String id) { + return projectRepository.findById(id).get(); + } + + public void deleteProject(String id) { + projectRepository.deleteById(id); + } + + public List getTasks() { + List tasks = new ArrayList(); + taskRepository.findAll().forEach(task -> tasks.add(task)); + return tasks; + } + + public List getTasksByUserId(String userId) { + List taskRes = taskRepository.findByUserId(userId); + List tasks = new ArrayList(); + if (!taskRes.isEmpty()) { + taskRes.forEach(task -> tasks.add(task)); + } + return tasks; + } + + public void saveOrUpdate(Task task) { + taskRepository.save(task); + } + + public void updateById(Task task, String id) { + Optional taskRes = taskRepository.findById(id); + if (taskRes.isPresent()) { + taskRes.get().setStatus(task.getStatus()); + taskRes.get().setDescription(task.getDescription()); + taskRes.get().setTitle(task.getTitle()); + taskRes.get().setProjectId(task.getProjectId()); + taskRes.get().setUserId(task.getUserId()); + taskRepository.save(taskRes.get()); + } + } + + public void updateById(Task task, String id, String userId) { + Optional taskRes = taskRepository.findById(id); + if (taskRes.isPresent()) { + if (taskRes.get().getUserId().equalsIgnoreCase(userId)) { + taskRes.get().setStatus(task.getStatus()); + taskRepository.save(taskRes.get()); + } + } + } + + public Task getTaskById(String id) { + return taskRepository.findById(id).get(); + } + + public void deleteTask(String id) { + taskRepository.deleteById(id); + } + +}