Skip to content

Feature/rest api #9

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 28 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>
Expand All @@ -27,6 +28,31 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.0.2</version>
</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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.accenture.codingtest.springbootcodingtest.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.exception.RecordNotFoundException;
import com.accenture.codingtest.springbootcodingtest.entity.model.ProjectDto;
import com.accenture.codingtest.springbootcodingtest.service.ProjectService;

@RestController
@RequestMapping(value = "/api/v1/projects")
public class ProjectController {

@Autowired
private ProjectService projectService;

@PostMapping("")
public ProjectDto create(@RequestBody ProjectDto projectDto) {
return this.convertProjectEntityToProjectDto(
projectService.save(this.convertProjectDtoToProjectEntity(projectDto)));
}

@GetMapping("")
public List<ProjectDto> findAll() {
return projectService.findAll().stream().map(this::convertProjectEntityToProjectDto)
.collect(Collectors.toList());
}

@GetMapping("/{id}")
public ProjectDto findById(@PathVariable String id) {
return projectService.findById(id).map(this::convertProjectEntityToProjectDto)
.orElseThrow(RecordNotFoundException::new);
}

private ProjectDto convertProjectEntityToProjectDto(Project project) {
ProjectDto projectDto = new ProjectDto();
projectDto.setId(project.getId());
projectDto.setName(projectDto.getName());

return projectDto;
}

private Project convertProjectDtoToProjectEntity(ProjectDto projectDto) {
Project project = new Project();
project.setId(projectDto.getId());
project.setName(projectDto.getName());

return project;
}

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

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
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.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.TaskStatusEnum;
import com.accenture.codingtest.springbootcodingtest.entity.User;
import com.accenture.codingtest.springbootcodingtest.entity.exception.RecordNotFoundException;
import com.accenture.codingtest.springbootcodingtest.entity.model.TaskDto;
import com.accenture.codingtest.springbootcodingtest.service.ProjectService;
import com.accenture.codingtest.springbootcodingtest.service.TaskService;
import com.accenture.codingtest.springbootcodingtest.service.UserService;

@RestController
@RequestMapping(value = "/api/v1/tasks")
public class TaskController {

@Autowired
private TaskService taskService;

@Autowired
private UserService userService;

@Autowired
private ProjectService projectService;

@PostMapping("")
public TaskDto create(@RequestBody TaskDto taskDto) {
Task task = taskService.save(this.convertTaskDtoToTaskEntity(taskDto));
return this.convertTaskEntityToTaskDto(task);
}

@PatchMapping("/{id}")
public TaskDto update(@RequestBody TaskDto taskDto, @PathVariable String id) {
Task task = taskService.update(this.convertTaskDtoToTaskEntity(taskDto), id);
return this.convertTaskEntityToTaskDto(task);
}

@GetMapping("")
public List<TaskDto> findAll() {
return taskService.findAll().stream().map(this::convertTaskEntityToTaskDto).collect(Collectors.toList());
}

@GetMapping("/project/{projectId}")
public List<TaskDto> findByProjectId(@PathVariable String projectId) {
return taskService.findByProjectId(projectId).stream().map(this::convertTaskEntityToTaskDto)
.collect(Collectors.toList());
}

@GetMapping("/user/{userId}")
public List<TaskDto> findByUserId(@PathVariable String userId) {
return taskService.findByUserId(userId).stream().map(this::convertTaskEntityToTaskDto)
.collect(Collectors.toList());
}

private TaskDto convertTaskEntityToTaskDto(Task task) {
TaskDto taskDto = new TaskDto();
taskDto.setId(task.getId());
taskDto.setTitle(task.getTitle());
taskDto.setDescription(task.getDescription());
taskDto.setStatus(task.getStatus().toString());
taskDto.setProjectId(task.getProject().getId());
taskDto.setProjectName(task.getProject().getName());
taskDto.setUserId(task.getUser().getId());
taskDto.setUsername(task.getUser().getUsername());

return taskDto;
}

private Task convertTaskDtoToTaskEntity(TaskDto taskDto) {
Task task = new Task();
task.setId(taskDto.getId());
task.setTitle(taskDto.getTitle());
task.setDescription(taskDto.getDescription());
task.setStatus(TaskStatusEnum.valueOf(taskDto.getStatus()));

User user = userService.findByUserId(taskDto.getUserId()).orElseThrow(RecordNotFoundException::new);
Project project = projectService.findById(taskDto.getProjectId()).orElseThrow(RecordNotFoundException::new);

task.setUser(user);
task.setProject(project);

return task;
}

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

import java.util.List;
import java.util.stream.Collectors;

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.User;
import com.accenture.codingtest.springbootcodingtest.entity.exception.RecordNotFoundException;
import com.accenture.codingtest.springbootcodingtest.entity.model.UserDto;
import com.accenture.codingtest.springbootcodingtest.service.UserService;

@RestController
@RequestMapping(value = "/api/v1/users")
public class UserController {

@Autowired
private UserService userService;

@GetMapping(value = "")
public List<UserDto> findAll() {
return userService.findAll().stream().map(this::convertUserEntityToUserDto).collect(Collectors.toList());
}

@GetMapping(value = "/{userId}")
public UserDto findByUserId(@PathVariable String userId) {
return userService.findByUserId(userId).map(this::convertUserEntityToUserDto)
.orElseThrow(RecordNotFoundException::new);
}

@PostMapping("")
public UserDto create(@RequestBody UserDto userDto) {
return this.convertUserEntityToUserDto(userService.save(this.convertUserDtoToUserEntity(userDto)));
}

@PutMapping("/{userId}")
public UserDto updateIdempotent(@RequestBody UserDto userDto, @PathVariable String userId) {
return this.convertUserEntityToUserDto(userService.update(this.convertUserDtoToUserEntity(userDto), userId));
}

@PatchMapping("/{userId}")
public UserDto update(@RequestBody UserDto userDto, @PathVariable String userId) {
return this.convertUserEntityToUserDto(userService.update(this.convertUserDtoToUserEntity(userDto), userId));
}

@DeleteMapping("/{userId}")
public void delete(@PathVariable String userId) {
userService.delete(userId);
}

private UserDto convertUserEntityToUserDto(User user) {
UserDto userDto = new UserDto();
userDto.setId(user.getId());
userDto.setUsername(user.getUsername());

return userDto;
}

private User convertUserDtoToUserEntity(UserDto userDto) {
User user = new User();
user.setId(userDto.getId());
user.setUsername(userDto.getUsername());
user.setPassword(userDto.getPassword());

return user;
}

}
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,27 @@
package com.accenture.codingtest.springbootcodingtest.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import lombok.Data;

@Data
@Entity
@Table(name = "project")
public class Project {

@Id
@Column(name = "id", length = 40)
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

@Column(name = "name", nullable = false, unique = true)
private String name;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import lombok.Data;

@Data
@Entity
@Table(name = "task")
public class Task {

@Id
@Column(name = "id", length = 40)
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

@Column(name = "title", nullable = false)
private String title;

@Column(name = "description", nullable = true)
private String description;

@Enumerated(EnumType.STRING)
@Column(name = "status", length = 40, nullable = false)
private TaskStatusEnum status;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "project_id", nullable = false)
private Project project;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

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

public enum TaskStatusEnum {
NOT_STARTED, IN_PROGRESS, READY_FOR_TEST, COMPLETED
}
Loading