diff --git a/src/main/java/com/codingwasabi/trti/domain/location/model/City.java b/src/main/java/com/codingwasabi/trti/domain/location/model/City.java index fcb6789..1db58f3 100644 --- a/src/main/java/com/codingwasabi/trti/domain/location/model/City.java +++ b/src/main/java/com/codingwasabi/trti/domain/location/model/City.java @@ -16,4 +16,17 @@ public enum City { INCHEON("인천광역시"); private String name; + + public static City parseName(String korName) { + for (City city : City.values()) { + if (city.toString().equals(korName)) { + return city; + } + } + + throw new IllegalArgumentException("[ERROR] 잘못된 도시 이름이 입력되었습니다."); + } + public String toString() { + return name; + } } diff --git a/src/main/java/com/codingwasabi/trti/domain/location/repository/LocationRepository.java b/src/main/java/com/codingwasabi/trti/domain/location/repository/LocationRepository.java index 0a27587..95dad12 100644 --- a/src/main/java/com/codingwasabi/trti/domain/location/repository/LocationRepository.java +++ b/src/main/java/com/codingwasabi/trti/domain/location/repository/LocationRepository.java @@ -1,7 +1,11 @@ package com.codingwasabi.trti.domain.location.repository; +import com.codingwasabi.trti.domain.location.model.City; import com.codingwasabi.trti.domain.location.model.Location; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface LocationRepository extends JpaRepository { + Optional findByCity(City city); } diff --git a/src/main/java/com/codingwasabi/trti/domain/member/MemberService.java b/src/main/java/com/codingwasabi/trti/domain/member/MemberService.java index 969a6c4..2f3ba62 100644 --- a/src/main/java/com/codingwasabi/trti/domain/member/MemberService.java +++ b/src/main/java/com/codingwasabi/trti/domain/member/MemberService.java @@ -1,8 +1,12 @@ package com.codingwasabi.trti.domain.member; import com.codingwasabi.trti.domain.member.model.entity.Member; +import com.codingwasabi.trti.domain.member.model.request.RequestExistMemberDto; +import com.codingwasabi.trti.domain.member.model.response.ResponseExistMemberDto; import com.codingwasabi.trti.domain.member.model.response.ResponseMyInfoDto; public interface MemberService { ResponseMyInfoDto getMemberInfo(Member member); + + ResponseExistMemberDto existMember(RequestExistMemberDto requestDto); } diff --git a/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberController.java b/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberController.java index 20e9b66..cecf8e1 100644 --- a/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberController.java +++ b/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberController.java @@ -2,23 +2,31 @@ import com.codingwasabi.trti.config.auth.security.MemberAdaptor; import com.codingwasabi.trti.domain.member.MemberService; +import com.codingwasabi.trti.domain.member.model.request.RequestExistMemberDto; +import com.codingwasabi.trti.domain.member.model.response.ResponseExistMemberDto; import com.codingwasabi.trti.domain.member.model.response.ResponseMyInfoDto; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequiredArgsConstructor -@RequestMapping("/my") public class MemberController { private final MemberService memberService; - @GetMapping("/info") + @GetMapping("/my/info") public ResponseEntity getMyInfo(@AuthenticationPrincipal MemberAdaptor memberAdaptor) { return ResponseEntity.ok() .body(memberService.getMemberInfo(memberAdaptor.getMember())); } + + @PostMapping("/member") + public ResponseEntity existMember(@RequestBody RequestExistMemberDto requestDto) { + return ResponseEntity.ok(memberService.existMember(requestDto)); + } + } diff --git a/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberServiceImpl.java b/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberServiceImpl.java index 5c49dc1..d63f8ec 100644 --- a/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberServiceImpl.java +++ b/src/main/java/com/codingwasabi/trti/domain/member/impl/MemberServiceImpl.java @@ -2,13 +2,29 @@ import com.codingwasabi.trti.domain.member.MemberService; import com.codingwasabi.trti.domain.member.model.entity.Member; +import com.codingwasabi.trti.domain.member.model.request.RequestExistMemberDto; +import com.codingwasabi.trti.domain.member.model.response.ResponseExistMemberDto; import com.codingwasabi.trti.domain.member.model.response.ResponseMyInfoDto; +import com.codingwasabi.trti.domain.member.repository.MemberRepository; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @Service +@RequiredArgsConstructor public class MemberServiceImpl implements MemberService { + private final MemberRepository memberRepository; + @Override public ResponseMyInfoDto getMemberInfo(Member member) { return ResponseMyInfoDto.getEntity(member); } + + @Override + public ResponseExistMemberDto existMember(RequestExistMemberDto requestDto) { + // Error Code 생성 + Member member = memberRepository.findByEmail(requestDto.getEmail()) + .orElseThrow(() -> new IllegalArgumentException("[ERROR] 입력된 이메일의 회원은 존재하지 않습니다.")); + + return ResponseExistMemberDto.from(member); + } } diff --git a/src/main/java/com/codingwasabi/trti/domain/member/model/request/RequestExistMemberDto.java b/src/main/java/com/codingwasabi/trti/domain/member/model/request/RequestExistMemberDto.java new file mode 100644 index 0000000..e2b788d --- /dev/null +++ b/src/main/java/com/codingwasabi/trti/domain/member/model/request/RequestExistMemberDto.java @@ -0,0 +1,12 @@ +package com.codingwasabi.trti.domain.member.model.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class RequestExistMemberDto { + private String email; +} diff --git a/src/main/java/com/codingwasabi/trti/domain/member/model/response/ResponseExistMemberDto.java b/src/main/java/com/codingwasabi/trti/domain/member/model/response/ResponseExistMemberDto.java new file mode 100644 index 0000000..f5a9bcb --- /dev/null +++ b/src/main/java/com/codingwasabi/trti/domain/member/model/response/ResponseExistMemberDto.java @@ -0,0 +1,16 @@ +package com.codingwasabi.trti.domain.member.model.response; + +import com.codingwasabi.trti.domain.member.model.entity.Member; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseExistMemberDto { + private String nickname; + private Long id; + + public static ResponseExistMemberDto from(Member member) { + return new ResponseExistMemberDto(member.getNickname(), member.getId()); + } +} diff --git a/src/main/java/com/codingwasabi/trti/domain/memberInParty/model/MemberInParty.java b/src/main/java/com/codingwasabi/trti/domain/memberInParty/model/MemberInParty.java index 65e6ac2..3384a75 100644 --- a/src/main/java/com/codingwasabi/trti/domain/memberInParty/model/MemberInParty.java +++ b/src/main/java/com/codingwasabi/trti/domain/memberInParty/model/MemberInParty.java @@ -2,6 +2,8 @@ import com.codingwasabi.trti.domain.party.model.Party; import com.codingwasabi.trti.domain.member.model.entity.Member; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -9,7 +11,9 @@ @Entity @Getter +@Builder @NoArgsConstructor +@AllArgsConstructor public class MemberInParty { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -20,4 +24,11 @@ public class MemberInParty { @ManyToOne(fetch = FetchType.LAZY) private Member member; + + public static MemberInParty of(Party party, Member member) { + return MemberInParty.builder() + .party(party) + .member(member) + .build(); + } } diff --git a/src/main/java/com/codingwasabi/trti/domain/memberInParty/repository/MemberInGroupRepository.java b/src/main/java/com/codingwasabi/trti/domain/memberInParty/repository/MemberInPartyRepository.java similarity index 77% rename from src/main/java/com/codingwasabi/trti/domain/memberInParty/repository/MemberInGroupRepository.java rename to src/main/java/com/codingwasabi/trti/domain/memberInParty/repository/MemberInPartyRepository.java index a3b59a8..39a8b17 100644 --- a/src/main/java/com/codingwasabi/trti/domain/memberInParty/repository/MemberInGroupRepository.java +++ b/src/main/java/com/codingwasabi/trti/domain/memberInParty/repository/MemberInPartyRepository.java @@ -3,5 +3,5 @@ import com.codingwasabi.trti.domain.memberInParty.model.MemberInParty; import org.springframework.data.jpa.repository.JpaRepository; -public interface MemberInGroupRepository extends JpaRepository { +public interface MemberInPartyRepository extends JpaRepository { } diff --git a/src/main/java/com/codingwasabi/trti/domain/party/PartyAPI.java b/src/main/java/com/codingwasabi/trti/domain/party/PartyAPI.java index 5a268fa..ce550a3 100644 --- a/src/main/java/com/codingwasabi/trti/domain/party/PartyAPI.java +++ b/src/main/java/com/codingwasabi/trti/domain/party/PartyAPI.java @@ -1,4 +1,9 @@ package com.codingwasabi.trti.domain.party; +import com.codingwasabi.trti.config.auth.security.MemberAdaptor; +import com.codingwasabi.trti.domain.party.model.request.RequestCreatePartyDto; +import org.springframework.http.ResponseEntity; + public interface PartyAPI { + ResponseEntity createParty(MemberAdaptor memberAdaptor, RequestCreatePartyDto requestDto); } diff --git a/src/main/java/com/codingwasabi/trti/domain/party/PartyService.java b/src/main/java/com/codingwasabi/trti/domain/party/PartyService.java index 791b8ed..27cf395 100644 --- a/src/main/java/com/codingwasabi/trti/domain/party/PartyService.java +++ b/src/main/java/com/codingwasabi/trti/domain/party/PartyService.java @@ -1,4 +1,9 @@ package com.codingwasabi.trti.domain.party; +import com.codingwasabi.trti.domain.member.model.entity.Member; +import com.codingwasabi.trti.domain.party.model.request.RequestCreatePartyDto; +import com.codingwasabi.trti.domain.party.model.response.ResponseCreatePartyDto; + public interface PartyService { + ResponseCreatePartyDto create(Member member, RequestCreatePartyDto requestDto); } diff --git a/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyController.java b/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyController.java index d2c0902..e6d63a2 100644 --- a/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyController.java +++ b/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyController.java @@ -1,6 +1,27 @@ package com.codingwasabi.trti.domain.party.implementation; +import com.codingwasabi.trti.config.auth.security.MemberAdaptor; import com.codingwasabi.trti.domain.party.PartyAPI; +import com.codingwasabi.trti.domain.party.PartyService; +import com.codingwasabi.trti.domain.party.model.request.RequestCreatePartyDto; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +@RestController +@RequiredArgsConstructor +@RequestMapping("/group") public class PartyController implements PartyAPI { + private final PartyService partyService; + + @Override + @PostMapping + public ResponseEntity createParty(@AuthenticationPrincipal MemberAdaptor memberAdaptor, + @RequestBody RequestCreatePartyDto requestDto) { + return ResponseEntity.ok(partyService.create(memberAdaptor.getMember(), requestDto)); + } } diff --git a/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyServiceImpl.java b/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyServiceImpl.java index a2cfe14..3f95dda 100644 --- a/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyServiceImpl.java +++ b/src/main/java/com/codingwasabi/trti/domain/party/implementation/PartyServiceImpl.java @@ -1,6 +1,60 @@ package com.codingwasabi.trti.domain.party.implementation; +import com.codingwasabi.trti.domain.location.model.City; +import com.codingwasabi.trti.domain.location.repository.LocationRepository; +import com.codingwasabi.trti.domain.member.model.entity.Member; +import com.codingwasabi.trti.domain.member.repository.MemberRepository; +import com.codingwasabi.trti.domain.memberInParty.model.MemberInParty; +import com.codingwasabi.trti.domain.memberInParty.repository.MemberInPartyRepository; import com.codingwasabi.trti.domain.party.PartyService; +import com.codingwasabi.trti.domain.party.model.Party; +import com.codingwasabi.trti.domain.party.model.request.RequestCreatePartyDto; +import com.codingwasabi.trti.domain.party.model.response.ResponseCreatePartyDto; +import com.codingwasabi.trti.domain.party.repository.PartyRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.util.List; + +@Service +@RequiredArgsConstructor public class PartyServiceImpl implements PartyService { + private final PartyRepository partyRepository; + private final MemberInPartyRepository memberInPartyRepository; + private final MemberRepository memberRepository; + private final LocationRepository locationRepository; + + @Override + @Transactional + public ResponseCreatePartyDto create(Member member, RequestCreatePartyDto requestDto) { + Party party = requestDto.getEntityFrom(); + + setPartyCaptain(member, party); + setPartyCity(requestDto, party); + partyRepository.save(party); + + putMemberInParty(party, requestDto.getMembers()); + + return ResponseCreatePartyDto.from(party); + } + + private void setPartyCaptain(Member member, Party party) { + party.setCaptain(member); + } + + private void setPartyCity(RequestCreatePartyDto requestDto, Party party) { + City.parseName(requestDto.getLocation()); + party.setLocation(locationRepository + .findByCity(City.parseName(requestDto.getLocation())) + .orElseThrow(() -> new IllegalArgumentException("[ERROR] 도시의 정보가 존재하지 않습니다."))); + } + + private void putMemberInParty(Party party, List memberIdList) { + memberIdList.stream() + .map((id) -> memberRepository + .findById(id) + .orElseThrow(() -> new IllegalArgumentException("[ERROR] 존재하지 않는 회원의 id가 입력되었습니다."))) + .forEach((member) -> memberInPartyRepository.save(MemberInParty.of(party, member))); + } } diff --git a/src/main/java/com/codingwasabi/trti/domain/party/model/Party.java b/src/main/java/com/codingwasabi/trti/domain/party/model/Party.java index 24621e9..ffc06b8 100644 --- a/src/main/java/com/codingwasabi/trti/domain/party/model/Party.java +++ b/src/main/java/com/codingwasabi/trti/domain/party/model/Party.java @@ -2,15 +2,21 @@ import com.codingwasabi.trti.domain.common.Period; import com.codingwasabi.trti.domain.location.model.Location; +import com.codingwasabi.trti.domain.member.model.entity.Member; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import javax.persistence.*; import java.time.LocalDate; +import java.util.Optional; @Entity @Getter +@Builder @NoArgsConstructor +@AllArgsConstructor public class Party extends Period { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -26,6 +32,22 @@ public class Party extends Period { private boolean isAgreed; + @OneToOne(fetch = FetchType.LAZY) + private Member captain; + @ManyToOne(fetch = FetchType.LAZY) private Location location; + + public void setCaptain(Member member) { + // Error code + if (member == null) { + throw new IllegalArgumentException("[EROOR] 잘못된 접근입니다. (사용자가 유효하지 않음)"); + } + + this.captain = member; + } + + public void setLocation(Location city) { + this.location = city; + } } diff --git a/src/main/java/com/codingwasabi/trti/domain/party/model/request/RequestCreatePartyDto.java b/src/main/java/com/codingwasabi/trti/domain/party/model/request/RequestCreatePartyDto.java new file mode 100644 index 0000000..05985f5 --- /dev/null +++ b/src/main/java/com/codingwasabi/trti/domain/party/model/request/RequestCreatePartyDto.java @@ -0,0 +1,29 @@ +package com.codingwasabi.trti.domain.party.model.request; + +import com.codingwasabi.trti.domain.location.model.Location; +import com.codingwasabi.trti.domain.party.model.Party; +import jdk.javadoc.doclet.Taglet; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDate; +import java.util.List; + +@Getter +@AllArgsConstructor +public class RequestCreatePartyDto { + private String title; + private String image; + private String location; + private List members; + private String[] period; + + public Party getEntityFrom() { + return Party.builder() + .title(title) + .imageLink(image) + .startDate(LocalDate.parse(period[0])) + .endDate(LocalDate.parse(period[1])) + .build(); + } +} diff --git a/src/main/java/com/codingwasabi/trti/domain/party/model/response/ResponseCreatePartyDto.java b/src/main/java/com/codingwasabi/trti/domain/party/model/response/ResponseCreatePartyDto.java new file mode 100644 index 0000000..1c9b655 --- /dev/null +++ b/src/main/java/com/codingwasabi/trti/domain/party/model/response/ResponseCreatePartyDto.java @@ -0,0 +1,15 @@ +package com.codingwasabi.trti.domain.party.model.response; + +import com.codingwasabi.trti.domain.party.model.Party; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseCreatePartyDto { + private Long id; + + public static ResponseCreatePartyDto from(Party party) { + return new ResponseCreatePartyDto(party.getId()); + } +} diff --git a/src/main/java/com/codingwasabi/trti/domain/party/repository/GroupRepository.java b/src/main/java/com/codingwasabi/trti/domain/party/repository/PartyRepository.java similarity index 73% rename from src/main/java/com/codingwasabi/trti/domain/party/repository/GroupRepository.java rename to src/main/java/com/codingwasabi/trti/domain/party/repository/PartyRepository.java index 324e052..fc5059e 100644 --- a/src/main/java/com/codingwasabi/trti/domain/party/repository/GroupRepository.java +++ b/src/main/java/com/codingwasabi/trti/domain/party/repository/PartyRepository.java @@ -3,5 +3,5 @@ import com.codingwasabi.trti.domain.party.model.Party; import org.springframework.data.jpa.repository.JpaRepository; -public interface GroupRepository extends JpaRepository { +public interface PartyRepository extends JpaRepository { }