/// <summary> /// 获取一个修订版本的所有字段。 /// </summary> /// <param name="version"></param> /// <returns></returns> public Dictionary <FieldType, string> GetVersionFields(GroupVersion version) { // Get All FieldTypes var fieldTypes = GetAllFieldTypes(version.Group.Collection.CollectionId); Dictionary <FieldType, string> fields = new Dictionary <FieldType, string>(); foreach (var type in fieldTypes) { fields[type] = null; } if (version == null) { return(fields); } // Get All GVRefersSuggestions for a version List <GroupVersionRefersSuggestion> references = new List <GroupVersionRefersSuggestion>(); if (context.GVSuggestions.Count() > 0) { references = context.GVSuggestions.Include(i => i.GroupVersion) .Where(i => i.GroupVersion.GroupVersionId == version.GroupVersionId) .ToList(); } foreach (var item in references) { var suggestion = context.Entry(item) .Reference(i => i.Suggestion).Query().Single(); fields[item.FieldType] = suggestion.Content; } return(fields); }
/// <summary> /// 添加一条新Suggestion (用户提交) /// </summary> /// <param name="group"></param> /// <param name="fields"></param> public void GroupNewSuggestion(int groupId, Dictionary <FieldType, string> newFields, ApplicationUser user = null) { var fieldTypes = GetAllFieldTypesByGroup(groupId); var oldFields = GetLastestVersionFields(groupId); var latestVersion = GetLastestVersion(groupId); var now = DateTime.UtcNow; // for each field type // check if the existing suggestion is the same // add a new Suggestion if changed // add GroupVersionRefersSuggestion for each field type var newVersion = new GroupVersion() { Group = context.Groups.Single(g => g.GroupId == groupId), NextVersion = null, Created = now }; foreach (var type in fieldTypes) { var newReference = new GroupVersionRefersSuggestion() { FieldType = type, GroupVersion = newVersion }; if (newFields[type] != null) { if ((latestVersion == null) || (oldFields[type] == null) || (oldFields[type] != null && oldFields[type] != newFields[type])) { var newSuggestion = new Suggestion() { Content = newFields[type], Author = user, Created = now }; newReference.Suggestion = newSuggestion; context.Suggestions.Add(newSuggestion); } else { var oldSuggestion = context.Entry(latestVersion) .Collection(v => v.FieldSuggestions) .Query() .Include(f => f.Suggestion) .Single(f => f.FieldType == type).Suggestion; newReference.Suggestion = oldSuggestion; } context.GVSuggestions.Add(newReference); } else { //Don't add suggestion // do nothing } } if (latestVersion != null) { latestVersion.NextVersion = newVersion; } context.GroupVersions.Add(newVersion); context.SaveChanges(); }
public async Task RunAsync(IWebHost host, string[] args) { Console.WriteLine("Load English Text and Groups [Collection 1]"); if (args.Count() != 1) { Console.WriteLine("Invalid Arguments"); return; } TextReader file; try { file = File.OpenText(args[0]); } catch (Exception) { Console.WriteLine($"Error opening file: {args[0]}."); return; } var csv = new CsvReader(file); var services = (IServiceScopeFactory)host.Services.GetService(typeof(IServiceScopeFactory)); using (var scope = services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); var firstCollection = context.Collections.First(); var englishType = context.FieldTypes.Single(t => t.Name == "TextEnglish" && t.Collection.CollectionId == firstCollection.CollectionId); int i = 0; if (context.Groups.Any()) { Console.WriteLine("Rows existed in Groups. Exiting..."); return; } while (csv.Read()) { var now = DateTime.UtcNow; ++i; var filename = csv.GetField <string>("FileName"); var eng = csv.GetField <string>("Text"); Console.WriteLine($"[{i}] {filename} - {eng}"); //if (i == 100) break; var newGroup = new Group() { Collection = firstCollection, GroupMetadata = JsonConvert.SerializeObject(new Dictionary <string, string> { { "ImgFileName", filename } }), GroupId = i, }; context.Groups.Add(newGroup); var newVersion = new GroupVersion() { Created = now, Group = newGroup, }; context.GroupVersions.Add(newVersion); var newSuggestion = new Suggestion() { Content = eng, Created = now }; context.Suggestions.Add(newSuggestion); var newGVSuggestion = new GroupVersionRefersSuggestion() { GroupVersion = newVersion, FieldType = englishType, Suggestion = newSuggestion }; context.GVSuggestions.Add(newGVSuggestion); } context.SaveChanges(); Console.WriteLine($"{i} rows imported."); } // using scope } // method