Beispiel #1
0
        /// <summary>
        /// Обновить фото клиента
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public async Task <BaseApiResponse> UpdateClientAvatarAsync(int fileId)
        {
            if (!IsAuthenticated)
            {
                return(new BaseApiResponse(false, ValidationMessages.YouAreNotAuthorized));
            }

            var userRepo = GetRepository <Client>();

            var userToEditEntity = await userRepo.Query().FirstOrDefaultAsync(x => x.Id == UserId);

            if (userToEditEntity == null)
            {
                return(new BaseApiResponse(false, ValidationMessages.UserNotFound));
            }

            var file = await FilesQueryService.GetFileDataById(fileId);

            if (file == null)
            {
                return(new BaseApiResponse(false, ValidationMessages.FileIsNotFoundById));
            }

            if (!FileChecker.IsImage(file))
            {
                return(new BaseApiResponse(false, ValidationMessages.FileIsNotImage));
            }

            var oldFileId = userToEditEntity.AvatarFileId;

            userToEditEntity.AvatarFileId = fileId;

            userRepo.UpdateHandled(userToEditEntity);

            return(await TryExecuteCodeAndReturnSuccessfulResultAsync(async() =>
            {
                await SaveChangesAsync();

                await DbFileRelationManager.AddOrUpdateFileRelation(new AddOrUpdateFileRelation <Client>
                {
                    FileId = fileId,
                    EntityKey = userToEditEntity.Id,
                    RelationCustomData = null,
                    RelationName = ClientAvatarRelationName,
                    RelationValue = userToEditEntity.Id
                });

                await PublishMessageAsync(new ClientDataUpdatedEvent
                {
                    Id = UserId
                });
                await ClientDataRefresher.UpdateUserDataAsync(await GetUserByIdAsync(userToEditEntity.Id), userToEditEntity);

                return new BaseApiResponse(true, ClientResource.ClientAvatarUpdated);
            }));
        }