Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.devsuperior.bds01.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.devsuperior.bds01.dto.DepartmentDTO;
import com.devsuperior.bds01.services.DepartmentSerice;

@RestController
@RequestMapping(value = "/departments")
public class DepartmentController {

@Autowired
private DepartmentSerice service;

@GetMapping
public ResponseEntity<List<DepartmentDTO>> findAll(){

List<DepartmentDTO> departments = service.findAll();

return ResponseEntity.ok().body(departments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.devsuperior.bds01.controllers;

import java.net.URI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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 org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.devsuperior.bds01.dto.EmployeeDTO;
import com.devsuperior.bds01.services.EmployeeService;

@RestController
@RequestMapping(value = "/employees")
public class EmployeeController {

@Autowired
private EmployeeService service;

@GetMapping
public ResponseEntity<Page<EmployeeDTO>> findAll(Pageable pageable) {
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),Sort.by("name"));
Page<EmployeeDTO> employees = service.findAll(pageRequest);
return ResponseEntity.ok().body(employees);
}

@PostMapping
public ResponseEntity<EmployeeDTO> insert(@RequestBody EmployeeDTO employeeDTO){

employeeDTO = service.insert(employeeDTO);
URI uri = ServletUriComponentsBuilder.fromCurrentContextPath().path("/{id}").buildAndExpand(employeeDTO.getId())
.toUri();
return ResponseEntity.created(uri).body(employeeDTO);
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/devsuperior/bds01/dto/DepartmentDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.devsuperior.bds01.dto;

import java.io.Serializable;

import com.devsuperior.bds01.entities.Department;

public class DepartmentDTO implements Serializable{

private static final long serialVersionUID = 8606549653896788793L;

public Long id;
public String name;

public DepartmentDTO() {
super();
}

public DepartmentDTO(Long id, String name) {

this.id = id;
this.name = name;
}

public DepartmentDTO(Department department) {

this.id = department.getId();
this.name = department.getName();
}

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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.devsuperior.bds01.repositories;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.devsuperior.bds01.entities.Department;

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {

List<Department> findByOrderByNameAsc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.devsuperior.bds01.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.devsuperior.bds01.entities.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{


}
29 changes: 29 additions & 0 deletions src/main/java/com/devsuperior/bds01/services/DepartmentSerice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.devsuperior.bds01.services;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.devsuperior.bds01.dto.DepartmentDTO;
import com.devsuperior.bds01.entities.Department;
import com.devsuperior.bds01.repositories.DepartmentRepository;

@Service
public class DepartmentSerice {

@Autowired
private DepartmentRepository repository;

@Transactional(readOnly = true)
public List<DepartmentDTO> findAll() {

List<Department> findAll = repository.findAll(Sort.by("Name"));

return findAll.stream().map(x -> new DepartmentDTO(x)).collect(Collectors.toList());
}

}
41 changes: 41 additions & 0 deletions src/main/java/com/devsuperior/bds01/services/EmployeeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.devsuperior.bds01.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.devsuperior.bds01.dto.EmployeeDTO;
import com.devsuperior.bds01.entities.Department;
import com.devsuperior.bds01.entities.Employee;
import com.devsuperior.bds01.repositories.EmployeeRepository;

@Service
public class EmployeeService {

@Autowired
private EmployeeRepository repository;

@Transactional(readOnly = true)
public Page<EmployeeDTO> findAll(Pageable pageable) {
Page<Employee> employees = repository.findAll(pageable);
return employees.map(EmployeeDTO::new);
}


@Transactional
public EmployeeDTO insert(EmployeeDTO employeeDTO) {

Employee employee = new Employee();
employee.setEmail(employeeDTO.getEmail());
employee.setName(employeeDTO.getName());
employee.setDepartment(new Department(employeeDTO.getDepartmentId(), null));

employee = repository.save(employee);
return new EmployeeDTO(employee);
}



}