コード例 #1
0
        public async Task Post_given_valid_dto_returns_CreatedAtAction()
        {
            var dto  = new EventStockDTO();
            var repo = new Mock <IEventStockRepository>();

            repo.Setup(s => s.CreateAsync(It.IsAny <EventStockDTO>())).ReturnsAsync(1);
            var controller = new EventStockController(repo.Object);

            var post = await controller.Post(dto);

            Assert.IsType <CreatedAtActionResult>(post.Result);
        }
コード例 #2
0
        public async Task Put_given_repository_returns_false_returns_NotFound()
        {
            var repository = new Mock <IEventStockRepository>();

            var controller = new EventStockController(repository.Object);

            var dto = new EventStockDTO();

            var put = await controller.Put(42, dto);

            Assert.IsType <NotFoundResult>(put);
        }
コード例 #3
0
        public async Task Get_existing_id_returns_dto()
        {
            var dto  = new EventStockDTO();
            var repo = new Mock <IEventStockRepository>();

            repo.Setup(s => s.FindAsync(42)).ReturnsAsync(dto);
            var controller = new EventStockController(repo.Object);

            var get = await controller.Get(42);

            Assert.Equal(dto, get.Value);
        }
コード例 #4
0
        public async Task Put_given_dto_updates_eventStock()
        {
            var repository = new Mock <IEventStockRepository>();

            var controller = new EventStockController(repository.Object);

            var dto = new EventStockDTO();

            await controller.Put(42, dto);

            repository.Verify(s => s.UpdateAsync(dto));
        }
コード例 #5
0
        public async Task Put_returns_NoContent()
        {
            var dto        = new EventStockDTO();
            var repository = new Mock <IEventStockRepository>();

            repository.Setup(s => s.UpdateAsync(dto)).ReturnsAsync(true);
            var controller = new EventStockController(repository.Object);

            var put = await controller.Put(42, dto);

            Assert.IsType <NoContentResult>(put);
        }
コード例 #6
0
        public async Task <ActionResult> Put(int id, [FromBody] EventStockDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var updated = await _repo.UpdateAsync(dto);

            if (updated)
            {
                return(NoContent());
            }
            return(NotFound());
        }
コード例 #7
0
        public async Task <ActionResult <EventStockDTO> > Post([FromBody] EventStockDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (dto == null)
            {
                return(BadRequest());
            }

            var create = await _repo.CreateAsync(dto);

            return(CreatedAtAction(nameof(Get), create));
        }
コード例 #8
0
        public async Task <int> CreateAsync(EventStockDTO eventStock)
        {
            var eventStockEntity = new EventStock
            {
                EventId  = eventStock.EventId,
                PersonId = eventStock.PersonId,
                WineId   = eventStock.WineId
            };

            await _context.EventStocks.AddAsync(eventStockEntity);

            await _context.SaveChangesAsync();

            return(eventStockEntity.Id);
        }
コード例 #9
0
        public async Task UpdateAsync_given_non_existing_eventStock_returns_false()
        {
            using (var connection = await CreateConnectionAsync())
                using (var context = await CreateContextAsync(connection))
                {
                    var repository = new EventStockRepository(context);

                    var eventStock = new EventStockDTO {
                        Id = 42
                    };

                    var updated = await repository.UpdateAsync(eventStock);

                    Assert.False(updated);
                }
        }
コード例 #10
0
        public async Task <bool> UpdateAsync(EventStockDTO eventStock)
        {
            var eventStockEntity = await _context.EventStocks.FindAsync(eventStock.Id);

            if (eventStockEntity == null)
            {
                return(false);
            }

            eventStockEntity.EventId  = eventStock.EventId;
            eventStockEntity.PersonId = eventStock.PersonId;
            eventStockEntity.WineId   = eventStock.WineId;

            await _context.SaveChangesAsync();

            return(true);
        }
コード例 #11
0
        public async Task UpdateAsync_given_eventStock_updates_it()
        {
            using (var connection = await CreateConnectionAsync())
                using (var context = await CreateContextAsync(connection))
                {
                    var entity = new EventStock
                    {
                        EventId  = 1,
                        PersonId = 1,
                        WineId   = 1
                    };

                    context.EventStocks.Add(entity);
                    await context.SaveChangesAsync();

                    var id = entity.Id;

                    var repository = new EventStockRepository(context);

                    var eventStock = new EventStockDTO
                    {
                        Id       = id,
                        EventId  = 2,
                        PersonId = 3,
                        WineId   = 4
                    };

                    var updated = await repository.UpdateAsync(eventStock);

                    Assert.True(updated);

                    var updatedEntity = await context.EventStocks.FindAsync(id);

                    Assert.Equal(2, updatedEntity.EventId);
                    Assert.Equal(3, updatedEntity.PersonId);
                    Assert.Equal(4, updatedEntity.WineId);
                }
        }
コード例 #12
0
        public async Task CreateAsync_given_eventStock_creates()
        {
            using (var connection = await CreateConnectionAsync())
                using (var context = await CreateContextAsync(connection))
                {
                    var repository = new EventStockRepository(context);

                    var eventStock = new EventStockDTO
                    {
                        EventId  = 1,
                        PersonId = 1,
                        WineId   = 1
                    };

                    var created = await repository.CreateAsync(eventStock);

                    var entity = await context.EventStocks.FindAsync(created);

                    Assert.Equal(1, entity.EventId);
                    Assert.Equal(1, entity.PersonId);
                    Assert.Equal(1, entity.WineId);
                }
        }