Spring
[인프런 워밍업 클럽] 미니프로젝트 1단계
yes_truly
2024. 3. 4. 22:13
728x90
🖊️구현해야 할 기능
🖊️데이터베이스
MySql을 이용해 데이터베이스를 만들었다.
테이블은 team과 employee로 구분해 만들어줬다.
- team 테이블
create table team
(
id bigint auto_increment,
name varchar(40),
manager_name varchar(20),
member_count bigint,
primary key (id)
);
- employee 테이블
create table employee
(
id bigint auto_increment,
name varchar(20),
team_id bigint,
role varchar(20),
birthday date,
work_start_date date,
primary key (id)
);
🖊️팀 등록, 조회 기능
- Entity
package com.office.commute.domain.team;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
@Entity
@Getter
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = null;
@Column
private String name;
@Column(name = "manager_name")
private String managerName;
@Column(name = "member_count")
private Long memberCount;
public Team(String name){
this.name = name;
this.managerName = null;
this.memberCount = 0L;
}
public Team() {}
public void setManagerName(String managerName) {
this.managerName = managerName;
}
public void setMemberCount() {
this.memberCount++;
}
}
- Controller
package com.office.commute.controller.team;
import com.office.commute.dto.request.team.TeamCreateRequest;
import com.office.commute.dto.response.team.TeamListResponse;
import com.office.commute.service.team.TeamService;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TeamController {
private final TeamService teamService;
public TeamController(TeamService teamService) {
this.teamService = teamService;
}
@PostMapping("/team")
public void createTeam(@RequestBody TeamCreateRequest request){
teamService.createTeam(request);
}
@GetMapping("/team")
public List<TeamListResponse> getTeamList(){
return teamService.getTeamList();
}
}
- Service
package com.office.commute.service.team;
import com.office.commute.domain.team.Team;
import com.office.commute.dto.request.team.TeamCreateRequest;
import com.office.commute.dto.response.team.TeamListResponse;
import com.office.commute.repository.team.TeamRepository;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class TeamService {
private final TeamRepository teamRepository;
@Transactional
public void createTeam(TeamCreateRequest request) {
teamRepository.save(new Team(request.getName()));
}
public List<TeamListResponse> getTeamList() {
List<Team> teamList= teamRepository.findAll();
return teamList.stream().map(TeamListResponse::new)
.collect(Collectors.toList());
}
}
- Repository
package com.office.commute.repository.team;
import com.office.commute.domain.team.Team;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TeamRepository extends JpaRepository<Team, Long> {
Optional<Team> findTeamByName(String name);
}
- Request
package com.office.commute.dto.request.team;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
public class TeamCreateRequest {
private String name;
public TeamCreateRequest(String name){
this.name = name;
}
}
- Response
package com.office.commute.dto.response.team;
import com.office.commute.domain.team.Team;
import lombok.Getter;
@Getter
public class TeamListResponse {
private String name;
private String manager;
private Long memberCount;
public TeamListResponse(Team team) {
this.name = team.getName();
this.manager = team.getManagerName();
this.memberCount = team.getMemberCount();
}
}
🖊️포스트맨 테스트
- 팀 등록 기능
등록해준 팀은 데이터베이스에 잘 저장되어있는 것을 확인할 수 있다.
- 팀 조회 기능
데이터베이스에 저장되어있는 팀 정보가 잘 반환되는 것을 확인할 수 있다.
🖊️직원 등록, 조회 기능
- Entity
package com.office.commute.domain.employee;
import com.office.commute.domain.employee.status.Role;
import com.office.commute.domain.team.Team;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDate;
import lombok.Getter;
@Entity
@Getter
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;
@Enumerated(EnumType.STRING)
private Role role;
private LocalDate birthday;
@Column(name = "work_start_date")
private LocalDate workStartDate;
public Employee(){}
public Employee(String name,Team team, Role role, LocalDate workStartDate, LocalDate birthday){
this.name = name;
this.team = team;
this.role = role;
this.workStartDate = workStartDate;
this.birthday = birthday;
}
}
package com.office.commute.domain.employee.status;
public enum Role {
MANAGER,
MEMBER
}
Role은 enum타입으로 정의해줬다.
- Controller
package com.office.commute.controller.employee;
import com.office.commute.dto.request.employee.EmployeeCreateRequest;
import com.office.commute.dto.response.Employee.EmployeeListResponse;
import com.office.commute.service.employee.EmployeeService;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
private final EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@PostMapping("/employee")
public void createEmployee(@RequestBody EmployeeCreateRequest request){
employeeService.createEmployee(request);
}
@GetMapping("/employee")
public List<EmployeeListResponse> getEmployeeList(){
return employeeService.getEmployeeList();
}
}
- Service
package com.office.commute.service.employee;
import com.office.commute.domain.employee.Employee;
import com.office.commute.domain.employee.status.Role;
import com.office.commute.domain.team.Team;
import com.office.commute.dto.request.employee.EmployeeCreateRequest;
import com.office.commute.dto.response.Employee.EmployeeListResponse;
import com.office.commute.repository.employee.EmployeeRepository;
import com.office.commute.repository.team.TeamRepository;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class EmployeeService {
private final EmployeeRepository employeeRepository;
private final TeamRepository teamRepository;
public EmployeeService(EmployeeRepository employeeRepository, TeamRepository teamRepository) {
this.employeeRepository = employeeRepository;
this.teamRepository = teamRepository;
}
@Transactional
public void createEmployee(EmployeeCreateRequest request) {
Team team = teamRepository.findTeamByName(request.getTeamName())
.orElseThrow(IllegalArgumentException::new);
if(request.getRole().equals(Role.MANAGER)){
team.setManagerName(request.getName());
}
team.setMemberCount();
employeeRepository.save(new Employee(request.getName(),team,request.getRole(), request.getWorkStartDate(), request.getBirthday()));
}
public List<EmployeeListResponse> getEmployeeList() {
List<Employee> employeeList = employeeRepository.findAll();
return employeeList.stream().map(EmployeeListResponse::new).collect(Collectors.toList());
}
}
- Repository
package com.office.commute.repository.team;
import com.office.commute.domain.team.Team;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TeamRepository extends JpaRepository<Team, Long> {
Optional<Team> findTeamByName(String name);
}
- Request
package com.office.commute.dto.request.employee;
import com.office.commute.domain.employee.status.Role;
import java.time.LocalDate;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
public class EmployeeCreateRequest {
private String name;
private String teamName;
private Role role;
private LocalDate workStartDate;
private LocalDate birthday;
public EmployeeCreateRequest(String name, String teamName, Role role, LocalDate workStartDate,
LocalDate birthday) {
this.name = name;
this.teamName = teamName;
this.role = role;
this.workStartDate = workStartDate;
this.birthday = birthday;
}
}
- Response
package com.office.commute.dto.response.Employee;
import com.office.commute.domain.employee.Employee;
import com.office.commute.domain.employee.status.Role;
import java.time.LocalDate;
import lombok.Getter;
@Getter
public class EmployeeListResponse {
private String name;
private String teamName;
private Role role;
private LocalDate birthday;
private LocalDate workStartDate;
public EmployeeListResponse(Employee employee){
this.name = employee.getName();
this.teamName = employee.getTeam().getName();
this.role = employee.getRole();
this.birthday =employee.getBirthday();
this.workStartDate = employee.getWorkStartDate();
}
}
🖊️포스트맨 테스트
- 직원 등록 기능
등록해준 직원은 데이터베이스에 잘 저장된 것과 해당 부서의 인원이 증가한 것을 확인할 수 있다.
- 직원 조회 기능
데이터베이스에 저장되어있는 직원 정보가 잘 반환되는 것을 확인할 수 있다.
코드를 좀 더 보완할 필요는 있는 것 같다.
예를 들어, 팀장이 그냥 직원으로 직책이 변경되는 경우나 팀 이름이 겹친다거나...
차차 보완해나가기로 아자자잣
자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지] 강의 - 인프런 (inflearn.com)
728x90