Example #1
0
        public void Execute(IJobExecutionContext context)
        {
            List <Source> sources = db.Sources.ToList();

            if (sources == null)
            {
                return;
            }

            foreach (var source in db.Sources)
            {
                DateTimeOffset?entityLastDateTime = db.Entities.Where(e => e.SourceId == source.Id).Max(e => e.PublicationDate);

                List <Entity> entities = FeedHelper.GetEntitiesFromFeed(source.Link).ToList();
                foreach (Entity item in entities)
                {
                    if (item.PublicationDate > entityLastDateTime || entityLastDateTime == null)
                    {
                        db.Entities.Add(new Entity()
                        {
                            Title           = item.Text,
                            PublicationDate = item.PublicationDate,
                            Link            = item.Link,
                            SourceId        = source.Id
                        });
                    }
                }
            }

            db.SaveChangesAsync();
        }
        public async Task <IHttpActionResult> PostSource(int id, Source source)
        {
            Log.Debug("Add feed link to collection");
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // get userId
            int userId = this.User.Identity.GetUserId <int>();

            // check if user has collection with this id
            if (!await db.IsUserHasCollection(id, userId))
            {
                return(BadRequest("You don't have collection with this id"));
            }
            // check if collection has this source
            if (await db.IsCollectionHasSource(id, source.Link))
            {
                return(BadRequest("You already have source with the same link"));
            }

            string        sourceTypeName = String.Empty;
            List <Entity> entities       = FeedHelper.GetEntitiesFromFeed(source.Link).ToList();

            if (entities == null)
            {
                return(BadRequest("Wrong feed format or url"));
            }

            // check if db has this source
            var sourceInDb = await db.Sources.FirstOrDefaultAsync(s => s.Link == source.Link);

            if (sourceInDb == null)
            {
                db.Sources.Add(source);
                await db.SaveChangesAsync();

                var lastAddedSource = await db.Sources.OrderByDescending(s => s.Id).FirstOrDefaultAsync();

                db.SourceToCollections.Add(new SourceToCollection()
                {
                    CollectionId = id, SourceId = lastAddedSource.Id
                });

                foreach (var item in entities)
                {
                    db.Entities.Add(new Entity()
                    {
                        Title           = item.Title,
                        PublicationDate = item.PublicationDate,
                        Link            = item.Link,
                        SourceId        = source.Id
                    });
                }
            }
            else
            {
                db.SourceToCollections.Add(new SourceToCollection()
                {
                    CollectionId = id, SourceId = sourceInDb.Id
                });
            }

            await db.SaveChangesAsync();

            return(Ok());
        }