-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented StaffService with CRUD operation
- Loading branch information
1 parent
47e98e6
commit 1fcc3e7
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.wsmsdemo.service; | ||
|
||
import com.wsmsdemo.model.Staff; | ||
import com.wsmsdemo.repository.StaffRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class StaffService { | ||
|
||
@Autowired | ||
private StaffRepository staffRepository; | ||
|
||
public List<Staff> findAll() { | ||
return staffRepository.findAll(); | ||
} | ||
|
||
public Staff create(Staff staff) { | ||
return staffRepository.save(staff); | ||
} | ||
|
||
public Staff update(Long id, Staff staff) { | ||
Optional<Staff> existingStaff = staffRepository.findById(id); | ||
if (existingStaff.isPresent()) { | ||
Staff updatedStaff = existingStaff.get(); | ||
updatedStaff.setStaffName(staff.getStaffName()); | ||
updatedStaff.setCheckCode(staff.getCheckCode()); | ||
updatedStaff.setIsDelete(staff.getIsDelete()); | ||
updatedStaff.setIsLock(staff.getIsLock()); | ||
updatedStaff.setErrorCheckCodeCounter(staff.getErrorCheckCodeCounter()); | ||
return staffRepository.save(updatedStaff); | ||
} else { | ||
throw new RuntimeException("Staff not found with id " + id); | ||
} | ||
} | ||
|
||
public void delete(Long id) { | ||
staffRepository.deleteById(id); | ||
} | ||
|
||
public Staff partialUpdate(Long id, Staff staff) { | ||
Optional<Staff> existingStaff = staffRepository.findById(id); | ||
if (existingStaff.isPresent()) { | ||
Staff updatedStaff = existingStaff.get(); | ||
if (staff.getStaffName() != null) updatedStaff.setStaffName(staff.getStaffName()); | ||
if (staff.getCheckCode() != null) updatedStaff.setCheckCode(staff.getCheckCode()); | ||
if (staff.getIsDelete() != null) updatedStaff.setIsDelete(staff.getIsDelete()); | ||
if (staff.getIsLock() != null) updatedStaff.setIsLock(staff.getIsLock()); | ||
if (staff.getErrorCheckCodeCounter() != null) updatedStaff.setErrorCheckCodeCounter(staff.getErrorCheckCodeCounter()); | ||
return staffRepository.save(updatedStaff); | ||
} else { | ||
throw new RuntimeException("Staff not found with id " + id); | ||
} | ||
} | ||
} |