Skip to content

Commit ec13609

Browse files
author
=
committed
feat: some user operations added
1 parent b69b20a commit ec13609

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

src/main/kotlin/com/example/service/DatabaseModule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object DatabaseModule {
2222
}
2323
}
2424

25-
suspend fun <T> dbQuery(block: suspend () -> T): T =
25+
suspend fun <T> dbQuery(block: suspend () -> T): Result<T> = runCatching {
2626
newSuspendedTransaction(Dispatchers.IO) { block() }
27+
}
2728
}

src/main/kotlin/com/example/service/UserService.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import com.example.dto.UserLoginDto
55
import com.example.dto.UserRegisterDto
66
import com.example.dto.UserUpdateLocationDto
77
import kotlinx.coroutines.flow.Flow
8+
import java.util.UUID
89

910
interface UserService {
1011
suspend fun login(userLoginDto: UserLoginDto): Result<String>
1112
suspend fun register(userRegisterDto: UserRegisterDto): Result<Boolean>
12-
fun getProfile(userId: Int): Flow<UserDto>
13+
suspend fun getProfileFlow(userId: Int): Result<Flow<UserDto>>
14+
suspend fun getProfile(userId: UUID): Result<UserDto>
1315
suspend fun updateUserLocation(userId: Int, userUpdateLocationDto: UserUpdateLocationDto): Result<Boolean>
14-
}
16+
}

src/main/kotlin/com/example/service/impl/UserServiceImpl.kt

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,58 @@ import com.example.service.DatabaseModule.dbQuery
88
import com.example.service.UserService
99
import com.example.table.Users
1010
import kotlinx.coroutines.flow.Flow
11+
import kotlinx.coroutines.flow.asFlow
12+
import org.jetbrains.exposed.sql.ResultRow
1113
import org.jetbrains.exposed.sql.and
14+
import org.jetbrains.exposed.sql.insert
1215
import org.jetbrains.exposed.sql.select
16+
import java.util.UUID
1317

14-
class UserServiceImpl: UserService {
18+
class UserServiceImpl : UserService {
1519
override suspend fun login(userLoginDto: UserLoginDto): Result<String> = dbQuery {
1620
val loginStatement = Users.select {
1721
Users.email eq userLoginDto.email and (Users.password eq userLoginDto.password)
1822
}
1923
val user = loginStatement.singleOrNull()
2024
if (user != null) {
21-
Result.success("Login successful")
25+
user[Users.userId].toString()
2226
} else {
23-
Result.failure(Exception("Invalid email or password"))
27+
throw Exception("Invalid email or password")
2428
}
2529
}
2630

27-
override suspend fun register(userRegisterDto: UserRegisterDto): Result<Boolean> {
28-
TODO("Not yet implemented")
31+
override suspend fun register(userRegisterDto: UserRegisterDto): Result<Boolean> = dbQuery {
32+
val registerStatement = Users.select { Users.email eq userRegisterDto.email }
33+
val user = registerStatement.singleOrNull()
34+
if (user != null) {
35+
throw Exception("Email already exists")
36+
} else {
37+
Users.insert {
38+
it[fullName] = userRegisterDto.fullName
39+
it[email] = userRegisterDto.email
40+
it[password] = userRegisterDto.password
41+
}
42+
true
43+
}
2944
}
3045

31-
override fun getProfile(userId: Int): Flow<UserDto> {
32-
TODO("Not yet implemented")
46+
override suspend fun getProfileFlow(userId: Int): Result<Flow<UserDto>> = dbQuery {
47+
val user = Users.select { Users.id eq userId }
48+
if (user.empty()) {
49+
throw Exception("User not found")
50+
} else {
51+
val userFlow = user.map(::resultRowToUserDto).asFlow()
52+
userFlow
53+
}
54+
}
55+
56+
override suspend fun getProfile(userId: UUID): Result<UserDto> = dbQuery {
57+
val user = Users.select { Users.userId eq userId }
58+
if (user.empty()) {
59+
throw Exception("User not found")
60+
} else {
61+
resultRowToUserDto(user.single())
62+
}
3363
}
3464

3565
override suspend fun updateUserLocation(
@@ -38,6 +68,20 @@ class UserServiceImpl: UserService {
3868
): Result<Boolean> {
3969
TODO("Not yet implemented")
4070
}
71+
72+
private fun resultRowToUserDto(row: ResultRow): UserDto = UserDto(
73+
id = row[Users.id],
74+
userId = row[Users.userId].toString(),
75+
fullName = row[Users.fullName],
76+
email = row[Users.email],
77+
password = row[Users.password],
78+
phoneNumber = row[Users.phoneNumber],
79+
occupation = row[Users.occupation],
80+
employer = row[Users.employer],
81+
country = row[Users.country],
82+
latitude = row[Users.latitude],
83+
longitude = row[Users.longitude]
84+
)
4185
}
4286

4387
val userService: UserService = UserServiceImpl()

0 commit comments

Comments
 (0)