예제 #1
0
        public void SavePolygon(FigureData polygon, string name)
        {
            var connection = PostgresConnection.GetInstance();

            try
            {
                connection.Open();

                string sql = "insert into polygons (name, type, x_coords, y_coords) " +
                             "values (@name, @type, @x_coords, @y_coords)";

                var command = new NpgsqlCommand(sql, connection);

                command.Parameters.AddWithValue("@name", name);
                command.Parameters.AddWithValue("@type", polygon.PolygonType);

                var dictionary = FigureData.GetArraysFromCoords(polygon.Coords);
                command.Parameters.AddWithValue("@x_coords", dictionary["xCoords"]);
                command.Parameters.AddWithValue("@y_coords", dictionary["yCoords"]);

                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                connection.Close();
            }
        }
예제 #2
0
        public bool SavePolyhedron(string name)
        {
            try
            {
                if (_figure is Polygon)
                {
                    throw new InvalidOperationException("Can`t save polygon to polyhedrons table");
                }

                Polyhedron polyhedron = (Polyhedron)_figure;
                FigureData figureData = new FigureData();

                figureData.PolyhedronType = polyhedron.Type;
                figureData.PolygonType    = polyhedron.GetBaseFigure().Type;
                figureData.Coords         = polyhedron.GetBaseFigure().GetCoords();
                figureData.Height         = polyhedron.GetHeight();

                _database.SavePolyhedron(figureData, name);

                return(true);
            }
            catch (Exception e)
            {
                Console.Clear();

                ConsoleView.ColorizeError(e.Message);

                Console.WriteLine();

                return(false);
            }
        }
예제 #3
0
        public FigureData LoadPolygon(string name)
        {
            var connection = PostgresConnection.GetInstance();

            try
            {
                connection.Open();

                string sql = "select type, x_coords, y_coords from polygons where name=@name";

                var command = new NpgsqlCommand(sql, connection);
                command.Parameters.AddWithValue("@name", name);

                var dataReader = command.ExecuteReader();

                if (!dataReader.Read())
                {
                    throw new DataException("Figure does not exist");
                }

                FigureData figureData = new FigureData();
                figureData.PolygonType = (string)dataReader[0];
                figureData.Coords      = FigureData.GetCoordsFromArrays((double[])dataReader[1], (double[])dataReader[2]);

                return(figureData);
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }
예제 #4
0
        public Polygon LoadPolygon(string name)
        {
            try
            {
                FigureData figureData = _database.LoadPolygon(name);
                return(CreatePolygon(figureData.PolygonType, figureData.Coords));
            }
            catch (Exception e)
            {
                Console.Clear();

                ConsoleView.ColorizeError(e.Message);

                Console.WriteLine();

                return(null);
            }
        }
예제 #5
0
        public Polyhedron LoadPolyhedron(string name)
        {
            try
            {
                FigureData figureData = _database.LoadPolyhedron(name);

                Polygon polygon = PolygonFactory.FactoryMethod(figureData.PolygonType, figureData.Coords);
                return(CreatePolyhedron(figureData.PolyhedronType, polygon, figureData.Height));
            }
            catch (Exception e)
            {
                Console.Clear();

                ConsoleView.ColorizeError(e.Message);

                Console.WriteLine();

                return(null);
            }
        }