Esempio n. 1
0
        public async Task <IActionResult> SetSubjectTags(int id, string[] tags)
        {
            var subject = _context.Subject.SingleOrDefault(s => s.ID == id);

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

            foreach (var tagName in tags)
            {
                var tag = _context.SubjectTag.SingleOrDefault(t => t.Name == tagName);
                if (tag == null)
                {
                    tag = new SubjectTag {
                        Name = tagName
                    };
                    _context.SubjectTag.Add(tag);
                    await _context.SaveChangesAsync();
                }
                if (_context.SubjectTagSet.SingleOrDefault(st => st.SubjectID == id && st.SubjectTagID == tag.ID) == null)
                {
                    _context.SubjectTagSet.Add(
                        new SubjectTagSet
                    {
                        SubjectID    = id,
                        SubjectTagID = tag.ID,
                    }
                        );
                    await _context.SaveChangesAsync();
                }
            }

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <ActionResultResponse <string> > Insert(List <SubjectTagMeta> subjectTagMetas)
        {
            if (subjectTagMetas == null || !subjectTagMetas.Any())
            {
                return(new ActionResultResponse <string>(-1,
                                                         _coreResourceService.GetString("List subject tags is null.")));
            }

            var subjectTags = new List <SubjectTag>();
            var subjectTag  = subjectTagMetas.FirstOrDefault();

            var tenantId        = subjectTag.TenantId;
            var creatorId       = subjectTag.CreatorId;
            var creatorFullName = subjectTag.CreatorFullName;
            var creatorAvata    = subjectTag.CreatorAvata;
            var subjectId       = subjectTag.SubjectId;

            //xoa truoc khi insert
            if (!string.IsNullOrEmpty(subjectId))
            {
                await _subjectTagRepository.DeleteBySubjectId(subjectId);
            }

            foreach (var subjectTagMeta in subjectTagMetas)
            {
                var     languageId = subjectTagMeta.LanguageId;
                var     tagMetas   = subjectTagMeta.Tags;
                TagType type       = subjectTag.Type;

                foreach (var tag in tagMetas)
                {
                    if (!string.IsNullOrEmpty(tag.Id))
                    {
                        var tagInfo =
                            await _tagRepository.GetInfo(tenantId, languageId, tag.Id);

                        if (tagInfo != null)
                        {
                            var subjectTagInsert = new SubjectTag
                            {
                                SubjectId = subjectId,
                                TagId     = tag.Id
                            };
                            subjectTags.Add(subjectTagInsert);
                        }
                        else
                        {
                            var isExists =
                                await _tagRepository.CheckExistsByName(type, tenantId, languageId, tag.Name);

                            if (!isExists)
                            {
                                var tagInsert = new Tag
                                {
                                    Id              = Guid.NewGuid().ToString(),
                                    LanguageId      = languageId,
                                    TenantId        = tenantId,
                                    Type            = type,
                                    Name            = tag.Name,
                                    UnsignName      = tag.Name?.StripVietnameseChars().ToUpper(),
                                    SeoLink         = tag.Name?.ToUrlString(),
                                    CreatorId       = creatorId,
                                    CreatorFullName = creatorFullName,
                                    CreatorAvatar   = creatorAvata
                                };
                                await _tagRepository.Insert(tagInsert);

                                var subjectTagInsert = new SubjectTag
                                {
                                    SubjectId = subjectId,
                                    TagId     = tagInsert.Id
                                };
                                subjectTags.Add(subjectTagInsert);
                            }
                            else
                            {
                                var subjectTagInsert = new SubjectTag
                                {
                                    SubjectId = subjectId,
                                    TagId     = await _tagRepository.GetIdByNameAndType(type, tenantId, languageId, tag.Name)
                                };
                                subjectTags.Add(subjectTagInsert);
                            }
                        }
                    }
                    else
                    {
                        var isTagExists =
                            await _tagRepository.CheckExistsByName(type, tenantId, languageId, tag.Name);

                        if (!isTagExists)
                        {
                            var tagInsert = new Tag
                            {
                                Id              = Guid.NewGuid().ToString(),
                                LanguageId      = languageId,
                                TenantId        = tenantId,
                                Type            = type,
                                Name            = tag.Name,
                                UnsignName      = tag.Name?.StripVietnameseChars().ToUpper(),
                                SeoLink         = tag.Name?.ToUrlString(),
                                CreatorId       = creatorId,
                                CreatorFullName = creatorFullName,
                                CreatorAvatar   = creatorAvata
                            };
                            await _tagRepository.Insert(tagInsert);

                            var subjectTagInsert = new SubjectTag
                            {
                                SubjectId = subjectId,
                                TagId     = tagInsert.Id
                            };
                            subjectTags.Add(subjectTagInsert);
                        }
                        else
                        {
                            var subjectTagInsert = new SubjectTag
                            {
                                SubjectId = subjectId,
                                TagId     = await _tagRepository.GetIdByNameAndType(type, tenantId, languageId, tag.Name)
                            };
                            subjectTags.Add(subjectTagInsert);
                        }
                    }
                }
            }

            var insertSubjectTags = subjectTags.GroupBy(x => new { x.SubjectId, x.TagId })
                                    .Select(x => x.First())
                                    .ToList();

            var result = await _subjectTagRepository.Inserts(insertSubjectTags);

            if (result > 0)
            {
                return(new ActionResultResponse <string>(result,
                                                         _coreResourceService.GetString("Update news subject tags successful.")));
            }

            return(new ActionResultResponse <string>(1,
                                                     _coreResourceService.GetString("Add new tag successful."),
                                                     string.Empty, subjectId));
        }
Esempio n. 3
0
 public async Task <int> Insert(SubjectTag subjectTag)
 {
     _subjectTagRepository.Create(subjectTag);
     return(await Context.SaveChangesAsync());
 }