Skip to content

Commit

Permalink
feat: 닉네임 조회 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
csb9427 committed Nov 23, 2023
1 parent 8dc4397 commit 3659b3f
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 1 deletion.
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
28 changes: 28 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,28 @@
package com.app3.server.user.controller;

import com.app3.server.user.controller.dto.response.UserGetResponse;
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 ResponseEntity<UserGetResponse> getUserProfile(@PathVariable Long id) {
return ResponseEntity.ok(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,13 @@
package com.app3.server.user.repository;


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

public interface UserJpaRepository extends JpaRepository<User, Long> {
default User findByIdOrThrow(Long id) {
return findById(id).orElseThrow(
() -> new EntityNotFoundException("회원이 존재하지 않습니다."));
}
}
Empty file.
23 changes: 23 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,23 @@
package com.app3.server.user.service;

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 UserGetResponse getUserById(Long id) {
return UserGetResponse.of(userJpaRepository.findByIdOrThrow(id));
}
}

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 3659b3f

Please sign in to comment.