public async Task Update(ProductModel model, PersonalizedProductModel pModel) { var modelToUpdate = await _context.Products.FirstOrDefaultAsync(s => s.Id == model.Id); modelToUpdate.Name = model.Name; modelToUpdate.Weight = model.Entity.Weight; modelToUpdate.WeightPrefix = model.Entity.WeightPrefix; modelToUpdate.Description = model.Entity.Description; modelToUpdate.Images = model.Entity.Images; var userId = _currentUser.GetUserId(); var personalizedProductsModel = await _context.PersonalizedProducts.FirstOrDefaultAsync(x => x.ProductId == model.Id && x.UserId == userId); if (personalizedProductsModel != null) { personalizedProductsModel.Category = pModel.Category; personalizedProductsModel.Star = pModel.Star; personalizedProductsModel.Wearable = pModel.Wearable; personalizedProductsModel.Consumables = pModel.Consumables; } else { PersonalizedProductModel pp = new PersonalizedProductModel(Guid.NewGuid(), userId, model.Id); pp.Update(pModel.Category, pModel.Star, pModel.Wearable, pModel.Consumables); await _context.AddAsync(pp.Entity); } await _context.SaveChangesAsync(); }
public async Task UpdateProduct(ProductViewModel model) { var userId = _currentUser.GetUserId(); var product = await this._productRepository.Get(model.Id); product.Update(model.Name, Convert.ToDecimal(model.Weight), model.WeightPrefix, model.Description); var newProductImages = model.Images.Where(x => x.Id != Guid.Empty).ToList(); var existingimages = product.Images; foreach (var image in newProductImages) { var img = existingimages.FirstOrDefault(x => x.Id == image.Id); if (img == null) { product.AddImg(image.Id, image.FileName); } } var pModel = await _personalizedProductRepository.Get(userId, model.Id); if (pModel == null) { pModel = new PersonalizedProductModel(Guid.Empty, userId, model.Id); } pModel.Update(model.Category, model.Star, model.Wearable, model.Consumables); await this._productRepository.Update(product, pModel); //check if productCategory exist var categoryExist = await this._productGroupRepository.GetByName(userId, model.Category); if (categoryExist == null) { var newProductGroup = new ProductGroupModel(new Persistence.Entity.ProductGroupEntity { Id = Guid.NewGuid(), Name = model.Category, Maximized = true, Owner = userId }); await _productGroupRepository.Add(newProductGroup); } _messageService.SendMessage(new StringMessage($"PackListService:Update")); _messageService.SendMessage(new StringMessage($"ProductService:Update")); }