public string WriteCode(OpenApiSchema schema, string name, string @namespace)
        {
            string rootNamespace      = Dependencies.Document.Options.RootNamespace;
            string entityNamespace    = Dependencies.Namespace.Entity(rootNamespace);
            string viewModelNamespace = Dependencies.Namespace.ViewModel(rootNamespace);

            Clear();
            GenerateFileHeader();
            WriteLine("using System;");
            WriteLine("using System.Linq;");
            WriteLine("using System.Threading;");
            WriteLine("using System.Diagnostics;");
            WriteLine("using System.Threading.Tasks;");
            WriteLine("using System.Collections.Generic;");
            WriteLine($"using {entityNamespace};");
            WriteLine($"using {viewModelNamespace};");
            WriteLine();
            WriteLine($"namespace {@namespace}");
            using (OpenBlock())
            {
                string converterName = Dependencies.Namer.Converter(name);
                string entityName    = Dependencies.Namer.Entity(name);
                string viewModelName = Dependencies.Namer.ViewModel(name);
                WriteLine($"public static class {converterName}");
                using (OpenBlock())
                {
                    WriteLine($"public static {viewModelName} Convert({entityName} entity)");
                    using (OpenBlock())
                    {
                        WriteLine($"return new {viewModelName}");
                        using (OpenBlockSemicolon())
                        {
                            foreach ((string key, OpenApiSchema value) in schema.GetSimpleProperties())
                            {
                                WriteLine($"{key} = entity.{key},");
                            }
                        }
                    }

                    WriteLine();
                    WriteLine($"public static List<{viewModelName}> ConvertList(IEnumerable<{entityName}> entities)");
                    using (OpenBlock())
                    {
                        WriteLine("return entities.Select(e =>");
                        using (OpenBlockString(").ToList();"))
                        {
                            WriteLine($"var model = new {viewModelName}();");

                            foreach ((string key, OpenApiSchema value) in schema.GetSimpleProperties())
                            {
                                WriteLine($"model.{key} = e.{key};");
                            }
                            WriteLine("return model;");
                        }
                    }
                }
            }

            return(GetText());
        }
Ejemplo n.º 2
0
        private void Process(string name, OpenApiSchema schema)
        {
            var    primaryKeyTypeName  = Dependencies.Document.Options.PrimaryKeyTypeName;
            string viewModelName       = Dependencies.Namer.ViewModel(name);
            string converterName       = Dependencies.Namer.Converter(name);
            string repositoryFieldName = Dependencies.Namer.RepositoryFieldName(name);

            WriteLine();
            WriteLine($"public async Task<List<{viewModelName}>> GetAll{name}Async(CancellationToken ct = default(CancellationToken))");
            using (OpenBlock())
            {
                WriteLine($"var entities = {converterName}.ConvertList(await {repositoryFieldName}.GetAllAsync(ct));");
                WriteLine($"return entities;");
            }

            WriteLine();
            WriteLine($"public async Task<{viewModelName}> Get{name}ByIdAsync({primaryKeyTypeName} id, CancellationToken ct = default(CancellationToken))");
            using (OpenBlock())
            {
                WriteLine($"var viewModel = {converterName}.Convert(await {repositoryFieldName}.GetByIdAsync(id, ct));");
                WriteLine($"return viewModel;");
            }


            WriteLine();
            WriteLine($"public async Task<{viewModelName}> Add{name}Async({viewModelName} input, CancellationToken ct = default(CancellationToken))");
            using (OpenBlock())
            {
                WriteLine($"var entity = new {name}();");
                foreach (var kvp in schema.GetSimpleProperties())
                {
                    WriteLine($"entity.{kvp.Key} = input.{kvp.Key};");
                }

                WriteLine($"entity = await {repositoryFieldName}.AddAsync(entity, ct);");
                WriteLine($"input.{name}ID = entity.{name}ID;");
                WriteLine($"return input;");
            }

            WriteLine();
            WriteLine($"public async Task<bool> Update{name}Async({viewModelName} input, CancellationToken ct = default(CancellationToken))");
            using (OpenBlock())
            {
                WriteLine($"var entity = await {repositoryFieldName}.GetByIdAsync(input.{name}ID, ct);");
                WriteLine($"if (entity == null) return false;");
                foreach (var kvp in schema.GetSimpleProperties())
                {
                    WriteLine($"entity.{kvp.Key} = input.{kvp.Key};");
                }

                WriteLine($"return await {repositoryFieldName}.UpdateAsync(entity, ct);");
            }

            WriteLine();
            WriteLine($"public async Task<bool> Delete{name}Async({primaryKeyTypeName} id, CancellationToken ct = default(CancellationToken))");
            using (OpenBlock())
            {
                WriteLine($"return await {repositoryFieldName}.DeleteAsync(id, ct);");
            }
        }
Ejemplo n.º 3
0
        public string WriteCode(OpenApiSchema schema, string name, string @namespace)
        {
            Clear();
            GenerateFileHeader();
            WriteLine("using System;");
            WriteLine("using System.Collections.Generic;");
            WriteLine();
            WriteLine($"namespace {@namespace}");
            using (OpenBlock())
            {
                string className = Dependencies.Namer.Entity(name);
                WriteLine($"public partial class {className}");
                using (OpenBlock())
                {
                    WriteLine($"public {className}()");
                    var simpleProperties     = schema.GetSimpleProperties();
                    var navigationProperties = schema.GetNavigationProperties();
                    var allproperties        = schema.GetAllPropertiesRecursive();

                    using (OpenBlock())
                    {
                        foreach (var kvp in navigationProperties)
                        {
                            string n = StringUtilities.MakePascal(kvp.Key);
                            string t = Dependencies.Schema.ConvertToType(kvp.Value);
                            if (!string.IsNullOrEmpty(t) && t.StartsWith("ICollection", StringComparison.CurrentCultureIgnoreCase))
                            {
                                t = t.Replace("ICollection", string.Empty);
                                t = t.TrimStart('<');
                                t = t.TrimEnd('>');
                                WriteLine($"{n} = new HashSet<{t}>();");
                            }
                        }
                    }

                    var primaryKeyTypeName = Dependencies.Document.Options.PrimaryKeyTypeName;

                    WriteLine($"public {primaryKeyTypeName} {className}ID {{ get; set; }}");


                    foreach (var kvp in allproperties)
                    {
                        string n = StringUtilities.MakePascal(kvp.Key);
                        string t = Dependencies.Schema.ConvertToType(kvp.Value);
                        WriteLine();

                        if (n == className)
                        {
                            n = StringUtilities.MakeCamel(n);
                        }

                        WriteLine($"public {t} {n} {{ get; set; }}");
                    }
                }
            }

            return(GetText());
        }