[인프런 워밍업 클럽 BE 0기] 미니 프로젝트 1단계
9개월 전
프로젝트 1단계
프로젝트 최초 설정이 이루어져야 합니다.
(스프링 부트를 시작하는 첫 번째 방법을 떠올려보세요)
팀 등록 기능
회사에 있는 팀을 등록할 수 있어야 합니다. 팀이 가져야할 필 수 정보는 다음과 같습니다.
팀 이름
직원 등록 기능
직원을 등록할 수 있어야 합니다. 직원이 가져야할 필수 정보는 다음과 같습니다.
직원 이름
팀의 매니저인지 매니저가 아닌지 여부
회사에 들어온 일자
생일
팀 조회 기능
모든 팀의 정보를 한 번에 조회할 수 있어야 합니다.
[
{
"name": "팀 이름"
"manager": "팀 매니저 이름" //없을 경우 null
"memberCount": 팀 인원 수 [숫자]
}, ...
]
직원 조회 기능
모든 직원의 정볼르 한 번에 조회할 수 있어야 합니다.
[
{
"name": "직원 이름",
"teamName": "소속 팀 이름",
"role": "MANAGER" or "MEMBER", //팀의 매니저인지 멤버인지
"birthday": "1989-01-01",
"workStartDate": "2024-01-01",
}, ...
]
Controller
@RestController
public class CompanyController {
private CompanyService companyService;
public CompanyController (CompanyService companyService) {
this.companyService = companyService;
}
@PostMapping("/team")
public void saveTeam(@RequestBody TeamCreateRequest request) {
companyService.saveTeam(request);
}
@PostMapping("/member")
public void saveMember(@RequestBody MemberCreateRequest request) {
companyService.saveMember(request);
}
@GetMapping("/team")
public List<TeamResponse> getTeam() {
return companyService.getTeam();
}
@GetMapping("/member")
public List<MemberResponse> getMember() {
return companyService.getMember();
}
}
Team
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String manager;
public Team(String name) {
this.name = name;
}
public void setTeamManager(String name) {
this.manager = name;
}
}
Member
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String teamName;
@Enumerated(EnumType.STRING)
private Role role;
private LocalDate birthday;
private LocalDate workStartDate;
public Member(String name, String teamName, Role role, LocalDate birthday, LocalDate workStartDate) {
this.name = name;
this.teamName = teamName;
this.role = role;
this.birthday = birthday;
this.workStartDate = workStartDate;
}
}
Role(Enum 클래스)
public enum Role {
Manager, Member;
}
TeamCreateRequest
@Getter
public class TeamCreateRequest {
private String name;
}
MemberCreateRequest
@Getter
public class MemberCreateRequest {
private String name;
private String teamName;
@JsonProperty("managerORmember")
private boolean memberORmeber;
private LocalDate birthday;
private LocalDate workStartDate;
}
Repository
@Repository
public interface MemberRepository extends JpaRepository<Member,Long> {
}
@Repository
public interface TeamRepository extends JpaRepository<Team, Long> {
Optional<Team> findByName(String name);
}
Response
@Getter
public class TeamResponse {
private String name;
private String manager;
public TeamResponse(Team team) {
this.name = team.getName();
this.manager = team.getManager();
}
}
@Getter
public class MemberResponse {
private String name;
private String teamName;
private Role role;
private LocalDate birthday;
private LocalDate workStartDate;
public MemberResponse(Member member) {
this.name = member.getName();
this.teamName = member.getTeamName();
this.role = member.getRole();
this.birthday = member.getBirthday();
this.workStartDate = member.getWorkStartDate();
}
}
CompanyService
@Service
public class CompanyService {
private final TeamRepository teamRepository;
private final MemberRepository memberRepository;
public CompanyService(TeamRepository teamRepository, MemberRepository memberRepository) {
this.teamRepository = teamRepository;
this.memberRepository = memberRepository;
}
@Transactional
public void saveTeam(TeamCreateRequest request) {
teamRepository.save(new Team(request.getName()));
}
@Transactional
public void saveMember(MemberCreateRequest request) {
Team team = teamRepository.findByName(request.getTeamName())
.orElseThrow(IllegalArgumentException::new);
Role role = request.isMemberORmeber() ? Role.Manager : Role.Member;
memberRepository.save(new Member(request.getName(), team.getName(), role,
request.getBirthday(), request.getWorkStartDate()));
if(role.equals(Role.Manager)) {
team.setTeamManager(request.getName());
teamRepository.save(team);
}
teamRepository.save(team);
}
public List<TeamResponse> getTeam() {
List<Team> teams = teamRepository.findAll();
return teams.stream()
.map(TeamResponse::new)
.collect(Collectors.toList());
}
public List<MemberResponse> getMember() {
List<Member> members = memberRepository.findAll();
return members.stream()
.map(MemberResponse::new)
.collect(Collectors.toList());
}
}
댓글을 작성해보세요.