Compare commits
3 Commits
ConnectAPI
...
master
Author | SHA1 | Date |
---|---|---|
|
ec33ebbea4 | 2 weeks ago |
|
fba1f2b595 | 2 weeks ago |
|
fa6921bd72 | 2 weeks ago |
@ -1,11 +1,6 @@
|
||||
package com.example.what_the_fantasy.data.model
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class Image(
|
||||
@SerializedName("idImage")
|
||||
val id: Int,
|
||||
|
||||
@SerializedName("imagePath")
|
||||
val url: String
|
||||
)
|
||||
|
@ -1,37 +1,16 @@
|
||||
package com.example.what_the_fantasy.data.model
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import java.util.Date
|
||||
|
||||
data class Quote (
|
||||
|
||||
@SerializedName("id")
|
||||
val id: Int,
|
||||
|
||||
@SerializedName("content")
|
||||
val content: String,
|
||||
|
||||
@SerializedName("like")
|
||||
var likes: Int,
|
||||
|
||||
@SerializedName("langage")
|
||||
val language: SrcLanguage,
|
||||
|
||||
@SerializedName("character")
|
||||
val character: String,
|
||||
|
||||
@SerializedName("titleSource")
|
||||
val source: String,
|
||||
|
||||
@SerializedName("imagePath")
|
||||
val imgUrl: String,
|
||||
|
||||
@SerializedName("type")
|
||||
val type: SrcType ,
|
||||
|
||||
@SerializedName("dateSource")
|
||||
val date: Int,
|
||||
|
||||
@SerializedName("isValide")
|
||||
val isValide : Boolean = true
|
||||
val type: SrcType,
|
||||
val date: Int
|
||||
|
||||
)
|
@ -1,31 +1,6 @@
|
||||
package com.example.what_the_fantasy.data.model
|
||||
import com.google.gson.*
|
||||
import java.lang.reflect.Type
|
||||
|
||||
enum class SrcLanguage(val code:Int) {
|
||||
vf(1),
|
||||
vo(0);
|
||||
|
||||
companion object {
|
||||
fun fromCode(code: Int): SrcLanguage? = values().find { it.code == code }
|
||||
}
|
||||
}
|
||||
|
||||
class LangAdapter : JsonSerializer<SrcLanguage>, JsonDeserializer<SrcLanguage> {
|
||||
override fun serialize(
|
||||
src: SrcLanguage?,
|
||||
typeOfSrc: Type?,
|
||||
context: JsonSerializationContext?
|
||||
): JsonElement = JsonPrimitive(src?.code)
|
||||
|
||||
override fun deserialize(
|
||||
json: JsonElement?,
|
||||
typeOfT: Type?,
|
||||
context: JsonDeserializationContext?
|
||||
): SrcLanguage {
|
||||
val code = json?.asInt
|
||||
return SrcLanguage.fromCode(code ?: throw JsonParseException("Code null"))
|
||||
?: throw JsonParseException("Unknown Status code: $code")
|
||||
}
|
||||
|
||||
enum class SrcLanguage {
|
||||
vf,
|
||||
vo
|
||||
}
|
@ -1,22 +1,11 @@
|
||||
package com.example.what_the_fantasy.data.model
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
class User(
|
||||
@SerializedName("id")
|
||||
val id:Int,
|
||||
@SerializedName("pseudo")
|
||||
var username:String,
|
||||
@SerializedName("email")
|
||||
var email:String,
|
||||
@SerializedName("date")
|
||||
var date:String,
|
||||
@SerializedName("imageProfil")
|
||||
val imgUrl: String,
|
||||
|
||||
@SerializedName("password")
|
||||
var password: String,
|
||||
|
||||
@SerializedName("lang")
|
||||
var langage : SrcLanguage
|
||||
)
|
@ -1,107 +0,0 @@
|
||||
package com.example.what_the_fantasy.data.retrofit
|
||||
|
||||
import com.example.what_the_fantasy.data.model.Image
|
||||
import com.example.what_the_fantasy.data.model.LangAdapter
|
||||
import com.example.what_the_fantasy.data.model.SrcTypeAdapter
|
||||
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.SrcType
|
||||
import com.example.what_the_fantasy.data.model.User
|
||||
import com.example.what_the_fantasy.data.services.APIReponceList
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
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.PUT
|
||||
import retrofit2.http.Path
|
||||
import retrofit2.http.Query
|
||||
|
||||
|
||||
interface ApiService {
|
||||
|
||||
@GET("users/username")
|
||||
suspend fun getUserByUsername(
|
||||
@Query("username") username: String
|
||||
): User
|
||||
|
||||
@GET("users/{id}")
|
||||
suspend fun getUserById(
|
||||
@Path("id") id: Int
|
||||
): User
|
||||
|
||||
@GET("users/existusername")
|
||||
suspend fun checkIfUsernameExists(
|
||||
@Query("username") username: String
|
||||
): Boolean
|
||||
|
||||
@GET("users/existemail")
|
||||
suspend fun checkIfEmailExists(
|
||||
@Query("email") email: String
|
||||
): Boolean
|
||||
|
||||
|
||||
@PUT("users")
|
||||
suspend fun updateUsername(
|
||||
@Query("id") id: Int,
|
||||
@Body user: User // Envoie un objet `User` avec les nouvelles données
|
||||
): Response<Unit>
|
||||
|
||||
@POST("users")
|
||||
suspend fun addUser(
|
||||
@Body newUser: User
|
||||
): Response<Unit>
|
||||
|
||||
|
||||
@GET("quote/dailyquote")
|
||||
suspend fun getDailyQuote(
|
||||
@Query("year") year: Int,
|
||||
@Query("month") month: Int,
|
||||
@Query("day") day: Int,
|
||||
@Query("lang") lang: SrcLanguage
|
||||
): Quote
|
||||
|
||||
@GET("quote/{id}")
|
||||
suspend fun getQuoteById(
|
||||
@Path("id")id : Int
|
||||
):Quote
|
||||
|
||||
@GET("quote/all")
|
||||
suspend fun getAllQuote(
|
||||
@Query("index") index: Int,
|
||||
@Query("count") count: Int
|
||||
): APIReponceList<Quote>
|
||||
|
||||
|
||||
@GET("image/all")
|
||||
suspend fun getAllImages(
|
||||
@Query("index") index: Int,
|
||||
@Query("count") count: Int
|
||||
): List<Image>
|
||||
|
||||
@GET("image/{id}")
|
||||
suspend fun getImageById(
|
||||
@Path("id") id: Int
|
||||
): Image
|
||||
}
|
||||
|
||||
|
||||
object RetrofitInstance {
|
||||
private const val BASE_URL = "https://codefirst.iut.uca.fr/containers/WhatTheFantasy-web-services/api/v1/"
|
||||
|
||||
private val gson: Gson = GsonBuilder()
|
||||
.registerTypeAdapter(SrcLanguage::class.java, LangAdapter())
|
||||
.registerTypeAdapter(SrcType::class.java, SrcTypeAdapter())
|
||||
.create()
|
||||
|
||||
val api: ApiService by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.build()
|
||||
.create(ApiService::class.java)
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.example.what_the_fantasy.data.services
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class APIReponceList<T> (
|
||||
@SerializedName("totalCount")
|
||||
val totalCount: Int,
|
||||
|
||||
@SerializedName("pageIndex")
|
||||
val index: Int,
|
||||
|
||||
@SerializedName("countPerPage")
|
||||
val count: Int,
|
||||
|
||||
@SerializedName("items")
|
||||
val items: MutableList<T>,
|
||||
)
|
@ -1,302 +1,89 @@
|
||||
package com.example.what_the_fantasy.data.services
|
||||
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.example.what_the_fantasy.data.local.ImageStub.allImages
|
||||
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.ui.viewModels.CurrentUserViewModel
|
||||
import java.time.LocalDate
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
|
||||
//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 suspend fun validLogin(
|
||||
username: String,
|
||||
passwd: String,
|
||||
navController: (Int) -> Unit,
|
||||
initialierCurrentUser: (Int) -> Unit,
|
||||
currentUserViewModel: CurrentUserViewModel
|
||||
): 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)
|
||||
currentUserViewModel.setUser(response)
|
||||
return true
|
||||
}
|
||||
else {
|
||||
return false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun EditUsername(username: String, index: Int, currentUserViewModel: CurrentUserViewModel): Boolean {
|
||||
try {
|
||||
val userExistsResponse = RetrofitInstance.api.checkIfUsernameExists(username)
|
||||
|
||||
Log.d("EditUsername", "Username check: $username exists = $userExistsResponse")
|
||||
|
||||
if (userExistsResponse) {
|
||||
Log.d("EditUsername", "Username $username already exists, cannot update.")
|
||||
return false
|
||||
}
|
||||
|
||||
val responseUser = RetrofitInstance.api.getUserById(index)
|
||||
|
||||
val updatedUser = User(
|
||||
id = index,
|
||||
username = username, // Nouveau nom d'utilisateur
|
||||
email = responseUser.email,
|
||||
password = responseUser.password,
|
||||
imgUrl = responseUser.imgUrl,
|
||||
langage = responseUser.langage,
|
||||
date = responseUser.date
|
||||
)
|
||||
|
||||
val response = RetrofitInstance.api.updateUsername(index, updatedUser)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
currentUserViewModel.setUser(updatedUser)
|
||||
Log.d("EditUsername", "Username updated successfully.")
|
||||
return true
|
||||
} else {
|
||||
Log.d("EditUsername", "Failed to update username, API response not successful.")
|
||||
return false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Log.d("EditUsername", "Exception occurred: ${e.message}")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override suspend fun EditEmail(email: String, index: Int,currentUserViewModel: CurrentUserViewModel): Boolean {
|
||||
try {
|
||||
val userExistsResponse = RetrofitInstance.api.checkIfEmailExists(email)
|
||||
|
||||
if (userExistsResponse) {
|
||||
Log.d("EditEmail", "Exception occurred: The email alredy exist")
|
||||
return false
|
||||
}
|
||||
|
||||
val responseUser = RetrofitInstance.api.getUserById(index)
|
||||
|
||||
val updatedUser = User(
|
||||
id = index,
|
||||
username = responseUser.username,
|
||||
email = email,// Nouvel email d'utilisateur
|
||||
password = responseUser.password,
|
||||
imgUrl = responseUser.imgUrl,
|
||||
langage = responseUser.langage,
|
||||
date = responseUser.date
|
||||
)
|
||||
|
||||
val response = RetrofitInstance.api.updateUsername(index, updatedUser)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
currentUserViewModel.setUser(updatedUser)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Log.d("EditEmail", "Exception occurred: ${e.message}")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun EditPasswd(passwd: String, index: Int, currentUserViewModel: CurrentUserViewModel): Boolean {
|
||||
try {
|
||||
val responseUser = RetrofitInstance.api.getUserById(index)
|
||||
|
||||
val updatedUser = User(
|
||||
id = index,
|
||||
username = responseUser.username,
|
||||
email = responseUser.username,
|
||||
password = passwd,
|
||||
//password = hashPassword(passwd),
|
||||
imgUrl = responseUser.imgUrl,
|
||||
langage = responseUser.langage,
|
||||
date = responseUser.date
|
||||
)
|
||||
|
||||
val response = RetrofitInstance.api.updateUsername(index, updatedUser)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
currentUserViewModel.setUser(updatedUser)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Log.d("EditPasswd", "Exception occurred: ${e.message}")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun EditImage(index: Int, currentUserViewModel : CurrentUserViewModel): String {
|
||||
try {
|
||||
|
||||
val responseUser = RetrofitInstance.api.getUserById(index)
|
||||
|
||||
val updatedUser = User(
|
||||
id = index,
|
||||
username = responseUser.username,
|
||||
email = responseUser.email,// Nouvel email d'utilisateur
|
||||
password = responseUser.password,
|
||||
imgUrl = randomImage(),
|
||||
langage = responseUser.langage,
|
||||
date = responseUser.date
|
||||
)
|
||||
|
||||
val response = RetrofitInstance.api.updateUsername(index, updatedUser)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
currentUserViewModel.setUser(updatedUser)
|
||||
return updatedUser.imgUrl
|
||||
} else {
|
||||
return responseUser.imgUrl
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return ""
|
||||
Log.d("EditEmail", "Exception occurred: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
override suspend fun ChangeLangage(index: Int, currentUserViewModel : CurrentUserViewModel): SrcLanguage {
|
||||
val responseUser = RetrofitInstance.api.getUserById(index)
|
||||
var langage : SrcLanguage
|
||||
|
||||
if(responseUser.langage == SrcLanguage.vf){
|
||||
langage = SrcLanguage.vo
|
||||
}
|
||||
else{
|
||||
langage = SrcLanguage.vf
|
||||
}
|
||||
|
||||
val updatedUser = User(
|
||||
id = index,
|
||||
username = responseUser.username,
|
||||
email = responseUser.email,
|
||||
password = responseUser.password,
|
||||
imgUrl = responseUser.imgUrl,
|
||||
langage = langage,
|
||||
date = responseUser.date
|
||||
)
|
||||
RetrofitInstance.api.updateUsername(index, updatedUser)
|
||||
currentUserViewModel.setUser(updatedUser)
|
||||
return langage
|
||||
}
|
||||
|
||||
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 suspend fun CreateUser(username: String, email: String, passwd: String): Boolean {
|
||||
val today = Calendar.getInstance().time
|
||||
val rep =RetrofitInstance.api.addUser( User(15,username,email,today.toString(),"img",passwd,SrcLanguage.vo))
|
||||
Log.d("test",rep.toString())
|
||||
if(rep.code() in 200..299) return true
|
||||
return false
|
||||
}
|
||||
|
||||
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 suspend fun getSomeQuotes(nb: Int, page: Int): MutableList<Quote> {
|
||||
val result = RetrofitInstance.api.getAllQuote(page,nb)
|
||||
return result.items
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
override suspend fun getDalyQuote(langage : SrcLanguage): Quote {
|
||||
val today = LocalDate.now()
|
||||
return RetrofitInstance.api.getDailyQuote(today.year, today.monthValue, today.dayOfMonth, langage)
|
||||
}
|
||||
|
||||
override fun search(type: String, search: String, indexCount: Int): List<Quote> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------
|
||||
suspend fun randomImage() : String{
|
||||
val imagesList = RetrofitInstance.api.getAllImages(0, 300)
|
||||
val sizeList = imagesList.size
|
||||
return RetrofitInstance.api.getImageById((0..sizeList).random()).url
|
||||
}
|
||||
|
||||
}
|
||||
//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")
|
||||
// }
|
||||
//}
|
Loading…
Reference in new issue