public async Task <IActionResult> Update(string id, [FromBody] WarehouseMeta warehouseMeta) { var result = await _warehouseService.Update(CurrentUser.TenantId, CurrentUser.Id, CurrentUser.FullName, CurrentUser.Avatar, id, warehouseMeta); if (result.Code <= 0) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvatar, WarehouseMeta warehouseMeta) { var warehouseId = Guid.NewGuid().ToString(); var warehouse = new Domain.Models.Warehouse { Id = warehouseId, ConcurrencyStamp = warehouseId, Name = warehouseMeta.Name.Trim(), Description = warehouseMeta.Description?.Trim(), Address = warehouseMeta.Address?.Trim(), UnsignName = warehouseMeta.Name?.StripVietnameseChars().ToUpper(), IsActive = warehouseMeta.IsActive, TenantId = tenantId, CreatorId = creatorId, CreatorFullName = creatorFullName }; var result = await _warehouseRepository.Insert(warehouse); if (result <= 0) { return(new ActionResultResponse <string>(result, _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong))); } // Thêm phương pháp tính tồn. await _warehouseConfigService.Save(new WarehouseConfigMeta { TenantId = tenantId, Value = ((int)warehouseMeta.InventoryCalculatorMethod).ToString(), Key = ClassHelper.GetPropertyNameAsKey <WarehouseConfigSetting>("CalculatorMethod"), WarehouseId = warehouseId, UserId = creatorId, FullName = creatorFullName, LanguageId = CultureInfo.CurrentCulture.Name, DisplayName = "Calculator method" }); #region insert WarehouseManagerConfigs. if (warehouseMeta.WarehouseManagerConfigs != null && warehouseMeta.WarehouseManagerConfigs.Count > 0 && warehouseMeta.WarehouseManagerConfigs.Any()) { var resultInsertWarehouseManagerConfig = await InsertWarehouseManagerConfig(); if (resultInsertWarehouseManagerConfig.Code <= 0) { await RollbackInsert(); return(resultInsertWarehouseManagerConfig); } } #endregion return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Add new warehouse successful."), string.Empty, warehouseId)); #region Local functions async Task <ActionResultResponse <string> > InsertWarehouseManagerConfig() { var warehouseManagerConfigs = new List <WarehouseManagerConfig>(); foreach (var warehouseManagerConfig in warehouseMeta.WarehouseManagerConfigs) { var warehouseManagerConfigInsert = new WarehouseManagerConfig { WarehouseId = warehouseId, TenantId = tenantId, UserId = warehouseManagerConfig.UserId.Trim(), FullName = warehouseManagerConfig.FullName.Trim(), Avatar = warehouseManagerConfig.Avatar?.Trim(), PhoneNumber = warehouseManagerConfig.PhoneNumber?.Trim(), UnsignName = warehouseManagerConfig.FullName?.StripVietnameseChars().ToUpper(), Permissions = warehouseManagerConfig.Permissions, CreatorId = creatorId, CreatorFullName = creatorFullName }; warehouseManagerConfigs.Add(warehouseManagerConfigInsert); } var resultWarehouseManagerConfig = await _warehouseManagerConfigRepository.Inserts(warehouseManagerConfigs); if (resultWarehouseManagerConfig > 0) { return(new ActionResultResponse <string>(resultWarehouseManagerConfig, _warehouseResourceService.GetString("Add new warehouse manager configs successful."))); } await RollbackInsertWarehouseManagerConfig(); await RollbackInsert(); return(new ActionResultResponse <string>(-2, _warehouseResourceService.GetString("Can not insert warehouse manager configs. Please contact with administrator."))); } async Task RollbackInsert() { await _warehouseRepository.ForceDelete(warehouseId); } async Task RollbackInsertWarehouseManagerConfig() { await _warehouseManagerConfigRepository.DeleteByWarehouseId(warehouseId); } #endregion Local functions }
public async Task <ActionResultResponse <string> > Update(string tenantId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvatar, string warehouseId, WarehouseMeta warehouseMeta) { var info = await _warehouseRepository.GetInfo(warehouseId); if (info == null) { return(new ActionResultResponse <string>(-1, _warehouseResourceService.GetString("Warehouse does not exists."))); } if (info.TenantId != tenantId) { return(new ActionResultResponse <string>(-2, _warehouseResourceService.GetString(ErrorMessage.NotHavePermission))); } if (info.ConcurrencyStamp != warehouseMeta.ConcurrencyStamp) { return(new ActionResultResponse <string>(-3, _warehouseResourceService.GetString( "The warehouse already updated by other people. you are not allowed to edit the warehouse information."))); } info.Name = warehouseMeta.Name.Trim(); info.Description = warehouseMeta.Description?.Trim(); info.Address = warehouseMeta.Address?.Trim(); info.UnsignName = warehouseMeta.Name?.StripVietnameseChars().ToUpper(); info.IsActive = warehouseMeta.IsActive; info.ConcurrencyStamp = Guid.NewGuid().ToString(); info.LastUpdateTime = DateTime.Now; info.LastUpdateUserId = lastUpdateUserId; info.LastUpdateFullName = lastUpdateFullName; await _warehouseRepository.Update(info); // Lấy về thông tin var warehouseConfig = await _warehouseConfigRepository.GetInventoryCalculatorMethod(tenantId, warehouseId); if (!warehouseConfig.HasValue || warehouseConfig.Value != warehouseMeta.InventoryCalculatorMethod) { await _warehouseConfigService.Save(new WarehouseConfigMeta { TenantId = tenantId, Value = ((int)warehouseMeta.InventoryCalculatorMethod).ToString(), Key = ClassHelper.GetPropertyNameAsKey <WarehouseConfigSetting>("CalculatorMethod"), WarehouseId = warehouseId, UserId = lastUpdateUserId, FullName = lastUpdateFullName, LanguageId = CultureInfo.CurrentCulture.Name, DisplayName = "Calculator method" }); } return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Update warehouse successful."), string.Empty, info.ConcurrencyStamp)); }