Skip to content

Commit

Permalink
@PutMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetyagiz committed Dec 20, 2024
1 parent c7b9718 commit 9d95fa3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ahmetyagiz.model.Employee;
import com.ahmetyagiz.model.UpdateEmployeeRequest;
import com.ahmetyagiz.services.EmployeeService;

@RestController
Expand Down Expand Up @@ -48,5 +50,14 @@ public Employee saveEmployee(@RequestBody Employee newEmployee) {
public boolean deleteEmployee(@PathVariable(name = "id") String id) {
return employeeService.deleteEmployee(id);
}

@PutMapping(path = "/update-employee/{id}")
public Employee updateEmployee(@PathVariable(name = "id") String id, @RequestBody UpdateEmployeeRequest request) {
return employeeService.updateEmployee(id, request);
}
}





Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ahmetyagiz.model;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UpdateEmployeeRequest {

private String firstName;

private String lastName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.stereotype.Repository;

import com.ahmetyagiz.model.Employee;
import com.ahmetyagiz.model.UpdateEmployeeRequest;

@Repository
public class EmployeeRepository {
Expand Down Expand Up @@ -74,12 +75,41 @@ public boolean deleteEmployee(String id) {
}
}

if(deleteEmployee == null) {
return false;
if(deleteEmployee != null) {
employeeList.remove(deleteEmployee);
return true;
}

employeeList.remove(deleteEmployee);
return true;
return false;
}

public Employee findEmployeeById(String id) {
Employee findEmployee = null;
for (Employee employee : employeeList) {
if(employee.getId().equals(id)){
findEmployee = employee;
break;
}
}
return findEmployee;
}

public Employee updateEmployee(String id, UpdateEmployeeRequest request) {

Employee findEmployee = findEmployeeById(id);

if(findEmployee != null){
deleteEmployee(id);

Employee updatedEmployee = new Employee();
updatedEmployee.setId(id);
updatedEmployee.setFirstName(request.getFirstName());
updatedEmployee.setLastName(request.getLastName());

employeeList.add(updatedEmployee);
return updatedEmployee;
}
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;

import com.ahmetyagiz.model.Employee;
import com.ahmetyagiz.model.UpdateEmployeeRequest;
import com.ahmetyagiz.repository.EmployeeRepository;

@Service
Expand Down Expand Up @@ -33,4 +34,8 @@ public Employee saveEmployee(Employee newEmployee) {
public boolean deleteEmployee(String id) {
return employeeRepository.deleteEmployee(id);
}

public Employee updateEmployee(String firstName, UpdateEmployeeRequest request) {
return employeeRepository.updateEmployee(firstName, request);
}
}

0 comments on commit 9d95fa3

Please sign in to comment.