Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
withinJoel committed Aug 23, 2024
1 parent 9841f80 commit 11831a1
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 42 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 22 additions & 8 deletions build/resources/main/static/employees.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,36 @@ <h2 class="card-title">Employee List</h2>
}

function fetchDepartments() {
fetch(departmentApiUrl)
.then(response => response.json())
const apiKey = 'xyXfX4pzoBhJYIMygh1eu8qPvlp8dkBEjKRzzOK90yQYgV9W6jMrHBbDdyM2tPuT';

fetch(departmentApiUrl, {
method: 'GET',
headers: {
'API-Key': apiKey,
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('API key invalid or network response was not ok');
}
return response.json();
})
.then(data => {
const select = document.getElementById('department-id');
select.innerHTML = '';
data.forEach(department => {
const option = document.createElement('option');
for (let i = 0; i < 11; i++) {
if (department.id = i){
option.textContent = department.name;
departmentNames.push(department.name);
}
}
option.value = department.id;
option.textContent = department.name;
select.appendChild(option);
departmentNames.push(department.name);
});
M.FormSelect.init(select); // Initialize Materialize select component
})
.catch(error => {
console.error('Error:', error);
// Handle the error appropriately in your UI
});
}

Expand Down
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.teamsync.controller.dto.DepartmentDto;
import com.example.teamsync.service.DepartmentService;
import com.example.teamsync.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -15,31 +16,50 @@ public class DepartmentController {
@Autowired
private DepartmentService departmentService;

@Autowired
private EmployeeService employeeService;

@GetMapping
public List<DepartmentDto> getAllDepartments() {
return departmentService.getAllDepartments();
public ResponseEntity<?> getAllDepartments() {
return ResponseEntity.ok(departmentService.getAllDepartments());
}

@GetMapping("/{id}")
public ResponseEntity<DepartmentDto> getDepartmentById(@PathVariable Long id) {
return departmentService.getDepartmentById(id)
.map(department -> ResponseEntity.ok(departmentService.convertToDto(department)))
.orElse(ResponseEntity.notFound().build());
public ResponseEntity<?> getDepartmentById(@PathVariable Long id, @RequestHeader("API-Key") String apiKey) {
if (isValidApiKey(apiKey)) {
return departmentService.getDepartmentById(id)
.map(department -> ResponseEntity.ok(departmentService.convertToDto(department)))
.orElse(ResponseEntity.notFound().build());
}
return ResponseEntity.status(401).body("Invalid API Key");
}

@PostMapping
public DepartmentDto createDepartment(@RequestBody DepartmentDto departmentDto) {
return departmentService.createDepartment(departmentDto);
public ResponseEntity<?> createDepartment(@RequestBody DepartmentDto departmentDto, @RequestHeader("API-Key") String apiKey) {
if (isValidApiKey(apiKey)) {
return ResponseEntity.ok(departmentService.createDepartment(departmentDto));
}
return ResponseEntity.status(401).body("Invalid API Key");
}

@PutMapping("/{id}")
public ResponseEntity<DepartmentDto> updateDepartment(@PathVariable Long id, @RequestBody DepartmentDto departmentDto) {
return ResponseEntity.ok(departmentService.updateDepartment(id, departmentDto));
public ResponseEntity<?> updateDepartment(@PathVariable Long id, @RequestBody DepartmentDto departmentDto, @RequestHeader("API-Key") String apiKey) {
if (isValidApiKey(apiKey)) {
return ResponseEntity.ok(departmentService.updateDepartment(id, departmentDto));
}
return ResponseEntity.status(401).body("Invalid API Key");
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteDepartment(@PathVariable Long id) {
departmentService.deleteDepartment(id);
return ResponseEntity.noContent().build();
public ResponseEntity<?> deleteDepartment(@PathVariable Long id, @RequestHeader("API-Key") String apiKey) {
if (isValidApiKey(apiKey)) {
departmentService.deleteDepartment(id);
return ResponseEntity.noContent().build();
}
return ResponseEntity.status(401).body("Invalid API Key");
}

private boolean isValidApiKey(String apiKey) {
return employeeService.isValidToken(apiKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

@RestController
Expand All @@ -27,20 +26,23 @@ public class EmployeeController {
private EmployeeResourceAssembler assembler;

@GetMapping
public ResponseEntity<?> getAllEmployees() {
try {
List<Employee> employees = employeeService.getAllEmployees();
List<EmployeeResource> employeeResources = employees.stream()
.map(assembler::toModel)
.collect(Collectors.toList());
public ResponseEntity<?> getAllEmployees(@RequestHeader("API-Key") String apiKey) {
if (employeeService.isValidToken(apiKey)){
try {
List<Employee> employees = employeeService.getAllEmployees();
List<EmployeeResource> employeeResources = employees.stream()
.map(assembler::toModel)
.collect(Collectors.toList());

return ResponseEntity.ok(employeeResources);
} catch (Exception e) {
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("error", "An error occurred while fetching employees.");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(errorResponse);
return ResponseEntity.ok(employeeResources);
} catch (Exception e) {
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("error", "An error occurred while fetching employees.");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(errorResponse);
}
}
return ResponseEntity.status(401).body("Invalid API Key");
}

@GetMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {
boolean existsByToken(String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;


public boolean isValidToken(String token) {
return employeeRepository.existsByToken(token);
}

// Retrieve all employees
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
Expand Down
30 changes: 22 additions & 8 deletions src/main/resources/static/employees.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,36 @@ <h2 class="card-title">Employee List</h2>
}

function fetchDepartments() {
fetch(departmentApiUrl)
.then(response => response.json())
const apiKey = 'xyXfX4pzoBhJYIMygh1eu8qPvlp8dkBEjKRzzOK90yQYgV9W6jMrHBbDdyM2tPuT';

fetch(departmentApiUrl, {
method: 'GET',
headers: {
'API-Key': apiKey,
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('API key invalid or network response was not ok');
}
return response.json();
})
.then(data => {
const select = document.getElementById('department-id');
select.innerHTML = '';
data.forEach(department => {
const option = document.createElement('option');
for (let i = 0; i < 11; i++) {
if (department.id = i){
option.textContent = department.name;
departmentNames.push(department.name);
}
}
option.value = department.id;
option.textContent = department.name;
select.appendChild(option);
departmentNames.push(department.name);
});
M.FormSelect.init(select); // Initialize Materialize select component
})
.catch(error => {
console.error('Error:', error);
// Handle the error appropriately in your UI
});
}

Expand Down

0 comments on commit 11831a1

Please sign in to comment.