public List <CategoryItem> GetAllItems(IConfiguration configuration)
        {
            var connString = new NpgsqlConnection(ConnectionService.GetConnectionString(configuration));

            connString.Open();

            var allItems = new List <CategoryItem>();

            using (var cmd = new NpgsqlCommand("getallcategoryitems", connString))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                using (var reader = cmd.ExecuteReader())
                {
                    var dataTable = new DataTable();
                    dataTable.Load(reader);
                    if (dataTable.Rows.Count > 0)
                    {
                        var serializedMyObjects = JsonConvert.SerializeObject(dataTable);
                        allItems.AddRange((List <CategoryItem>)JsonConvert.DeserializeObject(serializedMyObjects, typeof(List <CategoryItem>)));
                    }
                }
                connString.Close();
            }
            return(allItems);
        }
Esempio n. 2
0
 private dynamic CallPostgresFunction(string funcName)
 {
     var connection = new NpgsqlConnection(ConnectionService.GetConnectionString(_configuration));
     connection.Open();
     using (var cmd = new NpgsqlCommand(funcName, connection))
     {
         cmd.CommandType = CommandType.StoredProcedure;
         using (var reader = cmd.ExecuteReader())
         {
             var dataTable = new DataTable();
             dataTable.Load(reader);
             if (dataTable.Rows.Count > 0)
             {
                 return JsonConvert.SerializeObject(dataTable);
             }
         }
         connection.Close();
     }
     return string.Empty;
 }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseNpgsql(ConnectionService.GetConnectionString(Configuration)));

            services.AddControllers();
            services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "Money Please",
                    Description = "This is a demo API that will fetch data from a database without a direct connection.",
                    Contact     = new OpenApiContact
                    {
                        Name  = "Eli Jones",
                        Email = "*****@*****.**",
                        Url   = new Uri("https://www.ejdevspot.com/")
                    }
                });
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }