Skip to content

Implement features" #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.accenture.codingtest</groupId>
<artifactId>spring-boot-coding-test</artifactId>
@@ -27,6 +28,14 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.1</version>
</dependency>

</dependencies>

<build>
Original file line number Diff line number Diff line change
@@ -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<User> 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<Project> 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<Task> getTasks(@PathVariable("role") String role) {
if (Role.PRODUCT_OWNER.name().equalsIgnoreCase(role)) {
return testService.getTasks();
}else {
return null;
}
}

@GetMapping("/tasks/{role}/{userId}")
private List<Task> 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);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.accenture.codingtest.springbootcodingtest.controller;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.accenture.codingtest.springbootcodingtest.repository;

public interface ProjectRepository extends CrudRepository<Project, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.accenture.codingtest.springbootcodingtest.repository;

import java.util.List;

public interface TaskRepository extends CrudRepository<Task, String> {
List<Task> findByUserId(String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.accenture.codingtest.springbootcodingtest.repository;

public interface UserRepository extends CrudRepository<User, String> {
}
Original file line number Diff line number Diff line change
@@ -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<User> getUsers() {
List<User> users = new ArrayList<User>();
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<User> 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<Project> getProjects() {
List<Project> projects = new ArrayList<Project>();
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<Project> 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<Task> getTasks() {
List<Task> tasks = new ArrayList<Task>();
taskRepository.findAll().forEach(task -> tasks.add(task));
return tasks;
}

public List<Task> getTasksByUserId(String userId) {
List<Task> taskRes = taskRepository.findByUserId(userId);
List<Task> tasks = new ArrayList<Task>();
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<Task> 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<Task> 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);
}

}