-
Notifications
You must be signed in to change notification settings - Fork 0
Temporary work #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example | ||
|
|
||
| import com.example.dao.CardsDAO | ||
| import com.example.dao.UserLoginInfoDAO | ||
| import com.example.models.Card | ||
| import com.example.plugins.CardItem | ||
| import com.example.plugins.User | ||
| import com.example.plugins.UserRegister | ||
| import com.example.repository.CardsRepository | ||
| import com.example.repository.UserLoginInfoRepository | ||
| import com.example.repository.UserRepository | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.jetbrains.exposed.sql.transactions.transaction | ||
|
|
||
| class CardService { | ||
| private val cardRepository: CardsRepository = CardsDAO() | ||
| private val userRepository: UserLoginInfoRepository =UserLoginInfoDAO() | ||
| fun addCard(card: CardItem): Boolean { | ||
| transaction { Card.new { namee=card.namee;title=card.title;info=card.info;contact=card.contacts;image=card.image; } } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно же тут вызывать add из CardRepository |
||
| return true | ||
| } | ||
| fun getCards(username: String): List<Card>? | ||
| { | ||
| return cardRepository.getBunchOfCards(username) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.example.dao | ||
|
|
||
| import com.example.models.Card | ||
| import com.example.models.cards | ||
| import com.example.models.User | ||
| import com.example.repository.CardsRepository | ||
|
|
||
| import org.jetbrains.exposed.sql.SqlExpressionBuilder.neq | ||
| import org.jetbrains.exposed.sql.select | ||
| import org.jetbrains.exposed.sql.selectAll | ||
| import org.jetbrains.exposed.sql.transactions.transaction | ||
|
|
||
| class CardsDAO:CardsRepository { | ||
| override fun add(card: Card)= | ||
| transaction { | ||
| Card.new { namee=card.namee;title=card.title;info=card.info;contact=card.contact;image=card.image; } | ||
| } | ||
|
|
||
| override fun getBunchOfCards(name: String)= transaction { cards | ||
| .select(cards.namee neq name) | ||
| .limit(10).let { Card.wrapRows(it) }.toList() } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.example.models | ||
|
|
||
| import io.ktor.server.util.* | ||
| import org.jetbrains.exposed.dao.IntEntity | ||
| import org.jetbrains.exposed.dao.IntEntityClass | ||
| import org.jetbrains.exposed.dao.id.EntityID | ||
| import org.jetbrains.exposed.dao.id.IntIdTable | ||
|
|
||
|
|
||
| class Card(id: EntityID<Int>) : IntEntity(id) { | ||
| companion object : IntEntityClass<Card>(cards) | ||
| var namee by cards.namee | ||
| var title by cards.title | ||
| var info by cards.info | ||
| var contact by cards.contact | ||
| var image by cards.image | ||
|
|
||
|
|
||
| } | ||
| object cards : IntIdTable() { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. С большой буквы Cards |
||
| val namee = varchar("namee", 40) | ||
| val title = varchar("title", 40) | ||
| val info = varchar("info",250) | ||
| val image= varchar("image",500) | ||
| val contact = varchar("telegram", 50) | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| package com.example.plugins | ||
|
|
||
| import com.example.CardService | ||
| import com.example.dao.CardsDAO | ||
| import io.ktor.server.routing.* | ||
| import io.ktor.server.application.* | ||
| import io.ktor.server.auth.* | ||
| import io.ktor.server.request.* | ||
| import io.ktor.server.response.* | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
|
|
||
| @Serializable | ||
| data class CardItem(val namee:String,val title:String, val info:String, val contacts:String, val image:String) | ||
| fun Application.configureRouting() { | ||
| routing { | ||
| authorizedRouting() | ||
| unauthorizedRouting() | ||
| cardsRouting() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. роутинг надо в один из двух пихать, либо в авторизованные, либо не в авторизованные, скорее в первое |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -25,4 +33,18 @@ fun Route.unauthorizedRouting() { | |
| call.respondText("Hello, world!") | ||
| } | ||
| } | ||
| fun Route.cardsRouting() | ||
| { | ||
| get("/get_cards") | ||
| { | ||
| val cards=CardService().getCards(call.receiveText()) | ||
| if (cards != null) { | ||
| call.respond(cards.map {CardItem(it.namee,it.title,it.info,it.contact,it.image) }) | ||
| } | ||
| } | ||
| post("/add_card") { | ||
| val cardItem=call.receive<CardItem>() | ||
| CardService().addCard(cardItem) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.repository | ||
|
|
||
| import com.example.models.Card | ||
| import com.example.models.User | ||
|
|
||
|
|
||
| interface CardsRepository { | ||
| fun add(card: Card) : Card | ||
| fun getBunchOfCards(name: String):List<Card> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.example.xiinder | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class Card(val namee:String, val title:String, val info:String, val contacts:String, val image:String) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. может просто name а не namee? и что такое имя карточки, может лучше title, если это про заголовок
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Видимо не заголовок, непонятно в общем
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Username имелся в виду, все исправлю |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,49 @@ | ||
| package com.example.xiinder | ||
|
|
||
| import android.widget.Toast | ||
| import androidx.core.content.ContentProviderCompat.requireContext | ||
| import androidx.lifecycle.ViewModel | ||
| import com.example.xiinder.network.CardInfo | ||
| import com.example.xiinder.network.ProfileInfo | ||
| import io.ktor.client.call.* | ||
| import io.ktor.client.plugins.* | ||
| import io.ktor.client.request.* | ||
| import io.ktor.client.utils.EmptyContent.contentType | ||
| import io.ktor.http.* | ||
| import kotlinx.coroutines.coroutineScope | ||
|
|
||
| class DataSetForCards { | ||
| fun loadCards():Map<Int, CardInfo> | ||
| suspend fun loadCards(viewModel:SharedViewModel):Map<Int, CardInfo> | ||
| { | ||
| return hashMapOf( | ||
| 0 to CardInfo(0,1,"Ищу сокомандника на icpc", "Let's go drink",R.drawable.mobile_01), | ||
| 1 to CardInfo(1,2,"Помогите с задачей по матанализу", "Let's go study",R.drawable.mobile_02), | ||
| 2 to CardInfo(2,3,"Продаю операционные системы Танненбаума", "Let's go sing",R.drawable.mobile_03), | ||
| 3 to CardInfo(3,4,"Нужен человек, знающий js, для работы в проекте", "Let's go swim",R.drawable.mobile_04), | ||
| 4 to CardInfo(4,5,"Придумайте заголовки за меня", "Let's go eat",R.drawable.mobile_05), | ||
| 5 to CardInfo(5,6,"Ваше такси до Верхней Пышмы", "Let's go",R.drawable.mobile_06) | ||
| ) | ||
| val message = viewModel.client.get("http://192.168.0.10:8888/get_cards"){ // or your data class | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вынеси это(ip:port) куда нибудь в константу, или в conf файл, если еще нет, не надо дублировать |
||
| contentType(ContentType.Application.Json) | ||
| setBody(viewModel.getUsername()) | ||
| timeout { requestTimeoutMillis=10000 } | ||
| } | ||
| when (message.status.value) | ||
| { | ||
| 200-> | ||
| { | ||
| var a=message.body<List<Card>>() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. называй одинаковые модели которые гуляют по хттп одинаково, что на бэке то и на фронте |
||
| print(a) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем принт, ненужное убираем |
||
| a.map { CardInfo(0,1,it.title,it.info,R.drawable.mobile_01) } | ||
| return a.associate { a.indexOf(it) to CardInfo(0, 1, it.title, it.info, R.drawable.mobile_01) } | ||
| // return hashMapOf( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. удали закоменченное, оно же не надо |
||
| // 0 to CardInfo(0,1,"Ищу сокомандника на icpc", "Let's go drink",R.drawable.mobile_01), | ||
| // 1 to CardInfo(1,2,"Помогите с задачей по матанализу", "Let's go study",R.drawable.mobile_02), | ||
| // 2 to CardInfo(2,3,"Продаю операционные системы Танненбаума", "Let's go sing",R.drawable.mobile_03), | ||
| // 3 to CardInfo(3,4,"Нужен человек, знающий js, для работы в проекте", "Let's go swim",R.drawable.mobile_04), | ||
| // 4 to CardInfo(4,5,"Придумайте заголовки за меня", "Let's go eat",R.drawable.mobile_05), | ||
| // 5 to CardInfo(5,6,"Ваше такси до Верхней Пышмы", "Let's go",R.drawable.mobile_06) | ||
|
|
||
| } | ||
| 401-> | ||
| { | ||
| return hashMapOf() | ||
| } | ||
| else ->{ | ||
|
Comment on lines
+40
to
+44
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. одно и то же |
||
| return hashMapOf()} | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ package com.example.xiinder | |
| import androidx.lifecycle.ViewModel | ||
| import io.ktor.client.* | ||
| import io.ktor.client.engine.cio.* | ||
| import io.ktor.client.engine.okhttp.* | ||
| import io.ktor.client.plugins.* | ||
| import io.ktor.client.plugins.auth.* | ||
| import io.ktor.client.plugins.auth.providers.* | ||
| import io.ktor.client.plugins.contentnegotiation.* | ||
|
|
@@ -17,16 +19,23 @@ class SharedViewModel : ViewModel() { | |
| install(ContentNegotiation) { | ||
| json() | ||
| } | ||
| install(HttpTimeout) | ||
| } | ||
| private lateinit var storeToken:StoreToken | ||
| fun setTokenStorage(storeToken: StoreToken) | ||
| private lateinit var userName:String | ||
| fun setTokenStorage(storeToken: StoreToken,username:String) | ||
| { | ||
| this.storeToken=storeToken | ||
| this.userName=username | ||
| } | ||
| suspend fun getToken(): String? | ||
| { | ||
| return storeToken.getToken() | ||
| } | ||
| suspend fun getUsername(): String? | ||
| { | ||
| return userName | ||
| } | ||
| fun configAuthClient(token:Token) | ||
| { | ||
| client.config { install(Auth) | ||
|
|
@@ -40,7 +49,7 @@ class SharedViewModel : ViewModel() { | |
| } | ||
| suspend fun clientCheck(token: Token) | ||
| { | ||
| client.get("http://192.168.0.101:8888/hello"){ | ||
| client.get("http://192.168.0.10:8888/hello"){ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в конфиги |
||
| bearerAuth(token.tokenData) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы переименовал info, непонятно что значит