From 5516217e1e33eec1dc229f5b14f4f122651a3236 Mon Sep 17 00:00:00 2001 From: Marwan Qashwa Date: Fri, 16 May 2025 10:53:36 +0300 Subject: [PATCH] refactor methods name,replace single exp if,and use true choice before false --- .../csv_data_source/schema/AuditSchema.kt | 80 +++++++++++-------- .../csv_data_source/schema/ProjectSchema.kt | 55 ++++++++----- .../data/csv_data_source/schema/TaskSchema.kt | 35 ++++---- .../csv_data_source/schema/TaskStateSchema.kt | 41 +++++----- .../data/csv_data_source/schema/UserSchema.kt | 57 +++++++------ 5 files changed, 145 insertions(+), 123 deletions(-) diff --git a/src/main/kotlin/data/csv_data_source/schema/AuditSchema.kt b/src/main/kotlin/data/csv_data_source/schema/AuditSchema.kt index 87b7c26..32dc3bd 100644 --- a/src/main/kotlin/data/csv_data_source/schema/AuditSchema.kt +++ b/src/main/kotlin/data/csv_data_source/schema/AuditSchema.kt @@ -15,20 +15,26 @@ class AuditSchema( } override fun toRow(entity: AuditLogDto): List { - return if (checkAuditLogIsNotValid(entity)) emptyList() - else auditLogToStringsList(entity) + return if (checkAuditLogDtoIsNotValid(entity).not()) { + mapAuditLogDtoToList(entity) + } else { + emptyList() + } } override fun fromRow(row: List): AuditLogDto? { - return if (checkRowIsNotValidAuditLog(row)) null - else stringsListToAuditLog(row) + return if (checkRowIsNotValidAuditLogDto(row).not()) { + mapListToAuditLogDto(row) + } else { + null + } } override fun getId(entity: AuditLogDto): String? { return entity.id.ifEmpty { null } } - private fun auditLogToStringsList(auditLog: AuditLogDto): List { + private fun mapAuditLogDtoToList(auditLog: AuditLogDto): List { return listOf( auditLog.id, auditLog.timestamp.toString(), @@ -40,52 +46,60 @@ class AuditSchema( ) } - private fun stringsListToAuditLog(row: List): AuditLogDto { + private fun mapListToAuditLogDto(row: List): AuditLogDto { return AuditLogDto( id = row[AuditLogIndex.ID], - timestamp = row[AuditLogIndex.TIMES_TAMP].toLong() , + timestamp = row[AuditLogIndex.TIMES_TAMP].toLong(), createdByUserId = row[AuditLogIndex.CREATE_BY], - auditAction = stringToAuditAction(row[AuditLogIndex.AUDIT_ACTION]), + auditAction = mapStringToAuditAction(row[AuditLogIndex.AUDIT_ACTION]), changesDescription = row[AuditLogIndex.CHANGES_DESCRIPTION].ifEmpty { null }, - entityType = stringToEntityType(row[AuditLogIndex.ENTITY_TYPE]), + entityType = mapStringToEntityType(row[AuditLogIndex.ENTITY_TYPE]), entityId = row[AuditLogIndex.ENTITY_ID] ) } - private fun checkRowIsNotValidAuditLog(row: List): Boolean { - return (row[AuditLogIndex.ID].isEmpty() || - row[AuditLogIndex.TIMES_TAMP].isEmpty() || - row[AuditLogIndex.CREATE_BY].isEmpty() || - row[AuditLogIndex.AUDIT_ACTION].isEmpty() || - row[AuditLogIndex.ENTITY_ID].isEmpty() || - row[AuditLogIndex.AUDIT_ACTION] !in enumValues().map { it.name } || - row[AuditLogIndex.ENTITY_TYPE] !in enumValues().map { it.name } - ) + private fun checkRowIsNotValidAuditLogDto(row: List): Boolean { + return (row[AuditLogIndex.ID].isEmpty() || row[AuditLogIndex.TIMES_TAMP].isEmpty() || row[AuditLogIndex.CREATE_BY].isEmpty() || row[AuditLogIndex.AUDIT_ACTION].isEmpty() || row[AuditLogIndex.ENTITY_ID].isEmpty() || row[AuditLogIndex.AUDIT_ACTION] !in enumValues().map { it.name } || row[AuditLogIndex.ENTITY_TYPE] !in enumValues().map { it.name }) } - private fun stringToAuditAction(auditAction: String): AuditLog.AuditAction { + private fun mapStringToAuditAction(auditAction: String): AuditLog.AuditAction { return when (auditAction) { - AuditLog.AuditAction.CREATE.toString() -> AuditLog.AuditAction.CREATE - AuditLog.AuditAction.UPDATE.toString() -> AuditLog.AuditAction.UPDATE - AuditLog.AuditAction.DELETE.toString() -> AuditLog.AuditAction.DELETE - else -> AuditLog.AuditAction.CREATE + AuditLog.AuditAction.CREATE.toString() -> { + AuditLog.AuditAction.CREATE + } + + AuditLog.AuditAction.UPDATE.toString() -> { + AuditLog.AuditAction.UPDATE + } + + AuditLog.AuditAction.DELETE.toString() -> { + AuditLog.AuditAction.DELETE + } + + else -> { + AuditLog.AuditAction.CREATE + } } } - private fun stringToEntityType(entityType: String): AuditLog.EntityType { + private fun mapStringToEntityType(entityType: String): AuditLog.EntityType { return when (entityType) { - AuditLog.EntityType.TASK.toString() -> AuditLog.EntityType.TASK - AuditLog.EntityType.PROJECT.toString() -> AuditLog.EntityType.PROJECT - else -> AuditLog.EntityType.TASK + AuditLog.EntityType.TASK.toString() -> { + AuditLog.EntityType.TASK + } + + AuditLog.EntityType.PROJECT.toString() -> { + AuditLog.EntityType.PROJECT + } + + else -> { + AuditLog.EntityType.TASK + } } } - private fun checkAuditLogIsNotValid(auditLog: AuditLogDto): Boolean { - return (auditLog.id.isEmpty() || - auditLog.timestamp <= 0 || - auditLog.createdByUserId.isEmpty() || - auditLog.entityId.isEmpty() - ) + private fun checkAuditLogDtoIsNotValid(auditLog: AuditLogDto): Boolean { + return (auditLog.id.isEmpty() || auditLog.timestamp <= 0 || auditLog.createdByUserId.isEmpty() || auditLog.entityId.isEmpty()) } private companion object { diff --git a/src/main/kotlin/data/csv_data_source/schema/ProjectSchema.kt b/src/main/kotlin/data/csv_data_source/schema/ProjectSchema.kt index 0040a15..39698ab 100644 --- a/src/main/kotlin/data/csv_data_source/schema/ProjectSchema.kt +++ b/src/main/kotlin/data/csv_data_source/schema/ProjectSchema.kt @@ -4,8 +4,7 @@ import com.berlin.data.ProjectIndex import com.berlin.data.dto.ProjectDto class ProjectSchema( - override val fileName: String, - override val header: List + override val fileName: String, override val header: List ) : BaseSchema { init { @@ -13,20 +12,26 @@ class ProjectSchema( } override fun toRow(entity: ProjectDto): List { - return if (checkProjectIsNotValid(entity)) emptyList() - else projectToStringsList(entity) + return if (checkProjectDtoIsNotValid(entity).not()) { + mapProjectDtoToList(entity) + } else { + emptyList() + } } override fun fromRow(row: List): ProjectDto? { - return if (checkRowIsNotValidProject(row)) null - else stringsListToProject(row) + return if (checkRowIsNotValidProjectDto(row).not()) { + mapListToProjectDto(row) + } else { + null + } } override fun getId(entity: ProjectDto): String? { return entity.id.ifEmpty { null } } - private fun projectToStringsList(project: ProjectDto): List { + private fun mapProjectDtoToList(project: ProjectDto): List { return listOf( project.id, project.title, @@ -36,31 +41,37 @@ class ProjectSchema( ) } - private fun stringsListToProject(row: List): ProjectDto { + private fun mapListToProjectDto(row: List): ProjectDto { return ProjectDto( id = row[ProjectIndex.ID], title = row[ProjectIndex.NAME], description = row[ProjectIndex.DESCRIPTION].ifEmpty { null }, - statesId = row[ProjectIndex.STATES_ID].let { if (it == "[]") null else stringListToList(it) }, - tasksId = row[ProjectIndex.TASKS_ID].let { if (it == "[]") null else stringListToList(it) } - ) + statesId = row[ProjectIndex.STATES_ID].let { + if ((it == "[]").not()) { + mapRowToList(it) + } else { + null + } + }, + tasksId = row[ProjectIndex.TASKS_ID].let { + if ((it == "[]").not()) { + mapRowToList(it) + } else { + null + } + }) } - private fun checkRowIsNotValidProject(row: List): Boolean { - return (row.isEmpty()|| - row[ProjectIndex.ID].isEmpty() || - row[ProjectIndex.NAME].isEmpty()) + private fun checkRowIsNotValidProjectDto(row: List): Boolean { + return (row.isEmpty() || row[ProjectIndex.ID].isEmpty() || row[ProjectIndex.NAME].isEmpty()) } - private fun checkProjectIsNotValid(project: ProjectDto): Boolean { - return project.id.isEmpty() || - project.title.isEmpty() + private fun checkProjectDtoIsNotValid(project: ProjectDto): Boolean { + return project.id.isEmpty() || project.title.isEmpty() } - private fun stringListToList(listString: String): List { - return listString - .removeSurrounding("[", "]") - .split(",") + private fun mapRowToList(row: String): List { + return row.removeSurrounding("[", "]").split(",") } private companion object { diff --git a/src/main/kotlin/data/csv_data_source/schema/TaskSchema.kt b/src/main/kotlin/data/csv_data_source/schema/TaskSchema.kt index ea3858b..66e432c 100644 --- a/src/main/kotlin/data/csv_data_source/schema/TaskSchema.kt +++ b/src/main/kotlin/data/csv_data_source/schema/TaskSchema.kt @@ -4,8 +4,7 @@ import com.berlin.data.TaskIndex import com.berlin.data.dto.TaskDto class TaskSchema( - override val fileName: String, - override val header: List + override val fileName: String, override val header: List ) : BaseSchema { init { @@ -13,20 +12,26 @@ class TaskSchema( } override fun toRow(entity: TaskDto): List { - return if (checkTaskIsNotValid(entity)) emptyList() - else taskToStringsList(entity) + return if (checkTaskIsNotValid(entity).not()) { + mapTaskDtoToList(entity) + } else { + emptyList() + } } override fun fromRow(row: List): TaskDto? { - return if (checkRowIsNotValidTask(row)) null - else stringsListToTask(row) + return if (checkRowIsNotValidTask(row).not()) { + mapListToTaskDto(row) + } else { + null + } } override fun getId(entity: TaskDto): String? { return entity.id.ifEmpty { null } } - private fun taskToStringsList(task: TaskDto): List { + private fun mapTaskDtoToList(task: TaskDto): List { return listOf( task.id, task.projectId, @@ -38,7 +43,7 @@ class TaskSchema( ) } - private fun stringsListToTask(row: List): TaskDto { + private fun mapListToTaskDto(row: List): TaskDto { return TaskDto( id = row[TaskIndex.ID], projectId = row[TaskIndex.PROJECT_ID], @@ -51,21 +56,11 @@ class TaskSchema( } private fun checkRowIsNotValidTask(row: List): Boolean { - return (row[TaskIndex.ID].isEmpty() || - row[TaskIndex.PROJECT_ID].isEmpty() || - row[TaskIndex.TITLE].isEmpty() || - row[TaskIndex.STATE_ID].isEmpty() || - row[TaskIndex.ASSIGNED_TO_USER_ID].isEmpty() || - row[TaskIndex.CREATE_BY_USER_ID].isEmpty()) + return (row[TaskIndex.ID].isEmpty() || row[TaskIndex.PROJECT_ID].isEmpty() || row[TaskIndex.TITLE].isEmpty() || row[TaskIndex.STATE_ID].isEmpty() || row[TaskIndex.ASSIGNED_TO_USER_ID].isEmpty() || row[TaskIndex.CREATE_BY_USER_ID].isEmpty()) } private fun checkTaskIsNotValid(task: TaskDto): Boolean { - return (task.id.isEmpty() || - task.projectId.isEmpty() || - task.title.isEmpty() || - task.stateId.isEmpty() || - task.assignedToUserId.isEmpty() || - task.createByUserId.isEmpty()) + return (task.id.isEmpty() || task.projectId.isEmpty() || task.title.isEmpty() || task.stateId.isEmpty() || task.assignedToUserId.isEmpty() || task.createByUserId.isEmpty()) } private companion object { diff --git a/src/main/kotlin/data/csv_data_source/schema/TaskStateSchema.kt b/src/main/kotlin/data/csv_data_source/schema/TaskStateSchema.kt index a73a2ee..5918dc0 100644 --- a/src/main/kotlin/data/csv_data_source/schema/TaskStateSchema.kt +++ b/src/main/kotlin/data/csv_data_source/schema/TaskStateSchema.kt @@ -4,8 +4,7 @@ import com.berlin.data.StateIndex import com.berlin.data.dto.TaskStateDto class TaskStateSchema( - override val fileName: String, - override val header: List + override val fileName: String, override val header: List ) : BaseSchema { init { @@ -13,45 +12,43 @@ class TaskStateSchema( } override fun toRow(entity: TaskStateDto): List { - return if (checkStateIsNotValid(entity)) emptyList() - else stateToStringsList(entity) + return if (checkTaskStateDtoIsNotValid(entity).not()) { + mapTaskStateDtoToList(entity) + } else { + emptyList() + } } override fun fromRow(row: List): TaskStateDto? { - return if (checkRowIsNotValidState(row)) null - else stringsListToState(row) + return if (checkRowIsNotValidTaskStateDto(row).not()) { + mapListToTaskStateDto(row) + } else { + null + } } override fun getId(entity: TaskStateDto): String? { return entity.id.ifEmpty { null } } - private fun stateToStringsList(state: TaskStateDto): List { + private fun mapTaskStateDtoToList(state: TaskStateDto): List { return listOf( - state.id, - state.name, - state.projectId + state.id, state.name, state.projectId ) } - private fun stringsListToState(row: List): TaskStateDto { + private fun mapListToTaskStateDto(row: List): TaskStateDto { return TaskStateDto( - id = row[StateIndex.ID], - name = row[StateIndex.NAME], - projectId = row[StateIndex.PROJECT_ID] + id = row[StateIndex.ID], name = row[StateIndex.NAME], projectId = row[StateIndex.PROJECT_ID] ) } - private fun checkRowIsNotValidState(row: List): Boolean { - return (row[StateIndex.ID].isEmpty() || - row[StateIndex.NAME].isEmpty() || - row[StateIndex.PROJECT_ID].isEmpty()) + private fun checkRowIsNotValidTaskStateDto(row: List): Boolean { + return (row[StateIndex.ID].isEmpty() || row[StateIndex.NAME].isEmpty() || row[StateIndex.PROJECT_ID].isEmpty()) } - private fun checkStateIsNotValid(state: TaskStateDto): Boolean { - return (state.id.isEmpty() || - state.name.isEmpty() || - state.projectId.isEmpty()) + private fun checkTaskStateDtoIsNotValid(state: TaskStateDto): Boolean { + return (state.id.isEmpty() || state.name.isEmpty() || state.projectId.isEmpty()) } private companion object { diff --git a/src/main/kotlin/data/csv_data_source/schema/UserSchema.kt b/src/main/kotlin/data/csv_data_source/schema/UserSchema.kt index 0770e13..524171c 100644 --- a/src/main/kotlin/data/csv_data_source/schema/UserSchema.kt +++ b/src/main/kotlin/data/csv_data_source/schema/UserSchema.kt @@ -5,8 +5,7 @@ import com.berlin.data.dto.UserDto import com.berlin.domain.model.user.User class UserSchema( - override val fileName: String, - override val header: List + override val fileName: String, override val header: List ) : BaseSchema { init { @@ -14,55 +13,61 @@ class UserSchema( } override fun toRow(entity: UserDto): List { - return if (checkUserIsNotValid(entity)) emptyList() - else userToStringsList(entity) + return if (checkUserDtoIsNotValid(entity).not()) { + mapUserDtoToList(entity) + } else { + emptyList() + } } override fun fromRow(row: List): UserDto? { - return if (checkRowIsNotValidUser(row)) null - else stringsListToUser(row) + return if (checkRowIsNotValidUserDto(row).not()) { + mapListToUserDto(row) + } else { + null + } } override fun getId(entity: UserDto): String? { return entity.id.ifEmpty { null } } - private fun userToStringsList(user: UserDto): List { + private fun mapUserDtoToList(user: UserDto): List { return listOf( - user.id, - user.userName, - user.password, - user.role.toString() + user.id, user.userName, user.password, user.role.toString() ) } - private fun stringsListToUser(row: List): UserDto{ + private fun mapListToUserDto(row: List): UserDto { return UserDto( id = row[UserIndex.ID], userName = row[UserIndex.USER_NAME], password = row[UserIndex.PASSWORD], - role = stringToUserRole(row[UserIndex.ROLE]) + role = mapStringToUserRole(row[UserIndex.ROLE]) ) } - private fun checkRowIsNotValidUser(row: List): Boolean { - return (row[UserIndex.ID].isEmpty() || - row[UserIndex.USER_NAME].isEmpty() || - row[UserIndex.PASSWORD].isEmpty() || - row[UserIndex.ROLE] !in enumValues().map { it.name }) + private fun checkRowIsNotValidUserDto(row: List): Boolean { + return (row[UserIndex.ID].isEmpty() || row[UserIndex.USER_NAME].isEmpty() || row[UserIndex.PASSWORD].isEmpty() || row[UserIndex.ROLE] !in enumValues().map { it.name }) } - private fun checkUserIsNotValid(user: UserDto): Boolean { - return (user.id.isEmpty() || - user.userName.isEmpty() || - user.password.isEmpty()) + private fun checkUserDtoIsNotValid(user: UserDto): Boolean { + return (user.id.isEmpty() || user.userName.isEmpty() || user.password.isEmpty()) } - private fun stringToUserRole(roleString: String): User.UserRole { + private fun mapStringToUserRole(roleString: String): User.UserRole { return when (roleString) { - User.UserRole.ADMIN.toString() -> User.UserRole.ADMIN - User.UserRole.MATE.toString() -> User.UserRole.MATE - else -> User.UserRole.MATE + User.UserRole.ADMIN.toString() -> { + User.UserRole.ADMIN + } + + User.UserRole.MATE.toString() -> { + User.UserRole.MATE + } + + else -> { + User.UserRole.MATE + } } }