示例#1
0
        public ActionResult <GetCategoryReportResponse> GetCategoryById(Guid id, int?year, [Range(1, 12)] int?month)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var now = DateTime.Now;

            if (!month.HasValue)
            {
                month = now.Month;
            }

            if (!year.HasValue)
            {
                year = now.Year;
            }

            IEnumerable <BookingEntity> bookings;
            var report = context.CategoryReports.Include(r => r.Category)
                         .ThenInclude(c => c.Children).Include(r => r.Category)
                         .ThenInclude(c => c.Parent)
                         .SingleOrDefault(r => r.CategoryId == id && r.Month == month.Value && r.Year == year.Value);

            if (report == null)
            {
                var category = context.Categories.Find(id);
                if (category == null)
                {
                    return(NotFound());
                }

                report = new CategoryReportEntity()
                {
                    Month    = month.Value,
                    Year     = year.Value,
                    Category = category
                };
                bookings = new List <BookingEntity>();
            }
            else
            {
                bookings = context.Bookings.Include(b => b.Category).ThenInclude(c => c.Parent).Where(b => b.CategoryId == report.CategoryId || report.Category.Children.Select(c => c.Id).Any(c => c == b.CategoryId));
            }

            var reportResponse = mapper.Map <GetCategoryReportResponse>(report);

            reportResponse.Bookings          = bookings.Select(mapper.Map <GetCategoryReportResponse.GetBookingResponse>);
            reportResponse.Category.Children = reportResponse.Category.Children.Select(mapper.Map <GetMinCategoryResponse>);
            return(Ok(reportResponse));
        }
示例#2
0
        public ActionResult <GetCategoryReportsResponse> GetCategoryReports(int?year, [Range(1, 12)] int?month)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var categories     = context.Categories.Where(c => !c.ParentId.HasValue);
            var reportEntities = new List <CategoryReportEntity>();
            var now            = DateTime.Now;

            if (!month.HasValue)
            {
                month = now.Month;
            }

            if (!year.HasValue)
            {
                year = now.Year;
            }
            foreach (var category in categories)
            {
                var report = context.CategoryReports.Include(r => r.Category).SingleOrDefault(r => r.CategoryId == category.Id && r.Month == month.Value && r.Year == year.Value);
                if (report == null)
                {
                    report = new CategoryReportEntity()
                    {
                        Category = category, CategoryId = category.Id, Month = month.Value, Year = year.Value, Spent = 0M
                    };
                }
                reportEntities.Add(report);
            }
            var reports = reportEntities.Select(r => mapper.Map <GetCategoryReportsResponse.GetMinCategoryReportRepsponse>(r));

            var response = new GetCategoryReportsResponse()
            {
                Month = month.Value, Year = year.Value, Reports = reports
            };

            return(Ok(response));
        }
        private BookingEntity UpdateReports(PutBookingRequest booking)
        {
            var year  = booking.TimeStamp.Year;
            var month = booking.TimeStamp.Month;

            var categoryId  = booking.CategoryId;
            var subCategory = context.Categories.SingleOrDefault(c => c.ParentId == categoryId && EF.Functions.Like(c.Name, $"%{booking.SubCategoryName}%"));
            CategoryReportEntity subCategoryReport = null;

            if (subCategory == null)
            {
                subCategory = new CategoryEntity()
                {
                    Name = booking.SubCategoryName, ParentId = booking.CategoryId
                };
            }
            else
            {
                subCategoryReport = context.CategoryReports.Find(subCategory.Id, year, month);
            }


            var createSubCategoryReport = subCategoryReport == null;

            if (createSubCategoryReport)
            {
                subCategoryReport = new CategoryReportEntity {
                    Category = subCategory, Year = year, Month = month
                }
            }
            ;

            subCategoryReport.Spent += (decimal)booking.Amount;


            if (createSubCategoryReport)
            {
                context.CategoryReports.Add(subCategoryReport);
            }
            else
            {
                context.Update(subCategoryReport);
            }

            var categoryReport = context.CategoryReports
                                 .Find(categoryId, year, month);
            var create = categoryReport == null;

            if (create)
            {
                categoryReport = new CategoryReportEntity()
                {
                    CategoryId = categoryId, Year = year, Month = month
                };
            }
            categoryReport.Spent += (decimal)booking.Amount;
            if (create)
            {
                context.CategoryReports.Add(categoryReport);
            }
            else
            {
                context.Update(categoryReport);
            }

            var bookingEntity = mapper.Map <BookingEntity>(booking);

            bookingEntity.CategoryId = subCategory.Id;
            context.Bookings.Add(bookingEntity);

            context.SaveChanges();

            return(bookingEntity);
        }