예제 #1
0
        public static string ExportCategoryStatistics(FastFoodDbContext context, string categoriesString)
        {
            List <CategoryDto> resultCategoryDtos = new List <CategoryDto>();

            string[] inputCategoriesNames = categoriesString.Split(",");

            foreach (string inputCategoryName in inputCategoriesNames)
            {
                List <CategoryItemDto> itemDtos = new List <CategoryItemDto>();

                Category category = context.Categories.SingleOrDefault(c => c.Name == inputCategoryName);

                Item[] categoryItems = category.Items
                                       .Where(i => i.Category.Name == inputCategoryName && i.OrderItems.Any())
                                       .ToArray();

                foreach (Item item in categoryItems)
                {
                    int quantity = item.OrderItems
                                   .Where(oi => oi.ItemId == item.Id)
                                   .Sum(oi => oi.Quantity);

                    decimal totalMoney = quantity * item.Price;

                    CategoryItemDto itemDto = new CategoryItemDto
                    {
                        Name      = item.Name,
                        TimesSold = quantity,
                        TotalMade = totalMoney
                    };

                    itemDtos.Add(itemDto);
                }

                decimal         maxTotalMoneyMade = itemDtos.Max(x => x.TotalMade);
                CategoryItemDto mostPopularItem   = itemDtos.Single(i => i.TotalMade == maxTotalMoneyMade);

                CategoryDto currentCategoryDto = new CategoryDto
                {
                    Name            = category.Name,
                    MostPopularItem = mostPopularItem
                };

                resultCategoryDtos.Add(currentCategoryDto);
            }

            resultCategoryDtos = resultCategoryDtos.OrderByDescending(x => x.MostPopularItem.TotalMade)
                                 .ThenByDescending(x => x.MostPopularItem.TimesSold)
                                 .ToList();

            StringBuilder sb         = new StringBuilder();
            XmlSerializer serializer = new XmlSerializer(typeof(List <CategoryDto>), new XmlRootAttribute("Categories"));

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

            string serializedCategories = sb.ToString();

            return(serializedCategories);
        }
        public async Task <IHttpActionResult> PutAsync(int id, [FromBody] CategoryItemDto model, CancellationToken cancellationToken)
        {
            if (model == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }
            var category = new CategoryItem {
                Id = id, Name = model.Name, Color = model.Color
            };
            await _categoryManager.UpdateAsync(category, cancellationToken);

            return(new CategoryContentResult(category, this));
        }
        public async Task <IHttpActionResult> PostAsync([FromBody] CategoryItemDto model, CancellationToken cancellationToken)
        {
            if (model == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }
            ProjectItem project = null;

            if (model.ProjectId != null)
            {
                project = await _projectManager.FindByIdAsync((int)model.ProjectId, cancellationToken);

                if (project == null)
                {
                    return(BadRequest("The specified project was not found."));
                }
            }
            var category = new CategoryItem {
                Name = model.Name, Color = model.Color
            };
            await _categoryManager.CreateAsync(new AccountItem { Id = ApiSecurity.CurrentUserId }, project, category, cancellationToken);

            return(new CategoryContentResult(HttpStatusCode.Created, category, this));
        }