public IActionResult CreateAuthor([FromBody] CreateAuthor author, [FromHeader(Name = Headers.Accept)] string mediaType)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var newAuthor = Mapper.Map <Entities.Author>(author);

            _repo.AddAuthor(newAuthor);
            if (!_repo.Save())
            {
                throw new Exception("Failed to Create new author");
            }
            var createdAuthor = Mapper.Map <Author>(newAuthor);

            var currentMediaType = new MediaType(mediaType);

            var includeLinks = currentMediaType.IsSubsetOf(VendorMediaType.HateoasLinksMediaType);

            if (includeLinks)
            {
                var links = CreateLinks(createdAuthor.Id, null);

                var linkedResourceToReturn = createdAuthor.ShapeData(null) as IDictionary <string, object>;
                linkedResourceToReturn.Add("links", links);


                return(CreatedAtRoute(nameof(GetAuthor), new { id = linkedResourceToReturn[nameof(Author.Id)] }, linkedResourceToReturn));
            }
            return(CreatedAtRoute(nameof(GetAuthor), new { id = createdAuthor.Id }, createdAuthor));
        }
        public async Task post_should_return_created_for_valid_input()
        {
            var login = new Login
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            var loginCredentials = GetPayload(login);
            var loginResponse    = await Client.PostAsync("user/login", loginCredentials);

            var loginContent = await loginResponse.Content.ReadAsStringAsync();

            var tokenDto = JsonConvert.DeserializeObject <TokenDto>(loginContent);

            Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenDto.Token);

            var commandCreateAuthor = new CreateAuthor
            {
                FirstName     = "Jeffrey",
                LastName      = "Richter",
                Description   = "Jeffrey Richter is a Software Architect on Microsoft's Azure team.",
                ImageUrl      = "https://www.stolarstate.pl/avatar/author/default.png",
                DateOfBirth   = new DateTime(1955, 12, 12),
                DateOfDeath   = null,
                BirthPlace    = "Chicago",
                AuthorWebsite = "https://csharpindepth.com",
                AuthorSource  = "https://csharpindepth.com"
            };

            var command      = GetPayload(commandCreateAuthor);
            var postResponse = await Client.PostAsync("author", command);

            postResponse.StatusCode.Should().BeEquivalentTo(HttpStatusCode.Created);
        }
        public async Task <IActionResult> Post([FromBody] CreateAuthor author)
        {
            var authorId = Guid.NewGuid();
            await _authorService.RegisterAsync(authorId, author.FirstName, author.LastName, author.Description, author.ImageUrl,
                                               author.DateOfBirth, author.DateOfDeath, author.BirthPlace, author.AuthorWebsite, author.AuthorSource, UserId);

            return(CreatedAtRoute("Get", new { id = authorId }, author));
        }
        public async Task post_without_authorization_should_return_unauthorized()
        {
            var commandCreateAuthor = new CreateAuthor
            {
                FirstName     = "Jeffrey",
                LastName      = "Richter",
                Description   = "Jeffrey Richter is a Software Architect on Microsoft's Azure team.",
                ImageUrl      = "https://www.stolarstate.pl/avatar/author/default.png",
                DateOfBirth   = new DateTime(1955, 12, 12),
                DateOfDeath   = null,
                BirthPlace    = "Chicago",
                AuthorWebsite = "https://csharpindepth.com",
                AuthorSource  = "https://csharpindepth.com"
            };

            var command      = GetPayload(commandCreateAuthor);
            var postResponse = await Client.PostAsync("author", command);

            postResponse.StatusCode.Should().BeEquivalentTo(HttpStatusCode.Unauthorized);
        }
        public async Task <ActionResult> Post([FromBody] CreateAuthor command)
        {
            await CommandDispatcher.DispatchAsync(command);

            return(Created("users", new object()));
        }
Beispiel #6
0
 public AuthorsController(GetAuthors getAuthors, CreateAuthor createAuthor, Dispatcher dispatcher)
 {
     this.getAuthors   = getAuthors;
     this.createAuthor = createAuthor;
     this.dispatcher   = dispatcher;
 }
        public async Task <IActionResult> Post(CreateAuthor command)
        {
            await Dispatcher.SendAsync(command);

            return(CreatedAtAction(nameof(Get), new { Id = command.Id }, command.Id));
        }
 public async Task <object> CreateAuthor([Service] Context context, [GraphQLNonNullType] CreateAuthor author)
 {
     return(await context.AddAuthorAsync(new AuthorDto { Name = author.Name }).ContinueWith(task => task.MapAsync(authorId =>
                                                                                                                  context.GetAuthorAsync(authorId)), TaskContinuationOptions.OnlyOnRanToCompletion)
            .Flatten().ContinueWith(t => t.Result.Flatten().Unwrap()));
 }