Esempio n. 1
0
        public async Task <Response> Handle(Request message)
        {
            var folder = await this.context.LinkedFolders
                         .Include(t => t.Inbox)
                         .SingleOrExceptionAsync(t => t.Id == message.LinkedFolderId);

            var repository = new ExchangeRepository(folder.Inbox.Email);

            await repository.Initialize(folder.NewItemsFolder, folder.ProcessedItemsFolder);

            var result = await repository.Read(new Paginator
            {
                PageSize  = int.MaxValue,
                PageIndex = 1
            });

            foreach (var item in result.Results)
            {
                var email = new ImportedEmail(folder, item);

                var alreadyAdded = this.context.ImportedEmails.Any(t =>
                                                                   t.LinkedFolderId == folder.Id &&
                                                                   t.MessageId == email.MessageId);

                if (!alreadyAdded)
                {
                    this.context.ImportedEmails.Add(email);
                }
            }

            await this.context.SaveChangesAsync();

            await repository.MoveItemsToProcessedFolder(result.Results.Select(t => t.Id).ToList());

            return(new Response());
        }