Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Compiled class file
*.class
#*.class

# Log file
*.log
Expand Down
10 changes: 10 additions & 0 deletions .idea/material_theme_project_new.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/main/java/app/api/controller/EmpAPIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app.api.controller;

import app.dto.EmpDto;
import app.entity.Dept;
import app.entity.Emp;
import app.repository.EmpRepository;
import app.service.EmpService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api")
public class EmpAPIController {

private final EmpService empService;

@PutMapping("/emp/{empno}")
public EmpDto updateEmp(@RequestBody EmpDto updateEmp, @PathVariable("empno") Integer empno) {

Emp emp = empService.updateEmp(updateEmp, empno);

return EmpDto.from(emp);
}
}
43 changes: 43 additions & 0 deletions src/main/java/app/dto/EmpDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package app.dto;

import app.entity.Emp;
import lombok.Getter;
import lombok.ToString;

import java.time.LocalDate;

@ToString
@Getter
public class EmpDto {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private LocalDate hiredate;
private Double sal;
private Double comm;
private Integer deptno;

public EmpDto(Integer empno, String ename, String job, Integer mgr, LocalDate hiredate, Double sal, Double comm, Integer deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}

public static EmpDto of(Integer empno, String ename, String job, Integer mgr, LocalDate hiredate, Double sal, Double comm, Integer deptno) {
return new EmpDto(empno, ename, job, mgr, hiredate, sal, comm, deptno);
}

public static EmpDto from(Emp emp){
return of(emp.getEmpno(), emp.getEname(), emp.getJob(), emp.getMgr(), emp.getHiredate(), emp.getSal(), emp.getComm(), emp.getDept().getDeptno());
}
}




67 changes: 67 additions & 0 deletions src/main/java/app/entity/Emp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package app.entity;

import app.dto.EmpDto;
import app.entity.Dept;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.time.LocalDate;

@NoArgsConstructor
@Getter
@ToString
@Builder
@Entity
public class Emp {
@Id
@Column(name = "empno")
private Integer empno;

@Column(name = "ename")
private String ename;

@Column(name = "job")
private String job;

@Column(name = "mgr")
private Integer mgr;

@Column(name = "hiredate")
private LocalDate hiredate;

@Column(name = "sal")
private Double sal;

@Column(name = "comm")
private Double comm;

@ManyToOne
@JoinColumn(name = "deptno")
private Dept dept;

@Builder
public Emp(int empno, String ename, String job, Integer mgr, LocalDate hiredate, Double sal, Double comm, Dept dept) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.dept = dept;
}

public void updateInfo(EmpDto updateEmp, Dept updateDept) {
this.empno = updateEmp.getEmpno();
this.ename = updateEmp.getEname();
this.job = updateEmp.getJob();
this.mgr = updateEmp.getMgr();
this.hiredate = updateEmp.getHiredate();
this.sal = updateEmp.getSal();
this.comm = updateEmp.getComm();
this.dept = updateDept;
}
}
32 changes: 32 additions & 0 deletions src/main/java/app/service/EmpService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app.service;

import app.dto.EmpDto;
import app.entity.Dept;
import app.entity.Emp;
import app.repository.DeptRepository;
import app.repository.EmpRepository;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

@RequiredArgsConstructor
@Service
public class EmpService {

private final EmpRepository empRepository;
private final DeptRepository deptRepository;

@Transactional
public Emp updateEmp(EmpDto updateEmp, Integer empno) {
Emp emp = empRepository.findById(empno)
.orElseThrow(() -> new EntityNotFoundException("해당 직원의 정보가 존재하지 않습니다."));
Dept dept =deptRepository.findById(updateEmp.getDeptno())
.orElseThrow(() -> new EntityNotFoundException("해당 부서가 존재하지 않습니다."));

emp.updateInfo(updateEmp, dept);

return emp;
}
}
Binary file added target/classes/app/AppApplication.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added target/classes/app/dto/EmpDto.class
Binary file not shown.
Binary file added target/classes/app/entity/Dept.class
Binary file not shown.
Binary file added target/classes/app/entity/Emp$EmpBuilder.class
Binary file not shown.
Binary file added target/classes/app/entity/Emp.class
Binary file not shown.
Binary file added target/classes/app/repository/DeptRepository.class
Binary file not shown.
Binary file added target/classes/app/repository/EmpRepository.class
Binary file not shown.
Binary file added target/classes/app/service/DeptService.class
Binary file not shown.
Binary file added target/classes/app/service/EmpService.class
Binary file not shown.
Binary file added target/test-classes/app/AppApplicationTests.class
Binary file not shown.