コード例 #1
0
        public async Task <CommonMealRegistration> UpdateRegistration(CommonMealRegistration registration)
        {
            var status = await _mealRepository.GetStatus(registration.CommonMealId);

            // Prevent registration if meal is closed (the GUI tries to prevent this - but might fail)
            if (status != "OPEN")
            {
                throw new AppException(AppErrorCodes.MealIsClosed, $"The meal with id '{registration.CommonMealId}' is not open for registration updates");
            }

            // Prevent registration if limit on meal size
            var maxPeopleStandard = _commonMealSettings.GetMaxPeople("STANDARD");
            var maxTakeAway       = _commonMealSettings.GetMaxPeople("TAKEAWAY");

            if (registration.Attending && (maxPeopleStandard > 0 || maxTakeAway > 0))
            {
                var registrations = await _registrationRepository.GetByCommonMealId(registration.CommonMealId);

                var othersAttendingStandard = registrations.Count(x => x.Attending && x.PersonId != registration.PersonId && !x.IsTakeAway);
                var othersAttendingTakeAway = registrations.Count(x => x.Attending && x.PersonId != registration.PersonId && x.IsTakeAway);

                if (!registration.IsTakeAway && othersAttendingStandard >= maxPeopleStandard)
                {
                    throw new AppException(AppErrorCodes.MealIsFull, $"The meal is full - maybe sign up for TAKE-AWAY instead?");
                }

                if (registration.IsTakeAway && othersAttendingTakeAway >= maxTakeAway)
                {
                    throw new AppException(AppErrorCodes.TakeAwayMealIsFull, $"The meal is full - maybe sign up for STANDARD instead?");
                }
            }

            registration.Timestamp = _timeProvider.Now();
            return(await _registrationRepository.Update(registration));
        }
コード例 #2
0
        public async Task <IImmutableList <CommonMeal> > GetByDateRange(DateTime dateFrom, DateTime dateTo)
        {
            const string query = " SELECT Id As Id, Date AS Date, Note As Note, Status As Status " +
                                 " FROM CommonMeal " +
                                 " WHERE Date >= @DateFrom AND Date <= @DateTo ";

            using (var connection = _connectionFactory.New())
            {
                var commonMeals = (await connection.QueryAsync <CommonMeal>(query, new { DateFrom = dateFrom, DateTo = dateTo })).ToImmutableList();

                foreach (var commonMeal in commonMeals)
                {
                    commonMeal.Registrations = await _commonMealRegistrationRepository.GetByCommonMealId(commonMeal.Id);

                    commonMeal.Chefs = await _commonMealChefRepository.GetByCommonMealId(commonMeal.Id);
                }

                return(commonMeals);
            }
        }