コード例 #1
0
        public async Task <ICommandResult <File> > CreateAsync(File model)
        {
            // Validate
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (model.FeatureId <= 0)
            {
                throw new ArgumentNullException(nameof(model.FeatureId));
            }

            if (model.Id > 0)
            {
                throw new ArgumentOutOfRangeException(nameof(model.Id));
            }

            if (String.IsNullOrWhiteSpace(model.Name))
            {
                throw new ArgumentNullException(nameof(model.Name));
            }

            if (model.CreatedUserId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(model.CreatedUserId));
            }

            if (model.CreatedDate == null)
            {
                throw new ArgumentNullException(nameof(model.CreatedDate));
            }

            model.Alias = await ParseAlias(model.Name);

            // Invoke FileCreating subscriptions
            foreach (var handler in _broker.Pub <File>(this, "FileCreating"))
            {
                model = await handler.Invoke(new Message <File>(model, this));
            }

            var result = new CommandResult <File>();

            var newFile = await _fileStore.CreateAsync(model);

            if (newFile != null)
            {
                // Invoke FileCreated subscriptions
                foreach (var handler in _broker.Pub <File>(this, "FileCreated"))
                {
                    newFile = await handler.Invoke(new Message <File>(newFile, this));
                }

                // Return success
                return(result.Success(newFile));
            }

            return(result.Failed(new CommandError("An unknown error occurred whilst attempting to create the file")));
        }