Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3주차] 기본 과제 & 심화 과제 #6

Merged
merged 18 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- name: ✔️ application.yml 파일을 생성합니다.
run: |
cd ./src/main
cd ./api/src/main
mkdir resources
cd ./resources
echo "$APPLICATION" > ./application.yml
Expand All @@ -32,4 +32,4 @@ jobs:
run: chmod +x gradlew

- name: ✔️ gradle build 합니다.
run: ./gradlew build
run: ./gradlew api:build
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ out/
.DS_Store

### Yml ###
src/main/resources/application.yml
api/src/main/resources/application.yml
16 changes: 16 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
dependencies {
// spring boot web
implementation 'org.springframework.boot:spring-boot-starter-web'
// spring data jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// h2
runtimeOnly 'com.h2database:h2'
// domain dependency
implementation project(path: ':domain')
// common dependency
implementation project(path: ':common')
}

jar {
enabled = false
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.seminar;
package org.sopt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.sopt.seminar.global.common;
package org.sopt.api.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.sopt.seminar.global.error.ErrorStatus;
import org.sopt.common.error.ErrorStatus;
import org.springframework.http.ResponseEntity;

@Builder(access = AccessLevel.PRIVATE)
Expand All @@ -18,22 +18,22 @@ public class ApiResponse<T> {
private final T data;

private static ApiResponse<?> of(SuccessStatus successStatus) {
return ApiResponse.builder()
return builder()
.status(successStatus.getHttpStatus().value())
.message(successStatus.getMessage())
.build();
}

private static <T> ApiResponse<?> of(SuccessStatus successStatus, T data) {
return ApiResponse.builder()
return builder()
.status(successStatus.getHttpStatus().value())
.message(successStatus.getMessage())
.data(data)
.build();
}

private static ApiResponse<?> of(ErrorStatus errorStatus) {
return ApiResponse.builder()
return builder()
.status(errorStatus.getHttpStatus().value())
.message(errorStatus.getMessage())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.seminar.global.common;
package org.sopt.api.common;

import lombok.AccessLevel;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.sopt.seminar.global.error;
package org.sopt.api.error;

import lombok.extern.slf4j.Slf4j;
import org.sopt.seminar.global.common.ApiResponse;
import org.sopt.api.common.ApiResponse;
import org.sopt.common.error.BusinessException;
import org.sopt.common.error.ErrorStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -11,7 +13,7 @@
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<ApiResponse<?>> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
protected ResponseEntity<ApiResponse<?>> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) {
log.error(">>> handle: HttpRequestMethodNotSupportedException ", e);
return ApiResponse.failure(ErrorStatus.METHOD_NOT_ALLOWED);
}
Expand All @@ -23,7 +25,7 @@ protected ResponseEntity<ApiResponse<?>> handleBusinessException(final BusinessE
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ApiResponse<?>> handleException(Exception e) {
protected ResponseEntity<ApiResponse<?>> handleException(final Exception e) {
log.error(">>> handle: Exception ", e);
return ApiResponse.failure(ErrorStatus.INTERNAL_SERVER_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.sopt.seminar.domain.member.api;
package org.sopt.api.member.api;

import lombok.RequiredArgsConstructor;
import org.sopt.seminar.domain.member.dto.request.MemberSaveRequest;
import org.sopt.seminar.domain.member.dto.request.MemberUpdateRequest;
import org.sopt.seminar.domain.member.dto.response.MemberGetResponse;
import org.sopt.seminar.domain.member.dto.response.MemberSaveResponse;
import org.sopt.seminar.domain.member.service.MemberService;
import org.sopt.seminar.global.common.ApiResponse;
import org.sopt.seminar.global.common.SuccessStatus;
import org.sopt.api.common.ApiResponse;
import org.sopt.api.common.SuccessStatus;
import org.sopt.api.member.dto.request.MemberSaveRequest;
import org.sopt.api.member.dto.request.MemberUpdateRequest;
import org.sopt.api.member.dto.response.MemberGetResponse;
import org.sopt.api.member.dto.response.MemberSaveResponse;
import org.sopt.api.member.service.MemberService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.sopt.seminar.domain.member.dto.request;
package org.sopt.api.member.dto.request;

import org.sopt.seminar.domain.member.domain.Sopt;
import org.sopt.domain.member.domain.Sopt;

public record MemberSaveRequest(
String name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.api.member.dto.request;

import org.sopt.domain.member.domain.Part;

public record MemberUpdateRequest(
int generation,
Part part) {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.sopt.seminar.domain.member.dto.response;
package org.sopt.api.member.dto.response;

import lombok.AccessLevel;
import lombok.Builder;
import org.sopt.seminar.domain.member.domain.Member;
import org.sopt.seminar.domain.member.domain.Sopt;
import org.sopt.domain.member.domain.Member;
import org.sopt.domain.member.domain.Sopt;

@Builder(access = AccessLevel.PRIVATE)
public record MemberGetResponse(
Expand All @@ -13,7 +13,7 @@ public record MemberGetResponse(
Sopt sopt
) {
public static MemberGetResponse of(Member member) {
return MemberGetResponse.builder()
return builder()
.name(member.getName())
.nickname(member.getNickname())
.age(member.getAge())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.sopt.seminar.domain.member.dto.response;
package org.sopt.api.member.dto.response;

import org.sopt.seminar.domain.member.domain.Member;
import org.sopt.domain.member.domain.Member;

public record MemberSaveResponse(
Long memberId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package org.sopt.seminar.domain.member.service;
package org.sopt.api.member.service;

import lombok.RequiredArgsConstructor;
import org.sopt.seminar.domain.member.domain.Member;
import org.sopt.seminar.domain.member.domain.Sopt;
import org.sopt.seminar.domain.member.dto.request.MemberSaveRequest;
import org.sopt.seminar.domain.member.dto.request.MemberUpdateRequest;
import org.sopt.seminar.domain.member.dto.response.MemberGetResponse;
import org.sopt.seminar.domain.member.dto.response.MemberSaveResponse;
import org.sopt.seminar.domain.member.repository.MemberRepository;
import org.sopt.seminar.global.error.ConflictException;
import org.sopt.seminar.global.error.ErrorStatus;
import org.sopt.common.error.ConflictException;
import org.sopt.common.error.ErrorStatus;
import org.sopt.domain.member.domain.Member;
import org.sopt.domain.member.domain.Sopt;
import org.sopt.api.member.dto.request.MemberSaveRequest;
import org.sopt.api.member.dto.request.MemberUpdateRequest;
import org.sopt.api.member.dto.response.MemberGetResponse;
import org.sopt.api.member.dto.response.MemberSaveResponse;
import org.sopt.domain.member.repository.MemberRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.sopt.seminar.domain.member.domain.Member.createMember;
import static org.sopt.seminar.domain.member.domain.Sopt.createSopt;
import static org.sopt.domain.member.domain.Member.createMember;
import static org.sopt.domain.member.domain.Sopt.createSopt;

@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand Down
56 changes: 56 additions & 0 deletions api/src/main/java/org/sopt/api/post/api/PostApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.sopt.api.post.api;

import lombok.RequiredArgsConstructor;
import org.sopt.api.common.ApiResponse;
import org.sopt.api.common.SuccessStatus;
import org.sopt.api.post.dto.request.PostSaveOrUpdateRequest;
import org.sopt.api.post.dto.response.PostGetResponse;
import org.sopt.api.post.dto.response.PostSaveResponse;
import org.sopt.api.post.service.PostService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RequestMapping("/api/post")
@Controller
public class PostApiController {
private static final String CUSTOM_AUTHENTICATION = "X-Auth-Id";
private final PostService postService;

@PostMapping
public ResponseEntity<ApiResponse<?>> savePost(@RequestHeader(CUSTOM_AUTHENTICATION) final Long memberId,
@RequestBody final PostSaveOrUpdateRequest postSaveOrUpdateRequest) {
final PostSaveResponse postSaveResponse = postService.savePost(memberId, postSaveOrUpdateRequest);
return ApiResponse.success(SuccessStatus.CREATED, postSaveResponse);
}

@GetMapping("/{postId}")
public ResponseEntity<ApiResponse<?>> getPost(@PathVariable final Long postId) {
final PostGetResponse post = postService.getPost(postId);
return ApiResponse.success(SuccessStatus.OK, post);
}

@GetMapping
public ResponseEntity<ApiResponse<?>> getPosts(@RequestHeader(CUSTOM_AUTHENTICATION) final Long memberId,
final Pageable pageable) {
final List<PostGetResponse> posts = postService.getPosts(memberId, pageable);
return ApiResponse.success(SuccessStatus.OK, posts);
}

@PatchMapping("/{postId}")
public ResponseEntity<ApiResponse<?>> updatePost(@PathVariable final Long postId,
@RequestBody final PostSaveOrUpdateRequest postSaveOrUpdateRequest) {
postService.updatePost(postId, postSaveOrUpdateRequest);
return ApiResponse.success(SuccessStatus.OK);
}

@DeleteMapping("/{postId}")
public ResponseEntity<ApiResponse<?>> deletePost(@PathVariable final Long postId) {
postService.deletePost(postId);
return ApiResponse.success(SuccessStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.api.post.dto.request;

public record PostSaveOrUpdateRequest(
String title,
String postContent,
String categoryContent
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.api.post.dto.response;

import lombok.AccessLevel;
import lombok.Builder;
import org.sopt.domain.post.domain.Category;
import org.sopt.domain.post.domain.Post;

@Builder(access = AccessLevel.PRIVATE)
public record PostGetResponse(
String title,
String postContent,
String categoryContent
) {
public static PostGetResponse of(Post post) {
Category category = post.getCategory();
return builder()
.title(post.getTitle())
.postContent(post.getContent())
.categoryContent(category.getContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sopt.api.post.dto.response;

import org.sopt.domain.post.domain.Post;

public record PostSaveResponse(
Long postId
) {
public static PostSaveResponse of(Post post) {
return new PostSaveResponse(post.getId());
}
}
Loading