Esempio n. 1
0
        public async Task <StorageServerResult> Handle(UpdateStorageServerCommand request, CancellationToken cancellationToken)
        {
            StorageServerResult Result = new StorageServerResult();

            // Check guid is valid
            if (!Guid.TryParse(request.Id, out Guid serverId))
            {
                Result.ErrorContent = new ErrorContent("Invalid server Id.", ErrorOrigin.Client);
                return(Result);
            }
            // Get the server from db
            StorageServer server = await _unitOfWork.StorageServer.FirstOrDefaultAsync(s => s.Id == serverId);

            if (server == null)
            {
                Result.ErrorContent = new ErrorContent("The server with the provided id was not found.", ErrorOrigin.Client);
                return(Result);
            }
            // update the server
            server.Address = request.StorageInfo.Address;
            server.State   = request.StorageInfo.State;

            // Update upload options config
            var writeUploadOptsResult = await _uploadOptsClientProxy.WriteUploadOptions(request.UploadOpts, request.StorageInfo.Address);

            if (writeUploadOptsResult.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent($"Error occured while writing {nameof(UploadOptions)} to server {request.StorageInfo.Address}", ErrorOrigin.Server);
                return(Result);
            }
            // Update hardware config
            var writeHardwareOptsResult = await _hardwareOptsProxy.WriteHardwareOptions(request.HardwareOpts, request.StorageInfo.Address);

            if (writeHardwareOptsResult.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent($"Error occured while writing {nameof(HardwareCheckOptions)} to server {request.StorageInfo.Address}", ErrorOrigin.Server);
                return(Result);
            }

            // everything ok, update the server
            Result = await _unitOfWork.CompleteAsync(Result);

            if (Result.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent("An error occured while saving the record to the database. " + Result.ErrorContent.Message, ErrorOrigin.Server);
                return(Result);
            }
            Result.Server = server;
            return(Result);
        }
Esempio n. 2
0
        public async Task <StorageServerResult> Handle(AddStorageServerCommand request, CancellationToken cancellationToken)
        {
            StorageServerResult Result = new StorageServerResult();
            StorageServer       server = await _unitOfWork.StorageServer.FirstOrDefaultAsync(s => s.Address == request.StorageInfo.Address);

            if (server != null)
            {
                Result.ErrorContent = new ErrorContent("A server with the same address already exists.", ErrorOrigin.Client);
                return(Result);
            }
            // Add the server to db
            StorageServer newServer = new StorageServer()
            {
                Address = request.StorageInfo.Address,
                State   = request.StorageInfo.State
            };

            _unitOfWork.StorageServer.Add(newServer);

            // Update upload options config
            var writeUploadOptsResult = await _uploadOptsClientProxy.WriteUploadOptions(request.UploadOpts, request.StorageInfo.Address);

            if (writeUploadOptsResult.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent($"Error occured while writing {nameof(UploadOptions)} to server {request.StorageInfo.Address}", ErrorOrigin.Server);
                return(Result);
            }
            // Update hardware config
            var writeHardwareOptsResult = await _hardwareOptsProxy.WriteHardwareOptions(request.HardwareOpts, request.StorageInfo.Address);

            if (writeHardwareOptsResult.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent($"Error occured while writing {nameof(HardwareCheckOptions)} to server {request.StorageInfo.Address}", ErrorOrigin.Server);
                return(Result);
            }

            // everything ok, save the new server to db
            Result = await _unitOfWork.CompleteAsync(Result);

            if (Result.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent("An error occured while saving the record to the database. " + Result.ErrorContent.Message, ErrorOrigin.Server);
                return(Result);
            }
            Result.Server = newServer;
            return(Result);
        }