class CountPlansTendersAwardsController extends GenericOCDSController {

    @ApiOperation(value = "Count the tenders and group the results by year. The year is calculated from "
            + "tender.tenderPeriod.startDate.")
    @RequestMapping(value = "/api/countTendersByYear",
            method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json")
    public List<Document> countTendersByYear(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

        DBObject project = new BasicDBObject();
        addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));

        Aggregation agg = Aggregation.newAggregation(match(
                where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).
                        andOperator(getYearDefaultFilterCriteria(filter,
                                MongoConstants.FieldNames.TENDER_PERIOD_START_DATE
                        ))),
                new CustomOperation(new Document("$project", project)),
                group(getYearlyMonthlyGroupingFields(filter)).count().as(Keys.COUNT),
                transformYearlyGrouping(filter).andInclude(Keys.COUNT),
                getSortByYearMonth(filter),
                skip(filter.getSkip()), limit(filter.getPageSize())
        );

        return releaseAgg(agg);
    }

}