commit
76a51f1de0
@ -1,22 +0,0 @@
|
|||||||
package com.example.veraxapplication.modele
|
|
||||||
|
|
||||||
import retrofit2.Retrofit
|
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
|
||||||
|
|
||||||
object RetrofitClient {
|
|
||||||
|
|
||||||
private const val BASE_URL = "http://181.214.189.133:9092/"
|
|
||||||
|
|
||||||
val retrofit: Retrofit by lazy {
|
|
||||||
Retrofit.Builder()
|
|
||||||
.baseUrl(BASE_URL)
|
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object ApiClient {
|
|
||||||
val apiService: ApiService by lazy {
|
|
||||||
RetrofitClient.retrofit.create(ApiService::class.java)
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.example.veraxapplication.modele
|
||||||
|
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
|
||||||
|
object RetrofitClientUser {
|
||||||
|
|
||||||
|
private const val BASE_URL = "https://codefirst.iut.uca.fr/containers/Verax-verax-api"
|
||||||
|
|
||||||
|
val retrofit: Retrofit by lazy {
|
||||||
|
Retrofit.Builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
// interface UserApiService {
|
||||||
|
// @GET("users")
|
||||||
|
// suspend fun getUsers() : List<User>
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
object ApiClientUser {
|
||||||
|
val apiService: UserApiService by lazy {
|
||||||
|
RetrofitClientUser.retrofit.create(UserApiService::class.java)
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.veraxapplication.modele
|
|
||||||
|
|
||||||
import com.example.veraxapplication.modele.articles.Article
|
|
||||||
import retrofit2.Call
|
|
||||||
import retrofit2.http.GET
|
|
||||||
|
|
||||||
interface ApiService {
|
|
||||||
@GET("articles/")
|
|
||||||
fun getArticles(): Call<List<Article>>
|
|
||||||
}
|
|
@ -0,0 +1,9 @@
|
|||||||
|
import com.example.veraxapplication.modele.user.User
|
||||||
|
|
||||||
|
|
||||||
|
interface IUsersDataManager {
|
||||||
|
val allUsers: List<Any?>?
|
||||||
|
|
||||||
|
fun getUser(pseudo : String): User?
|
||||||
|
fun getUsers(): List<User>
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.veraxapplication.modele
|
||||||
|
|
||||||
|
import com.example.veraxapplication.modele.user.User
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.http.GET
|
||||||
|
|
||||||
|
interface UserApiService {
|
||||||
|
@GET("users/")
|
||||||
|
suspend fun getUsers(): Call<List<User>>
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.example.veraxapplication.modele.api
|
||||||
|
|
||||||
|
import com.example.veraxapplication.modele.articles.Article
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ArticleDTO (
|
||||||
|
|
||||||
|
@SerializedName("id")
|
||||||
|
val id: Int,
|
||||||
|
@SerializedName("titre")
|
||||||
|
val titre: String,
|
||||||
|
@SerializedName("description")
|
||||||
|
val description: String,
|
||||||
|
@SerializedName("temps")
|
||||||
|
val temps: String,
|
||||||
|
@SerializedName("date")
|
||||||
|
val date: String,
|
||||||
|
@SerializedName("auteur")
|
||||||
|
val auteur: String,
|
||||||
|
@SerializedName("categorie")
|
||||||
|
val categorie: String,
|
||||||
|
@SerializedName("imagePrincipale")
|
||||||
|
val imagePrincipale: String,
|
||||||
|
@SerializedName("note")
|
||||||
|
val note: String,
|
||||||
|
|
||||||
|
) {
|
||||||
|
fun toModel(): Article {
|
||||||
|
return Article(
|
||||||
|
id,
|
||||||
|
titre,
|
||||||
|
description,
|
||||||
|
temps,
|
||||||
|
date,
|
||||||
|
auteur,
|
||||||
|
categorie,
|
||||||
|
imagePrincipale,
|
||||||
|
note,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data class ContenuDTO (
|
||||||
|
@SerializedName("id")
|
||||||
|
val id: Int,
|
||||||
|
@SerializedName("typeContenu")
|
||||||
|
val typeContenu: String,
|
||||||
|
@SerializedName("titre")
|
||||||
|
val titre: String,
|
||||||
|
@SerializedName("texte")
|
||||||
|
val texte: String,
|
||||||
|
)
|
@ -0,0 +1,54 @@
|
|||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.example.veraxapplication.modele.api.IArticleService
|
||||||
|
import com.example.veraxapplication.modele.articles.Article
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
|
||||||
|
object ArticleApiClient {
|
||||||
|
private const val BASE_URL = "http://181.214.189.133:9092/"
|
||||||
|
|
||||||
|
private val logging = HttpLoggingInterceptor().apply {
|
||||||
|
level = HttpLoggingInterceptor.Level.BODY
|
||||||
|
}
|
||||||
|
|
||||||
|
private val httpClient = OkHttpClient.Builder().apply {
|
||||||
|
addInterceptor(logging)
|
||||||
|
}.build()
|
||||||
|
|
||||||
|
val retrofit: Retrofit = Retrofit.Builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.client(httpClient)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
class ArticlesViewModel : ViewModel() {
|
||||||
|
private val _articles = MutableLiveData<List<Article>>()
|
||||||
|
val articles: LiveData<List<Article>> = _articles
|
||||||
|
|
||||||
|
private val service = ArticleApiClient.retrofit.create(IArticleService::class.java)
|
||||||
|
|
||||||
|
init {
|
||||||
|
loadArticles()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadArticles() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
try {
|
||||||
|
val articlesDto = service.getArticles() // Pas besoin d'appeler .execute()
|
||||||
|
// Convertissez les DTO en modèles de données si nécessaire
|
||||||
|
_articles.value = articlesDto.map { it.toModel() }
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("ArticlesViewModel", "Erreur lors du chargement des articles", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.veraxapplication.modele.api
|
||||||
|
|
||||||
|
import retrofit2.http.GET
|
||||||
|
|
||||||
|
interface IArticleService {
|
||||||
|
@GET("articles")
|
||||||
|
suspend fun getArticles(): List<ArticleDTO>
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.example.veraxapplication.modele.user
|
||||||
|
|
||||||
|
data class User(val pseudo : String, val mdp : String, val mail : String, val nom : String, val prenom : String, val role : Char)
|
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<network-security-config>
|
||||||
|
<domain-config cleartextTrafficPermitted="true">
|
||||||
|
<domain includeSubdomains="true">181.214.189.133</domain>
|
||||||
|
</domain-config>
|
||||||
|
</network-security-config>
|
Loading…
Reference in new issue