public async Task HandleAsync(AddOrUpdateBlogEntryFileCommand command)
        {
            string fileName = command.FileName.Replace('/', '\\');

            fileName = fileName.Substring(fileName.IndexOf('\\') + 1);

            string extension = fileName.Substring(fileName.LastIndexOf('.') + 1);

            BlogEntryFile blogEntryFile = await this.unitOfWork.BlogEntryFiles
                                          .SingleOrDefaultAsync(f => f.BlogEntryId == command.BlogEntryId && f.Name == fileName);

            if (blogEntryFile == null)
            {
                blogEntryFile = new BlogEntryFile()
                {
                    BlogEntryId = command.BlogEntryId,
                    Name        = fileName
                };

                this.unitOfWork.BlogEntryFiles.Add(blogEntryFile);
            }

            await this.fileProvider.AddFileAsync($"{blogEntryFile.Id}.{extension}", command.Data);

            await this.unitOfWork.SaveChangesAsync();
        }
Пример #2
0
        public IncrementBlogEntryFileCounterCommandHandlerTest()
        {
            this.unitOfWork = new InMemoryDatabaseFactory().CreateContext();

            var blogEntry = new BlogEntry()
            {
                ShortContent = "Test",
                Header       = "Test"
            };

            this.file = new BlogEntryFile()
            {
                Name        = "test.png",
                BlogEntryId = blogEntry.Id
            };
            this.unitOfWork.BlogEntries.Add(blogEntry);
            this.unitOfWork.BlogEntryFiles.Add(this.file);
            this.unitOfWork.SaveChanges();

            Assert.Single(this.unitOfWork.BlogEntryFiles);
        }
Пример #3
0
        public DeleteBlogEntryCommandHandlerTest()
        {
            this.unitOfWork = new InMemoryDatabaseFactory().CreateContext();
            this.blogEntryFileFileProvider = new Mock <IBlogEntryFileFileProvider>();

            this.blogEntry = new BlogEntry()
            {
                ShortContent = "Test",
                Header       = "Test"
            };

            this.file = new BlogEntryFile()
            {
                Name        = "test.pdf",
                BlogEntryId = this.blogEntry.Id
            };
            this.unitOfWork.BlogEntries.Add(this.blogEntry);
            this.unitOfWork.BlogEntryFiles.Add(this.file);
            this.unitOfWork.SaveChanges();

            Assert.Single(this.unitOfWork.BlogEntryFiles);
        }
Пример #4
0
        public void Handle(AddOrUpdateBlogEntryFileCommand command)
        {
            int    indexOfLastDot = command.FileName.LastIndexOf('.');
            string name           = command.FileName.Substring(0, indexOfLastDot);
            string extension      = command.FileName.Substring(indexOfLastDot + 1, command.FileName.Length - indexOfLastDot - 1);

            BlogEntryFile blogEntryFile = this.repository.BlogEntryFiles
                                          .SingleOrDefault(f => f.BlogEntryId == command.BlogEntryId && f.Name == name && f.Extension == extension);

            if (blogEntryFile == null)
            {
                blogEntryFile = new BlogEntryFile()
                {
                    BlogEntryId = command.BlogEntryId, Name = name, Extension = extension
                };
                this.repository.BlogEntryFiles.Add(blogEntryFile);
            }

            blogEntryFile.Data = command.Data;

            this.repository.SaveChanges();
        }