Beispiel #1
0
        public async Task <IActionResult> GetContentfile([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var s = await _context.Contentfile
                    .Include(e => e.Filecategory)
                    .ThenInclude(e => e.Category)
                    .FirstOrDefaultAsync(d => d.Id == id);

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

            var result = new ContentFileDto
            {
                Id            = s.Id,
                Name          = s.Name,
                CreatedDate   = s.CreatedDate,
                FilePathId    = s.FilePathId,
                Status        = s.Status,
                FinishedBy    = s.FinishedBy,
                VerifiedBy    = s.VerifiedBy,
                Categories    = s.Filecategory.Select(o => o.Category.Name).ToList(),
                Subcategories = s.Filesubcategory.Select(o => o.SubCategory.Name).ToList(),
                FinishedDate  = s.FinishedDate,
                VerifiedDate  = s.VerifiedDate
            };

            return(Ok(result));
        }
Beispiel #2
0
        public async Task <IActionResult> PutContentfile([FromRoute] int id, [FromBody] ContentFileDto contentfile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != contentfile.Id)
            {
                return(BadRequest());
            }

            var contentFile = await _context.Contentfile
                              .Include(e => e.Filecategory).ThenInclude(e => e.Category)
                              .Include(e => e.Filesubcategory).ThenInclude(e => e.SubCategory)
                              .FirstOrDefaultAsync(d => d.Id == id);

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

            // For Category
            var oldArray = contentFile.Filecategory.Select(o => o.Category.Name).ToList();

            oldArray.Sort();
            string oldKwds = string.Join(";", oldArray);

            contentfile.Categories.Sort();
            string newKwds = string.Join(";", contentfile.Categories);

            if (newKwds != oldKwds)
            {
                // Update data
                contentFile.Filecategory.Clear();
                foreach (var keywd in contentfile.Categories)
                {
                    var ck = await _context.Category.FirstOrDefaultAsync(k => k.Name.ToLower() == keywd.ToLower());

                    if (ck == null)
                    {
                        throw new Exception("Category not found: " + keywd);
                    }

                    contentFile.Filecategory.Add(new Filecategory {
                        File = contentFile, Category = ck
                    });
                }
            }

            // For SubCategory
            var oldSub = contentFile.Filesubcategory.Select(o => o.SubCategory.Name).ToList();

            oldSub.Sort();
            string oldSubj = string.Join(";", oldSub);

            contentfile.Subcategories.Sort();
            string newSubj = string.Join(";", contentfile.Subcategories);

            if (oldSubj != newSubj)
            {
                // Update data
                contentFile.Filesubcategory.Clear();
                foreach (var subj in contentfile.Subcategories)
                {
                    var ck = await _context.Subcategory.FirstOrDefaultAsync(k => k.Name.ToLower() == subj.ToLower());

                    if (ck == null)
                    {
                        throw new Exception("Subcategory not found: " + subj);
                    }

                    contentFile.Filesubcategory.Add(new FileSubcategory {
                        File = contentFile, SubCategory = ck
                    });
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ContentfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }