# Data Directory

{% hint style="info" %}
Data directory represent all the data classes used for data exchange in Matar app.
{% endhint %}

## Data directory

```
--data(directory)
 --model(directory)
 --network(directory)
 --repository(directory)
```

### Data/model directory

```shell
--model(directory)
  --auth(directory)
   --Login.kt
   --LogoutResponse.kt
   --User.kt
  --postactivity(directory)
   --Data.kt
   --PostActivityRequest.kt
   --PostActivityResponse.kt
  --userpostactivity(directory)
   --UserPostActivityResponse.kt
  --AllOrgResp.kt
  --ApiFailureLog.kt
  --BroadcastEvent.kt
  --DeepLinkData.kt
  --DeletePostResponse.kt
  --GetPostsResponse.kt
  --Language.kt
  --Languages.kt
  --Organization.kt
  --PageDetails.kt
  --User.kt
  --UserDetails.kt 
```

***

### Data/model/auth/Login.kt

```kotlin
package com.matar.app.data.model.auth

import com.google.gson.annotations.SerializedName

data class SendOtpRequest(
    @SerializedName("action") val action: String,
    @SerializedName("name") val name: String = "",
    @SerializedName("phone_number") val phoneNumber: String
)

data class SendOtpResponse(
    @SerializedName("message") val message: String
)
```

**Describing Code:**

The code defines two data classes in Kotlin, **SendOtpRequest** and **SendOtpResponse**, which are typically used for representing data in your application.

1. **SendOtpRequest**:
   * This data class is used to represent a request to send an OTP (One-Time Password).
   * It has three properties:
     * **action**

       (String): This is the action to be performed, which appears to be related to sending an OTP.
     * **name**

       (String, optional): This is the name associated with the request. It's an optional parameter and has a default value of an empty string.
     * **phoneNumber**

       (String): This is the phone number to which the OTP will be sent.
2. **SendOtpResponse**:
   * This data class is used to represent the response received after sending an OTP.
   * It has one property:
     * **message**

       (String): This property holds a message from the server, which might contain information about the status of the OTP sending process.

These data classes seem to be designed for handling authentication-related operations, where you can create a **SendOtpRequest** object to send a request to send an OTP, and you can receive a **SendOtpResponse** object as a response, containing a message.

Please note that these data classes are annotated with **@SerializedName**, which suggests that they are meant to be serialized/deserialized using Gson, a library for working with JSON data in Android and Java applications.

***

### Data/model/auth/LogoutResponse.kt

```kotlin
package com.matar.app.data.model.auth

import com.google.gson.annotations.SerializedName

data class LogoutResponse(
    @SerializedName("message") val message: String?
)

//data class LogoutResponse(
//    @SerializedName("description") val description: String,
//    @SerializedName("error") val error: String,
//)
```

**Describing Code:**

The **LogoutResponse** class has a single property:

1. \*\*message: \*\*This property is annotated with \*\*@SerializedName("message")\*\*indicating that it's intended to be serialized/deserialized using Gson, a popular JSON parsing library for Java and Kotlin. The property is of type \*\*String? \*\*which means it can hold a nullable string.

This class is likely used to represent the response from a server or API when a user logs out, with the server providing a message to indicate the result of the logout operation

***

### Data/network directory

```shell
--network(directory)
 --AuthApi.kt 
 --CategoryApi.kt
 --ChatGptApi.kt
 --PostActivityApi.kt
 --PostApi.kt
 --RecordHistoryApi.kt
 --UserApi.kt
```

***

### Data/network/AuthApi.kt

```kotlin
package com.matar.app.data.network

import com.matar.app.data.model.DeletePostResponse
import com.matar.app.data.model.UserDetails
import com.matar.app.data.model.auth.LogoutResponse
import com.matar.app.data.model.auth.SendOtpRequest
import com.matar.app.model.home_screen_data.ChildPosts
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST

interface AuthApi {

    @POST("v1/login")
    fun sendOtp(
        @Body sendOtpRequest: SendOtpRequest,
    ): ChildPosts

    @POST("v1/edit_user")
    fun editUser(
        @Body sendOtpRequest: SendOtpRequest,
    ): ChildPosts

    @GET("v1/get_current_user")
    suspend fun getCurrentUser(
        @Header("Authorization") auth: String
    ): UserDetails?

    @POST("v1/logout")
    suspend fun logout(
        @Header("Authorization") auth: String
    ): LogoutResponse
}
```

***

### Data/network/CategoryApi.kt

```kotlin

import com.matar.app.data.model.AllOrgResp
import com.matar.app.data.model.Languages
import com.matar.app.model.category.CategoriesModel
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Query

interface CategoryApi {

    @GET("v1/categories")
    suspend fun getAllCategories(
        @Header("Authorization") auth: String,
        @Query("organization_id") organizationId: String,
    ): CategoriesModel?

    @GET("v1/organizations")
    suspend fun getAllOrganizations(): AllOrgResp?

    @GET("v1/languages")
    suspend fun getAllLanguages(): Languages?

}
```

***

### Data/repository directory

```shell
--repository(directory)
 --AuthRepositoryImpl.kt
 --CategoryRepositoryImpl.kt
 --ChatGptRepositoryImpl.kt
 --FirebaseRepositoryImpl.kt
 --PostActivityRepositoryImpl.kt
 --PostRepositoryImpl.kt
 --RecordHistoryRepositoryImpl.kt
 --UserRepositoryImpl.kt
```

***

### Data/repository/AuthRepositoryImpl.kt

```kotlin
package com.matar.app.data.repository

import com.google.firebase.firestore.FirebaseFirestore
import com.matar.app.BuildConfig
import com.matar.app.MatarApp
import com.matar.app.data.model.UserDetails
import com.matar.app.data.model.auth.LogoutResponse
import com.matar.app.data.network.AuthApi
import com.matar.app.domain.repository.AuthRepository
import com.matar.app.retrofit.APIClient
import com.matar.app.ui.util.PreferenceManager
import com.matar.app.ui.util.toReadableTime
import retrofit2.HttpException
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
    private val authApi: AuthApi,
    private val firebaseFirestore: FirebaseFirestore,
    private val preferenceManager: PreferenceManager
) : AuthRepository {
    override suspend fun getCurrentUser(auth: String): UserDetails? {
        return try {
            val response = authApi.getCurrentUser(auth = auth)
            response
        } catch (e: Exception) {
            if (((e as HttpException).code() == 401)) {
                APIClient.callRefreshToken(MatarApp.mContext)
            }
            firebaseFirestore
                .collection(BuildConfig.ERROR_LOGGING_COLLECTION)
                .add(
                    hashMapOf(
                        Pair("api", "v1/get_current_user"),
                        Pair("error", "${e.message}"),
                        Pair("time", System.currentTimeMillis().toReadableTime()),
                        Pair("timeStamp", System.currentTimeMillis()),
                        Pair("userId", preferenceManager.getUserId())
                    )
                )
            e.printStackTrace()
            null
        }
    }

    override suspend fun logout(auth: String): LogoutResponse? {
        return try {
            val response = authApi.logout(auth = auth)
            response
        } catch (e: Exception) {
            firebaseFirestore
                .collection(BuildConfig.ERROR_LOGGING_COLLECTION)
                .add(
                    hashMapOf(
                        Pair("api", "v1/get_current_user"),
                        Pair("error", "${e.message}"),
                        Pair("time", System.currentTimeMillis().toReadableTime()),
                        Pair("timeStamp", System.currentTimeMillis()),
                        Pair("userId", preferenceManager.getUserId())
                    )
                )
            e.printStackTrace()
            null
        }
    }
}
```
