Ejemplo n.º 1
0
        public async Task <IActionResult> Create(WordDefinitionCreate request)
        {
            var created = await Mediator.Send(request with
            {
                UserId = RequestUserId,
            });

            await SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = created.Id }, created));
        }
Ejemplo n.º 2
0
        public async Task CRUD_Succeeds()
        {
            var creationRequest = new WordDefinitionCreate
            {
                Word         = Word,
                LanguageCode = "en",
                Meaning      = "Hello",
                Public       = false,
            };

            var creationResult =
                (await WordDefinitionsController.Create(creationRequest))
                .ShouldBeOfType <CreatedAtActionResult>();
            var createdId = creationResult.Value.ShouldBeOfType <WordDefinition>().Id;

            (await List()).Items.Count.ShouldBe(1);

            using (User(2))
            {
                (await List()).Items.Count.ShouldBe(0);
                (await WordDefinitionsController.Get(createdId, new WordDefinitionGet {
                })).ShouldBeOfType <NotFoundResult>();
            }

            await WordDefinitionsController.Update(createdId, new WordDefinitionUpdate { Public = true });

            using (User(2))
            {
                var created = await Get(createdId);

                created.Id.ShouldBe(createdId);
                created.LanguageCode.ShouldBe(creationRequest.LanguageCode);
                created.Meaning.ShouldBe(creationRequest.Meaning);

                (await WordDefinitionsController.Update(createdId, new WordDefinitionUpdate
                {
                    LanguageCode = "en",
                    Meaning = "Another way to say hello",
                })).ShouldBeOfType <NotFoundResult>();
            }

            var updateRequest = new WordDefinitionUpdate
            {
                LanguageCode = "sv",
                Meaning      = "ett hälsningsord",
            };
            await WordDefinitionsController.Update(createdId, updateRequest);

            var updated = await Get(createdId);

            updated.LanguageCode.ShouldBe(updateRequest.LanguageCode);
            updated.Meaning.ShouldBe(updateRequest.Meaning);

            using (User(2))
            {
                (await WordDefinitionsController.Delete(createdId, new WordDefinitionDelete {
                })).ShouldBeOfType <NotFoundResult>();
            }
            (await List()).Items.Count.ShouldBe(1);

            (await WordDefinitionsController.Delete(createdId, new WordDefinitionDelete {
            })).ShouldBeOfType <NoContentResult>();
            (await List()).Items.Count.ShouldBe(0);
            (await WordDefinitionsController.Get(createdId, new WordDefinitionGet {
            })).ShouldBeOfType <NotFoundResult>();
        }