public ActionResult <DTO.Brewery> Get(long id)
        {
            var result = _breweryConductor.Find(id);

            if (result.HasErrors || result.ResultObject is null)
            {
                return(NotFound());
            }

            return(_mapper.Map <DTO.Brewery>(result));
        }
Ejemplo n.º 2
0
        public async Task <ReviewPayload> CreateReview(
            ReviewInput input,
            [Service] BreweryDbContext dbContext,
            [Service] IBreweryConductor breweryConductor,
            [Service] ITopicEventSender eventSender,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(input.Subject))
            {
                throw ErrorBuilder.New()
                      .AsGraphQLException(SUBJECT_EMPTY, nameof(SUBJECT_EMPTY));
            }

            if (string.IsNullOrEmpty(input.Body))
            {
                throw ErrorBuilder.New()
                      .AsGraphQLException(BODY_EMPTY, nameof(BODY_EMPTY));
            }

            var review = new Review
            {
                Subject   = input.Subject,
                Body      = input.Body,
                BreweryId = input.BreweryId,
                CreatedOn = DateTimeOffset.Now,
            };

            _ = await dbContext.AddAsync(review);

            _ = await dbContext.SaveChangesAsync(cancellationToken);

            var breweryResult = breweryConductor.Find(input.BreweryId);

            if (breweryResult.HasErrorsOrResultIsNull())
            {
                throw breweryResult.Errors
                      .AsGraphQLException();
            }

            review.Brewery = breweryResult.ResultObject;

            await eventSender.SendAsync(
                nameof(ReviewSubscriptions.OnReviewReceived),
                review,
                cancellationToken);

            return(new ReviewPayload(review, input.ClientMutationId));
        }