public async Task ShouldPublishEventAfterCreateSuccessfully()
        {
            var itemGroup = new ItemGroup().Of(_token.TenantId);

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

            var path        = "/items";
            var jsonRequest = new SaveItemJson().Build(groupId: itemGroup.Id);
            var response    = await _client.PostJsonAsync(path, jsonRequest);

            var item = await _server.Database.Items.SingleAsync();

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

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(payload.Id, item.Id);
            Assert.Equal(payload.TenantId, item.TenantId);
            Assert.Equal(payload.Name, item.Name);
            Assert.Equal(payload.Description, item.Description);
            Assert.Equal(payload.Price, item.Price);
            Assert.Equal(payload.IsAvailable, item.IsAvailable);
        }
        public async Task ShouldCreateSuccessfully()
        {
            var itemGroup = new ItemGroup().Of(_token.TenantId);

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

            var path        = "/items";
            var jsonRequest = new SaveItemJson().Build(groupId: itemGroup.Id);
            var response    = await _client.PostJsonAsync(path, jsonRequest);

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

            var item = await _server.Database.Items.SingleAsync();

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(_token.TenantId, item.TenantId);
            Assert.Equal(jsonRequest.Name, item.Name);
            Assert.Equal(jsonRequest.Description, item.Description);
            Assert.Equal(jsonRequest.Price, item.Price);
            Assert.Equal(jsonRequest.IsAvailable, item.IsAvailable);
            Assert.Equal(item.Id, jsonResponse.Id);
            Assert.Equal(item.Name, jsonResponse.Name);
            Assert.Equal(item.Description, jsonResponse.Description);
            Assert.Equal(item.Price, jsonResponse.Price);
            Assert.Equal(item.IsAvailable, jsonResponse.Available);
        }
Ejemplo n.º 3
0
        public async Task ShouldUpdateSuccessfully()
        {
            var itemGroup1 = new ItemGroup().Of(_token.TenantId);
            var itemGroup2 = new ItemGroup().Of(_token.TenantId);
            var item       = new Item().To(itemGroup1);

            _server.Database.ItemGroups.AddRange(itemGroup1, itemGroup2);
            _server.Database.Items.Add(item);

            await _server.Database.SaveChangesAsync();

            var path        = $"/items/{item.Id}";
            var jsonRequest = new SaveItemJson().Build(groupId: itemGroup2.Id);
            var response    = await _client.PutJsonAsync(path, jsonRequest);

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

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

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(_token.TenantId, item.TenantId);
            Assert.Equal(jsonRequest.GroupId, item.ItemGroupId);
            Assert.Equal(jsonRequest.Name, item.Name);
            Assert.Equal(jsonRequest.Description, item.Description);
            Assert.Equal(jsonRequest.Price, item.Price);
            Assert.Equal(jsonRequest.IsAvailable, item.IsAvailable);
            Assert.Equal(item.Id, jsonResponse.Id);
            Assert.Equal(item.ItemGroupId, itemGroup2.Id);
            Assert.Equal(item.Name, jsonResponse.Name);
            Assert.Equal(item.Description, jsonResponse.Description);
            Assert.Equal(item.Price, jsonResponse.Price);
            Assert.Equal(item.IsAvailable, jsonResponse.Available);
        }
        public static SaveItemJson Build(this SaveItemJson json, long groupId)
        {
            json.GroupId     = groupId;
            json.Name        = ConstantFactory.Text();
            json.Description = ConstantFactory.Text();
            json.Price       = 5;
            json.IsAvailable = true;

            return(json);
        }
        public async Task ShouldRespond422ForInexistentGroupId()
        {
            var itemGroup = new ItemGroup().Of(_token.TenantId);

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

            var path        = "/items";
            var jsonRequest = new SaveItemJson().Build(groupId: 90);
            var response    = await _client.PostJsonAsync(path, jsonRequest);

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

            Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
            Assert.Equal("ITEM_GROUP_NOT_FOUND", jsonResponse.Error);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create([FromBody] SaveItemJson json)
        {
            var catalog = new ItemCatalog(_dbContext, _messageBroker);

            var item = json.MapTo(new Item
            {
                TenantId = User.Claims.TenantId()
            });

            await catalog.Add(item);

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

            return(new ItemJson(catalog.Item));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Update([FromRoute] long id,
                                                 [FromBody] SaveItemJson json)
        {
            var catalog  = new ItemCatalog(_dbContext, _messageBroker);
            var tenantId = User.Claims.TenantId();

            await catalog.Find(tenantId, id);

            if (catalog.ItemNotExists)
            {
                return(new ItemNotFoundError());
            }

            json.MapTo(catalog.Item);

            await catalog.Update();

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

            return(new ItemJson(catalog.Item));
        }