Skip to content

Commit

Permalink
Merge pull request #48 from Team-B1ND/Feature/#37
Browse files Browse the repository at this point in the history
Feature/#37
  • Loading branch information
suw0n authored Mar 26, 2024
2 parents 1766a6c + 192cbcb commit 72c0dc5
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public Response apply(@RequestBody @Valid ApplyBroadcastClubMemberReq req) {
return commandUseCase.apply(req);
}

@DeleteMapping("/{id}")
public Response delete(@PathVariable String id) {
return commandUseCase.delete(id);
}

@PatchMapping("/active/{id}")
public Response active(@PathVariable("id") String id) {
return commandUseCase.active(id);
Expand All @@ -51,8 +56,8 @@ public Response updatePassword(@RequestBody @Valid UpdatePasswordReq req) {
}

@PatchMapping("/info")
public Response updateMyInfo(@RequestBody UpdateMemberInfoReq req) {
return commandUseCase.updateMyInfo(req);
public Response updateMemberInfo(@RequestBody UpdateMemberInfoReq req) {
return commandUseCase.updateMemberInfo(req);
}

@PatchMapping("/student/info")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import b1nd.dodamcore.member.application.MemberSessionHolder;
import b1nd.dodamcore.member.domain.entity.Member;
import b1nd.dodamcore.member.domain.entity.Student;
import b1nd.dodamcore.member.domain.entity.Teacher;
import b1nd.dodamcore.member.domain.enums.AuthStatus;
import b1nd.dodamcore.member.domain.event.StudentRegisteredEvent;
import b1nd.dodamcore.member.domain.exception.ActiveMemberException;
import b1nd.dodamcore.member.domain.exception.BroadcastClubMemberDuplicateException;
import b1nd.dodamcore.member.domain.exception.MemberDuplicateException;
import b1nd.dodamcore.member.domain.exception.MemberNotFoundException;
import b1nd.dodamcore.member.domain.exception.StudentNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -29,11 +30,13 @@ public class MemberCommandUseCase {
private final ApplicationEventPublisher eventPublisher;

public Response join(JoinStudentReq req) {
checkIdDuplication(req.id());
Member member = req.mapToMember(passwordEncoder.encode(req.pw()));
Student student = req.mapToStudent(member);
throwExceptionWhenIdIsDuplicate(req.id());

service.save(member, student);
Student student = service.save(
req.mapToStudent(
encodePw(req.pw())
)
);
publishStudentRegisteredEvent(student);
return Response.created("학생 회원가입 성공");
}
Expand All @@ -43,34 +46,54 @@ private void publishStudentRegisteredEvent(Student student) {
}

public Response join(JoinTeacherReq req) {
checkIdDuplication(req.id());
Member member = req.mapToMember(passwordEncoder.encode(req.pw()));
Teacher teacher = req.mapToTeacher(member);
throwExceptionWhenIdIsDuplicate(req.id());

service.save(member, teacher);
service.save(
req.mapToTeacher(
encodePw(req.pw())
)
);
return Response.created("선생님 회원가입 성공");
}

private void checkIdDuplication(String id) {
private void throwExceptionWhenIdIsDuplicate(String id) {
if(service.checkIdDuplication(id)) {
throw new MemberDuplicateException();
}
}

private String encodePw(String rawPw) {
return passwordEncoder.encode(rawPw);
}

public Response apply(ApplyBroadcastClubMemberReq req) {
Member member = service.getById(req.id());
checkBroadcastClubMemberDuplication(member);
Member member = getMemberById(req.id());
throwExceptionWhenMemberIsBroadcastClubMember(member);

service.save(req.toEntity(member));
return Response.created("방송부원 등록 성공");
}

private void checkBroadcastClubMemberDuplication(Member member) {
private void throwExceptionWhenMemberIsBroadcastClubMember(Member member) {
if(service.checkBroadcastClubMember(member)) {
throw new BroadcastClubMemberDuplicateException();
}
}

public Response delete(String id) {
Member member = getMemberById(id);
throwExceptionWhenAuthStatusIsActive(member);

service.delete(member);
return Response.noContent("멤버 삭제 성공");
}

private void throwExceptionWhenAuthStatusIsActive(Member member) {
if(member.isActive()) {
throw new ActiveMemberException();
}
}

public Response active(String id) {
updateStatus(id, AuthStatus.ACTIVE);
return Response.ok("멤버 활성화 성공");
Expand All @@ -82,10 +105,15 @@ public Response deactivate(String id) {
}

private void updateStatus(String id, AuthStatus status) {
Member member = service.getById(id);
Member member = getMemberById(id);
member.updateStatus(status);
}

private Member getMemberById(String id) {
return service.getMemberById(id)
.orElseThrow(MemberNotFoundException::new);
}

public Response updatePassword(UpdatePasswordReq req) {
Member member = sessionHolder.current();
member.updatePw(req.password(), passwordEncoder);
Expand All @@ -94,7 +122,7 @@ public Response updatePassword(UpdatePasswordReq req) {
return Response.noContent("비밀번호 수정 성공");
}

public Response updateMyInfo(UpdateMemberInfoReq req) {
public Response updateMemberInfo(UpdateMemberInfoReq req) {
Member member = sessionHolder.current();
member.updateInfo(req.name(), req.email(), req.phone(), req.profileImage());

Expand All @@ -103,11 +131,15 @@ public Response updateMyInfo(UpdateMemberInfoReq req) {
}

public Response updateStudentInfo(UpdateStudentInfoReq req) {
Student student = service.getStudentByMember(sessionHolder.current())
.orElseThrow(StudentNotFoundException::new);
Student student = getStudentByMember(sessionHolder.current());
student.updateInfo(req.grade(), req.room(), req.number());

return Response.noContent("내 학생 정보 수정 성공");
}

private Student getStudentByMember(Member member) {
return service.getStudentByMember(member)
.orElseThrow(StudentNotFoundException::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import b1nd.dodamcore.member.domain.entity.Student;
import b1nd.dodamcore.member.domain.entity.Teacher;
import b1nd.dodamcore.member.domain.enums.AuthStatus;
import b1nd.dodamcore.member.domain.exception.MemberNotFoundException;
import b1nd.dodamcore.member.domain.vo.MemberInfoRes;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -23,32 +24,32 @@ public class MemberQueryUseCase {
private final MemberSessionHolder sessionHolder;

public ResponseData<MemberInfoRes> getById(String id) {
return ResponseData.ok("Id로 멤버 조회 성공", getInfo(service.getById(id)));
return ResponseData.ok("Id로 멤버 조회 성공", getMemberInfo(getMemberById(id)));
}

public ResponseData<MemberInfoRes> getMyInfo() {
return ResponseData.ok("내 정보 조회 성공", getInfo(sessionHolder.current()));
return ResponseData.ok("내 정보 조회 성공", getMemberInfo(sessionHolder.current()));
}

public ResponseData<List<MemberInfoRes>> searchByName(String name) {
return ResponseData.ok("이름으로 검색 성공", service.searchByName(name).parallelStream()
.map(this::getInfo)
.map(this::getMemberInfo)
.toList());
}

public ResponseData<List<MemberInfoRes>> getDeactivateMembers() {
return ResponseData.ok("비활성화된 멤버 조회 성공", service.getByStatus(AuthStatus.DEACTIVATE).parallelStream()
.map(this::getInfo)
.map(this::getMemberInfo)
.toList());
}

public ResponseData<List<MemberInfoRes>> getAll() {
return ResponseData.ok("모든 멤버 정보 조회 성공", service.getByStatus(AuthStatus.ACTIVE).parallelStream()
.map(this::getInfo)
.map(this::getMemberInfo)
.toList());
}

private MemberInfoRes getInfo(Member member) {
private MemberInfoRes getMemberInfo(Member member) {
Student student = service.getStudentByMember(member)
.orElse(null);
Teacher teacher = service.getTeacherByMember(member)
Expand All @@ -61,8 +62,13 @@ public ResponseData<Boolean> checkBroadcastClubMember() {
}

public ResponseData<Boolean> checkBroadcastClubMember(String id) {
Member member = service.getById(id);
Member member = getMemberById(id);
return ResponseData.ok("방송부원 확인 성공", service.checkBroadcastClubMember(member));
}

private Member getMemberById(String id) {
return service.getMemberById(id)
.orElseThrow(MemberNotFoundException::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@

public record JoinStudentReq(@NotEmpty String id, @NotEmpty String pw, @NotEmpty String name, @NotEmpty @Email String email,
@NotEmpty String phone, @NotNull int grade, @NotNull int room, @NotNull int number) {
public Student mapToStudent(String encodedPw) {
return Student.builder()
.member(mapToMember(encodedPw))
.grade(grade)
.room(room)
.number(number)
.build();
}

public Member mapToMember(String encodedPw) {
private Member mapToMember(String encodedPw) {
return Member.builder()
.id(id)
.pw(encodedPw)
Expand All @@ -22,13 +30,4 @@ public Member mapToMember(String encodedPw) {
.status(AuthStatus.DEACTIVATE)
.build();
}

public Student mapToStudent(Member member) {
return Student.builder()
.member(member)
.grade(grade)
.room(room)
.number(number)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@

public record JoinTeacherReq(@NotEmpty String id, @NotEmpty String pw, @NotEmpty String name, @NotEmpty @Email String email,
@NotEmpty String phone, @NotEmpty String tel, @NotEmpty String position) {
public Teacher mapToTeacher(String encodedPw) {
return Teacher.builder()
.member(mapToMember(encodedPw))
.tel(tel)
.position(position)
.build();
}

public Member mapToMember(String encodedPw) {
private Member mapToMember(String encodedPw) {
return Member.builder()
.id(id)
.pw(encodedPw)
Expand All @@ -21,12 +28,4 @@ public Member mapToMember(String encodedPw) {
.status(AuthStatus.DEACTIVATE)
.build();
}

public Teacher mapToTeacher(Member member) {
return Teacher.builder()
.member(member)
.tel(tel)
.position(position)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import b1nd.dodamcore.member.domain.entity.*;
import b1nd.dodamcore.member.domain.enums.AuthStatus;
import b1nd.dodamcore.member.domain.exception.MemberNotFoundException;
import b1nd.dodamcore.member.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -21,22 +20,28 @@ public class MemberService {
private final TeacherRepository teacherRepository;
private final BroadcastClubMemberRepository broadcastClubMemberRepository;

public void save(Member member, Student student) {
memberRepository.save(member);
studentRepository.save(student);
public Member save(Member member) {
return memberRepository.save(member);
}

public void save(Member member, Teacher teacher) {
memberRepository.save(member);
teacherRepository.save(teacher);
public Student save(Student student) {
memberRepository.save(student.getMember());
return studentRepository.save(student);
}

public void save(Member member) {
memberRepository.save(member);
public Teacher save(Teacher teacher) {
memberRepository.save(teacher.getMember());
return teacherRepository.save(teacher);
}

public void save(BroadcastClubMember broadcastClubMember) {
broadcastClubMemberRepository.save(broadcastClubMember);
public BroadcastClubMember save(BroadcastClubMember broadcastClubMember) {
return broadcastClubMemberRepository.save(broadcastClubMember);
}

public void delete(Member member) {
studentRepository.deleteByMember(member);
teacherRepository.deleteByMember(member);
memberRepository.delete(member);
}

public boolean checkIdDuplication(String id) {
Expand All @@ -47,9 +52,8 @@ public boolean checkBroadcastClubMember(Member member) {
return broadcastClubMemberRepository.existsByMember(member);
}

public Member getById(String id) {
return memberRepository.findById(id)
.orElseThrow(MemberNotFoundException::new);
public Optional<Member> getMemberById(String id) {
return memberRepository.findById(id);
}

public Optional<Student> getStudentByMember(Member member) {
Expand All @@ -68,8 +72,4 @@ public List<Member> getByStatus(AuthStatus status) {
return memberRepository.findByStatus(status);
}

public List<Member> getAll() {
return memberRepository.findAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public void updateInfo(String name, String email, String phone, String profileIm
this.profileImage = ModifyUtil.modifyIfNotNull(profileImage, this.profileImage);
}

public boolean isActive() {
return AuthStatus.ACTIVE == status;
}

@Builder
public Member(String id, String pw, String name, String email, MemberRole role, AuthStatus status, String profileImage, String phone) {
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package b1nd.dodamcore.member.domain.exception;

import b1nd.dodamcore.common.exception.custom.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.FORBIDDEN)
public class ActiveMemberException extends CustomException {

public ActiveMemberException() {
super(MemberExceptionCode.ACTIVE_MEMBER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum MemberExceptionCode implements ExceptionCode {

WRONG_PASSWORD(HttpStatus.UNAUTHORIZED, "잘못된 비밀번호"),
DEACTIVATE_MEMBER(HttpStatus.FORBIDDEN, "비활성화된 멤버"),
ACTIVE_MEMBER(HttpStatus.FORBIDDEN, "활성화된 멤버"),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "없는 멤버"),
STUDENT_NOT_FOUND(HttpStatus.NOT_FOUND, "없는 학생"),
PARENT_NOT_FOUND(HttpStatus.NOT_FOUND, "없는 부모님 정보"),
Expand Down
Loading

0 comments on commit 72c0dc5

Please sign in to comment.