public async Task <UserDTO> UpdateProfileInfo(int userId, UpdateUserProfileDTO userInfo) { var user = await _userRepo.GetByIdAsync(userId); string fileName = null; if (userInfo.File != null) { var photoFolderPath = Path.Combine(_host.WebRootPath, "UserProfilePhoto"); if (!Directory.Exists(photoFolderPath)) { Directory.CreateDirectory(photoFolderPath); } fileName = Guid.NewGuid().ToString() + Path.GetExtension(userInfo.File.FileName); var filePath = Path.Combine(photoFolderPath, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await userInfo.File.CopyToAsync(stream); } user.PhotoUrl = fileName; } user.FirstName = userInfo.FirstName; user.LastName = userInfo.LastName; user.Age = userInfo.Age; user.Gender = userInfo.Gender; try { _userRepo.Edit(user); return(_mapper.Map <UserDTO>(user)); } catch (Exception) { return(null); } }
public async Task <ActionResult> UpdateUserProfile(string userId, [FromForm] UpdateUserProfileDTO updateUserProfileDTO) { //TODO //Chequear el role de administrador o que el usuario este editando su propio perfil var userIdC = HttpContext.User.Claims.FirstOrDefault(y => y.Type == ClaimTypes.NameIdentifier); if (userIdC == null || userIdC.Value != userId) { return(Forbid()); } var user = await applicationDbContext.Users.FirstOrDefaultAsync(u => u.Id == userId); if (user == null) { return(NotFound()); } mapper.Map(updateUserProfileDTO, user); if (updateUserProfileDTO.ProfilePicture != null) { var bytes = await updateUserProfileDTO.ProfilePicture.ConvertToByteArray(); if (!string.IsNullOrEmpty(user.ProfilePicture)) { await fileStorage.RemoveFile(user.ProfilePicture, ApplicationConstants.ImageContainerNames.ProfilePicturesContainer); } user.ProfilePicture = await fileStorage.SaveFile(bytes, Path.GetExtension(updateUserProfileDTO.ProfilePicture.FileName), ApplicationConstants.ImageContainerNames.ProfilePicturesContainer, updateUserProfileDTO.ProfilePicture.ContentType, Guid.NewGuid().ToString()); } applicationDbContext.Entry(user).State = EntityState.Modified; bool haveSociableBadge = await applicationDbContext.ApplicationUserBadges.AnyAsync(x => x.UserId == userId && x.BadgeId == 1); if (!haveSociableBadge) { var sociableBadge = new ApplicationUserBadges { UserId = userId, BadgeId = 1, GetDate = DateTime.UtcNow }; applicationDbContext.Add(sociableBadge); } await applicationDbContext.SaveChangesAsync(); return(NoContent()); }