Exemplo n.º 1
0
        public async Task <(RustCategory, RustEditCategoryResult)> UpdateCategoryAsync(RustShopViewModel model)
        {
            var userForLog = await _userManager.FindByEmailAsync(_httpContextAccessor.HttpContext.User.Identity.Name);

            if (model.RustCategoryEditViewModel.Category.Id is null)
            {
                var shop = GetShopById(Guid.Parse(model.Id));

                if (shop is null)
                {
                    return(null, RustEditCategoryResult.Failed);
                }

                RustCategory newCategory = new RustCategory
                {
                    Id      = Guid.NewGuid(),
                    Index   = 1,
                    Name    = model.RustCategoryEditViewModel.Category.Name,
                    AppUser = await _userManager.FindByEmailAsync(_httpContextAccessor.HttpContext.User.Identity.Name),
                    Shop    = shop
                };

                _easyShopContext.RustCategories.Add(newCategory);
                await _easyShopContext.SaveChangesAsync();

                _logger.LogInformation("UserName: {0} | UserId: {1} | Request: {2} | Message: {3}",
                                       userForLog.UserName,
                                       userForLog.Id,
                                       _httpContextAccessor.HttpContext.Request.GetRawTarget(),
                                       $"Description was successfully created. CategoryId: {newCategory.Id}");

                return(newCategory, RustEditCategoryResult.Created);
            }

            var category = GetCategoryById(Guid.Parse(model.RustCategoryEditViewModel.Category.Id));

            if (category is null)
            {
                return(null, RustEditCategoryResult.Failed);
            }

            category.Index = model.RustCategoryEditViewModel.Category.Index;
            category.Name  = model.RustCategoryEditViewModel.Category.Name;

            _easyShopContext.RustCategories.Update(category);
            await _easyShopContext.SaveChangesAsync();

            _logger.LogInformation("UserName: {0} | UserId: {1} | Request: {2} | Message: {3}",
                                   userForLog.UserName,
                                   userForLog.Id,
                                   _httpContextAccessor.HttpContext.Request.GetRawTarget(),
                                   $"Description was successfully updated. CategoryId: {category.Id}");

            return(category, RustEditCategoryResult.Success);
        }
        private static RustCategoryViewModel CopyToCategoryRustShopViewModel(this RustCategory category, int?assignedItemsCount = null)
        {
            var model = new RustCategoryViewModel
            {
                Id                 = category.Id.ToString(),
                Index              = category.Index,
                ShopId             = category.Shop.Id.ToString(),
                Name               = category.Name,
                AssignedItemsCount = assignedItemsCount
            };

            return(model);
        }
        public static RustCategoryViewModel CreateRustCategoryViewModel(this RustCategory category, int?assignedItemsCount = null)
        {
            var model = category.CopyToCategoryRustShopViewModel(assignedItemsCount);

            return(model);
        }