public void AddGenericTag(ApplicationDbContext context, ApplicationVideo video, string genericTagId) { var refTag = context.ApplicationGenericTags.FirstOrDefault(x => x.Tag == genericTagId); if (refTag == null) { refTag = new ApplicationGenericTag() { Tag = genericTagId }; context.Add(refTag); context.SaveChanges(); } var tag = context.ApplicationVideoApplicationGenericTags.FirstOrDefault(x => x.VideoId == video.Id && x.Tag.Tag == genericTagId); // Do nothing in the case where the tag already exist // Generic tags can't be updated if (tag == null) { var newTag = new ApplicationVideoApplicationGenericTag() { VideoId = video.Id, TagId = refTag.Id }; context.Add(newTag); } }
public void ApplyEditGenericTag(ApplicationGenericTag tag, TagEdit edit) { if (edit.Name != null) { tag.Tag = SanitizeTags(edit.Name, true); } tag.UpdateDate = DateTime.UtcNow; }
public bool EditGenericTag(ApplicationDbContext context, string key, TagEdit edit) { switch (edit.Flag) { case EditType.New: { var newType = context.ApplicationGenericTags.FirstOrDefault(x => x.Tag == key); var newVideo = new ApplicationGenericTag() { }; ApplyEditGenericTag(newVideo, edit); context.Add(newVideo); context.SaveChanges(); } break; case EditType.Update: { var existingTag = context.ApplicationGenericTags.First(x => x.Tag == key); if (edit.UpdateDate == existingTag.UpdateDate) { ApplyEditGenericTag(existingTag, edit); } else { return(false); } } break; case EditType.Delete: { var existingTag = context.ApplicationGenericTags.First(x => x.Tag == key); context.Remove(existingTag); } break; default: throw new Exception("No edit flag passed"); } return(true); }