parent
ad5ae952ac
commit
09be084651
@ -0,0 +1,51 @@
|
||||
package com.example.what_the_fantasy.data.retrofit
|
||||
|
||||
import com.example.what_the_fantasy.data.model.SrcLanguage
|
||||
import com.example.what_the_fantasy.data.model.User
|
||||
import retrofit2.Response
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Query
|
||||
|
||||
data class UserUpdateRequest(
|
||||
val username: String,
|
||||
val email: String,
|
||||
val passwd: String,
|
||||
val langage: SrcLanguage,
|
||||
val imgUrl: String
|
||||
)
|
||||
|
||||
interface UserApiService {
|
||||
|
||||
@GET("users/username")
|
||||
suspend fun getUserByUsername(
|
||||
@Query("username") username: String
|
||||
): User
|
||||
|
||||
@GET("users/")
|
||||
suspend fun getUserById(
|
||||
@Query("id") id: Int
|
||||
): User
|
||||
|
||||
@POST("users")
|
||||
suspend fun updateUser(
|
||||
@Query("id") id: Int,
|
||||
@Body userUpdate: UserUpdateRequest
|
||||
): Response<User>
|
||||
}
|
||||
|
||||
|
||||
object RetrofitInstance {
|
||||
private const val BASE_URL = "https://codefirst.iut.uca.fr/containers/WhatTheFantasy-web-services/api/v1/"
|
||||
|
||||
val api: UserApiService by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(UserApiService::class.java)
|
||||
}
|
||||
}
|
@ -1,89 +1,162 @@
|
||||
package com.example.what_the_fantasy.data.services
|
||||
|
||||
import com.example.what_the_fantasy.data.local.UserStub.users
|
||||
import com.example.what_the_fantasy.data.model.Comment
|
||||
import com.example.what_the_fantasy.data.model.Favorite
|
||||
import com.example.what_the_fantasy.data.model.Quote
|
||||
import com.example.what_the_fantasy.data.model.SrcLanguage
|
||||
import com.example.what_the_fantasy.data.model.User
|
||||
import com.example.what_the_fantasy.data.retrofit.RetrofitInstance
|
||||
import com.example.what_the_fantasy.data.retrofit.UserUpdateRequest
|
||||
|
||||
//import com.example.what_the_fantasy.data.model.Comment
|
||||
//import com.example.what_the_fantasy.data.model.Favorite
|
||||
//import com.example.what_the_fantasy.data.model.Quote
|
||||
//import com.example.what_the_fantasy.data.model.User
|
||||
////import com.example.what_the_fantasy.ui.navigations.Destination
|
||||
|
||||
//class ServicesAPI : IServices {
|
||||
// override fun EditUsername(username: String, index : Int): Boolean {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun EditEmail(email: String, index : Int): Boolean {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun EditPasswd(passwd: String, index : Int) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun EditImage(imageURL: String, index : Int) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun ChangeLangage(user: User) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun AddFav(userId: Int, QuoteId: Int) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun SupFav(userId: Int, QuoteId: Int) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun AddComment(content: String) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun CreateUser(username: String, email: String, passwd: String, services: IServices) : Boolean {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getFavorite(user: User): List<Quote> {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun SearchQuote(quote: String) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getQuote(id: Int): Quote? {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun isFavorite(id: Int, user: User): Boolean {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getAllFavorite(): List<Favorite> {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getAllQuote(): List<Quote> {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getSomeQuotes(nb: Int, page: Int): MutableList<Quote> {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getAllUsers(): List<User> {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getComment(quoteId: Int): List<Comment> {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun getUserById(id: Int): User? {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//
|
||||
// override fun search(type : String ,search:String ,indexCount: Int): List<Quote>{
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
//}
|
||||
class ServicesAPI : IServices {
|
||||
|
||||
override suspend fun validLogin(
|
||||
username: String,
|
||||
passwd: String,
|
||||
navController: (Int) -> Unit,
|
||||
initialierCurrentUser: (Int) -> Unit
|
||||
): Boolean {
|
||||
try {
|
||||
val response = RetrofitInstance.api.getUserByUsername(username) // on récupère l'utilisateur
|
||||
val hashedPasswordAPI = response.password // récupère le mot de passe de l'api
|
||||
val index =response.id
|
||||
//val hashPassWd = hashPassword(passwd)// hache mot de passe demandé
|
||||
|
||||
if (passwd == hashedPasswordAPI) { // on compare les deux mots de passe
|
||||
navController(index)
|
||||
initialierCurrentUser(index)
|
||||
return true
|
||||
}
|
||||
else {
|
||||
return false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun EditUsername(username: String, index: Int): Boolean {
|
||||
try {
|
||||
val responseOldUser = RetrofitInstance.api.getUserById(index)
|
||||
val request = UserUpdateRequest(username,
|
||||
responseOldUser.email,
|
||||
responseOldUser.password,
|
||||
responseOldUser.langage,
|
||||
responseOldUser.imgUrl)
|
||||
|
||||
|
||||
return false
|
||||
}catch (e: Exception) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun EditEmail(email: String, index: Int): Boolean {
|
||||
try {
|
||||
val responseOldUser = RetrofitInstance.api.getUserById(index)
|
||||
val request = UserUpdateRequest(responseOldUser.username,
|
||||
email,
|
||||
responseOldUser.password,
|
||||
responseOldUser.langage,
|
||||
responseOldUser.imgUrl)
|
||||
|
||||
|
||||
return false
|
||||
}catch (e: Exception) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override suspend fun EditPasswd(passwd: String, index: Int) {
|
||||
try {
|
||||
val responseOldUser = RetrofitInstance.api.getUserById(index)
|
||||
val request = UserUpdateRequest(responseOldUser.username,
|
||||
responseOldUser.email,
|
||||
passwd,
|
||||
responseOldUser.langage,
|
||||
responseOldUser.imgUrl)
|
||||
|
||||
}catch (_: Exception) { }
|
||||
}
|
||||
|
||||
override fun EditImage(index: Int): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun ChangeLangage(index: Int): SrcLanguage {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun isUsernameExist(username: String): Boolean {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun isEmailExist(email: String): Boolean {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun AddFav(userId: Int, QuoteId: Int) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun SupFav(userId: Int, QuoteId: Int) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun AddComment(content: String) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun CreateUser(username: String, email: String, passwd: String): Boolean {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun getFavorite(user: User): List<Quote> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun getAllUsers(): List<User> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun getComment(quoteId: Int): List<Comment> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override suspend fun getUserById(id: Int): User? {
|
||||
return RetrofitInstance.api.getUserById(id)
|
||||
}
|
||||
|
||||
override fun getQuote(id: Int): Quote? {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override suspend fun isFavorite(idQuote: Int, iduser: Int): Boolean {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun getAllFavorite(): List<Favorite> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun getAllQuote(): List<Quote> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun getSomeQuotes(nb: Int, page: Int): MutableList<Quote> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun search(type: String, search: String, indexCount: Int): List<Quote> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue