Example #1
0
        public async Task <IActionResult> CreateItem()
        {
            var command = new CreateItemCommand("Smykker fra Romerriget", 2500);
            var result  = await _mediator.Send(command);

            return(Ok(result));
        }
 public ItemsViewModel()
 {
     CreateItemCommand = new CreateItemCommand(this);
     RemoveItemCommand = new RemoveItemCommand(this);
     ItemsList         = System.Threading.Tasks.Task.Run(() => GetItems()).Result;
     NewItemName       = "";
 }
Example #3
0
        public async Task <IActionResult> CreateItemAsync([FromBody] ItemCreateViewModel item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            try
            {
                var itemModel = new ItemModel(Guid.NewGuid(), item.Name);
                var command   = new CreateItemCommand(itemModel, User.Identity.Name);
                var evt       = await commandHandler.HandleAsync(command);

                var getViewModel = new ItemViewModel
                {
                    ItemId    = itemModel.Id,
                    Name      = itemModel.Name,
                    Created   = evt.EventDate,
                    Updated   = evt.EventDate,
                    CreatedBy = evt.UserName,
                    UpdatedBy = evt.UserName
                };
                // Can't return Created with Location, because this service does not now where this item can be accessed :(
                return(Ok(getViewModel));
            }
            catch (ItemAlreadyExistsException e)
            {
                return(this.BadRequest(e.Message));
            }
        }
Example #4
0
        public override Guid Create(string itemName, Guid templateId, Guid parentId, string language)
        {
            var parentItem = base.Get(parentId);

            if (parentItem == null)
            {
                return(Guid.Empty);
            }
            var path = parentItem[ItemModel.ItemPath].ToString();
            //
            var itemModel = new ItemModel();

            itemModel[ItemModel.ItemName]   = itemName;
            itemModel[ItemModel.TemplateID] = templateId.ToString();
            var handler = base.HandlerProvider.GetHandler <CreateItemHandler>();
            var command = new CreateItemCommand
            {
                ItemModel = itemModel,
                Path      = path,
                Language  = language
            };
            var response = handler.Handle(command) as CreateItemResponse;


            return(response.ItemId);
        }
        public void Create(CreateItemCommand message)
        {
            var item = new T();

            item.Id = message.Id;
            _repository.Save(item, -1);
        }
Example #6
0
        public async Task <IActionResult> CreateItem(
            [FromBody] CreateItemCommand command,
            [FromServices] IDocumentStore martenStore,
            [FromServices] IMessageContext context)
        {
            var item = createItem(command);


            using (var session = martenStore.LightweightSession())
            {
                // Directs the message context to hold onto
                // outgoing messages, and persist them
                // as part of the given Marten document
                // session when it is committed
                await context.EnlistInTransaction(session);

                var outgoing = new ItemCreated {
                    Id = item.Id
                };
                await context.Send(outgoing);

                session.Store(item);

                await session.SaveChangesAsync();
            }

            return(Ok());
        }
        public async Task <IActionResult> CreateItem()
        {
            var newCommand = new CreateItemCommand("Bil", "Volkswagen", 3600000);
            await _commandBus.Send(newCommand);

            return(Ok());
        }
Example #8
0
        public async Task <ActionResult> CreateItemAsync(
            [FromBody] CreateItemCommand createItemCommand,
            CancellationToken cancellationToken)
        {
            await _itemHandler.CreateItemAsync(createItemCommand, cancellationToken);

            return(Ok());
        }
Example #9
0
        public void ShouldMap_CreateItemCommand_To_Item()
        {
            var entity = new CreateItemCommand();

            var result = this.mapper.Map <Item>(entity);

            result.Should().NotBeNull();
            result.Should().BeOfType <Item>();
        }
Example #10
0
        public async Task <IActionResult> CreateItem([FromBody] CreateItemContract createItemContract)
        {
            var model   = itemCreationConverter.ToDomain(createItemContract);
            var command = new CreateItemCommand(model);

            await commandDispatcher.DispatchAsync(command, default);

            return(Ok());
        }
Example #11
0
        public async Task <IActionResult> CreateService(int sid, CreateItemCommand command)
        {
            if (sid != command.ShopId)
            {
                return(BadRequest());
            }

            command.SetItemType <Service>();
            return(await Handle(command));
        }
Example #12
0
        public static ItemCreated Handle(
            CreateItemCommand command,
            IDocumentSession session)
        {
            var item = createItem(command);

            session.Store(item);
            return(new ItemCreated {
                Id = item.Id
            });
        }
Example #13
0
        public async Task <IActionResult> Post(CreateItemCommand createItemCommand)
        {
            if (createItemCommand == null)
            {
                return(BadRequest());
            }

            var result = await _mediator.Send(createItemCommand);

            return(CreatedAtAction(nameof(GetById), new { id = result.Id }, result));
        }
        /// <summary>
        /// Create a new entity
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task ExecuteAsync(CreateItemCommand command)
        {
            // map object
            Item entity = _mapper.Map <CreateItemCommand, Item>(command);

            // entity added
            _dbContext.Set <Item>().Add(entity);

            // commit changes
            await _dbContext.SaveChangesAsync();
        }
Example #15
0
        public async Task <ActionResult <ItemViewModel> > CreateItem([FromBody] CreateItemCommand newItem)
        {
            var result = await _Mediator.Send(newItem);

            var newItemViewModel = await _Mediator.Send(new GetItemByIdQuery()
            {
                Id = result
            });

            return(StatusCode(201, newItemViewModel));
        }
Example #16
0
        public async Task ShouldCreateItemReturnsOk(CreateItemCommand createItemCommand, int output)
        {
            var endpoint = new Create(this.repository);

            var result = await endpoint.HandleAsync(createItemCommand);

            Assert.NotNull(result);
            Assert.IsAssignableFrom <ActionResult <int> >(result);
            var model = Assert.IsType <OkObjectResult>(result.Result);

            Assert.Equal(output, model.Value);
        }
        public void ShouldReturnCreatedItem(CreateItemCommand sut, Item item, Item destination)
        {
            // arrange
            sut.DataStorage.GetSitecoreItem(item.ID).Returns(item);
            sut.Initialize(item.ID, item.Name, item.TemplateID, destination);

            // act
            var result = ReflectionUtil.CallMethod(sut, "DoExecute");

            // assert
            result.Should().Be(item);
        }
Example #18
0
        public void Create_a_valid_item()
        {
            var id   = Guid.NewGuid();
            var name = "Create a new task";

            var item    = new CQRS.Todo.Items.Domain.Item(id, name);
            var command = new CreateItemCommand(id, name);

            _handler.Handle(command);

            this.ShouldHaveSave(item);
        }
        public async Task Handle_Given_InvalidModel_Should__Throw_BadRequestException()
        {
            var command = new CreateItemCommand
            {
                Title         = DataConstants.SampleItemTitle,
                Description   = DataConstants.SampleItemDescription,
                StartingPrice = DataConstants.SampleItemStartingPrice,
                MinIncrease   = DataConstants.SampleItemMinIncrease
            };

            await Assert.ThrowsAsync <BadRequestException>(() => this.handler.Handle(command, CancellationToken.None));
        }
Example #20
0
        public CreateItemCommandHandlerTest()
        {
            this.mockUnitOfWork     = new Mock <IUnitOfWork>(MockBehavior.Strict);
            this.mockItemRepository = new Mock <IItemRepository>(MockBehavior.Strict);
            this.mockUserRepository = new Mock <IUserRepository>(MockBehavior.Strict);

            this.command = new CreateItemCommand("test_text", 10);

            this.handler = new CreateItemCommandHandler(
                mockUnitOfWork.Object,
                mockItemRepository.Object,
                mockUserRepository.Object);
        }
Example #21
0
 public HttpResponseMessage AddItem([FromBody] ItemDto itemDto)
 {
     // Call command create
     try
     {
         CreateItemCommand <ItemDto, Item> CreateItem = new CreateItemCommand <ItemDto, Item>(itemDto, itemToInsert, repository);
         result = CreateItem.Execute();
         return(Request.CreateResponse(HttpStatusCode.OK, result));
     }
     catch
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest, "Error"));
     }
 }
Example #22
0
        public async Task CreateItemAsync(CreateItemCommand createItemCommand, CancellationToken cancellationToken) // Stworznie nowego Itemu
        {
            var item = ItemFactory.Create(
                createItemCommand.Name,
                createItemCommand.Description,
                createItemCommand.CategoryId,
                createItemCommand.QualityLevel,
                createItemCommand.Quantity,
                createItemCommand.OwnerId);

            await _itemRepository.CreateAsync(item, cancellationToken);

            await _itemRepository.SaveAsync(cancellationToken);
        }
        public async Task <ItemCreatedEvent> Handle(CreateItemCommand command, NpgsqlTransaction tx)
        {
            var item = new Item {
                Name = command.Name
            };

            // persist the new Item with the
            // current transaction
            await persist(tx, item);

            return(new ItemCreatedEvent {
                Item = item
            });
        }
        public void ShouldAddFakeItem(CreateItemCommand sut, string name, ID templateId, Item destination, ID newId)
        {
            // arrange
            sut.Initialize(newId, name, templateId, destination);

            // act
            ReflectionUtil.CallMethod(sut, "DoExecute");

            // assert
            sut.DataStorage.Received().AddFakeItem(Arg.Is <DbItem>(i => i.Name == name &&
                                                                   i.ID == newId &&
                                                                   i.TemplateID == templateId &&
                                                                   i.ParentID == destination.ID));
        }
Example #25
0
        public CreateItem(string itemType, EntityConfig nodeEntityConfig, Type containerType = null)
        {
            _nodeEntityConfig = nodeEntityConfig;

            Action = (ecs, config) =>
            {
                var createItemCommand = new CreateItemCommand()
                {
                    Archetype     = itemType,
                    SystemId      = _nodeEntityConfig.EntityId,
                    ContainerType = containerType,
                };
                ecs.EnqueueCommand(createItemCommand);
            };
        }
Example #26
0
        public async Task <IActionResult> CreateItem([FromBody] CreateItemDto createItemDto)
        {
            CreateItemCommand command = new CreateItemCommand()
            {
                Name           = createItemDto.Name,
                Description    = createItemDto.Description,
                ItemTypeId     = createItemDto.ItemTypeId,
                PurchaseDate   = createItemDto.PurchaseDate,
                PurchasePrice  = createItemDto.PurchasePrice,
                ExpirationDate = createItemDto.ExpirationDate,
                LastUsed       = createItemDto.LastUsed
            };

            await Mediator.Send(command);

            return(Ok());
        }
        public async Task <ActionResult> Create([Bind("Name,Description")] ItemViewModel item)
        {
            if (!ModelState.IsValid)
            {
                return(View(item));
            }

            var command = new CreateItemCommand {
                Id = Guid.NewGuid(), Name = item.Name, Description = item.Description
            };

            await _messaging.SendCommand(command);

            TempData["message"] = "New item added";

            return(RedirectToRoute("ListItems", new { lastCommandId = command.CommandId }));
        }
        public async Task <ApiResponse <int> > CreateItem(ItemViewModel itemDetailViewModel)
        {
            try
            {
                CreateItemCommand createItemCommand = _mapper.Map <CreateItemCommand>(itemDetailViewModel);
                var reponse = await _client.AddItemAsync(createItemCommand);

                return(new ApiResponse <int>()
                {
                    Data = reponse.Item.ItemId, Success = true
                });
            }
            catch (ApiException ex)
            {
                return(ConvertApiExceptions <int>(ex));
            }
        }
Example #29
0
        public void CreateItemCallsDispatch()
        {
            var postItem = new ItemPostDto()
            {
                Description = _chance.Sentence(5),
                Upc         = TestUtils.CreateUpc()
            };

            var createItemCommand = new CreateItemCommand()
            {
                Id          = Guid.NewGuid(),
                Description = postItem.Description,
                Upc         = postItem.Upc
            };

            _itemCommandFactory.CreateInstance().Returns(createItemCommand);
            _itemService.CreateItemAsync(postItem);
            _commandDispatcher.Received(1).SendAsync(createItemCommand);
        }
        public async Task Handle_Given_ValidModel_Should_Not_ThrowException_AndReturnCorrectModel()
        {
            var command = new CreateItemCommand
            {
                Title         = DataConstants.SampleItemTitle,
                Description   = DataConstants.SampleItemDescription,
                StartingPrice = DataConstants.SampleItemStartingPrice,
                MinIncrease   = DataConstants.SampleItemMinIncrease,
                StartTime     = this.dateTime.UtcNow,
                EndTime       = DataConstants.SampleItemEndTime,
                SubCategoryId = DataConstants.SampleSubCategoryId
            };

            var result = await this.handler.Handle(command, CancellationToken.None);

            result
            .Data
            .Should()
            .BeAssignableTo <ItemResponseModel>();
        }