Skip to content

Commit

Permalink
Merge pull request #5 from DO-SOPT-APP3-Airbnb/feature/1
Browse files Browse the repository at this point in the history
닉네임 조회 기능 구현
  • Loading branch information
csb9427 authored Nov 24, 2023
2 parents 13977c3 + 46555f1 commit d6cdb73
Show file tree
Hide file tree
Showing 25 changed files with 234 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
shell: bash

- name: Build with Gradle
run: ./gradlew build
run: ./gradlew build -x test
shell: bash
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ out/
.vscode/

src/main/resources/application-secret.properties
src/main/resources/application-dev.yaml
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation 'mysql:mysql-connector-java:8.0.33'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.app3.server.common.config;

public class JpaAuditingConfig {
}
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions src/main/java/com/app3/server/common/dto/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.app3.server.common.dto;

import com.app3.server.common.exception.enums.SuccessType;

public record ApiResponse<T> (
int status,
String message,
T data
) {
public static <T> ApiResponse<T> success(SuccessType successType, T data) {
return new ApiResponse(successType.getHttpStatus().value(), successType.getMessage(), data);
}

public static ApiResponse error(int httpStatus, String message) {

return new ApiResponse(httpStatus, message, "");
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.app3.server.common.exception;

import com.app3.server.common.dto.ApiResponse;
import com.app3.server.common.exception.model.NotFoundException;
import com.app3.server.common.exception.model.SoptException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
protected ApiResponse handleNotFoundException(final NotFoundException e) {
return ApiResponse.error(e.getHttpStatus(), e.getMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({SoptException.class})
protected ApiResponse handleSoptException(final SoptException e) {
return ApiResponse.error(e.getHttpStatus(), e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.app3.server.common.exception.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum ErrorType {
USER_NOT_FOUND_EXCEPTION(HttpStatus.NOT_FOUND, "해당 유저을 찾을 수 없습니다."),
REGION_NOT_FOUND_EXCEPTION(HttpStatus.NOT_FOUND, "지역 이미지를 찾을 수 없습니다"),

INTERNAL_SERVER_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류입니다.");
private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.app3.server.common.exception.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum SuccessType {

USER_SEARCH_SUCCESS(HttpStatus.OK, "유저 닉네임 조회에 성공하였습니다."),
REGION_SEARCH_SUCCESS(HttpStatus.OK, "지역 이미지 조회에 성공하였습니다.")
;

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.app3.server.common.exception.model;

import com.app3.server.common.exception.enums.ErrorType;

public class NotFoundException extends SoptException{

public NotFoundException(final ErrorType errorType) {
super(errorType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.app3.server.common.exception.model;

import com.app3.server.common.exception.enums.ErrorType;
public class SoptException extends RuntimeException{
private final ErrorType error;

public SoptException(final ErrorType error){
super(error.getMessage());
this.error = error;
}
public int getHttpStatus(){
return error.getHttpStatus().value();
}
}
Empty file.
31 changes: 31 additions & 0 deletions src/main/java/com/app3/server/user/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.app3.server.user.controller;

import com.app3.server.common.dto.ApiResponse;
import com.app3.server.common.exception.enums.SuccessType;
import com.app3.server.user.controller.dto.response.UserGetResponse;
import com.app3.server.user.domain.User;
import com.app3.server.user.repository.UserJpaRepository;
import com.app3.server.user.service.UserService;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

private final UserService userService;

// 목록 조회
@GetMapping("/{id}")
public ApiResponse<User> getUserProfile(@PathVariable Long id) {
return ApiResponse.success(SuccessType.USER_SEARCH_SUCCESS, userService.getUserById(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.app3.server.user.controller.dto.response;

import com.app3.server.user.domain.User;

public record UserGetResponse (
String nickname
) {
public static UserGetResponse of(User user) {
return new UserGetResponse(
user.getNickname()
);
}
}


Empty file.
28 changes: 28 additions & 0 deletions src/main/java/com/app3/server/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.app3.server.user.domain;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nickname;


@Builder
public User(String nickname) {
this.nickname = nickname;
}
}
1 change: 0 additions & 1 deletion src/main/java/com/app3/server/user/domain/temp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.app3.server.user.repository;


import com.app3.server.user.domain.User;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserJpaRepository extends JpaRepository<User, Long> {
}
Empty file.
26 changes: 26 additions & 0 deletions src/main/java/com/app3/server/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.app3.server.user.service;

import com.app3.server.common.exception.enums.ErrorType;
import com.app3.server.common.exception.model.NotFoundException;
import com.app3.server.user.controller.dto.response.UserGetResponse;
import com.app3.server.user.domain.User;
import com.app3.server.user.repository.UserJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
@RequiredArgsConstructor
public class UserService {
private final UserJpaRepository userJpaRepository;

public User getUserById(Long id) {
return userJpaRepository.findById(id)
.orElseThrow(() -> new NotFoundException(ErrorType.USER_NOT_FOUND_EXCEPTION));
}
}

Empty file.
Empty file.
15 changes: 15 additions & 0 deletions src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: sb03sb04!!

config:
activate:
on-profile: dev

jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
hibernate:
ddl-auto: create

0 comments on commit d6cdb73

Please sign in to comment.