示例#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 <CommonMealRegistration> Update(CommonMealRegistration registration)
        {
            const string query =
                " UPDATE CommonMealRegistration " +
                " SET personId = @PersonId, attending = @Attending, guests = @Guests, date = @Date, takeaway = @TakeAway " +
                " WHERE Id = @Id ";

            using (var connection = _connectionFactory.New())
            {
                await connection.QueryAsync(query, new
                {
                    Id        = registration.Id,
                    PersonId  = registration.PersonId,
                    Attending = registration.Attending,
                    Date      = registration.Timestamp,
                    Guests    = MapToGuestString(registration.Guests),
                    TakeAway  = registration.IsTakeAway
                });

                return(registration);
            }
        }
示例#3
0
        public async Task <CommonMealRegistration> Add(CommonMealRegistration registration)
        {
            const string query =
                " INSERT INTO commonMealRegistration (personId, commonMealId, attending, guests, date, takeaway) " +
                " VALUES (@PersonId, @CommonMealId, @Attending, @Guests, @Date, @TakeAway) " +
                " RETURNING id ";

            using (var connection = _connectionFactory.New())
            {
                var id = await connection.QueryAsync <int>(query, new
                {
                    PersonId     = registration.PersonId,
                    Attending    = registration.Attending,
                    CommonMealId = registration.CommonMealId,
                    Date         = registration.Timestamp,
                    Guests       = MapToGuestString(registration.Guests),
                    TakeAway     = registration.IsTakeAway
                });

                registration.Id = id.Single();

                return(registration);
            }
        }