Exemplo n.º 1
0
        public static async Task AddDefaultBlogCategories(IServiceProvider services)
        {
            var context = services.GetRequiredService <AppDbContext>();


            // first add the BlogCategoryName to be the parent of all BlogCategory in any languages
            string[] BlogCategoryNames   = { "Food", "Travel", "Music", "Lifestyle", "Fitness", "Sports" };
            int[]    BlogCategoryNamesId = { 1, 2, 3, 4, 5, 6 };

            for (int i = 0; i < BlogCategoryNames.Length; i++)
            {
                var name = new BlogSourceCategoryName
                {
                    Id   = BlogCategoryNamesId[i],
                    Name = BlogCategoryNames[i]
                };
                await context.BlogSourceCategoryName.AddAsync(name);
            }
            await context.SaveChangesAsync();


            string[] BlogCategoryNamesDe = { "Essen", "Reise", "Musik", "Lebensstil", "Fitness", "Sport" };
            string[] BlogCategoryNamesAr = { "طعام", "السفر", "موسيقى", "أسلوب الحياة", "اللياقه البدنيه", "رياضة" };

            for (int i = 0; i < BlogCategoryNames.Length; i++)
            {
                // De
                var lDe = new BlogCategory
                {
                    Name             = BlogCategoryNamesDe[i],
                    SourceCategoryId = BlogCategoryNamesId[i],
                    LanguageId       = LanguageCodeId[2]
                };
                await context.BlogCategory.AddAsync(lDe);

                // Ar
                var lAr = new BlogCategory
                {
                    Name             = BlogCategoryNamesAr[i],
                    SourceCategoryId = BlogCategoryNamesId[i],
                    LanguageId       = LanguageCodeId[1]
                };
                await context.BlogCategory.AddAsync(lAr);
            }
            await context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task <ActionResult <string> > PutSourceCategoryName([FromForm] BlogSourceCategoryDto sourcCate)
        {
            // check if this Source Name existing before
            var sourceName = await _context.BlogSourceCategoryName.FirstOrDefaultAsync(n => n.Name == sourcCate.Name);

            if (sourceName != null)
            {
                return(BadRequest(new ApiResponse(400, $"This {sourcCate.Name} category existing before!")));
            }

            // check if this name not exist
            var name = await _context.BlogSourceCategoryName.FirstOrDefaultAsync(n => n.Id == sourcCate.Id);

            if (name != null)
            {
                name.Name = sourcCate.Name;

                _context.Entry(name).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(Ok(new ApiResponse(201, $"Update {sourcCate.Name} Source BlogCategory Successfully")));
            }
            else if (name == null && sourcCate.Id != null)
            {   // if user add id not exist
                return(BadRequest(new ApiResponse(400, $"This Id {sourcCate.Id} not existing, If you want add new Source Blog's Category Set Id is empty or null!")));
            }
            else
            {
                var newSC = new BlogSourceCategoryName
                {
                    Name = sourcCate.Name
                };

                await _context.BlogSourceCategoryName.AddAsync(newSC);

                await _context.SaveChangesAsync();

                return(Ok(new ApiResponse(201, $"Add {sourcCate.Name} Source BlogCategory successfully")));
            }
        }
Exemplo n.º 3
0
        public async Task <BlogSourceCategoryName> GetSourceBlogCategoryName([FromForm] int id)
        {
            BlogSourceCategoryName categoriesName = await _context.BlogSourceCategoryName.Where(c => c.Id == id).FirstOrDefaultAsync();

            return(categoriesName);
        }