Skip to content

Commit

Permalink
✨ feat: 그룹 생성 API application (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
loveysuby committed Jul 12, 2024
1 parent 168681e commit da10fc4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package slvtwn.khu.toyouserver.application;

import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.common.ErrorType;
Expand All @@ -14,23 +15,31 @@
@Transactional(readOnly = true)
public class GroupService {

private final GroupRepository groupRepository;
private final UserRepository userRepository;
private final GroupRepository groupRepository;
private final UserRepository userRepository;

public GroupService(GroupRepository groupRepository, UserRepository userRepository) {
this.groupRepository = groupRepository;
this.userRepository = userRepository;
}
public GroupService(GroupRepository groupRepository, UserRepository userRepository) {
this.groupRepository = groupRepository;
this.userRepository = userRepository;
}

@Transactional
public GroupResponse registerUser(long groupId, long userId) {
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND));
@Transactional
public GroupResponse registerUser(long groupId, long userId) {
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND));

// TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다.
// TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다.
// group.addMember(user);
return new GroupResponse(group.getId(), group.getName());
}
return new GroupResponse(group.getId(), group.getName());
}

@Transactional
public GroupResponse create(String name) {
return Optional.of(new Group(name))
.map(groupRepository::save)
.map(group -> new GroupResponse(group.getId(), group.getName()))
.orElseThrow(() -> new ToyouException(ErrorType.INVALID_GROUP_DATA));
}
}
2 changes: 2 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/common/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ public enum ErrorType {
// 400 ~ 499 (요청 오류)
RESPONSE_FORMAT_ERROR(HttpStatus.BAD_REQUEST, "TYU-400", "응답 형식 오류"),
BAD_REQUEST(HttpStatus.BAD_REQUEST, "TYU-400", "잘못된 요청입니다."),
INVALID_GROUP_DATA(HttpStatus.BAD_REQUEST, "TYU-4001", "Invalid group data provided"),
NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-404", "요청한 자원을 찾을 수 없습니다."),
GROUP_NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-4041", "그룹을 찾을 수 없습니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-4042", "사용자를 찾을 수 없습니다."),

// 500 ~ 599 (서버 오류)
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "TYU-500", "서버 내부 오류");


ErrorType(
final HttpStatusCode httpStatusCode,
final String code,
Expand Down

0 comments on commit da10fc4

Please sign in to comment.