Skip to main content

Overview

The SectorsRepository provides CRUD operations for managing sectors within greenhouses. Sectors are subdivisions of greenhouses where devices are installed. All methods are suspend functions that return Result<T> for consistent error handling.

Methods

getSectorsByTenantId()

Fetches all sectors for a specific tenant.
suspend fun getSectorsByTenantId(tenantId: Long): Result<List<Sector>>
Parameters
tenantId
Long
required
The tenant ID to filter sectors by
Returns
Result<List<Sector>>
Result
Result containing a list of Sector objects or an error

createSector()

Creates a new sector for a tenant.
suspend fun createSector(
    tenantId: Long,
    greenhouseId: Long,
    name: String?
): Result<Sector>
Parameters
tenantId
Long
required
The tenant ID the sector belongs to
greenhouseId
Long
required
The greenhouse ID the sector belongs to
name
String
The name of the sector (nullable)
Returns
Result<Sector>
Result
Result containing the created Sector object or an error

updateSector()

Updates an existing sector.
suspend fun updateSector(
    tenantId: Long,
    sectorId: Long,
    greenhouseId: Long?,
    name: String?
): Result<Sector>
Parameters
tenantId
Long
required
The tenant ID the sector belongs to
sectorId
Long
required
The sector ID to update
greenhouseId
Long
New greenhouse ID (optional)
name
String
New name (optional)
Returns
Result<Sector>
Result
Result containing the updated Sector object or an error

deleteSector()

Deletes a sector by ID.
suspend fun deleteSector(tenantId: Long, sectorId: Long): Result<Unit>
Parameters
tenantId
Long
required
The tenant ID the sector belongs to
sectorId
Long
required
The sector ID to delete
Returns
Result<Unit>
Result
Result indicating success or error

Usage Example

class SectorsViewModel(
    private val sectorsRepository: SectorsRepository
) : ViewModel() {
    
    private val _sectors = MutableStateFlow<List<Sector>>(emptyList())
    val sectors: StateFlow<List<Sector>> = _sectors.asStateFlow()
    
    private val _error = MutableStateFlow<String?>(null)
    val error: StateFlow<String?> = _error.asStateFlow()
    
    fun loadSectors(tenantId: Long) {
        viewModelScope.launch {
            sectorsRepository.getSectorsByTenantId(tenantId)
                .onSuccess { _sectors.value = it }
                .onFailure { _error.value = it.message }
        }
    }
    
    fun createSector(
        tenantId: Long,
        greenhouseId: Long,
        name: String?
    ) {
        viewModelScope.launch {
            sectorsRepository.createSector(
                tenantId = tenantId,
                greenhouseId = greenhouseId,
                name = name
            )
                .onSuccess { loadSectors(tenantId) }
                .onFailure { _error.value = it.message }
        }
    }
    
    fun updateSector(
        tenantId: Long,
        sectorId: Long,
        greenhouseId: Long?,
        name: String?
    ) {
        viewModelScope.launch {
            sectorsRepository.updateSector(
                tenantId = tenantId,
                sectorId = sectorId,
                greenhouseId = greenhouseId,
                name = name
            )
                .onSuccess { loadSectors(tenantId) }
                .onFailure { _error.value = it.message }
        }
    }
    
    fun deleteSector(tenantId: Long, sectorId: Long) {
        viewModelScope.launch {
            sectorsRepository.deleteSector(tenantId, sectorId)
                .onSuccess { loadSectors(tenantId) }
                .onFailure { _error.value = it.message }
        }
    }
}

Dependency Injection

Inject the repository using Koin in your Composables:
import org.koin.compose.viewmodel.koinViewModel

@Composable
fun SectorsScreen(tenantId: Long) {
    val viewModel: SectorsViewModel = koinViewModel()
    val sectors by viewModel.sectors.collectAsState()
    
    LaunchedEffect(tenantId) {
        viewModel.loadSectors(tenantId)
    }
    
    // UI implementation
}

Build docs developers (and LLMs) love