/// <summary>
        /// Deletes a articleCategory mapping record
        /// </summary>
        /// <param name="articleCategoryMapping">ArticleCategory mapping record</param>
        public virtual void DeleteArticleCategoryMapping(ArticleCategoryMapping articleCategoryMapping)
        {
            if (articleCategoryMapping == null)
            {
                throw new ArgumentNullException("articleCategoryMapping");
            }

            _articleCategoryMappingRepository.Delete(articleCategoryMapping);

            //cache
            _cacheManager.RemoveByPattern(STOREMAPPING_PATTERN_KEY);
        }
        /// <summary>
        /// Inserts a articleCategory mapping record
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="categoryId">ArticleCategory id</param>
        /// <param name="entity">Entity</param>
        public virtual void InsertArticleCategoryMapping <T>(T entity, int categoryId) where T : BaseEntity
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (categoryId == 0)
            {
                throw new ArgumentOutOfRangeException("categoryId");
            }

            int    entityId   = entity.Id;
            string entityName = typeof(T).Name;

            var articleCategoryMapping = new ArticleCategoryMapping()
            {
                EntityId          = entityId,
                EntityName        = entityName,
                ArticleCategoryId = categoryId
            };

            InsertArticleCategoryMapping(articleCategoryMapping);
        }
Ejemplo n.º 3
0
        public override async Task Add(ArticleViewModel vm, ClaimsPrincipal user)
        {
            if (vm == null)
            {
                throw new ArgumentNullException(nameof(vm));
            }

            if (vm.Title == null)
            {
                throw new ArgumentException("Title is null");
            }
            if (vm.ArticleText == null)
            {
                throw new ArgumentException("Article text is null");
            }

            //get categires from database
            var availablecategories = await _ctx.Categories.ToListAsync();

            //create a list of string that contains only the category name
            List <string> availablecategorynames = new List <string>();

            //populate the list
            foreach (var category in availablecategories)
            {
                availablecategorynames.Add(category.Name);
            }
            // create an array of category names that can be added. except selects all the categories in the first set,
            // in our case vm.Categories -which come from the interface, that do not exist in the second set in our case
            // availablecategorynames whcih are read from the databse.

            var newcategs = vm.Categories.Except(availablecategorynames.ToArray());

            //var isAuthorized = await _authorizationService.AuthorizeAsync(user, article, ArticleOperations.Create);
            //if (!isAuthorized.Succeeded)
            //{
            //    throw new ArgumentException("The currently logged in user is not allowed to add articles");
            //}

            using (var transaction = _ctx.Database.BeginTransaction())
            {
                try
                {
                    var article = new Article()
                    {
                        Title          = vm.Title,
                        Image          = vm.Image,
                        ArticleText    = vm.ArticleText,
                        Author         = vm.Author,
                        IssueDate      = DateTime.Now,
                        ApprovedStatus = vm.ApprovedStatus
                    };
                    _ctx.Articles.Add(article);
                    await _ctx.SaveChangesAsync();

                    var isAuthorized = await _authorizationService.AuthorizeAsync(user, article, ArticleOperations.Create);

                    if (!isAuthorized.Succeeded)
                    {
                        throw new ArgumentException("The currently logged in user is not allowed to add articles");
                    }

                    foreach (var categ in newcategs)
                    {
                        var category = new Category()
                        {
                            Name = categ,
                        };
                        _ctx.Categories.Add(category);
                        await _ctx.SaveChangesAsync();
                    }
                    //Get all the categories that we should map from the database.
                    //Compare all the values from the DB with the ones from the interface
                    var categoriesToMap = await(from categs in _ctx.Categories
                                                where vm.Categories.Contains(categs.Name)
                                                select categs).ToListAsync();
                    foreach (var categ in categoriesToMap)
                    {
                        var artcategmapping = new ArticleCategoryMapping()
                        {
                            ArtId   = article.Id,
                            CategId = categ.Id,
                        };
                        _ctx.ArticleCategoryMappings.Add(artcategmapping);
                        await _ctx.SaveChangesAsync();
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }