From 83cfdcfecca6a71ff73340e830696603fe058a6d Mon Sep 17 00:00:00 2001 From: "Kim, yongwook" Date: Wed, 22 May 2024 11:45:21 +0900 Subject: [PATCH 1/3] feat. salary CRUD (#15) --- .../net/skhu/tastyinventory_be/exception/SuccessCode.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/skhu/tastyinventory_be/exception/SuccessCode.java b/src/main/java/net/skhu/tastyinventory_be/exception/SuccessCode.java index 8ab7b3e..e38e32d 100644 --- a/src/main/java/net/skhu/tastyinventory_be/exception/SuccessCode.java +++ b/src/main/java/net/skhu/tastyinventory_be/exception/SuccessCode.java @@ -32,8 +32,10 @@ public enum SuccessCode { */ UNLINK_SUCCESS(HttpStatus.NO_CONTENT, "사용자 탈퇴에 성공했습니다"), EMPLOYEE_DELETE_SUCCESS(HttpStatus.NO_CONTENT, "직원 삭제에 성공했습니다"), - EMPLOYEE_PATCH_SUCCESS(HttpStatus.NO_CONTENT, "직원 정보 수정이 완료되었습니다"), SALARY_DELETE_SUCCESS(HttpStatus.NO_CONTENT, "급여 삭제에 성공했습니다"), + EMPLOYEE_PATCH_SUCCESS(HttpStatus.NO_CONTENT, "직원 정보 수정이 완료되었습니다"), + SALARY_PATCH_SUCCESS(HttpStatus.NO_CONTENT, "급여 정보 수정이 완료되었습니다"), + ; From 9960064ce09febda646fdf1335cff6e917dc9d2c Mon Sep 17 00:00:00 2001 From: "Kim, yongwook" Date: Wed, 22 May 2024 11:45:44 +0900 Subject: [PATCH 2/3] feat. salary CRUD (#15) --- .../controller/salary/SalaryController.java | 99 +++++++++++++++++++ .../controller/salary/dto/SalaryEdit.java | 23 +++++ .../salary/dto/SalaryResponseDto.java | 27 +++++ .../domain/salary/Salary.java | 17 ++++ .../domain/salary/SalaryRepository.java | 9 ++ .../service/SalaryService.java | 65 ++++++++++++ 6 files changed, 240 insertions(+) create mode 100644 src/main/java/net/skhu/tastyinventory_be/controller/salary/SalaryController.java create mode 100644 src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryEdit.java create mode 100644 src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryResponseDto.java create mode 100644 src/main/java/net/skhu/tastyinventory_be/domain/salary/Salary.java create mode 100644 src/main/java/net/skhu/tastyinventory_be/domain/salary/SalaryRepository.java create mode 100644 src/main/java/net/skhu/tastyinventory_be/service/SalaryService.java diff --git a/src/main/java/net/skhu/tastyinventory_be/controller/salary/SalaryController.java b/src/main/java/net/skhu/tastyinventory_be/controller/salary/SalaryController.java new file mode 100644 index 0000000..7db2c6f --- /dev/null +++ b/src/main/java/net/skhu/tastyinventory_be/controller/salary/SalaryController.java @@ -0,0 +1,99 @@ +package net.skhu.tastyinventory_be.controller.salary; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import net.skhu.tastyinventory_be.common.dto.BaseResponse; +import net.skhu.tastyinventory_be.controller.employee.dto.EmployeeResponseDto; +import net.skhu.tastyinventory_be.domain.salary.Salary; +import net.skhu.tastyinventory_be.controller.salary.dto.SalaryResponseDto; +import net.skhu.tastyinventory_be.exception.ErrorCode; +import net.skhu.tastyinventory_be.exception.SuccessCode; +import net.skhu.tastyinventory_be.service.SalaryService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import net.skhu.tastyinventory_be.controller.salary.dto.SalaryEdit; + +import java.util.List; +import java.util.Optional; + +// BOOLEAN 추가/삭제 + + +@RequiredArgsConstructor +@RestController +@RequestMapping("salary") +@Slf4j +public class SalaryController { + + private final SalaryService salaryService; + + @GetMapping + public ResponseEntity>>findAll() { + return ResponseEntity.ok(BaseResponse.success(SuccessCode.GET_SUCCESS, salaryService.getSalary())); + } + + + @PostMapping + public ResponseEntity createSalary(@Valid @RequestBody SalaryEdit salaryEdit) { + + Salary salary = new Salary(); + salary.setName(salaryEdit.getName()); + salary.setPosition(salaryEdit.getPosition()); + salary.setBaseSalary(salaryEdit.getBaseSalary()); + + salaryService.save(salary); + + return ResponseEntity.ok(BaseResponse.success(SuccessCode.SALARY_CREATE_SUCCESS)); + } + + @GetMapping("{id}") + public ResponseEntity getSalaryDetails(@PathVariable(name ="id") Long id) { + SalaryResponseDto salary = salaryService.getSalaryDetails(id); + if (salary != null) { + return ResponseEntity.ok(BaseResponse.success(SuccessCode.GET_SUCCESS, salary)); + } else { + return ResponseEntity.notFound().build(); + } + } + + @PatchMapping("{id}") + public ResponseEntity editSalaryDetails(@PathVariable(name ="id") Long id, @Valid @RequestBody SalaryEdit salaryEdit) { + + + Optional salaryOptional = salaryService.findById(id); + if (salaryOptional.isPresent()) { + Salary salary = salaryOptional.get(); + + if (salaryEdit.getName() != null) { + salary.setName(salaryEdit.getName()); + } + if (salaryEdit.getPosition() != null) { + salary.setPosition(salaryEdit.getPosition()); + } + if (salaryEdit.getBaseSalary() != null) { + salary.setBaseSalary(salaryEdit.getBaseSalary()); + } + + + salaryService.save(salary); + return ResponseEntity.ok(BaseResponse.success(SuccessCode.SALARY_PATCH_SUCCESS)); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(BaseResponse.error(ErrorCode.NOT_FOUND_EMPLOYEE_EXCEPTION)); + } + } + + @DeleteMapping("{id}") + public ResponseEntity deleteSalary(@PathVariable(name ="id") Long id) { + Optional salaryOptional = salaryService.findById(id); + if (salaryOptional.isPresent()) { + salaryService.deleteById(id); + return ResponseEntity.ok(BaseResponse.success(SuccessCode.SALARY_DELETE_SUCCESS)); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(BaseResponse.error(ErrorCode.NOT_FOUND_EMPLOYEE_EXCEPTION)); + } + } +} \ No newline at end of file diff --git a/src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryEdit.java b/src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryEdit.java new file mode 100644 index 0000000..f7f709e --- /dev/null +++ b/src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryEdit.java @@ -0,0 +1,23 @@ +package net.skhu.tastyinventory_be.controller.salary.dto; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +@Data +public class SalaryEdit { + int id; + + @NotEmpty + @NotBlank + String name; + + @NotEmpty + @NotBlank + String position; + + @NotEmpty + @NotBlank + String baseSalary; + +} \ No newline at end of file diff --git a/src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryResponseDto.java b/src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryResponseDto.java new file mode 100644 index 0000000..ef94807 --- /dev/null +++ b/src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryResponseDto.java @@ -0,0 +1,27 @@ +package net.skhu.tastyinventory_be.controller.salary.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import net.skhu.tastyinventory_be.domain.salary.Salary; + +@Builder +@Data +@RequiredArgsConstructor +@AllArgsConstructor +public class SalaryResponseDto { + int id; + String name; + String position; + String baseSalary; + + public static SalaryResponseDto of(Salary salary) { + return SalaryResponseDto.builder() + .id(salary.getId()) + .name(salary.getName()) + .position(salary.getPosition()) + .baseSalary(salary.getBaseSalary()) + .build(); + } +} diff --git a/src/main/java/net/skhu/tastyinventory_be/domain/salary/Salary.java b/src/main/java/net/skhu/tastyinventory_be/domain/salary/Salary.java new file mode 100644 index 0000000..e6ce097 --- /dev/null +++ b/src/main/java/net/skhu/tastyinventory_be/domain/salary/Salary.java @@ -0,0 +1,17 @@ +package net.skhu.tastyinventory_be.domain.salary; + +import jakarta.persistence.*; +import lombok.Data; + +@Data +@Entity +public class Salary { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "salaryId") + int id; + String name; + String position; + String baseSalary; + +} diff --git a/src/main/java/net/skhu/tastyinventory_be/domain/salary/SalaryRepository.java b/src/main/java/net/skhu/tastyinventory_be/domain/salary/SalaryRepository.java new file mode 100644 index 0000000..9e7d6ee --- /dev/null +++ b/src/main/java/net/skhu/tastyinventory_be/domain/salary/SalaryRepository.java @@ -0,0 +1,9 @@ +package net.skhu.tastyinventory_be.domain.salary; +import net.skhu.tastyinventory_be.domain.salary.Salary; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface SalaryRepository extends JpaRepository { + Optional findById(Long id); +} \ No newline at end of file diff --git a/src/main/java/net/skhu/tastyinventory_be/service/SalaryService.java b/src/main/java/net/skhu/tastyinventory_be/service/SalaryService.java new file mode 100644 index 0000000..13da421 --- /dev/null +++ b/src/main/java/net/skhu/tastyinventory_be/service/SalaryService.java @@ -0,0 +1,65 @@ +package net.skhu.tastyinventory_be.service; + +import lombok.RequiredArgsConstructor; +import net.skhu.tastyinventory_be.controller.salary.dto.SalaryResponseDto; +import net.skhu.tastyinventory_be.domain.salary.Salary; +import net.skhu.tastyinventory_be.domain.salary.SalaryRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@RequiredArgsConstructor +@Service +public class SalaryService { + private final SalaryRepository salaryRepository; + + @Transactional + public void save(Salary salary) { + salaryRepository.save(salary); + } + + @Transactional + public List getSalary(){ + List salaries = salaryRepository.findAll(); + List salaryResponseDtos = new ArrayList<>(); + for (Salary salary : salaries) { + SalaryResponseDto salaryResponseDto = new SalaryResponseDto(); + salaryResponseDto.setId(salary.getId()); + salaryResponseDto.setName(salary.getName()); + salaryResponseDto.setBaseSalary(salary.getBaseSalary()); + salaryResponseDto.setPosition(salary.getPosition()); + salaryResponseDtos.add(salaryResponseDto); + } + return salaryResponseDtos; + } + + @Transactional(readOnly = true) + public SalaryResponseDto getSalaryDetails(Long id) { + Optional salaryOptional = salaryRepository.findById(id); + if (salaryOptional.isPresent()) { + Salary salary = salaryOptional.get(); + SalaryResponseDto salaryResponseDto = new SalaryResponseDto(); + salaryResponseDto.setId(salary.getId()); + salaryResponseDto.setName(salary.getName()); + salaryResponseDto.setBaseSalary(salary.getBaseSalary()); + salaryResponseDto.setPosition(salary.getPosition()); + // 나머지 필드도 필요에 따라 추가 + + return salaryResponseDto; + } else { + return null; + } + } + @Transactional + public void deleteById(Long id) { + salaryRepository.deleteById(id); + } + @Transactional + public Optional findById(Long id) { + return salaryRepository.findById(id); + } +} + From 7dd29f93bb5cd4be51e5377f5a6abd2defebc819 Mon Sep 17 00:00:00 2001 From: "Kim, yongwook" Date: Mon, 27 May 2024 13:08:31 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Revert=20"feat:=20=EC=9E=AC=EA=B3=A0=20?= =?UTF-8?q?=EA=B2=80=EC=83=89=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?(#6)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 682d0a28945782e581d68f20e14b7c19cba8ee6c. --- .../controller/inventory/InventoryController.java | 8 -------- .../domain/inventory/InventoryRepository.java | 3 --- .../skhu/tastyinventory_be/service/InventoryService.java | 5 ----- 3 files changed, 16 deletions(-) diff --git a/src/main/java/net/skhu/tastyinventory_be/controller/inventory/InventoryController.java b/src/main/java/net/skhu/tastyinventory_be/controller/inventory/InventoryController.java index 3ef4629..3d4719a 100644 --- a/src/main/java/net/skhu/tastyinventory_be/controller/inventory/InventoryController.java +++ b/src/main/java/net/skhu/tastyinventory_be/controller/inventory/InventoryController.java @@ -13,7 +13,6 @@ import org.springframework.web.multipart.MultipartFile; import java.util.List; -import java.util.Optional; @Slf4j @RequiredArgsConstructor @@ -46,11 +45,4 @@ public BaseResponse> findAllInventory() { final List data = inventoryService.findAllInventory(); return BaseResponse.success(SuccessCode.INVENTORY_GET_SUCCESS, data); } - - @GetMapping - @ResponseStatus(HttpStatus.OK) - public BaseResponse> findAllBySrchText(@RequestParam("srchText") Optional srchText) { - final List data = inventoryService.findInventoryWithSrchText(srchText.orElse("")); - return BaseResponse.success(SuccessCode.INVENTORY_GET_SUCCESS, data); - } } diff --git a/src/main/java/net/skhu/tastyinventory_be/domain/inventory/InventoryRepository.java b/src/main/java/net/skhu/tastyinventory_be/domain/inventory/InventoryRepository.java index 43f3574..8fb2fcd 100644 --- a/src/main/java/net/skhu/tastyinventory_be/domain/inventory/InventoryRepository.java +++ b/src/main/java/net/skhu/tastyinventory_be/domain/inventory/InventoryRepository.java @@ -2,8 +2,5 @@ import org.springframework.data.jpa.repository.JpaRepository; -import java.util.List; - public interface InventoryRepository extends JpaRepository { - List findAllByNameContaining(String srchText); } diff --git a/src/main/java/net/skhu/tastyinventory_be/service/InventoryService.java b/src/main/java/net/skhu/tastyinventory_be/service/InventoryService.java index f64574e..eabed70 100644 --- a/src/main/java/net/skhu/tastyinventory_be/service/InventoryService.java +++ b/src/main/java/net/skhu/tastyinventory_be/service/InventoryService.java @@ -46,9 +46,4 @@ public List findAllInventory() { List inventoryList = inventoryRepository.findAll(); return inventoryList.stream().map(InventoryResponseDto::from).collect(Collectors.toList()); } - - public List findInventoryWithSrchText(String srchText){ - List inventoryList = inventoryRepository.findAllByNameContaining(srchText); - return inventoryList.stream().map(InventoryResponseDto::from).collect(Collectors.toList()); - } }