public async Task <IActionResult> Update(string id, [FromBody] AttributeMeta attributeMeta) { var result = await _attributeService.Update(CurrentUser.TenantId, CurrentUser.Id, CurrentUser.FullName, CurrentUser.Avatar, id, attributeMeta); if (result.Code <= 0) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <ActionResultResponse <string> > Update(string tenantId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvatar, string productAttributeId, AttributeMeta attributeMeta) { var info = await _attributeRepository.GetInfo(productAttributeId); if (info == null) { return(new ActionResultResponse <string>(-1, _warehouseResourceService.GetString("Product attribute does not exists."))); } if (info.TenantId != tenantId) { return(new ActionResultResponse <string>(-2, _warehouseResourceService.GetString(ErrorMessage.NotHavePermission))); } if (info.ConcurrencyStamp != attributeMeta.ConcurrencyStamp) { return(new ActionResultResponse <string>(-3, _warehouseResourceService.GetString( "The product attribute already updated by other people. you are not allowed to edit the product attribute information."))); } // Todo Not allow update IsSelfContent if used by product. // Delete all attribute values when change IsSelfContent property.(IsSelfContent = true : delete ) if (attributeMeta.IsSelfContent) { await _attributeValueTranslationRepository.DeleteByProductAttribute(productAttributeId); await _attributeValueRepository.DeleteByProductAttribute(productAttributeId); } info.IsActive = attributeMeta.IsActive; info.IsRequire = attributeMeta.IsRequire; info.IsMultiple = attributeMeta.IsMultiple; info.IsSelfContent = attributeMeta.IsSelfContent; info.ConcurrencyStamp = Guid.NewGuid().ToString(); info.LastUpdateTime = DateTime.Now; info.LastUpdateUserId = lastUpdateUserId; info.LastUpdateFullName = lastUpdateFullName; await _attributeRepository.Update(info); #region translation. if (attributeMeta.Translations.Count > 0) { var resultUpdateTranslation = await UpdateTranslation(); if (resultUpdateTranslation.Code <= 0) { return(resultUpdateTranslation); } } #endregion return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Update product attribute successful."), string.Empty, info.ConcurrencyStamp)); async Task <ActionResultResponse <string> > UpdateTranslation() { foreach (var productAttributeTranslation in attributeMeta.Translations) { var isNameExists = await _attributeTranslationRepository.CheckExists(info.Id, tenantId, productAttributeTranslation.LanguageId, productAttributeTranslation.Name); if (isNameExists) { return(new ActionResultResponse <string>(-4, _warehouseResourceService.GetString("Product attribute name: \"{0}\" already exists."))); } var productAttributeTranslationInfo = await _attributeTranslationRepository.GetInfo(info.Id, productAttributeTranslation.LanguageId); if (productAttributeTranslationInfo != null) { productAttributeTranslationInfo.Name = productAttributeTranslation.Name.Trim(); productAttributeTranslationInfo.Description = productAttributeTranslation.Description?.Trim(); productAttributeTranslationInfo.UnsignName = productAttributeTranslation.Name?.StripVietnameseChars().ToUpper(); await _attributeTranslationRepository.Update(productAttributeTranslationInfo); } else { var productAttributeTranslationInsert = new AttributeTranslation { AttributeId = productAttributeId, TenantId = tenantId, LanguageId = productAttributeTranslation.LanguageId.Trim(), Name = productAttributeTranslation.Name.Trim(), Description = productAttributeTranslation.Description?.Trim(), UnsignName = productAttributeTranslation.Name.StripVietnameseChars().ToUpper() }; await _attributeTranslationRepository.Insert(productAttributeTranslationInsert); } } return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Update product attribute translation successful."), string.Empty, info.ConcurrencyStamp)); } }
public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvatar, AttributeMeta attributeMeta) { var productAttributeId = Guid.NewGuid().ToString(); var productAttribute = new Attribute { Id = productAttributeId, ConcurrencyStamp = productAttributeId, IsActive = attributeMeta.IsActive, IsRequire = attributeMeta.IsRequire, IsMultiple = attributeMeta.IsMultiple, IsSelfContent = attributeMeta.IsSelfContent, TenantId = tenantId, CreatorId = creatorId, CreatorFullName = creatorFullName, CreatorAvatar = creatorAvatar }; var result = await _attributeRepository.Insert(productAttribute); if (result <= 0) { return(new ActionResultResponse <string>(result, _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong))); } #region insert Translation. if (attributeMeta.Translations.Count > 0) { var resultInsertTranslation = await InsertTranslation(); if (resultInsertTranslation.Code <= 0) { return(resultInsertTranslation); } } #endregion return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Add new product attribute successful."), string.Empty, productAttributeId)); #region Local functions async Task <ActionResultResponse <string> > InsertTranslation() { var productAttributeTranslations = new List <AttributeTranslation>(); foreach (var productAttributeTranslation in attributeMeta.Translations) { // Check name exists. var isNameExists = await _attributeTranslationRepository.CheckExists(productAttributeId, tenantId, productAttributeTranslation.LanguageId, productAttributeTranslation.Name); if (isNameExists) { await RollbackInsert(); return(new ActionResultResponse <string>(-1, _warehouseResourceService.GetString("Product attribute name: \"{0}\" already exists.", productAttributeTranslation.Name))); } var productAttributeTranslationInsert = new AttributeTranslation { AttributeId = productAttributeId, TenantId = tenantId, LanguageId = productAttributeTranslation.LanguageId.Trim(), Name = productAttributeTranslation.Name.Trim(), Description = productAttributeTranslation.Description?.Trim(), UnsignName = productAttributeTranslation.Name?.StripVietnameseChars().ToUpper() }; productAttributeTranslations.Add(productAttributeTranslationInsert); } var resultTranslation = await _attributeTranslationRepository.Inserts(productAttributeTranslations); if (resultTranslation > 0) { return(new ActionResultResponse <string>(resultTranslation, _warehouseResourceService.GetString("Add new product attribute translation successful."))); } await RollbackInsertTranslation(); await RollbackInsert(); return(new ActionResultResponse <string>(-2, _warehouseResourceService.GetString("Can not insert product attribute translation. Please contact with administrator."))); } async Task RollbackInsert() { await _attributeRepository.ForceDelete(productAttributeId); } async Task RollbackInsertTranslation() { await _attributeTranslationRepository.ForceDelete(productAttributeId); } #endregion Local functions }
private AttributeMetaViewModel ToAttributeMetaViewModel(AttributeMeta attributeMeta) { return(_mapper.Map <AttributeMetaViewModel>(attributeMeta)); }