Updates the registry entry and returns it.
(
&self,
registry_id: &RegistryEntryId,
input: RegistryEntryUpdateInput,
)
| 159 | |
| 160 | /// Updates the registry entry and returns it. |
| 161 | pub fn edit( |
| 162 | &self, |
| 163 | registry_id: &RegistryEntryId, |
| 164 | input: RegistryEntryUpdateInput, |
| 165 | ) -> ServiceResult<RegistryEntry> { |
| 166 | let mut entry = self.get(registry_id)?; |
| 167 | |
| 168 | RegistryMapper::fill_from_update_input(&mut entry, &input); |
| 169 | |
| 170 | let mut previous_artifact_id = None; |
| 171 | let mut new_artifact_id = None; |
| 172 | |
| 173 | match (&input.value, &entry.value) { |
| 174 | ( |
| 175 | Some(control_panel_api::RegistryEntryValueInput::WasmModule(module)), |
| 176 | RegistryValue::WasmModule(current_module), |
| 177 | ) => { |
| 178 | previous_artifact_id = Some(current_module.wasm_artifact_id); |
| 179 | |
| 180 | let artifact_id = self.artifact_service.create(module.wasm_module.clone())?; |
| 181 | |
| 182 | entry.value = RegistryValue::WasmModule(WasmModuleRegistryValue { |
| 183 | wasm_artifact_id: artifact_id, |
| 184 | version: module.version.clone(), |
| 185 | dependencies: module |
| 186 | .dependencies |
| 187 | .iter() |
| 188 | .map(|dep| dep.clone().into()) |
| 189 | .collect(), |
| 190 | module_extra_chunks: module.module_extra_chunks.clone(), |
| 191 | }); |
| 192 | |
| 193 | new_artifact_id = Some(artifact_id); |
| 194 | } |
| 195 | (None, _) => (), |
| 196 | }; |
| 197 | |
| 198 | if let Err(err) = entry.validate() { |
| 199 | // Makes sure to remove the new artifact if the entry is invalid to avoid orphaned artifacts. |
| 200 | if let Some(artifact_id) = new_artifact_id { |
| 201 | self.artifact_service.remove_by_id(&artifact_id)?; |
| 202 | } |
| 203 | |
| 204 | Err(err)? |
| 205 | } |
| 206 | |
| 207 | // Remove the previous artifact if the new one is different. |
| 208 | if let Some(artifact_id) = previous_artifact_id { |
| 209 | self.artifact_service.remove_by_id(&artifact_id)?; |
| 210 | } |
| 211 | |
| 212 | self.apply_single_latest_tag_across_entries(&entry); |
| 213 | |
| 214 | self.registry_repository.insert(entry.id, entry.clone()); |
| 215 | |
| 216 | Ok(entry) |
| 217 | } |
| 218 |