-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
belinda-closet-android-02-224-add_product_functionality (#237)
* 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
1 parent
facfc42
commit 3a94c92
Showing
16 changed files
with
299 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ta/network/dto/auth_dto/ArchiveRequest.kt → ...network/dto/product_dto/ArchiveRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/network/dto/auth_dto/ArchiveResponse.kt → ...etwork/dto/product_dto/ArchiveResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ata/network/dto/auth_dto/DeleteRequest.kt → .../network/dto/product_dto/DeleteRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ta/network/dto/auth_dto/DeleteResponse.kt → ...network/dto/product_dto/DeleteResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/example/belindas_closet/data/network/dto/product_dto/ProductRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
46 changes: 46 additions & 0 deletions
46
...src/main/java/com/example/belindas_closet/data/network/dto/product_dto/ProductResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
6 changes: 3 additions & 3 deletions
6
...loset/data/network/auth/ArchiveService.kt → ...et/data/network/product/ArchiveService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...t/data/network/auth/ArchiveServiceImpl.kt → ...ata/network/product/ArchiveServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...closet/data/network/auth/DeleteService.kt → ...set/data/network/product/DeleteService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...et/data/network/auth/DeleteServiceImpl.kt → ...data/network/product/DeleteServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/example/belindas_closet/data/network/product/ProductService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "") ?: "" | ||
}, | ||
) | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/example/belindas_closet/data/network/product/ProductServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.