Skip to content

Commit

Permalink
🚀 :: Api-v0.0.7
Browse files Browse the repository at this point in the history
🚀 :: Api-v0.0.7
  • Loading branch information
ImNM authored Jan 26, 2023
2 parents 2ce28e9 + 8e4a909 commit 3bad6f9
Show file tree
Hide file tree
Showing 104 changed files with 2,030 additions and 358 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package band.gosrock.api.comment.controller;


import band.gosrock.api.comment.model.request.CreateCommentRequest;
import band.gosrock.api.comment.model.response.CreateCommentResponse;
import band.gosrock.api.comment.model.response.RetrieveCommentListResponse;
import band.gosrock.api.comment.service.CreateCommentUseCase;
import band.gosrock.api.comment.service.RetrieveCommentUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SecurityRequirement(name = "access-token")
@Tag(name = "응원톡 컨트롤러")
@RestController
@RequestMapping("/v1/event/{eventId}/comments")
@RequiredArgsConstructor
public class CommentController {

private final CreateCommentUseCase createCommentUseCase;

private final RetrieveCommentUseCase retrieveCommentUseCase;

@Operation(summary = "응원글을 생성합니다.")
@PostMapping
public CreateCommentResponse postComment(
@RequestBody @Valid CreateCommentRequest createCommentRequest,
@PathVariable Long eventId) {
return createCommentUseCase.execute(eventId, createCommentRequest);
}

@Operation(summary = "응원글을 조회합니다.")
@GetMapping
public RetrieveCommentListResponse getComments(
@PathVariable Long eventId, @RequestParam(required = false) Long lastId) {
return retrieveCommentUseCase.execute(eventId, lastId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package band.gosrock.api.comment.mapper;


import band.gosrock.api.comment.model.request.CreateCommentRequest;
import band.gosrock.api.comment.model.response.CreateCommentResponse;
import band.gosrock.api.comment.model.response.RetrieveCommentListResponse;
import band.gosrock.common.annotation.Mapper;
import band.gosrock.domain.domains.comment.adaptor.CommentAdaptor;
import band.gosrock.domain.domains.comment.domain.Comment;
import band.gosrock.domain.domains.comment.dto.condition.CommentCondition;
import band.gosrock.domain.domains.event.domain.Event;
import band.gosrock.domain.domains.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import org.springframework.transaction.annotation.Transactional;

@Mapper
@RequiredArgsConstructor
public class CommentMapper {

private final CommentAdaptor commentAdaptor;

public Comment toEntity(User user, Event event, CreateCommentRequest createDTO) {
return Comment.create(createDTO.getContent(), createDTO.getNickName(), user, event.getId());
}

@Transactional(readOnly = true)
public CreateCommentResponse toCreateCommentResponse(Comment comment, User user) {
return CreateCommentResponse.of(comment, user);
}

@Transactional(readOnly = true)
public RetrieveCommentListResponse toRetrieveCommentListResponse(
CommentCondition commentCondition, Long currentUserId) {
Slice<Comment> comments = commentAdaptor.searchComment(commentCondition);
return RetrieveCommentListResponse.of(comments, currentUserId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package band.gosrock.api.comment.model.request;


import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.Getter;

@Getter
public class CreateCommentRequest {

@NotBlank(message = "작성자 닉네임을 입력해주세요.")
@Size(min = 1, max = 15)
private String nickName;

@NotBlank(message = "댓글 내용을 입력해주세요.")
@Size(min = 1, max = 200)
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package band.gosrock.api.comment.model.response;


import band.gosrock.common.annotation.DateFormat;
import band.gosrock.domain.common.vo.UserInfoVo;
import band.gosrock.domain.domains.comment.domain.Comment;
import band.gosrock.domain.domains.user.domain.User;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CreateCommentResponse {

private final Long id;

private final String nickName;

private final String content;

@DateFormat private final LocalDateTime createdAt;

private final UserInfoVo userInfoVo;

public static CreateCommentResponse of(Comment comment, User user) {
return CreateCommentResponse.builder()
.id(comment.getId())
.nickName(comment.getNickName())
.content(comment.getContent())
.createdAt(comment.getCreatedAt())
.userInfoVo(user.toUserInfoVo())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package band.gosrock.api.comment.model.response;


import band.gosrock.domain.common.vo.CommentInfoVo;
import band.gosrock.domain.domains.comment.domain.Comment;
import java.util.Objects;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class RetrieveCommentDTO {

private final CommentInfoVo commentInfo;

private final Boolean isMine;

public static RetrieveCommentDTO of(Comment comment, Long currentUserId) {
return RetrieveCommentDTO.builder()
.commentInfo(comment.toCommentInfoVo())
.isMine(Objects.equals(comment.getUser().getId(), currentUserId))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package band.gosrock.api.comment.model.response;


import band.gosrock.domain.domains.comment.domain.Comment;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.domain.Slice;

@Getter
@Builder
public class RetrieveCommentListResponse {

private final Boolean hasNext;

private final List<RetrieveCommentDTO> comments;

public static RetrieveCommentListResponse of(Slice<Comment> comments, Long currentUserId) {
return RetrieveCommentListResponse.builder()
.hasNext(comments.hasNext())
.comments(
comments.stream()
.map(comment -> RetrieveCommentDTO.of(comment, currentUserId))
.toList())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package band.gosrock.api.comment.service;


import band.gosrock.api.comment.mapper.CommentMapper;
import band.gosrock.api.comment.model.request.CreateCommentRequest;
import band.gosrock.api.comment.model.response.CreateCommentResponse;
import band.gosrock.api.common.UserUtils;
import band.gosrock.common.annotation.UseCase;
import band.gosrock.domain.domains.comment.adaptor.CommentAdaptor;
import band.gosrock.domain.domains.comment.domain.Comment;
import band.gosrock.domain.domains.event.adaptor.EventAdaptor;
import band.gosrock.domain.domains.event.domain.Event;
import band.gosrock.domain.domains.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
public class CreateCommentUseCase {

private final UserUtils userUtils;

private final CommentMapper commentMapper;

private final CommentAdaptor commentAdaptor;

private final EventAdaptor eventAdaptor;

@Transactional
public CreateCommentResponse execute(Long eventId, CreateCommentRequest createDTO) {
User currentUser = userUtils.getCurrentUser();
Event event = eventAdaptor.findById(eventId);
Comment comment =
commentAdaptor.save(commentMapper.toEntity(currentUser, event, createDTO));
return commentMapper.toCreateCommentResponse(comment, currentUser);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package band.gosrock.api.comment.service;


import band.gosrock.api.comment.mapper.CommentMapper;
import band.gosrock.api.comment.model.response.RetrieveCommentListResponse;
import band.gosrock.api.common.UserUtils;
import band.gosrock.common.annotation.UseCase;
import band.gosrock.domain.domains.comment.dto.condition.CommentCondition;
import band.gosrock.domain.domains.event.adaptor.EventAdaptor;
import band.gosrock.domain.domains.event.domain.Event;
import lombok.RequiredArgsConstructor;

@UseCase
@RequiredArgsConstructor
public class RetrieveCommentUseCase {

private final CommentMapper commentMapper;

private final EventAdaptor eventAdaptor;

private final UserUtils userUtils;

public RetrieveCommentListResponse execute(Long eventId, Long lastId) {
Event event = eventAdaptor.findById(eventId);
Long currentUserId = userUtils.getCurrentUserId();
CommentCondition commentCondition = new CommentCondition(event.getId(), lastId);
return commentMapper.toRetrieveCommentListResponse(commentCondition, currentUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import band.gosrock.common.exception.GlobalErrorCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -111,6 +113,40 @@ public ResponseEntity<ErrorResponse> DuDoongCodeExceptionHandler(
.body(errorResponse);
}

/** Request Param Validation 예외 처리 */
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ErrorResponse> ConstraintViolationExceptionHandler(
ConstraintViolationException e, HttpServletRequest request) {
Map<String, Object> bindingErrors = new HashMap<>();
e.getConstraintViolations()
.forEach(
constraintViolation -> {
List<String> propertyPath =
List.of(
constraintViolation
.getPropertyPath()
.toString()
.split("\\."));
String path =
propertyPath.stream()
.skip(propertyPath.size() - 1L)
.findFirst()
.orElse(null);
bindingErrors.put(path, constraintViolation.getMessage());
});

ErrorReason errorReason =
ErrorReason.builder()
.code("BAD_REQUEST")
.status(400)
.reason(bindingErrors.toString())
.build();
ErrorResponse errorResponse =
new ErrorResponse(errorReason, request.getRequestURL().toString());
return ResponseEntity.status(HttpStatus.valueOf(errorReason.getStatus()))
.body(errorResponse);
}

@ExceptionHandler(DuDoongDynamicException.class)
public ResponseEntity<ErrorResponse> DuDoongDynamicExceptionHandler(
DuDoongDynamicException e, HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@


import band.gosrock.api.event.model.dto.request.CreateEventRequest;
import band.gosrock.api.event.model.dto.request.UpdateEventBasicRequest;
import band.gosrock.api.event.model.dto.request.UpdateEventDetailRequest;
import band.gosrock.api.event.model.dto.response.EventResponse;
import band.gosrock.api.event.service.CreateEventUseCase;
import band.gosrock.api.event.service.ReadEventUseCase;
import band.gosrock.api.event.service.ReadEventListUseCase;
import band.gosrock.api.event.service.UpdateEventBasicUseCase;
import band.gosrock.api.event.service.UpdateEventDetailUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -20,24 +24,37 @@
@RequiredArgsConstructor
public class EventController {

private final ReadEventListUseCase readHostEventListUseCase;
private final CreateEventUseCase createEventUseCase;
private final ReadEventUseCase readEventUseCase;
private final UpdateEventBasicUseCase updateEventBasicUseCase;
private final UpdateEventDetailUseCase updateEventDetailUseCase;

@Operation(summary = "내가 관리하는 이벤트 리스트를 가져옵니다")
// todo :: querydsl + 검색 기능 작동하도록 만들기
@Operation(summary = "특정 호스트가 관리 중인 이벤트 리스트를 가져옵니다")
@GetMapping
public List<EventResponse> getAllEvents() {
return readEventUseCase.execute();
public List<EventResponse> getAllEventByHostId(@RequestParam Long hostId) {
return readHostEventListUseCase.execute(hostId);
}

// @Operation(summary = "특정 이벤트의 상세 정보를 가져옵니다")
// @GetMapping("/{eventId}")
// public List<EventResponse> getEventById() {
// return readEventUseCase.execute();
// }

@Operation(summary = "새로운 이벤트(공연)를 생성합니다")
@Operation(summary = "공연 기본 정보를 등록하여, 새로운 이벤트(공연)를 생성합니다")
@PostMapping
public EventResponse createEvent(@RequestBody @Valid CreateEventRequest createEventRequest) {
return createEventUseCase.execute(createEventRequest);
}

@Operation(summary = "공연 기본 정보를 등록하여, 새로운 이벤트(공연)를 생성합니다")
@PatchMapping("/{eventId}/basic")
public EventResponse updateEventBasic(
@PathVariable Long eventId,
@RequestBody @Valid UpdateEventBasicRequest updateEventBasicRequest) {
return updateEventBasicUseCase.execute(eventId, updateEventBasicRequest);
}

@Operation(summary = "공연 상세 정보를 등록합니다.")
@PatchMapping("/{eventId}/details")
public EventResponse updateEventDetail(
@PathVariable Long eventId,
@RequestBody @Valid UpdateEventDetailRequest updateEventDetailRequest) {
return updateEventDetailUseCase.execute(eventId, updateEventDetailRequest);
}
}
Loading

0 comments on commit 3bad6f9

Please sign in to comment.