-
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.
Merge branch 'main' of https://github.com/Tasty-Inventory/Tasty-Inven…
- Loading branch information
Showing
10 changed files
with
243 additions
and
17 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
99 changes: 99 additions & 0 deletions
99
src/main/java/net/skhu/tastyinventory_be/controller/salary/SalaryController.java
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,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)); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryEdit.java
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,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; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/net/skhu/tastyinventory_be/controller/salary/dto/SalaryResponseDto.java
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,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(); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/net/skhu/tastyinventory_be/domain/salary/Salary.java
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,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; | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/net/skhu/tastyinventory_be/domain/salary/SalaryRepository.java
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,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); | ||
} |
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
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
65 changes: 65 additions & 0 deletions
65
src/main/java/net/skhu/tastyinventory_be/service/SalaryService.java
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,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); | ||
} | ||
} | ||
|