public static async Task <HttpResponseMessage> Run
        (
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "heroes")] CreateHeroCommand command,
            HttpRequestMessage request,
            [Inject] IHeroService heroService,
            ILogger logger
        )
        {
            try
            {
                Hero hero = await heroService.Create(command);

                return(request.CreateResponse(HttpStatusCode.Created, hero));
            }
            catch (Exception ex) when(ex is ArgumentNullException || ex is FailedValidationException)
            {
                return(request.CreateResponse(HttpStatusCode.BadRequest));
            }
            catch (DuplicateException)
            {
                return(request.CreateResponse(HttpStatusCode.Conflict));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                throw;
            }
        }
コード例 #2
0
        [HttpPost] // POST: heroes
        public async Task <IActionResult> Post([FromBody] HeroDto newHero)
        {
            var query = new CreateHeroCommand()
            {
                NewHero = newHero
            };
            var result = await _mediator.Send(query);

            return(Accepted(result));
        }
コード例 #3
0
        public async Task <IActionResult> Post([FromBody] CreateHeroCommand command)
        {
            try
            {
                Hero hero = await _heroService.Create(command);

                return(CreatedAtAction(nameof(Get), new { id = hero.Id }, hero));
            }
            catch (Exception exception) when(exception is ArgumentException || exception is FailedValidationException)
            {
                return(BadRequest());
            }
            catch (DuplicateDocumentException)
            {
                return(StatusCode((int)HttpStatusCode.Conflict));
            }
        }
コード例 #4
0
        public async Task <Hero> Create(CreateHeroCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (!command.IsValid())
            {
                throw new FailedValidationException();
            }

            Hero hero = new Hero(string.Empty, command.Name);

            if (await _heroRepository.IsDuplicate(hero))
            {
                throw new DuplicateDocumentException();
            }

            return(await _heroRepository.Create(hero));
        }