Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
docs: add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed May 9, 2023
1 parent c6d4ddf commit 4da1101
Show file tree
Hide file tree
Showing 20 changed files with 373 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import access.communication.User as UserRemote
import access.communication.Role as RoleRemote
import access.communication.user as userRemote

/**
* Adapter is used to convert data from domain to remote and vice versa.
*/
object Adapter {

/**
* Converts a user from domain to remote.
* @return the converted remote user.
*/
fun User.fromDomainToRemote(): UserRemote {
val user = this
return userRemote {
Expand All @@ -26,6 +33,10 @@ object Adapter {
}
}

/**
* Converts a user from remote to domain.
* @return the converted domain user.
*/
fun UserRemote.fromRemoteToDomain(): User {
val user = this
return User.build {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@ import retrofit2.http.HTTP
import retrofit2.http.POST
import retrofit2.http.PUT

/**
* AccessApi is the API interface for the access data.
*
* Use retrofit annotations to define the HTTP request.
*/
interface AccessApi {

/**
* Logs a user.
* @param user is the user to log.
* @return the [Call] with the logged user response or error.
*/
@POST("/login")
fun getUser(@Body user: User): Call<UserResponse>

/**
* Creates a new user.
* @param user is the user to create.
* @return the [Call] with the created user or error.
*/
@PUT("/register")
fun createNewUser(@Body user: User): Call<User>

/**
* Deletes a user.
* @param user is the user to delete.
* @return the [Call] with the deleted user response or error.
*/
@HTTP(method = "DELETE", path = "/remove", hasBody = true)
fun deleteUser(@Body user: User): Call<UserResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import io.github.andreabrighi.converter.RetrofitJsonConverterFactory
import okhttp3.OkHttpClient
import retrofit2.Retrofit

/**
* RetrofitHelper is an helper to create a Retrofit instance.
*/
object RetrofitHelper {
/**
* Creates a Retrofit instance.
* @param baseUrl is the base url of the API.
* @return the Retrofit instance.
*/
fun getInstance(baseUrl: String): Retrofit {
val httpClient = OkHttpClient.Builder()
return Retrofit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,75 @@ package com.intelligentbackpack.accessdata.datasource
import com.intelligentbackpack.accessdomain.entities.User
import com.intelligentbackpack.accessdomain.repository.AccessDomainRepository

/**
* AccessDomainRepositoryImpl is the implementation of the AccessDomainRepository.
* @param accessLocalDataStorage is the local data storage.
* @param accessRemoteDataStorage is the remote data storage.
*/
class AccessDomainRepositoryImpl(
private val accessLocalDataStorage: AccessLocalDataStorage,
private val accessRemoteDataStorage: AccessRemoteDataStorage
) : AccessDomainRepository {

/**
* Creates a new user.
* @param user is the user to create.
* @return the created user.
* @throws Exception if the user cannot be created.
*
* The user is created in the remote data storage and then saved in the local data storage.
*/
override fun createUser(user: User): User =
accessRemoteDataStorage.createUser(user)
.also { accessLocalDataStorage.saveUser(it) }

/**
* Checks if a user is logged.
* @return true if the user is logged, false otherwise.
*
* The user is checked in the local data storage.
*/
override fun isUserLogged(): Boolean = accessLocalDataStorage.isUserSaved()

/**
* Logs a user using email and password.
* @param email is the user email.
* @param password is the user password.
* @return the logged user.
* @throws Exception if the user cannot be logged.
*
* The user is logged in the remote data storage and then saved in the local data storage.
*/
override fun loginWithData(email: String, password: String): User =
accessRemoteDataStorage
.accessWithData(email, password)
.also { accessLocalDataStorage.saveUser(it) }

/**
* Logs the saved user.
* @return the logged user.
* @throws Exception if the user cannot be logged.
*
* The user is logged in the remote data storage.
*/
override fun automaticLogin(): User =
accessLocalDataStorage.getUser()


/**
* Logs out the user.
* @throws Exception if the user cannot be logged out.
*
* The user is deleted in the local data storage.
*/
override fun logoutUser() =
accessLocalDataStorage.deleteUser()

/**
* Deletes the user.
* @throws Exception if the user cannot be deleted.
*
* The user is deleted in the remote data storage and then in the local data storage.
*/
override fun deleteUser() {
accessRemoteDataStorage.deleteUser(accessLocalDataStorage.getUser())
accessLocalDataStorage.deleteUser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ package com.intelligentbackpack.accessdata.datasource

import com.intelligentbackpack.accessdomain.entities.User

/**
* AccessLocalDataStorage is the interface for access to the local data storage.
*/
interface AccessLocalDataStorage {

/**
* Checks if the user is saved.
* @return true if the user is saved, false otherwise.
*/
fun isUserSaved(): Boolean

/**
* Saves the user.
* @param user is the user to save.
*/
fun saveUser(user: User)

/**
* Gets the user.
* @return the user.
*/
fun getUser(): User

/**
* Deletes the user.
*/
fun deleteUser()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,36 @@ import com.intelligentbackpack.accessdata.exception.SavingUserException
import com.intelligentbackpack.accessdata.storage.UserStorage
import com.intelligentbackpack.accessdomain.entities.User

/**
* AccessLocalDataStorageImpl is the implementation of AccessLocalDataStorage.
* @param storage is the storage of the user data [UserStorage].
*/
class AccessLocalDataStorageImpl(private val storage: UserStorage) : AccessLocalDataStorage {

override fun isUserSaved(): Boolean {
return storage.isUserSaved()
}

/**
* Saves the user.
* @param user is the user to save.
* @throws SavingUserException if the user cannot be saved.
*/
@Throws(SavingUserException::class)
override fun saveUser(user: User) {
try {
storage.saveUser(user)
}
catch (e: Exception) {
} catch (e: Exception) {
throw SavingUserException()
}
}

/**
* Gets the user.
* @return the user.
* @throws MissingUserException if the user is not saved.
*/
@Throws(MissingUserException::class)
override fun getUser(): User {
if (!storage.isUserSaved())
throw MissingUserException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ package com.intelligentbackpack.accessdata.datasource

import com.intelligentbackpack.accessdomain.entities.User

/**
* AccessRemoteDataStorage is the interface for access to the remote data storage.
*/
interface AccessRemoteDataStorage {

/**
* Creates a user.
* @param user is the user to create.
* @return the created user.
*/
fun createUser(user: User): User

fun accessWithData(email: String, password: String) : User
/**
* Logs a user using email and password.
* @param email is the user email.
* @param password is the user password.
* @return the logged user.
*/
fun accessWithData(email: String, password: String): User

/**
* Logs the saved user.
* @return the logged user.
*/
fun deleteUser(user: User)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ import com.intelligentbackpack.accessdata.adapter.Adapter.fromRemoteToDomain
import com.intelligentbackpack.accessdata.exception.DownloadException
import org.json.JSONObject

/**
* AccessRemoteDataStorageImpl is the implementation of AccessRemoteDataStorage.
* @param baseUrl is the base url of the remote data storage.
*/
class AccessRemoteDataStorageImpl(baseUrl: String) : AccessRemoteDataStorage {

private val accessApi = RetrofitHelper.getInstance(baseUrl).create(AccessApi::class.java)

/**
* Creates a user.
* @param user is the user to create.
* @return the created user.
* @throws DownloadException if the user cannot be created.
*/
@Throws(DownloadException::class)
override fun createUser(user: User): User {
val response = accessApi.createNewUser(user.fromDomainToRemote()).execute()
return if (response.isSuccessful) {
Expand All @@ -22,6 +33,14 @@ class AccessRemoteDataStorageImpl(baseUrl: String) : AccessRemoteDataStorage {
}
}

/**
* Logs a user using email and password.
* @param email is the user email.
* @param password is the user password.
* @return the logged user.
* @throws DownloadException if the user cannot be logged.
*/
@Throws(DownloadException::class)
override fun accessWithData(email: String, password: String): User {
val response = accessApi.getUser(
access.communication.User.newBuilder()
Expand All @@ -37,6 +56,11 @@ class AccessRemoteDataStorageImpl(baseUrl: String) : AccessRemoteDataStorage {
}
}

/**
* Delete the user.
* @throws DownloadException if the user cannot be deleted.
*/
@Throws(DownloadException::class)
override fun deleteUser(user: User) {
val response = accessApi.deleteUser(user.fromDomainToRemote()).execute()
if (!response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.intelligentbackpack.accessdata.exception

/**
* DownloadException is an exception that is thrown when there is an error during the download.
* @param message is the message of the exception.
*/
class DownloadException(message: String) : Exception(message)
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package com.intelligentbackpack.accessdata.exception

/**
* MissingUserException is an exception that is thrown when the user is not saved.
*/
class MissingUserException : Exception()
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package com.intelligentbackpack.accessdata.exception

/**
* SavingUserException is an exception that is thrown when there is an error during the saving of the user.
*/
class SavingUserException : Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@ package com.intelligentbackpack.accessdata.storage

import com.intelligentbackpack.accessdomain.entities.User

/**
* UserStorage is the interface for storage the user data.
*/
interface UserStorage {

/**
* Checks if the user is saved.
* @return true if the user is saved, false otherwise.
*/
fun isUserSaved(): Boolean

/**
* Saves the user.
* @param user is the user to save.
*/
fun saveUser(user: User)

/**
* Gets the user.
* @return the user.
*/
fun getUser(): User

/**
* Deletes the user.
*/
fun deleteUser()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import androidx.security.crypto.MasterKey
import com.intelligentbackpack.accessdomain.entities.Role
import com.intelligentbackpack.accessdomain.entities.User

/**
* UserStorageImpl is the implementation of UserStorage.
* @param context is the context of the application.
*
* This class uses SharedPreferences to save the user.
*/
class UserStorageImpl(private val context: Context) : UserStorage {

private val name = "IntelligentBackpackSharedPref"
Expand All @@ -17,6 +23,12 @@ class UserStorageImpl(private val context: Context) : UserStorage {
return sp.contains("email")
}

/**
* Saves the user.
* @param user is the user to save.
*
* This function save the user password encrypted.
*/
override fun saveUser(user: User) {
val sp: SharedPreferences = context.getSharedPreferences(name, MODE_PRIVATE)
val masterKey: MasterKey =
Expand Down
Loading

0 comments on commit 4da1101

Please sign in to comment.