public async Task <ActionResult> Set(UserProfileEditBL model)
        {
            try
            {
                await _service.UpdateUserProfile(model);

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "ProfileAPIController.Get");
                return(new StatusCodeResult(500));
            }
        }
        public async Task <IActionResult> Update(ProfileViewModel viewmodel, IFormFile uploadedFile)
        {
            if (ModelState.IsValid)
            {
                var model = new UserProfileEditBL()
                {
                    Email      = viewmodel.Email,
                    FamilyName = viewmodel.FamilyName,
                    Name       = viewmodel.Name,
                    Patronymic = viewmodel.Patronymic,
                    Telephone  = viewmodel.telephone,
                };
                model.delPhoto = viewmodel.delPhoto;
                if (viewmodel.delPhoto)
                {
                    viewmodel.HasPhoto = false;
                }
                if (uploadedFile != null && viewmodel.delPhoto == false)
                {
                    using (var ms = new MemoryStream())
                    {
                        uploadedFile.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        model.PhotoData = new UserPhotoBL()
                        {
                            ContentType = uploadedFile.ContentType,
                            Body        = fileBytes
                        };
                        model.Photo = uploadedFile.FileName;
                    }
                    viewmodel.HasPhoto = true;
                }
                await _client.Post("api/profile", model);

                ViewData["ok"] = "true";
            }
            return(View(viewmodel));
        }
Exemple #3
0
        public async Task <APIResult> UpdateUserProfile(UserProfileEditBL profile)
        {
            ApplicationUser user = await GetUserAsync();

            user.Email           = profile.Email;
            user.NormalizedEmail = profile.Email.ToUpper();
            user.FamilyName      = profile.FamilyName;
            user.Name            = profile.Name;
            user.Patronymic      = profile.Patronymic;
            user.PhoneNumber     = profile.Telephone;
            if (profile.delPhoto && !string.IsNullOrWhiteSpace(user.Photo))
            {
                File.Delete(_hostingEnvironment.ContentRootPath + "\\Profiles\\" + user.Photo);
                user.Photo            = "";
                user.PhotoContentType = "";
            }
            else
            if (profile.PhotoData != null)
            {
                user.Photo            = user.Id.ToString() + Path.GetExtension(profile.Photo);
                user.PhotoContentType = profile.PhotoData.ContentType;
                string path = _hostingEnvironment.ContentRootPath + "\\Profiles\\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                await File.WriteAllBytesAsync(path + user.Photo, profile.PhotoData.Body);
            }
            await _userManager.UpdateAsync(user);

            return(new APIResult()
            {
                Success = true
            });
        }