/// <summary>
        /// Handles the request
        /// </summary>
        /// <param name="request">A <see cref="DeleteEventSeriesesCommand"></see> request object.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"></see></param>
        /// <returns>A <see cref="Unit"></see> containing the results of the operation.</returns>
        /// <exception cref="NotFoundException">Thrown when no <see cref="EventSeries"></see> with the request Id parameter was found.</exception>
        /// <exception cref="DeleteFailureException">Thrown when the <see cref="EventSeries"></see> being deleted has <see cref="Event"></see>s.</exception>
        public async Task <Unit> Handle(DeleteEventSeriesesCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.EventSeries.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(EventSeries), request.Id);
            }
            var hasEvents = _context.Events.Any(x => x.EventSeriesId == request.Id);

            if (hasEvents)
            {
                throw new DeleteFailureException(nameof(EventSeries), request.Id, "There are Events associated with this Event Series.");
            }
            _context.EventSeries.Remove(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        /// <summary>
        /// Handles the request to insert or update the <see cref="EventSeries"></see>
        /// </summary>
        /// <param name="request">The command</param>
        /// <param name="cancellationToken">The cancellationToken</param>
        /// <returns>A <see cref="Task"> containing the Integer Id of the upserted entity.</see></returns>
        /// <exception cref="ValidationException">Throw when the "Title" property of the request parameter is in use by another <see cref="EventSeries"></see></exception>
        /// <exception cref="NotFoundException">Throw when the "Id" property of the request parameter is present, but does not match the Id of any existing <see cref="EventSeries"></see></exception>
        public async Task <int> Handle(UpsertEventSeriesesCommand request, CancellationToken cancellationToken)
        {
            EventSeries entity;

            if (request.Id.HasValue)
            {
                // we are updating an existing Event Series
                var titleIsTaken = _context.EventSeries.Any(x => x.Title == request.Title && x.Id != request.Id);
                if (titleIsTaken)
                {
                    throw new ValidationException(new List <ValidationFailure>()
                    {
                        new ValidationFailure(nameof(EventSeries.Title), $"The title \"{request.Title}\" is already in use.")
                    });
                }
                entity = await _context.EventSeries.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(EventSeries), request.Id);
                }
                entity.UpdateTitle(request.Title);
                entity.UpdateDescription(request.Description);
            }
            else
            {
                // we are creating a new Event Series
                var titleIsTaken = _context.EventSeries.Any(x => x.Title == request.Title);
                if (titleIsTaken)
                {
                    throw new ValidationException(new List <ValidationFailure>()
                    {
                        new ValidationFailure(nameof(EventSeries.Title), $"The title \"{request.Title}\" is already in use.")
                    });
                }
                entity = new EventSeries(request.Title, request.Description);
                await _context.EventSeries.AddAsync(entity);
            }
            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
        /// <summary>
        /// Handles the request
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"></see></param>
        /// <returns>A <see cref="Unit"></see> containing the results of the operation.</returns>
        /// <exception cref="NotFoundException">Thrown when no <see cref="Rank"></see> with the request Id parameter was found.</exception>
        /// <exception cref="DeleteFailureException">Thrown when the <see cref="Rank"></see> being deleted has <see cref="User"></see>s.</exception>
        public async Task <Unit> Handle(DeleteRankCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Ranks
                         .FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Rank), request.Id);
            }
            var hasUsers = _context.Users.Any(x => x.RankId == request.Id);

            if (hasUsers)
            {
                throw new DeleteFailureException(nameof(Rank), request.Id, "There are existing Users with this Rank.");
            }
            _context.Ranks.Remove(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #4
0
        /// <summary>
        /// Handles the request to insert the <see cref="Event"></see>
        /// </summary>
        /// <param name="request">The command</param>
        /// <param name="cancellationToken">The cancellationToken</param>
        /// <returns>A <see cref="Task"> containing the Integer Id of the newly inserted entity.</see></returns>
        /// <exception cref="ValidationException">
        /// Throw when:
        /// <list type="bullet">
        /// <item><description>the EventTypeId property of the request parameter does not match any existing <see cref="EventType"></see></description></item>
        /// <item><description>the EventSeriesId property of the request parameter is present but does not match any existing <see cref="EventSeries"></see></description></item>
        /// <item><description>one of the Dates used to construct the <see cref="EventDates"></see> value object caused an error in that object's constructor.</description></item>
        /// <item><description>an error was thrown in the constructor of the <see cref="EventRegistrationRules"></see> value object, likely because of a bad registration count value.</description></item>
        /// <item><description>an error was thrown in the constructor of the <see cref="Event"></see> entity object, likely because of a bad parameter that was not caught by validation.</description></item>
        /// </list>
        /// </exception>
        public async Task <int> Handle(CreateEventCommand request, CancellationToken cancellationToken)
        {
            // Attempt to find the Event Type that matches request.EventTypeId
            var type = _context.EventTypes.Find(request.EventTypeId);

            if (type == null)
            {
                // throw if no EventType was found
                throw new ValidationException(new List <ValidationFailure>()
                {
                    new ValidationFailure(nameof(Event.EventTypeId), $"No Event Type with Id: \"{request.EventTypeId}\" was found.")
                });
            }
            // Attempt to find the Event Series that matches request.EventSeriesId
            EventSeries series = null;

            if (request.EventSeriesId.HasValue) // request.EventSeriesId is an optional parameter, so check if it is present
            {
                // Attempt to locate the matching series
                series = _context.EventSeries.Find(request.EventSeriesId);
                if (series == null)
                {
                    // throw if no matching series was found
                    throw new ValidationException(new List <ValidationFailure>()
                    {
                        new ValidationFailure(nameof(Event.EventSeriesId), $"No Event Series with Id: \"{request.EventSeriesId}\" was found.")
                    });
                }
            }
            try
            {
                Event entity = new Event(
                    request.Title,
                    request.Description,
                    request.EventTypeId,
                    request.EventSeriesId,
                    request.StartDate,
                    request.EndDate,
                    request.RegStartDate,
                    request.RegEndDate,
                    request.MaxRegsCount,
                    request.MinRegsCount,
                    request.MaxStandbyCount,
                    request.Street,
                    request.Suite,
                    request.City,
                    request.State,
                    request.Zip
                    );
                await _context.Events.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
            catch (Exception e)
            {
                // throw if the Event constructor threw an error, which is likely because a bad parameter made it through validation
                throw new ValidationException(new List <ValidationFailure>()
                {
                    new ValidationFailure(nameof(Event), e.Message)
                });
            }
        }