public async Task <IActionResult> GetAvatar(Guid?id) { var Id = id == null?Guid.Parse(httpContextAccessor.HttpContext.User.Identity.GetUserId()) : id; if (Id != null) { var user = await mediator.Send(new GerUserByIdQuery((Guid)Id)); if (user.Success) { var path = Path.Combine(UploadFolderPath.PathAvatarUserUploadFolder(), user.Result.Photo); var memory = new MemoryStream(); using (var stream = new FileStream(path, FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; return(File(memory, GetContentType(path), Path.GetFileName(path))); } return(BadRequest(user.ErrorMessage)); } return(BadRequest("Id not valid")); }
public async Task <OperationResult <bool> > Handle(UpdateUserCommand request, CancellationToken cancellationToken) { string fileName = null; var getUser = await unitOfWork.UsersRepository.GetUserByIdAsync(request.Id, cancellationToken); if (getUser.Result != null) { if (request.Photo != null) { var uploadFile = await UploadUtiltie.UploadInCustomePath(request.Photo, ".png", request.Username, UploadFolderPath.PathUserUploadFolder(), UploadFolderPath.PathAvatarUserUploadFolder()); fileName = uploadFile.Result; } getUser.Result.UpdateProperties(request.Username, request.Name, request.Family, request.Email, fileName); var addUser = unitOfWork.UsersRepository.Update(getUser.Result, cancellationToken); if (addUser.Success) { try { unitOfWork.CommitSaveChange(); return(OperationResult <bool> .BuildSuccessResult(true)); } catch (Exception ex) { return(OperationResult <bool> .BuildFailure(ex.Message)); } } } return(OperationResult <bool> .BuildFailure(getUser.ErrorMessage)); }
public async Task <OperationResult <string> > Handle(CreateUserCommand request, CancellationToken cancellationToken) { string fileName = null; if (request.Photo != null) { var uploadFile = await UploadUtiltie.UploadInCustomePath(request.Photo, ".png", request.Username, UploadFolderPath.PathUserUploadFolder(), UploadFolderPath.PathAvatarUserUploadFolder()); fileName = uploadFile.Result; } /// Register User var user = new User(request.Username, request.Password, request.Name, request.Family, request.PhoneNumber, request.Email, fileName); var addUser = await unitOfWork.UsersRepository.AddAsync(user, cancellationToken); if (addUser.Success) { try { var registerSetting = await unitOfWork.SettingRepository.Get <RegisterUserSetting>(SettingEnum.RegisterUserSetting.EnumToString(), cancellationToken); if (registerSetting.Result != null) { /// Add User Role var addUserRole = await mediator.Send(new CreateUserRoleCommand(registerSetting.Result.RegisterRoleByAdmin, user.Id)); if (addUserRole.Success) { /// Add User Generation Code ActivationCode acCode = new ActivationCode(user.Id, CodeTypes.PhoneConfirmed, Utility.Hash(user.Username)); var gerateActivationCode = await unitOfWork.UsersRepository.ActivationCodeRepository.AddAsync(acCode, cancellationToken); if (gerateActivationCode.Success) { /// Send Registration Code SendNotif sendNotif = sendNotifs.Where(x => x.SendCodeType == registerSetting.Result.SendCodeVerifications) .FirstOrDefault(); var sendCode = await sendNotif.SendCodeAsync(gerateActivationCode.Result.Item2.ToString(), user.PhoneNumber, cancellationToken); if (sendCode.Success) { /// Save to Database await unitOfWork.CommitSaveChangeAsync(); return(OperationResult <string> .BuildSuccessResult(gerateActivationCode.Result.Item1)); } } } } } catch (Exception ex) { return(OperationResult <string> .BuildFailure(ex.Message)); } } return(OperationResult <string> .BuildFailure(addUser.ErrorMessage)); }