forked from gothinkster/realworld-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
216 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 62 additions & 5 deletions
67
backend/src/main/kotlin/org/kotlin/everywhere/realworld/api.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,72 @@ | ||
package org.kotlin.everywhere.realworld | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.kotlin.everywhere.net.Kenet | ||
import org.kotlin.everywhere.net.invoke | ||
import java.util.* | ||
|
||
class Api : Kenet() { | ||
val greeting by c<String, String>() | ||
val signUp by c<SignUpReq, SignUpRes>() | ||
val signIn by c<SignInReq, SignInRes>() | ||
} | ||
|
||
@Serializable | ||
class SignUpReq(val name: String, val email: String, val password: String) | ||
|
||
@Serializable | ||
class SignUpRes(val errors: List<String> = listOf()) | ||
|
||
@Serializable | ||
class SignInReq(val email: String, val password: String) | ||
|
||
@Serializable | ||
data class Res<T : Any>(val errors: List<String>, val data: T? = null) | ||
|
||
@Serializable | ||
class SignInRes(val errors: List<String> = listOf(), val data: Data? = null) { | ||
@Serializable | ||
class Data( | ||
val name: String, | ||
val email: String, | ||
val note: String, | ||
val profilePictureUrl: String, | ||
val accessToken: String, | ||
) | ||
} | ||
|
||
fun Api.init() { | ||
greeting { | ||
"Hello, named = $it!" | ||
signUp { req -> | ||
val emailTaken = users.any { it.email == req.email } | ||
if (emailTaken) { | ||
return@signUp SignUpRes(errors = listOf("That email is already taken")) | ||
} | ||
users.add(User(name = req.name, email = req.email, password = req.password)) | ||
SignUpRes() | ||
} | ||
println("greeting = $greeting") | ||
} | ||
|
||
signIn { req -> | ||
val user = users.firstOrNull { it.email == req.email && it.password == req.password } | ||
?: return@signIn SignInRes(errors = listOf("Email or password is worng")) | ||
|
||
val accessToken = UUID.randomUUID().toString() | ||
user.accessTokens.add(accessToken) | ||
SignInRes(data = SignInRes.Data( | ||
name = user.name, | ||
email = user.email, | ||
note = user.note, | ||
profilePictureUrl = user.profilePictureUrl, | ||
accessToken = accessToken, | ||
)) | ||
} | ||
} | ||
|
||
data class User( | ||
val name: String, | ||
val email: String, | ||
val password: String, | ||
val accessTokens: MutableList<String> = mutableListOf(), | ||
val note: String = "", | ||
val profilePictureUrl: String = "", | ||
) | ||
|
||
val users = mutableListOf<User>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Api } from "./api/api"; | ||
|
||
export const api = new Api("http://localhost:5000"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { makeAutoObservable } from "mobx"; | ||
|
||
export class SiteModel { | ||
user: SiteUser | null = null; | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
} | ||
|
||
setUser(user: SiteUser) { | ||
this.user = user; | ||
} | ||
} | ||
|
||
interface SiteUser { | ||
accessToken: string; | ||
profilePictureUrl: string; | ||
name: string; | ||
note: string; | ||
} | ||
|
||
export const siteModel = new SiteModel(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.