Exemplo n.º 1
0
        public async Task UpdateItemBidAsyncThowsBadRequestException()
        {
            var mockLogger       = new Mock <ILogger <ItemController> >();
            var item             = GetMockItemBid();
            var user             = GetMockUser();
            var createBidRequest = new CreateBidRequest();

            createBidRequest.BidAmount = 450;
            var itemBidStatus = new ItemBidStatus();

            itemBidStatus.CanCreateBid = false;
            itemBidStatus.ErrorMessage = "Auction End Date expired";
            var itemRepository = new Mock <IItemRepository>();

            itemRepository.Setup(t => t.GetItemAsync(It.IsAny <int>())).Returns(Task.FromResult(item));
            itemRepository.Setup(u => u.GetItemBidStatus(It.IsAny <int>())).Returns(Task.FromResult(itemBidStatus));

            var userRepository = new Mock <IUserRepository>();

            userRepository.Setup(u => u.GetUserAsync(It.IsAny <int>())).Returns(Task.FromResult(user));
            var itemController = new ItemController(mockLogger.Object, itemRepository.Object, userRepository.Object);
            var controller     = await itemController.UpdateItemBidAsync(createBidRequest, "1", 2);

            var result = Assert.IsType <BadRequestObjectResult>(controller.Result);

            Assert.Equal(400, result.StatusCode);
            Assert.Equal("Auction End Date expired", result.Value);
        }
Exemplo n.º 2
0
        public async Task UpdateItemBidAsyncReturnsItemResponse()
        {
            var mockLogger       = new Mock <ILogger <ItemController> >();
            var item             = GetMockItemBid();
            var user             = GetMockUser();
            var createBidRequest = new CreateBidRequest();

            createBidRequest.BidAmount = 450;
            var itemBidStatus = new ItemBidStatus();

            itemBidStatus.CanCreateBid = true;
            itemBidStatus.ErrorMessage = string.Empty;
            var itemRepository = new Mock <IItemRepository>();

            itemRepository.Setup(t => t.GetItemAsync(It.IsAny <int>())).Returns(Task.FromResult(item));
            itemRepository.Setup(u => u.GetItemBidStatus(It.IsAny <int>())).Returns(Task.FromResult(itemBidStatus));
            itemRepository.Setup(s => s.CreateBid(It.IsAny <CreateBidRequest>())).Returns(Task.FromResult(item));

            var userRepository = new Mock <IUserRepository>();

            userRepository.Setup(u => u.GetUserAsync(It.IsAny <int>())).Returns(Task.FromResult(user));
            var itemController = new ItemController(mockLogger.Object, itemRepository.Object, userRepository.Object);
            var controller     = await itemController.UpdateItemBidAsync(createBidRequest, "1", 2);

            var actionResult = Assert.IsType <OkObjectResult>(controller.Result);
            var result       = actionResult.Value as ItemResponse;

            Assert.NotNull(result);
            Assert.Equal(2, result.Id);
            Assert.Equal(2346, result.ItemNumber);
            Assert.Equal("Iphone X 512 GB White", result.Description);
            Assert.Single(result.Bids);
            Assert.Equal(450, result.Bids[0].BidAmount);
        }
Exemplo n.º 3
0
        public async Task <ItemResponse> CreateBid(CreateBidRequest createBidRequest)
        {
            var bidEntity = _mapper.Map <Bid>(createBidRequest);
            await _edappContext.Bid.AddAsync(bidEntity);

            await _edappContext.SaveChangesAsync();

            var itemEntity = await _edappContext.Item.FindAsync(createBidRequest.ItemId);

            var item = _mapper.Map <ItemResponse>(itemEntity);
            var bids = await _edappContext.Bid.Where(b => b.ItemId == itemEntity.Id).ToListAsync();

            item.Bids = _mapper.Map <List <BidResponse> >(bids);
            return(item);
        }
Exemplo n.º 4
0
        public async Task <ActionResult <CreateBidResponse> > Post([FromForm] CreateBidRequest request,
                                                                   CancellationToken cancellationToken)
        {
            var bid = new Bid(request.TraderId, request.SlotId, request.Amount, request.Date);
            // append layer in infrastructure for bidding (generate idea)
            await _bidRepository.AddAsync(bid, cancellationToken);

            var bidsSpecification = new BidSpecification(request.SlotId);
            var bids = await _bidRepository.ListAsync(bidsSpecification, cancellationToken);

            var response = new CreateBidResponse(request.CorrelationId());

            response.Bids = bids.Select(Mapper.Map <BidDto>).ToList();

            return(Ok(response));
        }
Exemplo n.º 5
0
        public IActionResult Create([FromBody] CreateBidRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = bidsService.AddBid(new EdAppTest.Domain.Bid
            {
                Price     = request.Price.Value,
                ProductId = Guid.Parse(request.ProductId),
                UserId    = HttpContext.UserId()
            });

            return(CreatedAtAction(nameof(Get), new { id = result.Id }, result));
        }
Exemplo n.º 6
0
        public async Task UpdateItemBidAsyncThowsNotFoundException()
        {
            var          mockLogger       = new Mock <ILogger <ItemController> >();
            ItemResponse item             = null;
            var          user             = GetMockUser();
            var          createBidRequest = new CreateBidRequest();

            createBidRequest.BidAmount = 450;
            var itemRepository = new Mock <IItemRepository>();

            itemRepository.Setup(t => t.GetItemAsync(It.IsAny <int>())).Returns(Task.FromResult(item));

            var userRepository = new Mock <IUserRepository>();

            userRepository.Setup(u => u.GetUserAsync(It.IsAny <int>())).Returns(Task.FromResult(user));
            var itemController = new ItemController(mockLogger.Object, itemRepository.Object, userRepository.Object);
            var controller     = await itemController.UpdateItemBidAsync(createBidRequest, "1", 2);

            var result = Assert.IsType <NotFoundResult>(controller.Result);

            Assert.Equal(404, result.StatusCode);
        }
Exemplo n.º 7
0
        public async Task <ActionResult <ItemResponse> > UpdateItemBidAsync([FromBody] CreateBidRequest createBidRequest, [FromHeader(Name = "user-id")] string userid, [FromRoute] int id)
        {
            if (ModelState.IsValid)
            {
                var userId = int.Parse(userid);
                var user   = await _userRepository.GetUserAsync(userId);

                if (user == null)
                {
                    return(Unauthorized());
                }

                var item = await _itemRepository.GetItemAsync(id);

                if (item == null)
                {
                    return(NotFound());
                }

                var itemBidStatus = await _itemRepository.GetItemBidStatus(item.Id);

                if (itemBidStatus.CanCreateBid)
                {
                    createBidRequest.UserId         = userId;
                    createBidRequest.BidCreatedDate = DateTime.Now;
                    createBidRequest.ItemId         = item.Id;
                    var updatedItem = await _itemRepository.CreateBid(createBidRequest);

                    return(Ok(updatedItem));
                }
                else
                {
                    return(BadRequest(itemBidStatus.ErrorMessage));
                }
            }

            return(BadRequest());
        }