Ejemplo n.º 1
0
        /// <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();
        }
Ejemplo n.º 2
0
        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