Exemplo n.º 1
0
        public string ExportCategoriesByProductsCount()
        {
            var categories = context.Categories
                             .Include(c => c.CategoryProducts)
                             .ThenInclude(cp => cp.Product)
                             .OrderByDescending(c => c.CategoryProducts.Count)
                             .ToArray();

            var categoryDtos = new CategoryDto[categories.Length];

            for (var i = 0; i < categories.Length; i++)
            {
                var category = categories[i];

                categoryDtos[i] = new CategoryDto
                {
                    Name          = category.Name,
                    ProductsCount = category.CategoryProducts.Count,
                    AveragePrice  = category.CategoryProducts.Average(cp => cp.Product.Price),
                    TotalRevenue  = category.CategoryProducts.Sum(cp => cp.Product.Price)
                };
            }

            var sb = new StringBuilder();

            var serializer = new XmlSerializer(categoryDtos.GetType(), new XmlRootAttribute("categories"));

            serializer.Serialize(new StringWriter(sb), categoryDtos, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));

            return(sb.ToString());
        }
Exemplo n.º 2
0
        public void WhenOnlyTheNamePropertyIsSetInDto_GivenEntityWithMultiplePropertiesSetIncludingTheName_ShouldReturnEntityWithOnlyTheNameChanged()
        {
            var entity = new Category()
            {
                Name        = "entity name",
                Description = "some description"
            };

            var dto = new CategoryDto()
            {
                Name = "updated name"
            };

            //Attributes are readonly - http://stackoverflow.com/questions/10046601/change-custom-attributes-parameter-at-runtime
            // So we are going to try to replace the attribute with new instance.

            //******************************

            PropertyOverridingTypeDescriptor ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(dto).GetTypeDescriptor(dto));

            PropertyDescriptor desriptionPorpertyDescriptor = ctd.GetProperties().Find("Description", true);

            PropertyDescriptor pd2 =
                TypeDescriptor.CreateProperty(
                    dto.GetType(),
                    desriptionPorpertyDescriptor, // base property descriptor to which we want to add attributes
                    // The PropertyDescriptor which we'll get will just wrap that
                    // base one returning attributes we need.
                    new Updated(false)
                    // this method really can take as many attributes as you like,
                    // not just one
                    );

            // and then we tell our new PropertyOverridingTypeDescriptor to override that property
            ctd.OverrideProperty(pd2);

            // then we add new descriptor provider that will return our descriptor istead of default
            TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), dto);

            //******************************

            Category resultEntity = entity.Merge(dto);

            // The name should be updated
            Assert.AreEqual(dto.Name, resultEntity.Name);
            // The description shouldn't be updated
            Assert.NotNull(entity.Description);
            Assert.AreEqual(entity.Description, resultEntity.Description);
        }
        private static CategoryDto CreateCategoryFromInput()
        {
            CategoryDto category = new CategoryDto();

            Clear();
            WriteLine("Press Esc to when done editing.".PadRight(Program.WindowWidth, '#'));
            WriteLine("".PadRight(Program.WindowWidth, '#'));
            OptionsPrinter("    ");


            Coordinates categoryPropertiesCoordinates = new Coordinates(ContentCursorPosLeft, ContentCursorPosTop);

            category.PrintProperties(categoryPropertiesCoordinates);



            void EraseOldText(int length)
            {
                int cursorLeft = CursorLeft;
                int cursorTop  = CursorTop;

                Write(" ".PadRight(length));
                SetCursorPosition(cursorLeft, cursorTop);
            }

            List <string> propertyNames = categoryPropertiesCoordinates.SavedCoordinates.Keys.ToList();



            int moveCursor = 0;

            bool shouldNotExit = true;

            int propertysValuesSpacing = 10;

            ConsoleKeyInfo consoleKeyInfo;


            bool correktKey;

            while (shouldNotExit)
            {
                SetCursorPosition(categoryPropertiesCoordinates.SavedCoordinates[propertyNames[moveCursor]].X + propertysValuesSpacing, categoryPropertiesCoordinates.SavedCoordinates[propertyNames[moveCursor]].Y);

                do
                {
                    consoleKeyInfo = ReadKey(true);

                    correktKey = !(consoleKeyInfo.Key == ConsoleKey.UpArrow || consoleKeyInfo.Key == ConsoleKey.DownArrow || consoleKeyInfo.Key == ConsoleKey.Escape || consoleKeyInfo.Key == ConsoleKey.Enter);
                } while (correktKey);

                switch (consoleKeyInfo.Key)
                {
                case ConsoleKey.UpArrow:

                    if (moveCursor > 0)
                    {
                        moveCursor--;
                    }

                    break;

                case ConsoleKey.DownArrow:

                    if (moveCursor < propertyNames.Count - 1)
                    {
                        moveCursor++;
                    }

                    break;

                case ConsoleKey.Enter:

                    string currentProperty = propertyNames[moveCursor];


                    if (!currentProperty.ToUpper().Contains("ID"))
                    {
                        EraseOldText(100);

                        string input = ReadLine();


                        foreach (var property in category.GetType().GetProperties())
                        {
                            if (property.Name == currentProperty)
                            {
                                property.SetValue(category, input);
                            }
                        }
                    }


                    break;

                case ConsoleKey.Escape:
                    shouldNotExit = false;

                    break;
                }


                SetCursorPosition(categoryPropertiesCoordinates.SavedCoordinates[propertyNames[moveCursor]].X + propertysValuesSpacing, categoryPropertiesCoordinates.SavedCoordinates[propertyNames[moveCursor]].Y);
            }


            if (category.ImageUrl == null && category.Name == null)
            {
                return(null);
            }

            bool b;

            OptionsPrinter("Save category? (Y)es (N)o");
            do
            {
                consoleKeyInfo = ReadKey(true);

                b = !(consoleKeyInfo.Key == ConsoleKey.Y || consoleKeyInfo.Key == ConsoleKey.N);
            } while (b);

            switch (consoleKeyInfo.Key)
            {
            case ConsoleKey.Y:
                Clear();


                return(category);

            case ConsoleKey.N:
                Clear();
                SetCursorPosition(MenuCursorPosLeft, MenuCursorPosTop);

                WriteLine("No changes applied");
                Thread.Sleep(1500);

                return(null);

            default:
                return(null);
            }
        }