[미니 프로젝트] Step 1
9개월 전
controller
package com.example.company.controller;
import com.example.company.dto.MemberResponse;
import com.example.company.dto.TeamResponse;
import com.example.company.dto.createTeamRequest;
import com.example.company.dto.createMemberRequest;
import com.example.company.service.CompanyService;
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;
import java.util.List;
@RestController
public class CompanyController {
private CompanyService companyService;
public CompanyController(CompanyService companyService) {
this.companyService = companyService;
}
@PostMapping("api/team")
public void saveTeam(@RequestBody createTeamRequest request){
companyService.saveTeam(request);
}
@PostMapping("api/member")
public void saveMember(@RequestBody createMemberRequest request){
companyService.saveMember(request);
}
@GetMapping("api/team")
public List<TeamResponse> getTeams(){
return companyService.getTeams();
}
@GetMapping("api/member")
public List<MemberResponse> getMembers(){
return companyService.getMembers();
}
}
team
package com.example.company.domain;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Entity
@Getter
@NoArgsConstructor
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String managerName;
private long memberCount;
public Team(String name) {
this.name = name;
}
public void setTeamManager(String name){
this.managerName = name;
}
public void setMemberCount(){
this.memberCount++;
}
}
create team request
package com.example.company.dto;
import lombok.Getter;
@Getter
public class createTeamRequest {
private String name;
}
team repository
package com.example.company.repository;
import com.example.company.domain.Team;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface TeamRepository extends JpaRepository<Team,Long> {
Optional<Team> findByName(String name);
}
company service
@Transactional
public void saveTeam(createTeamRequest request){
teamRepository.save(new Team(request.getName()));
}
team response
package com.example.company.dto;
import com.example.company.domain.Team;
import lombok.Getter;
@Getter
public class TeamResponse {
private String name;
private String manager;
private long memberCount;
public TeamResponse(Team team) {
this.name = team.getName();
this.manager = team.getManagerName();
this.memberCount = team.getMemberCount();
}
}
company service
public List<TeamResponse> getTeams(){
List<Team> teams = teamRepository.findAll();
return teams.stream()
.map(TeamResponse::new)
.collect(Collectors.toList());
}
member
package com.example.company.domain;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
@Entity
@Getter
@NoArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private long teamId;
@Enumerated(EnumType.STRING)
private Role role;
private LocalDate birthday;
private LocalDate workStartDate;
public Member(String name, long teamId, Role role,
LocalDate birthday, LocalDate workStartDate) {
this.name = name;
this.teamId = teamId;
this.role = role;
this.birthday = birthday;
this.workStartDate = workStartDate;
}
}
role
package com.example.company.domain;
public enum Role {
Manager,
Member;
}
create member request
package com.example.company.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import java.time.LocalDate;
@Getter
public class createMemberRequest {
private String name;
private String teamName;
@JsonProperty("isManager")
private boolean isManager;
private LocalDate workStartDate;
private LocalDate birthday;
}
member repository
package com.example.company.repository;
import com.example.company.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<Member,Long> {
}
company service
@Transactional
public void saveMember(createMemberRequest request){
Team team = teamRepository.findByName(request.getTeamName())
.orElseThrow(IllegalArgumentException::new);
Role role = request.isManager() ? Role.Manager : Role.Member;
memberRepository.save(new Member(request.getName(),team.getId(),
role, request.getBirthday(),
request.getWorkStartDate()));
if(role.equals(Role.Manager)){
team.setTeamManager(request.getName());
teamRepository.save(team);
}
team.setMemberCount();
teamRepository.save(team);
}
<미니 프로젝트 소감>
스스로 풀어보고자 하였으나... 접근을 어떤식으로 해야할지 감이 안잡혀 어떤 하나를 잡고 인강을 들으면서 ppt 자료를 같이 참고하여 이해를 하면서 따라 쳐보는 식으로 하였습니다.
남은 기간 동안 계속 돌리면서 익히고 암기하는 식으로 진행하겠습니다.
댓글을 작성해보세요.