Skip to content

Commit

Permalink
Merge pull request #21 from CodingWasabi/feature-19/createGroup
Browse files Browse the repository at this point in the history
[FEAT] 파티 생성
  • Loading branch information
daehwan2yo authored Dec 28, 2021
2 parents 3381db4 + 434b361 commit 44de9cf
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Location, Long> {
Optional<Location> findByCity(City city);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseMyInfoDto> getMyInfo(@AuthenticationPrincipal MemberAdaptor memberAdaptor) {
return ResponseEntity.ok()
.body(memberService.getMemberInfo(memberAdaptor.getMember()));
}

@PostMapping("/member")
public ResponseEntity<ResponseExistMemberDto> existMember(@RequestBody RequestExistMemberDto requestDto) {
return ResponseEntity.ok(memberService.existMember(requestDto));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

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;

import javax.persistence.*;

@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MemberInParty {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.codingwasabi.trti.domain.memberInParty.model.MemberInParty;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberInGroupRepository extends JpaRepository<MemberInParty, Long> {
public interface MemberInPartyRepository extends JpaRepository<MemberInParty, Long> {
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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<Long> memberIdList) {
memberIdList.stream()
.map((id) -> memberRepository
.findById(id)
.orElseThrow(() -> new IllegalArgumentException("[ERROR] 존재하지 않는 회원의 id가 입력되었습니다.")))
.forEach((member) -> memberInPartyRepository.save(MemberInParty.of(party, member)));
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/codingwasabi/trti/domain/party/model/Party.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Long> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.codingwasabi.trti.domain.party.model.Party;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GroupRepository extends JpaRepository<Party, Long> {
public interface PartyRepository extends JpaRepository<Party, Long> {
}

0 comments on commit 44de9cf

Please sign in to comment.