Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
djdongjae committed May 27, 2024
2 parents 98b0d74 + 7dd29f9 commit f032744
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -46,11 +45,4 @@ public BaseResponse<List<InventoryResponseDto>> findAllInventory() {
final List<InventoryResponseDto> data = inventoryService.findAllInventory();
return BaseResponse.success(SuccessCode.INVENTORY_GET_SUCCESS, data);
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public BaseResponse<List<InventoryResponseDto>> findAllBySrchText(@RequestParam("srchText") Optional<String> srchText) {
final List<InventoryResponseDto> data = inventoryService.findInventoryWithSrchText(srchText.orElse(""));
return BaseResponse.success(SuccessCode.INVENTORY_GET_SUCCESS, data);
}
}
Original file line number Diff line number Diff line change
@@ -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<BaseResponse<List<SalaryResponseDto>>>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<Salary> 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<Salary> 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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@

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

import java.util.List;

public interface InventoryRepository extends JpaRepository<Inventory, Long> {
List<Inventory> findAllByNameContaining(String srchText);
}
17 changes: 17 additions & 0 deletions src/main/java/net/skhu/tastyinventory_be/domain/salary/Salary.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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<Salary, Long> {
Optional<Salary> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, "급여 정보 수정이 완료되었습니다"),

;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,4 @@ public List<InventoryResponseDto> findAllInventory() {
List<Inventory> inventoryList = inventoryRepository.findAll();
return inventoryList.stream().map(InventoryResponseDto::from).collect(Collectors.toList());
}

public List<InventoryResponseDto> findInventoryWithSrchText(String srchText){
List<Inventory> inventoryList = inventoryRepository.findAllByNameContaining(srchText);
return inventoryList.stream().map(InventoryResponseDto::from).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -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<SalaryResponseDto> getSalary(){
List<Salary> salaries = salaryRepository.findAll();
List<SalaryResponseDto> 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<Salary> 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<Salary> findById(Long id) {
return salaryRepository.findById(id);
}
}

0 comments on commit f032744

Please sign in to comment.