public async Task <IActionResult> Register(UserModel model)
        {
            var applicationUser = new ApplicationUser()
            {
                Email      = model.Email,
                Name       = model.Name,
                UserName   = model.Email,
                AvatarLink = _baseUrlHelper.GetBaseUrl() + "/Images/defaultAvatar.png",
            };

            applicationUser.AccountCreateDate = DateTime.Now;
            applicationUser.Status            = true;

            try
            {
                var result = await _userManager.CreateAsync(applicationUser, model.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(applicationUser, "User");

                    return(Ok(result));
                }
                else
                {
                    return(BadRequest(result));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <object> PostApplicationUser(UserModel model)
        {
            model.Role = "User";
            var home            = new Home();
            var applicationUser = new ApplicationUser()
            {
                UserName       = model.UserName,
                Email          = model.Email,
                FullName       = model.FullName,
                AvatarLocation = _baseUrlHelper.GetBaseUrl() + "/Images/defaultAvatar.png",
            };

            try
            {
                var result = await _userManager.CreateAsync(applicationUser, model.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(applicationUser, model.Role);

                    home.ApplicationUserId = applicationUser.Id;
                    _homeService.Add(home);
                    _homeService.SaveChanges();
                }
                return(Ok(result));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //[Route("Upload")]
        public async Task <IActionResult> PostImage([FromForm] IFormFile file)
        {
            string userId;

            try
            {
                userId = User.Claims.First(c => c.Type == "UserID").Value;
            }
            catch
            {
                return(Unauthorized());
            }
            var imageName = await _imageFileService.UploadImage(file);

            if (!"failed".Equals(imageName))
            {
                Photo photo = new Photo()
                {
                    Name              = imageName,
                    Location          = _baseUrlHelper.GetBaseUrl() + "/Images/" + imageName,
                    CreateDate        = DateTime.Now,
                    ApplicationUserId = userId
                };
                _imageService.Add(photo);
                _imageService.SaveChanges();
                return(Ok(photo));
            }
            else
            {
                return(BadRequest(new { message = "Invalid image type!" }));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> PostUserAvatar([FromForm] IFormFile file)
        {
            string userId = GetUserId();

            if (userId == "error")
            {
                return(Unauthorized());
            }
            var imageName = await _imageFileService.UploadImage(file);

            if (!"failed".Equals(imageName))
            {
                var user = _userService.GetSingleByCondition(s => s.Id == userId, null);
                user.AvatarLink = _baseUrlHelper.GetBaseUrl() + "/Images/" + imageName;
                _userService.Update(user);
                _userService.SaveChanges();
                return(Ok(user));
            }
            else
            {
                return(BadRequest(new { message = "Invalid image type !" }));
            }
        }