Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions app/src/main/java/com/moscow/tudee/data/local/mapper/mapper.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.moscow.tudee.data.local.mapper

import com.moscow.tudee.data.local.entity.CategoryEntity
Expand Down Expand Up @@ -29,7 +28,7 @@ fun Category.toCategoryEntity(): CategoryEntity {

fun TaskEntity.toTask(category: Category): Task {
return Task(
id = id.takeIf { it.toInt() != 0 },
id = id,
title = title,
description = description,
priority = getPriorityFromString(priority),
Expand All @@ -45,7 +44,7 @@ fun TaskEntity.toTask(category: Category): Task {

fun Task.toTaskEntity(): TaskEntity {
return TaskEntity(
id = id ?: 0,
id = id,
title = title,
description = description,
priority = priority.toString(),
Expand Down Expand Up @@ -84,4 +83,19 @@ fun List<TaskEntity>.toTasksByCategories(categoriesById: Map<Long, CategoryEntit
fun List<TaskEntity>.toTasksBySingleCategory(categoryEntity: CategoryEntity): List<Task> {
val domainCategory = categoryEntity.toCategory()
return this.map { it.toTask(domainCategory) }
}

fun createCategoryEntity(
title: String,
iconUri: String,
isPredefined: Boolean,
countOfTasks: Int
): CategoryEntity {
return CategoryEntity(
id = 0,
title = title,
iconUri = iconUri,
isPredefined = isPredefined,
countOfTasks = countOfTasks
)
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package com.moscow.tudee.data.service

import com.moscow.tudee.data.local.dao.CategoryDao
import com.moscow.tudee.data.local.mapper.createCategoryEntity
import com.moscow.tudee.data.local.mapper.toCategory
import com.moscow.tudee.data.local.mapper.toCategoryEntity
import com.moscow.tudee.data.local.mapper.toTask
import com.moscow.tudee.domain.entity.Category
import com.moscow.tudee.domain.entity.Task
import com.moscow.tudee.domain.service.CategoryServices

class CategoryServicesImpl(
private val categoryDao: CategoryDao
): CategoryServices {
) : CategoryServices {

override suspend fun getCategories(): List<Category> {
return categoryDao.getCategories().map { it.toCategory() }
}

override suspend fun addCategory(category: Category) {
categoryDao.addCategory(category.toCategoryEntity())
override suspend fun addCategory(
title: String,
iconUri: String,
isPredefined: Boolean,
countOfTasks: Int
) {
categoryDao.addCategory(createCategoryEntity(title, iconUri, isPredefined, countOfTasks))
}

override suspend fun updateCategory(category: Category) {
Expand All @@ -29,6 +33,7 @@ class CategoryServicesImpl(
}

override suspend fun getCategoryById(categoryId: Long): Category {
return categoryDao.getCategoryById(categoryId)?.toCategory() ?: throw Exception("task category found")
return categoryDao.getCategoryById(categoryId)?.toCategory()
?: throw Exception("task category found")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class TasksServicesImpl(
}

override suspend fun updateTask(task: Task) {
task.id?.let { taskId ->
task.id.let { taskId ->
taskDao.getTaskById(taskId)?.let { oldTask ->
categoryDao.getCategoryById(oldTask.categoryId)?.let { oldCategory ->
categoryDao.decrementTaskCount(oldCategory.id)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/moscow/tudee/domain/entity/Task.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import kotlinx.datetime.LocalDateTime


data class Task(
val id: Long? = null,
val id: Long,
val title: String,
val description: String,
val priority: Priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ interface CategoryServices {

suspend fun getCategories(): List<Category>

suspend fun addCategory(category: Category)
suspend fun addCategory(
title: String,
iconUri: String,
isPredefined: Boolean = false,
countOfTasks: Int = 0
)

suspend fun updateCategory(category: Category)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun Category.toCategoryUi() = CategoryUi(

fun TaskUi.toTask(): Task =
Task(
id = id,
id = id ?: 0L,
title = title,
description = description,
priority = priority ?: Priority.LOW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.moscow.tudee.R
import com.moscow.tudee.domain.entity.Category
import com.moscow.tudee.domain.service.CategoryServices
import com.moscow.tudee.presentation.base.BaseViewModel
import com.moscow.tudee.presentation.mapper.toCategory
import com.moscow.tudee.presentation.mapper.toCategoryUi
import com.moscow.tudee.presentation.model.CategoryUi
import com.moscow.tudee.presentation.screen.category.CategoriesScreenState
Expand Down Expand Up @@ -60,7 +59,14 @@ class CategoryViewModel(

override fun onAddCategory(categoryUi: CategoryUi) {
launchWithResult(
action = { categoryServices.addCategory(categoryUi.toCategory()) },
action = {
categoryServices.addCategory(
title = categoryUi.title,
iconUri = categoryUi.iconUrl,
isPredefined = categoryUi.isPredefined,
countOfTasks = categoryUi.countOfTasks,
)
},
onSuccess = { onAddCategorySuccess() },
onError = ::onAddCategoryFailed,
onStart = ::onLoading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AddTaskBottomSheetViewModel(
action = {
tasksServices.addTask(
Task(
id = null,
id = 0L,
title = uiState.value.title,
description = uiState.value.description,
priority = uiState.value.priority ?: return@launchWithResult onErrorAddTask(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class TasksServicesImplTest {
@Test
fun `should insert task into dao when adding new task`() = runTest {
val newTask = Task(
id = null,
id = 0L,
title = "Test Task",
description = "Description",
priority = Task.Priority.HIGH,
Expand All @@ -217,7 +217,7 @@ class TasksServicesImplTest {
title = "Test Task",
description = "Description",
priority = Task.Priority.HIGH.name,
categoryId = sampleCategory.id!!,
categoryId = sampleCategory.id,
status = Task.Status.TODO.name,
date = "2025-06-18"
)
Expand All @@ -226,7 +226,7 @@ class TasksServicesImplTest {
tasksServices.addTask(newTask)

coVerify { taskDao.addTask(expectedEntity) }
coVerify { categoryDao.incrementTaskCount(sampleCategory.id!!) }
coVerify { categoryDao.incrementTaskCount(sampleCategory.id) }
}

@Test
Expand All @@ -245,7 +245,7 @@ class TasksServicesImplTest {
title = "Updated Task",
description = "Updated Description",
priority = Task.Priority.MEDIUM.name,
categoryId = sampleCategory.id!!,
categoryId = sampleCategory.id,
status = Task.Status.IN_PROGRESS.name,
date = "2025-06-18"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class CategoryViewModelTest {

@Test
fun `should return success state when category is added successfully`() {
coEvery { categoryServices.addCategory(any()) } returns Unit
coEvery { categoryServices.addCategory(any(), any(), any(), any()) } returns Unit
coEvery { categoryServices.getCategories() } returns listOf(category)

viewModel.onAddCategory(category.toCategoryUi())
Expand All @@ -106,7 +106,14 @@ class CategoryViewModelTest {

@Test
fun `should return error state when adding category fails`() {
coEvery { categoryServices.addCategory(any()) } throws Exception("Failed")
coEvery {
categoryServices.addCategory(
any(),
any(),
any(),
any()
)
} throws Exception("Failed")

viewModel.onAddCategory(category.toCategoryUi())
testScope.advanceUntilIdle()
Expand Down