Skip to content

Commit

Permalink
belinda-closet-android-02-224-add_product_functionality (#237)
Browse files Browse the repository at this point in the history
* restructure files + add new product function

* change return type in serivces

* add CREATOR role to add new products

* removed nav to Login

---------

Co-authored-by: E. Brink <[email protected]>
  • Loading branch information
tinpham5614 and brinkbrink authored Jan 4, 2024
1 parent facfc42 commit 3a94c92
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object HttpRoutes {
private const val BASE_URL = "http://10.0.2.2:3000/api"
const val PRODUCTS = "$BASE_URL/products"
const val PRODUCT = "$BASE_URL/products/{id}"
const val ADD_PRODUCT = "$BASE_URL/products/new"
const val ARCHIVE = "$BASE_URL/products/archive"
const val DELETE = "$BASE_URL/products/remove"
const val LOGIN = "$BASE_URL/auth/login"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.belindas_closet.data.network.dto.auth_dto
package com.example.belindas_closet.data.network.dto.product_dto

import com.example.belindas_closet.data.network.dto.auth_dto.Role
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.belindas_closet.data.network.dto.auth_dto
package com.example.belindas_closet.data.network.dto.product_dto

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.belindas_closet.data.network.dto.auth_dto
package com.example.belindas_closet.data.network.dto.product_dto

import com.example.belindas_closet.data.network.dto.auth_dto.Role
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.belindas_closet.data.network.dto.auth_dto
package com.example.belindas_closet.data.network.dto.product_dto

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.belindas_closet.data.network.dto.product_dto

import com.example.belindas_closet.model.ProductGender
import com.example.belindas_closet.model.ProductSizes
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ProductRequest(
@SerialName("productType")
val productType: String,

@SerialName("productGender")
val productGender: ProductGender,

@SerialName("productSizeShoe")
val productSizeShoe: Int?,

@SerialName("productSizes")
val productSizes: ProductSizes,

@SerialName("productSizePantsWaist")
val productSizePantsWaist: Int?,

@SerialName("productSizePantsInseam")
val productSizePantsInseam: Int?,

@SerialName("productDescription")
val productDescription: String,

@SerialName("productImage")
val productImage: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.belindas_closet.data.network.dto.product_dto

import com.example.belindas_closet.model.ProductGender
import com.example.belindas_closet.model.ProductSizePantsInseam
import com.example.belindas_closet.model.ProductSizePantsWaist
import com.example.belindas_closet.model.ProductSizeShoes
import com.example.belindas_closet.model.ProductSizes
import com.example.belindas_closet.model.ProductType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ProductResponse(
@SerialName("productType")
val productType: ProductType,

@SerialName("productGender")
val productGender: ProductGender,

@SerialName("productSizeShoe")
val productSizeShoe: ProductSizeShoes,

@SerialName("productSizes")
val productSizes: ProductSizes,

@SerialName("productSizePantsWaist")
val productSizePantsWaist: ProductSizePantsWaist,

@SerialName("productSizePantsInseam")
val productSizePantsInseam: ProductSizePantsInseam,

@SerialName("productDescription")
val productDescription: String,

@SerialName("productImage")
val productImage: String,

@SerialName("isHidden")
val isHidden: Boolean,

@SerialName("isSold")
val isSold: Boolean,

@SerialName("id")
val id: String
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.belindas_closet.data.network.auth
package com.example.belindas_closet.data.network.product

import com.example.belindas_closet.MainActivity
import com.example.belindas_closet.data.network.dto.auth_dto.ArchiveRequest
import com.example.belindas_closet.data.network.dto.auth_dto.ArchiveResponse
import com.example.belindas_closet.data.network.dto.product_dto.ArchiveRequest
import com.example.belindas_closet.data.network.dto.product_dto.ArchiveResponse
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.belindas_closet.data.network.auth
package com.example.belindas_closet.data.network.product

import com.example.belindas_closet.data.network.HttpRoutes
import com.example.belindas_closet.data.network.dto.auth_dto.ArchiveRequest
import com.example.belindas_closet.data.network.dto.auth_dto.ArchiveResponse
import com.example.belindas_closet.data.network.dto.product_dto.ArchiveRequest
import com.example.belindas_closet.data.network.dto.product_dto.ArchiveResponse
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.ClientRequestException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.belindas_closet.data.network.auth
package com.example.belindas_closet.data.network.product

import com.example.belindas_closet.MainActivity
import com.example.belindas_closet.data.network.dto.auth_dto.DeleteRequest
import com.example.belindas_closet.data.network.dto.auth_dto.DeleteResponse
import com.example.belindas_closet.data.network.dto.product_dto.DeleteRequest
import com.example.belindas_closet.data.network.dto.product_dto.DeleteResponse
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.belindas_closet.data.network.auth
package com.example.belindas_closet.data.network.product

import com.example.belindas_closet.data.network.HttpRoutes
import com.example.belindas_closet.data.network.dto.auth_dto.DeleteRequest
import com.example.belindas_closet.data.network.dto.auth_dto.DeleteResponse
import com.example.belindas_closet.data.network.dto.product_dto.DeleteRequest
import com.example.belindas_closet.data.network.dto.product_dto.DeleteResponse
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.ClientRequestException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.belindas_closet.data.network.product

import com.example.belindas_closet.MainActivity
import com.example.belindas_closet.data.network.dto.product_dto.ProductRequest
import com.example.belindas_closet.data.network.dto.product_dto.ProductResponse
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.DEFAULT
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json

interface ProductService {
suspend fun getProduct(productRequest: ProductRequest): ProductResponse?

suspend fun getProducts(): List<ProductResponse>?

suspend fun addProduct(productRequest: ProductRequest): ProductResponse?

companion object {
fun create(): ProductService {
return ProductServiceImpl(
client = HttpClient(Android) {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
install (Logging) {
level = LogLevel.ALL
logger = Logger.DEFAULT
}
},
getToken = suspend {
MainActivity.getPref().getString("token", "") ?: ""
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.belindas_closet.data.network.product

import com.example.belindas_closet.data.network.HttpRoutes
import com.example.belindas_closet.data.network.dto.product_dto.ProductRequest
import com.example.belindas_closet.data.network.dto.product_dto.ProductResponse
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.plugins.RedirectResponseException
import io.ktor.client.plugins.ServerResponseException
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.post
import io.ktor.client.request.url
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.util.InternalAPI
import kotlinx.serialization.json.Json
import okhttp3.ResponseBody.Companion.toResponseBody

class ProductServiceImpl (
private val client: HttpClient,
private val getToken: suspend () -> String
) : ProductService {
override suspend fun getProduct(productRequest: ProductRequest): ProductResponse? {
TODO("Not yet implemented")
}

override suspend fun getProducts(): List<ProductResponse>? {
return try {
client.get {
url (HttpRoutes.PRODUCTS)
}
emptyList()
} catch (e: RedirectResponseException) {
println("Error: ${e.response.status.description}")
emptyList()
} catch (e: ClientRequestException) {
println("Error: ${e.response.status.description}")
emptyList()
} catch (e: ServerResponseException) {
println("Error: ${e.response.status.description}")
emptyList()
} catch (e: Exception) {
println("Error: ${e.message}")
emptyList()
}
}

@OptIn(InternalAPI::class)
override suspend fun addProduct(productRequest: ProductRequest): ProductResponse? {
return try {
val token = getToken()
val response = client.post {
url (HttpRoutes.ADD_PRODUCT)
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Authorization, "Bearer $token")
body = Json.encodeToString(ProductRequest.serializer(), productRequest)
}
response.body()
} catch (e: RedirectResponseException) {
println("Error: ${e.response.status.description}")
null
} catch (e: ClientRequestException) {
println("Error: ${e.response.status.description}")
null
} catch (e: ServerResponseException) {
println("Error: ${e.response.status.description}")
null
} catch (e: Exception) {
println("Error: ${e.message}")
null
}
}
}
Loading

0 comments on commit 3a94c92

Please sign in to comment.