Operation is not supported for read-only collection with a mutable list

30 Views Asked by At

I am doing a function to create items and add them to a mutableList.

In my controller, i have a function to create inventary (which contains a mutable list of items), another function to getinventoryById, a createItem (this is where i have the problem when i am testing). I dont know why this mutable list is throwing an error like this.

/**
   * Function that create a new item
   */
  @ApiIgnore
  @ApiOperation(value = "", nickname = "createItem")
  @PostMapping(
    value = ["/inventory/{inventoryId}/item"],
    produces = [org.springframework.http.MediaType.APPLICATION_JSON_VALUE]
  )
  fun createItem(
    @RequestBody dto: CreateItem,
    @PathVariable (required = true) inventoryId: String
  ): IdResponse = authorize(
    listOf(
      com.qmplus.shared.common.ApplicationPrivilege.MENU_ADMIN_FORMS.value
    )
  ) { callContext ->
    // Get a data source
    val mongoDatabase = mongoClient!!.getDatabase(callContext.mongoDbName)

    // Get languages list
    val languages = com.qmplus.web.model.odm.language.Language.findAll(mongoDatabase)

    // Validate the dto
    dto.validate(
      database = mongoDatabase
    )

    // Get the user
    val user = User.findById(mongoDatabase, callContext.getWriteUserId())

    // Create an inventory entry
    val item = Item(mongoDatabase)
    item.serialNumber = dto.serialNumber
    item.id = dto.id
    item.createdOn = LocalDateTime.now(ZoneId.of("UTC"))
    item.updateOn = LocalDateTime.now(ZoneId.of("UTC"))
    item.createdBy = ItemUser(
      id = user?.id,
      username = user?.username,
      firstName = user?.firstName,
      middleName = user?.middleName,
      lastName = user?.lastName,
      contactNumber = user?.contactNumber,
      email = user?.email
    )

    item.name = dto.name
    item.characteristics = dto.characteristics
    item.description = dto.description
    item.insert()

    // USE THE PRIVATE FUNCTION
    val insertTo = addItemToInventory(item.id!!, inventoryId)

    // Return the new item instance
    IdResponse(true, item.id!!)
  }

  /**
   * Extract the inventory and its respective parameters. Finally, add the item
   */
  private fun addItemToInventory(itemId: String, inventoryId: String){

  // Get a data source
    val mongoDatabase = mongoClient!!.getDatabase(callContext?.mongoDbName)


    val inventory = Inventory.findById(mongoDatabase, inventoryId
    ) ?: throw ValidationError(
      ErrorTypes.NOT_FOUND, "failed validation of incoming inventory", listOf(
        ApiError(
          ErrorTypes.NOT_FOUND,
          listOf("inventories"),
          listOf(ErrorMessage(Locale.US, "the inventory with id $inventoryId doesn't exist at inventories, you must create one before insert an item"))
        )
      )
    )

    // Find the item by id
    val item = Item.findById(database = mongoDatabase, itemId
    ) ?: throw ValidationError(
      ErrorTypes.NOT_FOUND, "failed validation of incoming item", listOf(
        ApiError(
          ErrorTypes.NOT_FOUND,
          listOf("item"),
          listOf(ErrorMessage(Locale.US, "the item with id $itemId doesn't exist or its empty"))
        )
      )
    )

    inventory.containerItems?.add(item!!)
  }
}```
0

There are 0 best solutions below