Example #1
0
        public override Result <IEnumerable <ArdCategoryInfoDto> > GetCategories(string targetUrl, ContinuationToken continuationToken = null)
        {
            continuationToken ??= new ContinuationToken()
            {
                { _categoryLevel, 0 }
            };

            var currentLevel = continuationToken.GetValueOrDefault(_categoryLevel) as int? ?? 0;

            Log.Debug($"GetCategories current Level: {currentLevel}");

            var json          = WebClient.GetWebData <JObject>(targetUrl, proxy: WebRequest.GetSystemWebProxy());
            var categoryInfos = currentLevel switch
            {
                0 => CategoryDeserializer.ParseWidgets(json, hasSubCategories: true), // load A - Z
                1 => LoadCategoriesWithDetails(json),                                 // load e.g. Abendschau - skip level, (load infos from nextlevel) for each category load url and read synopsis
                //2 => categoryDeserializer.ParseTeasers(json), // videos...
                _ => throw new ArgumentOutOfRangeException(),
            };

            var newToken = new ContinuationToken(continuationToken);

            newToken[_categoryLevel] = currentLevel + 1;
            return(new Result <IEnumerable <ArdCategoryInfoDto> >
            {
                ContinuationToken = newToken,
                Value = categoryInfos
            });
        }
Example #2
0
        public IActionResult Create([FromBody] CategoryDeserializer categoryDsr)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!Context.Category.Any(a => a.Id == categoryDsr.ParentCategory))
            {
                categoryDsr.ParentCategory = null;
            }

            Category category = new Category()
            {
                Name           = categoryDsr.Name,
                ParentCategory = categoryDsr.ParentCategory
            };

            try {
                Context.Category.Add(category);
                Context.SaveChanges();
            } catch (Exception e) {
                string msg = e.InnerException.Message;
                if (msg.Contains("Duplicate"))
                {
                    msg = $"Le nom {category.Name} est déjà utilisé";
                }
                else
                {
                    msg = "Erreur interne";
                }
                return(BadRequest(msg));
            }

            return(new OkObjectResult(category));
        }
Example #3
0
        private IEnumerable <ArdCategoryInfoDto> LoadCategoriesWithDetails(JObject json)
        {
            var categories = CategoryDeserializer.ParseTeasers(json);

            foreach (var category in categories)
            {
                //TODO workaround
                var details     = WebClient.GetWebData <JObject>(category.TargetUrl, proxy: WebRequest.GetSystemWebProxy());
                var newCategory = CategoryDeserializer.ParseTeaser(details);
                category.Title       = newCategory.Title;
                category.Description = newCategory.Description;
                yield return(category);
                // sub item sind videos, aber ...
            }
        }
Example #4
0
        public void UpdateFromDeserializer(ref CategoryDeserializer deserializer,
                                           ref Category category)
        {
            category.Name = deserializer.Name;
            if (deserializer.ParentCategory.HasValue)
            {
                int parentId = (int)deserializer.ParentCategory;
                var parent   = Context.Category.FirstOrDefault(arg => arg.Id == parentId);

                if (parent != null && !parent.ParentCategory.HasValue)
                {
                    category.ParentCategory = parentId;
                }
            }
        }
            public async void ShouldNullableValueForParentWhenNotFound(string email)
            {
                string nameAsGuid  = Guid.NewGuid().ToString().Substring(0, 20);
                var    categoryDsr = new CategoryDeserializer()
                {
                    Name = nameAsGuid, ParentCategory = int.MaxValue - 1
                };
                var admin = Context.User.Include(e => e.Role).FirstOrDefault(e => e.Email == email);

                var newContent = new StringContent(JsonConvert.SerializeObject(categoryDsr),
                                                   Encoding.UTF8, "application/json");

                HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
                                                                                               TokenBearerHelper.GetTokenFor(admin, Context));
                HttpResponseMessage response = await HttpClient.PostAsync("api/category", newContent);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.True(Context.Category.Any(c => c.Name.Equals(nameAsGuid)));
            }
Example #6
0
        public async Task <IActionResult> Update(int id,
                                                 [FromBody] CategoryDeserializer categoryFromRequest)
        {
            Category category = await Context.FindAsync <Category>(id);

            if (category == null)
            {
                return(NotFound());
            }

            try {
                Services.UpdateFromDeserializer(ref categoryFromRequest, ref category);
                Context.Category.Update(category);
                await Context.SaveChangesAsync();
            } catch (DbUpdateException db) {
                return(BadRequest(db.InnerException.Message));
            }

            return(Ok(category));
        }