public async Task <IActionResult> Create(ProductCreateDTO createDTO)
        {
            if (ModelState.IsValid)
            {
                Product product = new Product
                {
                    CategoryId  = createDTO.CategoryId,
                    Stock       = createDTO.Stock,
                    Description = createDTO.Description,
                    IsPublish   = createDTO.IsPublish,
                    Price       = createDTO.Price,
                    Title       = createDTO.Title
                };
                product.ImageName = await _fileWorker.AddFileToPathAsync(createDTO.Image, "img");

                if (product.IsPublish)
                {
                    product.PublishDate = DateTime.Now;
                }
                await _repository.CreateAsync(product);

                await _context.SaveChangesAsync();

                return(LocalRedirect("/admin/products/index"));
            }
            SetCategories();
            return(View(createDTO));
        }
예제 #2
0
        public async Task <IActionResult> Create(UserCreateDTO userCreate)
        {
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser
                {
                    Address        = userCreate.Address,
                    Email          = userCreate.Email,
                    PostalCode     = userCreate.PostalCode,
                    UserName       = userCreate.Username,
                    FirstName      = userCreate.FirstName,
                    PhoneNumber    = userCreate.PhoneNumber,
                    EmailConfirmed = true,
                    LastName       = userCreate.LastName,
                };
                if (userCreate.ImageFile != null)
                {
                    user.ImageName = await _fileWorker.AddFileToPathAsync(userCreate.ImageFile, "img");
                }
                var result = await _userManager.CreateAsync(user, userCreate.Password);

                if (result.Succeeded)
                {
                    string[] roles = userCreate.UserRoles.Split(',');
                    if (roles.Any())
                    {
                        await _userManager.AddToRolesAsync(user, roles);
                    }
                    return(LocalRedirect("/admin/usersmanager"));
                }
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(item.Code, item.Description);
                }
            }
            await SetRoles();

            return(View(userCreate));
        }