From b2c7eb0644062f8b44297d563c2fc5f1bdb666af Mon Sep 17 00:00:00 2001 From: "DESKTOP-HM1TM18\\RBTSB" Date: Sun, 28 May 2023 14:09:03 +0800 Subject: [PATCH 1/3] test --- .../codingtest/springbootcodingtest/controller/package-info.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/controller/package-info.java 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 From aca21403c899cefe8e091fc14eb369cd10441b8e Mon Sep 17 00:00:00 2001 From: "DESKTOP-HM1TM18\\RBTSB" Date: Sun, 28 May 2023 14:13:58 +0800 Subject: [PATCH 2/3] Create entities --- pom.xml | 21 ++++- .../springbootcodingtest/entity/Project.java | 47 ++++++++++ .../springbootcodingtest/entity/Task.java | 87 +++++++++++++++++++ .../springbootcodingtest/entity/User.java | 60 +++++++++++++ 4 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Project.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Task.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/entity/User.java diff --git a/pom.xml b/pom.xml index b0a6389..4365294 100644 --- a/pom.xml +++ b/pom.xml @@ -27,8 +27,27 @@ spring-boot-starter-test test - + + org.projectlombok + lombok + 1.18.24 + provided + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + mysql + mysql-connector-java + + 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..1fb7065 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Project.java @@ -0,0 +1,47 @@ +package com.accenture.codingtest.springbootcodingtest.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "Project") +public class Project { + + @Id + private String id; + private String name; + + public Project() { + super(); + // TODO Auto-generated constructor stub + } + + 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; + } + + public Project(String id, String name) { + super(); + this.id = id; + this.name = name; + } + + @Override + public String toString() { + return "Project [id=" + id + ", name=" + name + "]"; + } + +} 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..a45c8a2 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/Task.java @@ -0,0 +1,87 @@ +package com.accenture.codingtest.springbootcodingtest.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "Task") +public class Task { + @Id + private String id; + private String title; + private String description; + private String status; + private String project_id; + private String user_id; + + public Task() { + super(); + // TODO Auto-generated constructor stub + } + + public Task(String id, String title, String description, String status, String project_id, String user_id) { + super(); + this.id = id; + this.title = title; + this.description = description; + this.status = status; + this.project_id = project_id; + this.user_id = user_id; + } + + 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 getProject_id() { + return project_id; + } + + public void setProject_id(String project_id) { + this.project_id = project_id; + } + + public String getUser_id() { + return user_id; + } + + public void setUser_id(String user_id) { + this.user_id = user_id; + } + + @Override + public String toString() { + return "Task [id=" + id + ", title=" + title + ", description=" + description + ", status=" + status + + ", project_id=" + project_id + ", user_id=" + user_id + "]"; + } + +} 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..cea7fd5 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/entity/User.java @@ -0,0 +1,60 @@ +package com.accenture.codingtest.springbootcodingtest.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@Entity +@Table(name = "User") +@JsonSerialize +public class User { + + @Id + private String id; + private String username; + private String password; + + public User() { + super(); + // TODO Auto-generated constructor stub + } + + public User(String id, String username, String password) { + super(); + this.id = id; + this.username = username; + this.password = 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; + } + + @Override + public String toString() { + return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; + } + +} From 2f46db10ee225f6217d69a2ee8429a772df93515 Mon Sep 17 00:00:00 2001 From: "DESKTOP-HM1TM18\\RBTSB" Date: Sun, 28 May 2023 21:57:09 +0800 Subject: [PATCH 3/3] Create REST APIs --- .../constants/Status.java | 5 ++ .../controller/ProjectController.java | 44 ++++++++++ .../controller/TaskController.java | 36 ++++++++ .../controller/UserController.java | 16 ++++ .../repository/ProjectRepository.java | 7 ++ .../repository/TaskRepository.java | 7 ++ .../repository/UserRepository.java | 7 ++ .../service/ProjectService.java | 65 +++++++++++++++ .../service/TaskService.java | 83 +++++++++++++++++++ .../service/UserService.java | 67 +++++++++++++++ src/main/resources/application.properties | 10 +++ 11 files changed, 347 insertions(+) create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/constants/Status.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/controller/ProjectController.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/controller/TaskController.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/controller/UserController.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/repository/ProjectRepository.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/repository/TaskRepository.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/repository/UserRepository.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/service/ProjectService.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/service/TaskService.java create mode 100644 src/main/java/com/accenture/codingtest/springbootcodingtest/service/UserService.java diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/constants/Status.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/constants/Status.java new file mode 100644 index 0000000..f26a4db --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/constants/Status.java @@ -0,0 +1,5 @@ +package com.accenture.codingtest.springbootcodingtest.constants; + +public enum Status { + NOT_STARTED, IN_PROGRESS, READY_FOR_TEST, COMPLETED +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/ProjectController.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/ProjectController.java new file mode 100644 index 0000000..12dc41d --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/ProjectController.java @@ -0,0 +1,44 @@ +package com.accenture.codingtest.springbootcodingtest.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +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.service.ProjectService; + +@RestController +@RequestMapping("/api") +public class ProjectController { + @Autowired + private ProjectService projectService; + + @GetMapping("/v1/projects") + public ResponseEntity> getAllProjects() { + return projectService.getAllProjects(); + } + + @GetMapping("/v1/projects/{project_id}") + public ResponseEntity getProjectById(@PathVariable("project_id") String project_id) { + return projectService.getProjectById(project_id); + } + + @PutMapping("/v1/projects") + public ResponseEntity updateProject(@RequestBody Project project) { + return projectService.updateProject(project); + } + + @DeleteMapping("/v1/projects/{project_id}") + public ResponseEntity deleteProjectById(@PathVariable("project_id") String project_id) { + return projectService.deleteProject(project_id); + } + +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/TaskController.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/TaskController.java new file mode 100644 index 0000000..bcab6b7 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/TaskController.java @@ -0,0 +1,36 @@ +package com.accenture.codingtest.springbootcodingtest.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.accenture.codingtest.springbootcodingtest.entity.Task; +import com.accenture.codingtest.springbootcodingtest.service.TaskService; + +@RestController +@RequestMapping("/api") +public class TaskController { + @Autowired + private TaskService taskService; + + @GetMapping("/v1/tasks") + public ResponseEntity> getAllTasks() { + return taskService.getAllTasks(); + } + + @GetMapping("/v1/tasks/{task_id}") + public ResponseEntity getTaskById(@PathVariable("task_id") String task_id) { + return taskService.getTaskById(task_id); + } + + @DeleteMapping("/v1/tasks/{task_id}") + public ResponseEntity deleteTaskById(@PathVariable("task_id") String task_id) { + return taskService.deleteTask(task_id); + } +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/UserController.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/UserController.java new file mode 100644 index 0000000..7b3d2d8 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/controller/UserController.java @@ -0,0 +1,16 @@ +package com.accenture.codingtest.springbootcodingtest.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.accenture.codingtest.springbootcodingtest.service.UserService; + +@RestController +@RequestMapping("/api") +public class UserController { + + @Autowired + private UserService userService; + +} 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..b1a9458 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/ProjectRepository.java @@ -0,0 +1,7 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +import com.accenture.codingtest.springbootcodingtest.entity.Project; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProjectRepository extends JpaRepository { +} 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..2cba613 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/TaskRepository.java @@ -0,0 +1,7 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +import com.accenture.codingtest.springbootcodingtest.entity.Task; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TaskRepository extends JpaRepository { +} 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..046e205 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.accenture.codingtest.springbootcodingtest.repository; + +import com.accenture.codingtest.springbootcodingtest.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/ProjectService.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/ProjectService.java new file mode 100644 index 0000000..d0deef1 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/ProjectService.java @@ -0,0 +1,65 @@ +package com.accenture.codingtest.springbootcodingtest.service; + +import java.util.List; +import java.util.Optional; + +import com.accenture.codingtest.springbootcodingtest.entity.Project; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import com.accenture.codingtest.springbootcodingtest.repository.ProjectRepository; + +@Service +public class ProjectService { + @Autowired + private ProjectRepository projectRepository; + + public ResponseEntity saveProject(Project project) { + ResponseEntity response = null; + if (project != null) { + response = new ResponseEntity<>(projectRepository.save(project), HttpStatus.OK); + } else { + response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + public ResponseEntity> getAllProjects() { + return new ResponseEntity<>(projectRepository.findAll(), HttpStatus.OK); + } + + public ResponseEntity getProjectById(String id) { + return new ResponseEntity<>(projectRepository.findById(id).get(), HttpStatus.OK); + } + + public ResponseEntity updateProject(Project project) { + ResponseEntity updatedProject = null; + String id = project.getId(); + Optional oldProjectOp = projectRepository.findById(id); + + if(oldProjectOp.isPresent()) { + Project oldProject = oldProjectOp.get(); + + oldProject.setName(project.getName()); + // oldProject.setProject_id(project.getProject_id()); + + updatedProject = new ResponseEntity<>(projectRepository.save(oldProject), HttpStatus.OK); + } else { + updatedProject = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return updatedProject; + } + + public ResponseEntity deleteProject(String id) { + ResponseEntity response = null; + if (projectRepository.existsById(id)) { + projectRepository.deleteById(id); + response = new ResponseEntity<>(HttpStatus.OK); + } else { + response = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return response; + } + +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/TaskService.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/TaskService.java new file mode 100644 index 0000000..ab0747e --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/TaskService.java @@ -0,0 +1,83 @@ +package com.accenture.codingtest.springbootcodingtest.service; + +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +import com.accenture.codingtest.springbootcodingtest.constants.Status; +import com.accenture.codingtest.springbootcodingtest.entity.Task; +import com.accenture.codingtest.springbootcodingtest.repository.TaskRepository; + +@Service +public class TaskService { + + @Autowired + private TaskRepository taskRepository; + + public ResponseEntity saveTask(Task task) { + ResponseEntity response = null; + if (task != null) { + //initial status + task.setStatus(Status.NOT_STARTED.toString()); + response = new ResponseEntity<>(taskRepository.save(task), HttpStatus.OK); + } else { + response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + public ResponseEntity> getAllTasks() { + return new ResponseEntity<>(taskRepository.findAll(), HttpStatus.OK); + } + + public ResponseEntity getTaskById(String id) { + return new ResponseEntity<>(taskRepository.findById(id).get(), HttpStatus.OK); + } + + public ResponseEntity updateTask(Task task) { + ResponseEntity updatedTask = null; + String id = task.getId(); + Optional oldTaskOp = taskRepository.findById(id); + + if(oldTaskOp.isPresent()) { + Task oldTask = oldTaskOp.get(); + + oldTask.setDescription(task.getDescription()); + oldTask.setTitle(task.getTitle()); + oldTask.setStatus(task.getStatus()); + oldTask.setUser_id(task.getUser_id()); + // oldTask.setProject_id(task.getProject_id()); + + updatedTask = new ResponseEntity<>(taskRepository.save(oldTask), HttpStatus.OK); + } else { + updatedTask = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return updatedTask; + } + public ResponseEntity updateTask(Task task, String userId) { + ResponseEntity response = null; + Optional taskRes = taskRepository.findById(task.getId()); + if (taskRes.isPresent()) { + if (taskRes.get().getUser_id().equalsIgnoreCase(userId)) { + taskRes.get().setStatus(task.getStatus()); + response =new ResponseEntity<>(taskRepository.save(taskRes.get()), HttpStatus.OK); + } + } + return response; + } + + public ResponseEntity deleteTask(String id) { + ResponseEntity response = null; + if (taskRepository.existsById(id)) { + taskRepository.deleteById(id); + response = new ResponseEntity<>(HttpStatus.OK); + } else { + response = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return response; + } +} diff --git a/src/main/java/com/accenture/codingtest/springbootcodingtest/service/UserService.java b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/UserService.java new file mode 100644 index 0000000..9230755 --- /dev/null +++ b/src/main/java/com/accenture/codingtest/springbootcodingtest/service/UserService.java @@ -0,0 +1,67 @@ +package com.accenture.codingtest.springbootcodingtest.service; + +import com.accenture.codingtest.springbootcodingtest.entity.User; +import com.accenture.codingtest.springbootcodingtest.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class UserService { + + @Autowired + private UserRepository userRepository; + + public ResponseEntity saveUser(User user) { + ResponseEntity response = null; + if (user != null) { + response = new ResponseEntity<>(userRepository.save(user), HttpStatus.OK); + } else { + response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + + + public ResponseEntity> getAllUsers() { + return new ResponseEntity<>(userRepository.findAll(), HttpStatus.OK); + } + + public ResponseEntity getUserById(String id) { + return new ResponseEntity<>(userRepository.findById(id).get(), HttpStatus.OK); + } + + public ResponseEntity updateUser(User user) { + ResponseEntity updatedUser = null; + String id = user.getId(); + Optional oldUserOp = userRepository.findById(id); + + if(oldUserOp.isPresent()) { + User oldUser = oldUserOp.get(); + + oldUser.setUsername(user.getUsername()); + oldUser.setPassword(user.getPassword()); + + updatedUser = new ResponseEntity<>(userRepository.save(oldUser), HttpStatus.OK); + } else { + updatedUser = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return updatedUser; + } + + public ResponseEntity deleteUser(String id) { + ResponseEntity response = null; + if (userRepository.existsById(id)) { + userRepository.deleteById(id); + response = new ResponseEntity<>(HttpStatus.OK); + } else { + response = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return response; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..9991ffb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,11 @@ +spring.datasource.url=jdbc:mysql://localhost:3306/test +#spring.datasource.driverClassName=com.mysql.jdbc.Driver +spring.datasource.username=root +spring.datasource.password=123456 + + + +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect +spring.jpa.hibernate.ddl-auto= update