Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
withinJoel committed Aug 20, 2024
1 parent ce15027 commit 570f58a
Show file tree
Hide file tree
Showing 58 changed files with 311 additions and 98 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/classes/java/main/com/example/teamsync/model/Employee.class
Binary file not shown.
Binary file modified build/classes/java/main/com/example/teamsync/model/Project.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.teamsync.controller;

import com.example.teamsync.dto.DepartmentDto;
import com.example.teamsync.controller.dto.DepartmentDto;
import com.example.teamsync.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.example.teamsync.controller;

import com.example.teamsync.assembler.EmployeeResourceAssembler;
import com.example.teamsync.dto.EmployeeDto;
import com.example.teamsync.controller.assembler.EmployeeResourceAssembler;
import com.example.teamsync.controller.dto.EmployeeDto;
import com.example.teamsync.model.Employee;
import com.example.teamsync.controller.assembler.resource.EmployeeResource;
import com.example.teamsync.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -24,16 +25,15 @@ public class EmployeeController {
@Autowired
private EmployeeResourceAssembler assembler;

// List all employees using EmployeeDto
@GetMapping
public ResponseEntity<?> getAllEmployees() {
try {
List<Employee> employees = employeeService.getAllEmployees();
List<EmployeeDto> employeeDtos = employees.stream()
List<EmployeeResource> employeeResources = employees.stream()
.map(assembler::toModel)
.collect(Collectors.toList());

return ResponseEntity.ok(employeeDtos);
return ResponseEntity.ok(employeeResources);
} catch (Exception e) {
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("error", "An error occurred while fetching employees.");
Expand All @@ -42,16 +42,14 @@ public ResponseEntity<?> getAllEmployees() {
}
}

// Get an employee by ID
@GetMapping("/{id}")
public ResponseEntity<EmployeeDto> getEmployeeById(@PathVariable Long id) {
public ResponseEntity<EmployeeResource> getEmployeeById(@PathVariable Long id) {
Employee employee = employeeService.getEmployeeById(id);
return ResponseEntity.ok(assembler.toModel(employee));
}

// Create a new employee using Employee entity
@PostMapping
public ResponseEntity<EmployeeDto> createEmployee(@RequestBody EmployeeDto employeeDto) {
public ResponseEntity<EmployeeResource> createEmployee(@RequestBody EmployeeDto employeeDto) {
Employee employee = new Employee();
employee.setName(employeeDto.getName());
employee.setDepartment(employeeDto.getDepartment());
Expand All @@ -62,8 +60,6 @@ public ResponseEntity<EmployeeDto> createEmployee(@RequestBody EmployeeDto emplo
return ResponseEntity.ok(assembler.toModel(savedEmployee));
}


// Delete an employee by ID using Employee entity
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteEmployee(@PathVariable Long id) {
try {
Expand All @@ -74,4 +70,4 @@ public ResponseEntity<String> deleteEmployee(@PathVariable Long id) {
.body("An error occurred while deleting employee with ID " + id);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.example.teamsync.controller;

import com.example.teamsync.dto.ProjectDto;
import com.example.teamsync.controller.assembler.ProjectResourceAssembler;
import com.example.teamsync.controller.dto.ProjectDto;
import com.example.teamsync.model.Project;
import com.example.teamsync.controller.assembler.resource.ProjectResource;
import com.example.teamsync.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

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

@RestController
@RequestMapping("/api/projects")
Expand All @@ -17,22 +23,38 @@ public class ProjectController {
@Autowired
private ProjectService projectService;

@Autowired
private ProjectResourceAssembler assembler;

@GetMapping
public ResponseEntity<List<Project>> getAllProjects() {
public ResponseEntity<CollectionModel<ProjectResource>> getAllProjects() {
List<Project> projects = projectService.getAllProjects();
return ResponseEntity.ok(projects);
List<ProjectResource> projectResources = projects.stream()
.map(assembler::toModel)
.collect(Collectors.toList());

CollectionModel<ProjectResource> projectResourceCollection = CollectionModel.of(projectResources);
projectResourceCollection.add(linkTo(methodOn(ProjectController.class).getAllProjects()).withSelfRel());

return ResponseEntity.ok(projectResourceCollection);
}

@GetMapping("/{id}")
public ResponseEntity<ProjectResource> getProjectById(@PathVariable Long id) {
Project project = projectService.getProjectById(id);
return ResponseEntity.ok(assembler.toModel(project));
}

@PostMapping
public ResponseEntity<Project> addProject(@RequestBody ProjectDto projectDto) {
public ResponseEntity<ProjectResource> addProject(@RequestBody ProjectDto projectDto) {
Project project = projectService.addProject(projectDto);
return ResponseEntity.status(HttpStatus.CREATED).body(project);
return ResponseEntity.status(HttpStatus.CREATED).body(assembler.toModel(project));
}

@PutMapping("/{id}")
public ResponseEntity<Project> updateProject(@PathVariable Long id, @RequestBody ProjectDto projectDto) {
public ResponseEntity<ProjectResource> updateProject(@PathVariable Long id, @RequestBody ProjectDto projectDto) {
Project project = projectService.updateProject(id, projectDto);
return ResponseEntity.ok(project);
return ResponseEntity.ok(assembler.toModel(project));
}

@DeleteMapping("/{id}")
Expand All @@ -41,10 +63,16 @@ public ResponseEntity<Void> deleteProject(@PathVariable Long id) {
return ResponseEntity.noContent().build();
}

@GetMapping("/{id}")
public ResponseEntity<Project> getProjectById(@PathVariable Long id) {
Project project = projectService.getProjectById(id);
return ResponseEntity.ok(project);
}
@GetMapping("/employee/{employeeId}")
public ResponseEntity<CollectionModel<ProjectResource>> getProjectsByEmployeeId(@PathVariable Long employeeId) {
List<Project> projects = projectService.getProjectsByEmployeeId(employeeId);
List<ProjectResource> projectResources = projects.stream()
.map(assembler::toModel)
.collect(Collectors.toList());

CollectionModel<ProjectResource> projectResourceCollection = CollectionModel.of(projectResources);
projectResourceCollection.add(linkTo(methodOn(ProjectController.class).getProjectsByEmployeeId(employeeId)).withSelfRel());

}
return ResponseEntity.ok(projectResourceCollection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.teamsync.controller.assembler;

public class DepartmentResourceAssembler {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.teamsync.controller.assembler;

import com.example.teamsync.controller.EmployeeController;
import com.example.teamsync.controller.assembler.resource.EmployeeResource;
import com.example.teamsync.model.Employee;
import com.example.teamsync.controller.assembler.resource.ProjectResource;
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport;
import org.springframework.stereotype.Component;

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

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@Component
public class EmployeeResourceAssembler extends RepresentationModelAssemblerSupport<Employee, EmployeeResource> {

private final ProjectResourceAssembler projectResourceAssembler;

public EmployeeResourceAssembler(ProjectResourceAssembler projectResourceAssembler) {
super(EmployeeController.class, EmployeeResource.class);
this.projectResourceAssembler = projectResourceAssembler;
}

@Override
public EmployeeResource toModel(Employee employee) {
EmployeeResource resource = new EmployeeResource();
resource.setId(employee.getId());
resource.setName(employee.getName());
resource.setAge(employee.getAge());
resource.setEmail(employee.getEmail());
resource.setDepartment(employee.getDepartment());

// Populate the projects field
List<ProjectResource> projectResources = employee.getProjects().stream()
.map(projectResourceAssembler::toModel)
.collect(Collectors.toList());
resource.setProjects(projectResources);

// Add self link
resource.add(linkTo(methodOn(EmployeeController.class).getEmployeeById(employee.getId())).withSelfRel());

return resource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.teamsync.controller.assembler;

import com.example.teamsync.controller.ProjectController;
import com.example.teamsync.model.Project;
import com.example.teamsync.controller.assembler.resource.ProjectResource;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.stereotype.Component;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@Component
public class ProjectResourceAssembler implements RepresentationModelAssembler<Project, ProjectResource> {

@Override
public ProjectResource toModel(Project project) {
ProjectResource resource = new ProjectResource();
resource.setId(project.getId());
resource.setName(project.getName());
resource.setDescription(project.getDescription());

// Set employeeId if needed
if (project.getEmployee() != null) {
resource.setEmployeeId(project.getEmployee().getId());
}

// Add self link
resource.add(linkTo(methodOn(ProjectController.class).getProjectById(project.getId())).withSelfRel());

return resource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.teamsync.controller.assembler.resource;

public class DepartmentResource {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.example.teamsync.controller.assembler.resource;

import org.springframework.hateoas.RepresentationModel;

import java.util.List;

public class EmployeeResource extends RepresentationModel<EmployeeResource> {
private Long id;
private String name;
private int age;
private String email;
private String department;
private List<ProjectResource> projects;

// Getters and Setters
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public List<ProjectResource> getProjects() {
return projects;
}

public void setProjects(List<ProjectResource> projects) {
this.projects = projects;
}
}
Loading

0 comments on commit 570f58a

Please sign in to comment.