Exemplo n.º 1
0
        public async Task ShouldRespond422ForInexistentId()
        {
            var path        = "/item-groups/5";
            var jsonRequest = new SaveItemGroupJson().Build();
            var response    = await _client.PutJsonAsync(path, jsonRequest);

            var jsonResponse = await _client.ReadJsonAsync <UnprocessableEntityError>(response);

            Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
            Assert.Equal("ITEM_GROUP_NOT_FOUND", jsonResponse.Error);
        }
        public async Task <IActionResult> Create([FromBody] SaveItemGroupJson json)
        {
            var catalog = new ItemGroupCatalog(_dbContext, _messageBroker);

            var itemGroup = json.MapTo(new ItemGroup
            {
                TenantId = User.Claims.TenantId()
            });

            await catalog.Add(itemGroup);

            return(new ItemGroupJson(catalog.ItemGroup));
        }
Exemplo n.º 3
0
        public async Task ShouldCreateSuccessfully()
        {
            var path        = "/item-groups";
            var jsonRequest = new SaveItemGroupJson().Build();
            var response    = await _client.PostJsonAsync(path, jsonRequest);

            var jsonResponse = await _client.ReadJsonAsync <ItemGroupJson>(response);

            var itemGroup = await _server.Database.ItemGroups.SingleAsync();

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(_token.TenantId, itemGroup.TenantId);
            Assert.Equal(jsonRequest.Title, itemGroup.Title);
        }
Exemplo n.º 4
0
        public async Task ShouldPublishEventAfterCreateSuccessfully()
        {
            var path        = "/item-groups";
            var jsonRequest = new SaveItemGroupJson().Build();
            var response    = await _client.PostJsonAsync(path, jsonRequest);

            var itemGroup = await _server.Database.ItemGroups.SingleAsync();

            var publishedEvent = _server.EventBus.PublishedEvents
                                 .Single(@event => @event.Name == "menu.item-group.created");
            var payload = (ItemGroupPayload)publishedEvent.Payload;

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(payload.Id, itemGroup.Id);
            Assert.Equal(payload.TenantId, itemGroup.TenantId);
            Assert.Equal(payload.Title, itemGroup.Title);
        }
        public async Task <IActionResult> Update([FromRoute] long id, [FromBody] SaveItemGroupJson json)
        {
            var catalog  = new ItemGroupCatalog(_dbContext, _messageBroker);
            var tenantId = User.Claims.TenantId();

            await catalog.Find(tenantId, id);

            if (catalog.GroupNotExists)
            {
                return(new ItemGroupNotFoundError());
            }

            json.MapTo(catalog.ItemGroup);

            await catalog.Update();

            return(new ItemGroupJson(catalog.ItemGroup));
        }
Exemplo n.º 6
0
        public async Task ShouldUpdateSuccessfully()
        {
            var itemGroup = new ItemGroup().Of(_token.TenantId);

            _server.Database.ItemGroups.Add(itemGroup);
            await _server.Database.SaveChangesAsync();

            var path        = $"/item-groups/{itemGroup.Id}";
            var jsonRequest = new SaveItemGroupJson().Build();
            var response    = await _client.PutJsonAsync(path, jsonRequest);

            var jsonResponse = await _client.ReadJsonAsync <ItemGroupJson>(response);

            await _server.Database.Entry(itemGroup).ReloadAsync();

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(_token.TenantId, itemGroup.TenantId);
            Assert.Equal(jsonRequest.Title, itemGroup.Title);
        }
Exemplo n.º 7
0
        public async Task ShouldPublishEventAfterUpdateSuccessfully()
        {
            var itemGroup = new ItemGroup().Of(_token.TenantId);

            _server.Database.ItemGroups.Add(itemGroup);
            await _server.Database.SaveChangesAsync();

            var path        = $"/item-groups/{itemGroup.Id}";
            var jsonRequest = new SaveItemGroupJson().Build();
            var response    = await _client.PutJsonAsync(path, jsonRequest);

            var publishedEvent = _server.EventBus.PublishedEvents
                                 .Single(@event => @event.Name == "menu.item-group.updated");
            var payload = (ItemGroupPayload)publishedEvent.Payload;

            await _server.Database.Entry(itemGroup).ReloadAsync();

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(payload.Id, itemGroup.Id);
            Assert.Equal(payload.TenantId, itemGroup.TenantId);
            Assert.Equal(payload.Title, itemGroup.Title);
        }
Exemplo n.º 8
0
        public static SaveItemGroupJson Build(this SaveItemGroupJson json)
        {
            json.Title = ConstantFactory.Text();

            return(json);
        }