diff --git a/pom.xml b/pom.xml index b0a6389..56d4394 100644 --- a/pom.xml +++ b/pom.xml @@ -21,12 +21,24 @@ org.springframework.boot spring-boot-starter - + + org.springframework.boot + spring-boot-starter-web + org.springframework.boot spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/CodingTestController.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/CodingTestController.java new file mode 100644 index 0000000..7fda351 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/CodingTestController.java @@ -0,0 +1,213 @@ +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; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.accenture.codingtest.springbootcodingtest.entity.Project; +import com.accenture.codingtest.springbootcodingtest.entity.Task; +import com.accenture.codingtest.springbootcodingtest.entity.User; +import com.accenture.codingtest.springbootcodingtest.service.CodingTestService; + + +@RestController +@RequestMapping(path = "api/v1") +public class CodingTestController { + + + @Autowired + CodingTestService 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 deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Project.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Project.java new file mode 100644 index 0000000..e3da3e5 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Project.java @@ -0,0 +1,35 @@ +package com.accenture.codingtest.springbootcodingtest.entity; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "project") +public class Project { + + @Id + @Column(name = "id") + private String id; + @Column(name = "name", length = 250, unique = true) + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Task.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Task.java new file mode 100644 index 0000000..aaa1176 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Task.java @@ -0,0 +1,75 @@ +package com.accenture.codingtest.springbootcodingtest.entity; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "task") +public class Task { + + @Id + @Column(name = "id") + private String id; + @Column(name = "title", length = 250, nullable = false) + private String title; + @Column(name = "description") + private String description; + @Column(name = "status", nullable = false) + private String status; + @Column(name = "project_id", nullable = false) + private String projectId; + @Column(name = "user_id", nullable = false) + private String userId; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getProjectId() { + return projectId; + } + + public void setProjectId(String projectId) { + this.projectId = projectId; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } +} + diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/User.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/User.java new file mode 100644 index 0000000..52519ec --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/User.java @@ -0,0 +1,45 @@ +package com.accenture.codingtest.springbootcodingtest.entity; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "user") +public class User { + + @Id + @Column(name = "id") + private String id; + @Column(name = "user_name", length = 150, unique = true) + private String userName; + @Column(name = "password", length = 50) + private String password; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/package-info.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/package-info.java deleted file mode 100644 index f87ee93..0000000 --- a/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.accenture.codingtest.springbootcodingtest.entity; \ 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..8f98334 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/ProjectRepository.java @@ -0,0 +1,11 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + + +import org.springframework.data.repository.CrudRepository; + +import com.accenture.codingtest.springbootcodingtest.entity.Project; + + + +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..bd1e455 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/TaskRepository.java @@ -0,0 +1,11 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +import java.util.List; + +import org.springframework.data.repository.CrudRepository; + +import com.accenture.codingtest.springbootcodingtest.entity.Task; + +public interface TaskRepository extends CrudRepository { + List findByUserId(String userId); +} 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..40d0754 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/UserRepository.java @@ -0,0 +1,11 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + + +import org.springframework.data.repository.CrudRepository; + +import com.accenture.codingtest.springbootcodingtest.entity.User; + + + +public interface UserRepository extends CrudRepository { +} \ No newline at end of file diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/CodingTestService.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/CodingTestService.java new file mode 100644 index 0000000..c094c74 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/CodingTestService.java @@ -0,0 +1,130 @@ +package com.accenture.codingtest.springbootcodingtest.service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.accenture.codingtest.springbootcodingtest.entity.Project; +import com.accenture.codingtest.springbootcodingtest.entity.Task; +import com.accenture.codingtest.springbootcodingtest.entity.User; +import com.accenture.codingtest.springbootcodingtest.repository.ProjectRepository; +import com.accenture.codingtest.springbootcodingtest.repository.TaskRepository; +import com.accenture.codingtest.springbootcodingtest.repository.UserRepository; + +@Service +public class CodingTestService { + + @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); + } +} \ No newline at end of file diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/package-info.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/package-info.java deleted file mode 100644 index ad5937c..0000000 --- a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.accenture.codingtest.springbootcodingtest.service; \ No newline at end of file diff --git a/src/main/resources/Data.sql b/src/main/resources/Data.sql new file mode 100644 index 0000000..54fc587 --- /dev/null +++ b/src/main/resources/Data.sql @@ -0,0 +1,3 @@ +CREATE TABLE USER (id varchar,user_name varchar(150) unique,password varchar(50)); +CREATE TABLE PROJECT (id varchar,name varchar(150)); +CREATE TABLE TASK (id varchar,title varchar(250),description varchar,status varchar,project_id varchar,user_id varchar); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..aa9aa18 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,8 @@ +spring.datasource.url=jdbc:h2:mem:testdb +spring.h2.console.enabled=true +spring.jpa.hibernate.ddl-auto=none +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect +#logging level +logging.level.org.springframework.web=DEBUG \ No newline at end of file