Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
UserAPI: Add functions getAll*()
Browse files Browse the repository at this point in the history
  • Loading branch information
DareFox committed Apr 22, 2022
1 parent 5cadc84 commit 7e234ff
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 1 deletion.
77 changes: 76 additions & 1 deletion src/main/kotlin/kmtt/api/user/AuthUserAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kmtt.api.user

import io.ktor.client.request.*
import io.ktor.http.*
import kmtt.constants.Content
import kmtt.ktor.IHttpClient
import kmtt.ktor.request
import kmtt.models.Notification
Expand Down Expand Up @@ -102,6 +101,25 @@ internal class AuthUserAPI(
return response.result
}

override suspend fun getAllMyComments(): List<Comment> {
val user = getMe()

val commentCount = user.counters?.comments
requireNotNull(commentCount) {
"comment count in null"
}

val comments = mutableListOf<Comment>()

for (offset in 0 until commentCount step 50) {
val comment = getMyComments(50, offset.toInt())

comments += comment
}

return comments
}

override suspend fun getMyEntries(count: Int, offset: Int): List<Entry> {
val endpointURL = "/user/me/comments"
val params = mutableListOf<Pair<String, String>>()
Expand All @@ -122,6 +140,25 @@ internal class AuthUserAPI(
return response.result
}

override suspend fun getAllMyEntries(): List<Entry> {
val user = getMe()

val entriesCounter = user.counters?.entries
requireNotNull(entriesCounter) {
"comment count in null"
}

val entries = mutableListOf<Entry>()

for (offset in 0 until entriesCounter step 50) {
val entry = getMyEntries(50, offset.toInt())

entries += entry
}

return entries
}

override suspend fun getMyFavoriteEntries(count: Int, offset: Int): List<Entry> {
val endpointURL = "/user/me/favorites/entries"
val params = mutableListOf<Pair<String, String>>()
Expand All @@ -142,6 +179,25 @@ internal class AuthUserAPI(
return response.result
}

override suspend fun getAllMyFavoriteEntries(): List<Entry> {
val user = getMe()

val entriesCounter = user.counters?.favorites
requireNotNull(entriesCounter) {
"comment count in null"
}

val entries = mutableListOf<Entry>()

for (offset in 0 until entriesCounter step 50) {
val entry = getMyFavoriteEntries(50, offset.toInt())

entries += entry
}

return entries
}

override suspend fun getMyFavoriteComments(count: Int, offset: Int): List<Comment> {
val endpointURL = "/user/me/favorites/comments"
val params = mutableListOf<Pair<String, String>>()
Expand All @@ -162,4 +218,23 @@ internal class AuthUserAPI(
return response.result
}

override suspend fun getAllMyFavoriteComments(): List<Comment> {
val user = getMe()

val entriesCounter = user.counters?.favorites
requireNotNull(entriesCounter) {
"comment count in null"
}

val comments = mutableListOf<Comment>()

for (offset in 0 until entriesCounter step 50) {
val comment = getMyFavoriteComments( 50, offset.toInt())

comments += comment
}

return comments
}

}
42 changes: 42 additions & 0 deletions src/main/kotlin/kmtt/api/user/IPublicUserAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ interface IPublicUserAPI {

/**
* Получить комментарии пользователя
*
* Max count: 50
*/
suspend fun getUserComments(userID: Long, count: Int, offset: Int): List<Comment>

/**
* Получить все комментарии пользователя
*/
suspend fun getAllUserComments(userID: Long): List<Comment>

/**
* Получить записи пользователя
*
* Max count: 50
*/
suspend fun getUserEntries(userID: Long, count: Int, offset: Int): List<Entry>

/**
* Получить записи пользователя
*/
suspend fun getAllUserEntries(userID: Long): List<Entry>
}

interface IAuthUserAPI : IPublicUserAPI, Authable {
Expand Down Expand Up @@ -52,21 +66,49 @@ interface IAuthUserAPI : IPublicUserAPI, Authable {

/**
* Получить комментарии текущего пользователя
*
* Max count: 50
*/
suspend fun getMyComments(count: Int, offset: Int): List<Comment>

/**
* Получить все комментарии текущего пользователя
*/
suspend fun getAllMyComments(): List<Comment>

/**
* Получить записи текущего пользователя
*
* Max count: 50
*/
suspend fun getMyEntries(count: Int, offset: Int): List<Entry>

/**
* Получить все записи текущего пользователя
*/
suspend fun getAllMyEntries(): List<Entry>

/**
* Получить избранные записи текущего пользователя
*
* Max count: 50
*/
suspend fun getMyFavoriteEntries(count: Int, offset: Int): List<Entry>

/**
* Получить все избранные записи текущего пользователя
*/
suspend fun getAllMyFavoriteEntries(): List<Entry>

/**
* Получить избранные комментарии текущего пользователя
*
* Max count: 50
*/
suspend fun getMyFavoriteComments(count: Int, offset: Int): List<Comment>

/**
* Получить все избранные комментарии текущего пользователя
*/
suspend fun getAllMyFavoriteComments(): List<Comment>
}
38 changes: 38 additions & 0 deletions src/main/kotlin/kmtt/api/user/PublicUserAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ internal class PublicUserAPI(private val httpClient: IHttpClient, private val si
return response.result
}

override suspend fun getAllUserComments(userID: Long): List<Comment> {
val user = getUserByID(userID)

val commentCount = user.counters?.comments
requireNotNull(commentCount) {
"comment count in null"
}

val comments = mutableListOf<Comment>()

for (offset in 0 until commentCount step 50) {
val comment = getUserComments(userID, 50, offset.toInt())

comments += comment
}

return comments
}

override suspend fun getUserEntries(userID: Long, count: Int, offset: Int): List<Entry> {
val endpointURL = "/user/$userID/entries"
val params = mutableListOf<Pair<String, String>>()
Expand All @@ -74,4 +93,23 @@ internal class PublicUserAPI(private val httpClient: IHttpClient, private val si

return response.result
}

override suspend fun getAllUserEntries(userID: Long): List<Entry> {
val user = getUserByID(userID)

val entriesCounter = user.counters?.entries
requireNotNull(entriesCounter) {
"comment count in null"
}

val entries = mutableListOf<Entry>()

for (offset in 0 until entriesCounter step 50) {
val entry = getUserEntries(userID, 50, offset.toInt())

entries += entry
}

return entries
}
}
29 changes: 29 additions & 0 deletions src/test/kotlin/kmtt/base/api/user/AuthUserAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,56 @@ internal class AuthUserAPITest {
}
}

@Test
fun getAllMyComments() {
assertDoesNotThrow {
api.getAllMyComments()
}
}


@Test
fun getMyEntries() {
assertDoesNotThrow {
api.getMyEntries(50, 0)
}
}

@Test
fun getAllMyEntries() {
assertDoesNotThrow {
api.getAllMyEntries()
}
}

@Test
fun getMyFavoriteEntries() {
assertDoesNotThrow {
api.getMyFavoriteEntries(50, 0)
}
}

@Test
fun getAllMyFavoriteEntries() {
assertDoesNotThrow {
api.getAllMyFavoriteEntries()
}
}

@Test
fun getMyFavoriteComments() {
assertDoesNotThrow {
api.getMyFavoriteEntries(50, 0)
}
}

@Test
fun getAllMyFavoriteComments() {
assertDoesNotThrow {
api.getAllMyFavoriteComments()
}
}

@Test
fun getUserByID() {
Shared.publicProfileID.forEach {
Expand Down

0 comments on commit 7e234ff

Please sign in to comment.