Exemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                string path      = Path.Combine(_environment.ContentRootPath, "wwwroot\\images\\");
                string photoPath = $"images/{model.File.FileName}";
                _uploadFileService.Upload(path, model.File.FileName, model.File);

                User user = new User
                {
                    Email      = model.Email,
                    UserName   = model.UserName,
                    BirthDate  = model.DateOfBirth,
                    FirstName  = model.FirstName,
                    AvatarPath = photoPath
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, false);

                    return(RedirectToAction("Index", "Account"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(String.Empty, error.Description);
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(CreatePostViewModel model)
        {
            if (ModelState.IsValid)
            {
                string path      = Path.Combine(_environment.ContentRootPath, "wwwroot/images/userPosts/");
                string photoPath = $"images/userPosts/{model.File.FileName}";
                _uploadFileService.Upload(path, model.File.FileName, model.File);
                Post post = new Post()
                {
                    Description = model.Description,
                    PhotoPath   = photoPath,
                    UserId      = _userManager.GetUserId(User)
                };
                var result = _db.Posts.AddAsync(post);
                if (result.IsCompleted)
                {
                    await _db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(model));
        }