Ejemplo n.º 1
0
 public async Task <IActionResult> SaveEntity(ProductViewModel productViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     else
     {
         productViewModel.SeoAlias = AliasHelper.ConvertToAlias(productViewModel.Name);
         if (productViewModel.Id == 0)
         {
             var notificationId = Guid.NewGuid().ToString();
             var announcement   = new AnnouncementViewModel
             {
                 Title       = User.GetSpecificClaim("FullName"),
                 DateCreated = DateTime.Now,
                 Content     = $"Product {productViewModel.Name} has been created",
                 Id          = notificationId,
                 UserId      = User.GetUserId(),
                 Image       = User.GetSpecificClaim("Avatar"),
                 Status      = Status.Active
             };
             var announcementUsers = new List <AnnouncementUserViewModel>()
             {
                 new AnnouncementUserViewModel()
                 {
                     AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
                 }
             };
             _productService.Add(announcement, announcementUsers, productViewModel);
             await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
         }
         else
         {
             var notificationId = Guid.NewGuid().ToString();
             var announcement   = new AnnouncementViewModel
             {
                 Title       = User.GetSpecificClaim("FullName"),
                 DateCreated = DateTime.Now,
                 Content     = $"Product {productViewModel.Name} has been updated",
                 Id          = notificationId,
                 UserId      = User.GetUserId(),
                 Image       = User.GetSpecificClaim("Avatar"),
                 Status      = Status.Active
             };
             var announcementUsers = new List <AnnouncementUserViewModel>()
             {
                 new AnnouncementUserViewModel()
                 {
                     AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
                 }
             };
             _productService.Update(announcement, announcementUsers, productViewModel);
             await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
         }
         _productService.Save();
         return(new OkObjectResult(productViewModel));
     }
 }
Ejemplo n.º 2
0
        public void Update(ProductViewModel productViewModel)
        {
            var productTags = new List <ProductTag>();

            if (!string.IsNullOrEmpty(productViewModel.Tags))
            {
                var tags = productViewModel.Tags.Split(',');
                foreach (var t in tags)
                {
                    var tagId = AliasHelper.ConvertToAlias(t);
                    if (!_tagRepository.FindAll(x => x.Id == tagId).Any())
                    {
                        var tag = new Tag {
                            Id = tagId, Name = t
                        };
                        _tagRepository.Add(tag);
                    }
                    _productTagRepository.RemoveMultiple(_productTagRepository.FindAll(x => x.Id == productViewModel.Id).ToList());
                    var productTag = new ProductTag
                    {
                        TagId = tagId
                    };
                    productTags.Add(productTag);
                }
            }

            var product = _mapper.Map <ProductViewModel, Product>(productViewModel);

            foreach (var productTag in productTags)
            {
                product.ProductTags.Add(productTag);
            }
            _productRepository.Update(product);
        }
Ejemplo n.º 3
0
        public ProductViewModel Add(ProductViewModel productViewModel)
        {
            var productTags = new List <ProductTag>();

            if (!string.IsNullOrEmpty(productViewModel.Tags))
            {
                var tags = productViewModel.Tags.Split(',');
                foreach (var t in tags)
                {
                    var tagId = AliasHelper.ConvertToAlias(t.Trim());
                    if (!_tagRepository.FindAll(x => x.Id == tagId).Any())
                    {
                        _tagRepository.Add(new Tag {
                            Id = tagId, Name = t
                        });
                    }
                    productTags.Add(new ProductTag {
                        TagId = tagId
                    });
                }
                var product = _mapper.Map <ProductViewModel, Product>(productViewModel);
                foreach (var productTag in productTags)
                {
                    product.ProductTags.Add(productTag);
                }
                _productRepository.Add(product);
            }
            return(productViewModel);
        }
Ejemplo n.º 4
0
        public GenericResult Update(AnnouncementViewModel announcementViewModel, List <AnnouncementUserViewModel> announcementUsers, ProductViewModel productViewModel)
        {
            try
            {
                var product = _mapper.Map <ProductViewModel, Product>(productViewModel);
                if (!string.IsNullOrEmpty(productViewModel.Tags))
                {
                    var productTags = new List <ProductTag>();
                    var tags        = productViewModel.Tags.Split(',');
                    foreach (var t in tags)
                    {
                        var tagId = AliasHelper.ConvertToAlias(t.Trim());
                        if (!_tagRepository.FindAll(x => x.Id == tagId).Any())
                        {
                            _tagRepository.Add(new Tag {
                                Id = tagId, Name = t
                            });
                        }

                        _productTagRepository.RemoveMultiple(_productTagRepository.FindAll(x => x.Id == productViewModel.Id)
                                                             .ToList());
                        productTags.Add(new ProductTag {
                            TagId = tagId
                        });
                    }

                    foreach (var productTag in productTags)
                    {
                        product.ProductTags.Add(productTag);
                    }
                }
                _productRepository.Update(product);
                // Real Time
                var announcement = _mapper.Map <AnnouncementViewModel, Announcement>(announcementViewModel);
                _announceRepository.Add(announcement);
                foreach (var announcementUserViewModel in announcementUsers)
                {
                    _announceUserRepository.Add(_mapper.Map <AnnouncementUserViewModel, AnnouncementUser>(announcementUserViewModel));
                }
                return(new GenericResult(true, "Update Successful", "Successful"));
            }
            catch (Exception)
            {
                return(new GenericResult(false, "Update Failed", "Error"));
            }
        }
 public IActionResult SaveEntity(ProductViewModel productViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     else
     {
         productViewModel.SeoAlias = AliasHelper.ConvertToAlias(productViewModel.Name);
         if (productViewModel.Id == 0)
         {
             _productService.Add(productViewModel);
         }
         else
         {
             _productService.Update(productViewModel);
         }
         _productService.Save();
         return(new OkObjectResult(productViewModel));
     }
 }
Ejemplo n.º 6
0
        public GenericResult Add(ProductViewModel productViewModel)
        {
            try
            {
                var product = _mapper.Map <ProductViewModel, Product>(productViewModel);
                if (!string.IsNullOrEmpty(productViewModel.Tags))
                {
                    var productTags = new List <ProductTag>();
                    var tags        = productViewModel.Tags.Split(',');
                    foreach (var t in tags)
                    {
                        var tagId = AliasHelper.ConvertToAlias(t.Trim());
                        if (!_tagRepository.FindAll(x => x.Id == tagId).Any())
                        {
                            _tagRepository.Add(new Tag {
                                Id = tagId, Name = t
                            });
                        }
                        productTags.Add(new ProductTag {
                            TagId = tagId
                        });
                    }

                    foreach (var productTag in productTags)
                    {
                        product.ProductTags.Add(productTag);
                    }
                }
                _productRepository.Add(product);
                return(new GenericResult(true, "Add Successful", "Successful"));
            }
            catch (Exception)
            {
                return(new GenericResult(false, "Add Failed", "Error"));
            }
        }