Exemplo n.º 1
0
        public void AddEntry(int blogId, string title, string body)
        {
            if (String.IsNullOrWhiteSpace(title))
            {
                throw new ArgumentNullException("title");
            }

            if (String.IsNullOrWhiteSpace(body))
            {
                throw new ArgumentNullException("body");
            }

            using (_context = _contextFactory.CreateContext())
            {
                Blog blog = _context.Blogs.SingleOrDefault(b => b.BlogId == blogId);

                if (blog == null)
                {
                    throw new ObjectDoesNotExistException("blog");
                }

                Entry entry = new Entry(title, body);

                blog.AddEntry(entry);

                _context.Entries.Add(entry);
                _context.SaveChanges();
            }
        }
        public void When_EditingEntry_Should_SaveToDB()
        {
            Blog blog = new Blog("Sharon");

            Entry entry = new Entry("title", "body");

            blog.AddEntry(entry);
            using (_context = new TestBlogContext())
            {
                _context.Blogs.Add(blog);
                _context.Entries.Add(entry);
                _context.SaveChanges();
            }

            _repository.EditEntry(entry.EntryId, "new title", "new body");

            using (_context = new TestBlogContext())
            {
                entry = _context.Entries.Single(e => e.EntryId == entry.EntryId);
            }

            Assert.That(entry, Is.Not.Null);
            Assert.That(entry.Title, Is.EqualTo("new title"));
            Assert.That(entry.Body, Is.EqualTo("new body"));
        }
        public void When_RemovingBlog_Should_RemoveFromDB()
        {
            Blog blog = new Blog("Sharon");

            Entry entry = new Entry("title", "body");

            using (_context = new TestBlogContext())
            {
                _context.Blogs.Add(blog);
                _context.SaveChanges();

                blog.AddEntry(entry);
                _context.Entries.Add(entry);

                _context.SaveChanges();
            }

            _repository.RemoveBlog(blog.BlogId);

            using (_context = new TestBlogContext())
            {
                entry = _context.Entries.SingleOrDefault(e => e.BlogId == blog.BlogId);
                blog = _context.Blogs.SingleOrDefault(b => b.BlogId == blog.BlogId);
            }

            Assert.That(blog, Is.Null);
            Assert.That(entry, Is.Null);
        }
        public void When_GettingBlogs_Should_GetFromDB()
        {
            Blog blog = new Blog("Sharon");

            Entry entry = new Entry("title", "body");

            using (_context = new TestBlogContext())
            {
                _context.Blogs.Add(blog);
                _context.SaveChanges();

                blog.AddEntry(entry);
                _context.Entries.Add(entry);

                _context.SaveChanges();
            }

            IList<Blog> blogs = _repository.GetBlogs();
            Blog dbBlog = blogs.Single(b => b.BlogId == blog.BlogId);

            Assert.That(blogs, Is.Not.Null);
            Assert.That(blogs.Count(), Is.GreaterThan(0));
            Assert.That(dbBlog, Is.Not.Null);
            Assert.That(dbBlog.Entries.Count(), Is.EqualTo(1));
            Assert.That(dbBlog.Entries.FirstOrDefault(o => o.EntryId == entry.EntryId), Is.Not.Null);
        }
Exemplo n.º 5
0
        public void AddEntry(Entry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

            _entries.Add(entry);
            entry.BlogId = BlogId;
        }
Exemplo n.º 6
0
        public void CheckIn(string beerName, string image)
        {
            var entry = new Entry(DateTime.UtcNow, Guid.NewGuid())
            {
                BeerName = beerName,
                ImageName = image
            };
            _timeline.Add(entry);

            Clients.All.entryAdded(entry);
        }
Exemplo n.º 7
0
        public void When_GettingEntry_Should_ReturnEntry()
        {
            Entry entry = new Entry("title", "body")
            {
                EntryId = 1
            };

            _entryRepository.Setup(o => o.GetEntry(entry.EntryId)).Returns(entry);

            EntryDto entryDto = _entryService.GetEntry(entry.EntryId);

            Assert.That(entryDto, Is.Not.Null);
            Assert.That(entryDto.Body, Is.EqualTo(entry.Body));
            Assert.That(entryDto.EntryId, Is.EqualTo(entryDto.EntryId));
        }
Exemplo n.º 8
0
        public void Add(Entry entry)
        {
            //var entry = new Entry();
            try
            {
                _logger.LogInformation("Adding entry");
                entry.PartitionKey = Guid.NewGuid().ToString();
                entry.RowKey = Guid.NewGuid().ToString();
                _logger.LogInformation("Create operation");
                var operation = TableOperation.Insert(entry);
                _logger.LogInformation("Execute");
                _table.Execute(operation);

                _logger.LogInformation("Entry added");


                SendEntry(entry).Wait();
            }
            catch (Exception ex)
            {
                _logger.LogError("Problem adding entry", ex);
            }
        }
        public void When_GettingEntries_Should_GetFromDB()
        {
            Blog blog = new Blog("Sharon");

            Entry entry = new Entry("title", "body");

            using (_context = new TestBlogContext())
            {
                _context.Blogs.Add(blog);
                _context.SaveChanges();

                blog.AddEntry(entry);
                _context.Entries.Add(entry);

                _context.SaveChanges();
            }

            IEnumerable<Entry> entries = _repository.GetEntries(blog.BlogId);

            Assert.That(entries.Count(), Is.EqualTo(1));
        }
        public void When_RemovingEntry_Should_RemoveFromBlog()
        {
            Blog blog = new Blog("Sharon");

            Entry entry = new Entry("title", "body");

            using (_context = new TestBlogContext())
            {
                _context.Blogs.Add(blog);
                _context.SaveChanges();

                blog.AddEntry(entry);
                _context.Entries.Add(entry);

                _context.SaveChanges();
            }

            _repository.RemoveEntry(blog.BlogId, entry.EntryId);

            using (_context = new TestBlogContext())
            {
                _blog = _context.Blogs.Single(b => b.BlogId == _blog.BlogId);

                Assert.That(_blog.Entries.Count, Is.EqualTo(0));
            }
        }
        public void When_GettingEntry_Should_GetFromDB()
        {
            Blog blog = new Blog("Sharon");

            Entry entry = new Entry("title", "body");

            blog.AddEntry(entry);
            using (_context = new TestBlogContext())
            {
                _context.Blogs.Add(blog);
                _context.Entries.Add(entry);
                _context.SaveChanges();
            }

            entry = _repository.GetEntry(entry.EntryId);

            Assert.That(entry, Is.Not.Null);
            Assert.That(entry.Title, Is.EqualTo("title"));
        }
Exemplo n.º 12
0
        // https://blogs.endjin.com/2015/02/send-data-into-azure-event-hubs-using-web-apis-httpclient/
        async Task SendEntry(Entry entry)
        {
            _logger.LogInformation("SendEntry");
            var json = _serializer.ToJson(entry);

            // Generate the shared access SAS URL with the tool from the link above
            var sas = "SharedAccessSignature sr=https%3a%2f%2f{namespace}.servicebus.windows.net%2factivity%2fpublishers%2f{partition}%2f{eventhub}s&sig={key}&skn={name of key}";

            // Namespace info.
            var serviceNamespace = "geekbeer";
            var hubName = "activity";
            var url = string.Format("{0}/publishers/{partition}/{eventhub}", hubName);
            
            _logger.LogInformation("Create client");
            var httpClient = new HttpClient
            {
                BaseAddress = new Uri(string.Format("https://{0}.servicebus.windows.net/", serviceNamespace))
            };

            _logger.LogInformation("Add authorization");
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", sas);
            
            
            _logger.LogInformation("Create content");
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var contentType = "application/atom+xml;type=entry;charset=utf-8";
            
            _logger.LogInformation("Adding content type");
            
            content.Headers.Add("ContentType", contentType);
            
            _logger.LogInformation("Posting");
            var response = await httpClient.PostAsync(url, content);
            
            _logger.LogInformation("Posted");
            var result = await response.Content.ReadAsStringAsync();

            _logger.LogInformation("Response status : "+response.StatusCode);
            _logger.LogInformation("Entry has been sent : " + result);
        }