Skip to content

Commit

Permalink
Merge pull request #3 from soma-baekgu/BG-139-persist-user
Browse files Browse the repository at this point in the history
[BG-139]: 유저 영속화 (4h / 1h)
  • Loading branch information
GGHDMS authored Jun 25, 2024
2 parents 9775608 + f2fbf6a commit 7f05321
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ out/

### Kotlin ###
.kotlin

.Ds_Store
35 changes: 35 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

plugins {
kotlin("jvm")
}

group = "com.backgu.amaker"
version = "0.0.1-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
implementation(project(":domain"))
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")

testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}

tasks.getByName<Jar>("bootJar") {
enabled = false
}

tasks.getByName<Jar>("jar") {
enabled = true
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.backgu.amaker

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.runApplication

@SpringBootApplication
class AMakerBeApplication
@EntityScan(basePackages = ["com.backgu.amaker"])
class AMakerApiApplication

fun main(args: Array<String>) {
runApplication<AMakerBeApplication>(*args)
runApplication<AMakerApiApplication>(*args)
}
7 changes: 7 additions & 0 deletions api/src/main/kotlin/com/backgu/amaker/user/dto/UserRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.backgu.amaker.user.dto

class UserRequest(
val name: String,
val email: String,
val picture: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backgu.amaker.user.repository

import com.backgu.amaker.user.domain.User
import org.springframework.data.jpa.repository.JpaRepository
import java.util.UUID

interface UserRepository : JpaRepository<User, UUID> {
fun findByEmail(email: String): User?
}
25 changes: 25 additions & 0 deletions api/src/main/kotlin/com/backgu/amaker/user/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.backgu.amaker.user.service

import com.backgu.amaker.user.domain.User
import com.backgu.amaker.user.dto.UserRequest
import com.backgu.amaker.user.repository.UserRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.UUID

@Service
@Transactional(readOnly = true)
class UserService(
val userRepository: UserRepository,
) {
@Transactional
fun saveUser(request: UserRequest): UUID =
userRepository
.save(
User(
name = request.name,
email = request.email,
picture = request.picture,
),
).id
}
16 changes: 16 additions & 0 deletions api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


spring:
application:
name: a-maker

datasource:
url: jdbc:mysql://localhost:3306/amaker
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
database: mysql
hibernate:
ddl-auto: create
14 changes: 14 additions & 0 deletions api/src/test/kotlin/com/backgu/amaker/user/fixture/UserFixture.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.backgu.amaker.user.fixture

import com.backgu.amaker.user.dto.UserRequest

class UserFixture {
companion object {
fun createUserRequest() =
UserRequest(
name = "name",
email = "email",
picture = "picture",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.backgu.amaker.user.service

import com.backgu.amaker.user.fixture.UserFixture
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class UserServiceTest {
@Autowired
private lateinit var userService: UserService

@Test
@DisplayName("사용자 저장 테스트")
fun saveUser() {
// given
val request = UserFixture.createUserRequest()

// when
val result = userService.saveUser(request)

// then
assertThat(result).isNotNull()
}
}
16 changes: 16 additions & 0 deletions api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


spring:
application:
name: a-maker

datasource:
username: sa
password:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test;MODE=MySQL

jpa:
database: mysql
hibernate:
ddl-auto: create
66 changes: 41 additions & 25 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "3.3.0"
id("io.spring.dependency-management") version "1.1.5"
id("org.springframework.boot") version "3.3.0" apply false
id("io.spring.dependency-management") version "1.1.5" apply false
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
kotlin("jvm") version "1.9.24"
kotlin("plugin.spring") version "1.9.24"
kotlin("plugin.jpa") version "1.9.24" apply false
kotlin("plugin.spring") version "1.9.24" apply false
}

group = "com.baek9"
version = "0.0.1-SNAPSHOT"
allprojects {
group = "com.backgu.amaker"
version = "0.0.1-SNAPSHOT"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
repositories {
mavenCentral()
}
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}

kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict")
tasks.withType<Test> {
useJUnitPlatform()
}

apply(plugin = "org.jlleitschuh.gradle.ktlint")
}

tasks.withType<Test> {
useJUnitPlatform()
subprojects {
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "org.jetbrains.kotlin.plugin.spring")
apply(plugin = "org.jetbrains.kotlin.plugin.jpa")

dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("io.github.oshai:kotlin-logging-jvm:5.1.1")
implementation("com.mysql:mysql-connector-j:8.4.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly("com.h2database:h2")
}
}
30 changes: 30 additions & 0 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

plugins {
kotlin("jvm")
}

group = "com.backgu.amaker"
version = "0.0.1-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}

tasks.getByName<Jar>("bootJar") {
enabled = false
}

tasks.getByName<Jar>("jar") {
enabled = true
}
10 changes: 10 additions & 0 deletions domain/src/main/kotlin/com/backgu/amaker/user/domain/Role.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.backgu.amaker.user.domain

enum class Role(
var key: String,
var value: String,
) {
USER("ROLE_USER", "User"),
MANAGER("ROLE_MANAGER", "Manager"),
ADMIN("ROLE_ADMIN", "Admin"),
}
25 changes: 25 additions & 0 deletions domain/src/main/kotlin/com/backgu/amaker/user/domain/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.backgu.amaker.user.domain

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.Id
import jakarta.persistence.Table
import java.util.UUID

@Entity
@Table(name = "users")
class User(
@Id
var id: UUID = UUID.randomUUID(),
@Column(nullable = false)
var name: String,
@Column(nullable = false)
val email: String,
@Column(nullable = false)
var picture: String,
@Enumerated(EnumType.STRING)
@Column(nullable = false)
val role: Role = Role.USER,
)
5 changes: 5 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
rootProject.name = "A-Maker-BE"
include("domain")
include("api")
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

13 changes: 0 additions & 13 deletions src/test/kotlin/com/backgu/amaker/AMakerBeApplicationTests.kt

This file was deleted.

0 comments on commit 7f05321

Please sign in to comment.