예제 #1
0
        public async Task <IActionResult> Create(ItemCreateInputModel itemCreate)
        {
            try
            {
                if (!this.ModelState.IsValid)
                {
                    return(this.View(itemCreate));
                }

                // var item = AutoMapper.Mapper.Map<Item>(itemCreate);
                var result = await this.itemsService.CreateAsync(itemCreate);

                return(this.RedirectToAction(nameof(this.Index)));
            }
            catch (ArgumentException e)
            {
                // TODO: Error message that item name exist
                this.ModelState.AddModelError(e.ParamName, e.Message);
                return(this.View(itemCreate));
            }
            catch (NullReferenceException e)
            {
                // TODO: Error message that item name exist
                this.ModelState.AddModelError(e.InnerException.Message, e.Message);
                return(this.View(itemCreate));
            }
        }
예제 #2
0
        public async Task CreateAsyncReturnsCorrectItem()
        {
            var secondItem = new ItemIndexViewModel
            {
                Id                       = 2,
                Name                     = SecondItemName,
                OrdinaryPrice            = SecondtemOrdinaryPrice,
                ExpressAddOnPrice        = SecondItemExpressAddOnPrice,
                FlavorAddOnPrice         = SecondItemFlavorAddOnPrice,
                VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice,
            };

            var expected = new List <ItemIndexViewModel>();

            expected.Add(secondItem);

            var addItem = new ItemCreateInputModel
            {
                Name                     = SecondItemName,
                OrdinaryPrice            = SecondtemOrdinaryPrice,
                ExpressAddOnPrice        = SecondItemExpressAddOnPrice,
                FlavorAddOnPrice         = SecondItemFlavorAddOnPrice,
                VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice,
            };

            var result = await this.ItemsServiceMock.CreateAsync(addItem);

            var actual = this.DbContext.Items;

            Assert.Equal(expected.Count, actual.Count());
            Assert.Equal(addItem.Name, result.Name);
        }
예제 #3
0
        public async Task EditByIdAsyncReturnsItem()
        {
            var addItem = new ItemCreateInputModel
            {
                Name                     = SecondItemName,
                OrdinaryPrice            = SecondtemOrdinaryPrice,
                ExpressAddOnPrice        = SecondItemExpressAddOnPrice,
                FlavorAddOnPrice         = SecondItemFlavorAddOnPrice,
                VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice,
            };

            await this.ItemsServiceMock.CreateAsync(addItem);

            var item = new ItemEditInputModel
            {
                Name                     = FirstItemName,
                OrdinaryPrice            = FistItemOrdinaryPrice,
                ExpressAddOnPrice        = SecondItemExpressAddOnPrice,
                FlavorAddOnPrice         = SecondItemFlavorAddOnPrice,
                VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice,
            };

            var result = await this.ItemsServiceMock.EditByIdAsync(1, item);

            Assert.Equal(item.OrdinaryPrice, result.OrdinaryPrice);
        }
예제 #4
0
        public async Task <ItemIndexViewModel> CreateAsync(ItemCreateInputModel itemFromView)
        {
            var itemToDb = itemFromView.To <Item>();

            await this.itemRepository.AddAsync(itemToDb);

            await this.itemRepository.SaveChangesAsync();

            return(itemToDb.To <ItemIndexViewModel>());
        }
예제 #5
0
        public async Task Create_WithCorrectData_ShouldReturnSuccessfullyCreate()
        {
            string errorMessagePrefix = "ItemService GetAllItems does not work properly.";

            var context = AuctionDbContextInMemory.InitializeContext();

            await SeedData(context);

            this.userService    = new UserService(context);
            this.receiptService = new ReceiptService(context);

            Account cloudinaryCredentials = new Account(
                "auction-cloud",
                "245777487727621",
                "VcLN0NRPB7qs0WZv_U2AxMR79sc");

            var cloudinary = new Cloudinary(cloudinaryCredentials);

            this.cloudinaryService = new CloudinaryService(cloudinary);
            this.itemService       = new ItemService(context, cloudinaryService, userService, receiptService);

            var user = new AuctionUser
            {
                Id = "Hugo"
            };

            context.Users.Add(user);

            using (var stream = File.OpenRead(@"./Images/test.jpg"))
            {
                var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(@"./Images/test.jpg"))
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "image/jpeg"
                };

                var dummyItem = new ItemCreateInputModel
                {
                    Name            = "Stick",
                    Category        = "Other",
                    Description     = "Somethin",
                    AuctionDuration = 12,
                    StartingPrice   = 334M,
                    BuyOutPrice     = 3412M,
                    Picture         = file,
                    AuctionHouse    = "Ember"
                };



                bool actualResult = await this.itemService.Create(dummyItem, user.Id);

                Assert.True(actualResult, errorMessagePrefix);
            }
        }
예제 #6
0
        public async Task Create_WithMissingAuctionHouse_ShouldThrowException()
        {
            var context = AuctionDbContextInMemory.InitializeContext();

            await SeedData(context);

            this.userService    = new UserService(context);
            this.receiptService = new ReceiptService(context);

            Account cloudinaryCredentials = new Account(
                "auction-cloud",
                "245777487727621",
                "VcLN0NRPB7qs0WZv_U2AxMR79sc");

            var cloudinary = new Cloudinary(cloudinaryCredentials);

            this.cloudinaryService = new CloudinaryService(cloudinary);
            this.itemService       = new ItemService(context, cloudinaryService, userService, receiptService);

            var user = new AuctionUser
            {
                Id = "Hugo"
            };

            context.Users.Add(user);

            using (var stream = File.OpenRead(@"./Images/test.jpg"))
            {
                var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(@"./Images/test.jpg"))
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "image/jpeg"
                };

                var dummyItem = new ItemCreateInputModel
                {
                    Name            = "Stick",
                    Category        = "Other",
                    Description     = "Somethin",
                    AuctionDuration = 12,
                    StartingPrice   = 334M,
                    BuyOutPrice     = 3412M,
                    Picture         = file,
                    AuctionHouse    = "Wrong"
                };


                await Assert.ThrowsAsync <ArgumentNullException>(() => this.itemService.Create(dummyItem, user.Id));
            }
        }
예제 #7
0
        public async Task <bool> Create(ItemCreateInputModel inputModel, string ownerId)
        {
            var startTime = DateTime.UtcNow;
            var endTime   = startTime.AddHours(inputModel.AuctionDuration);

            string pictureUrl = await this.cloudinaryService
                                .UploadPictureAsync(
                inputModel.Picture,
                inputModel.Name);

            var auctionHouseFromDb = this.context
                                     .AuctionHouses
                                     .FirstOrDefault(x => x.Name == inputModel.AuctionHouse);

            Category category;

            if (!Enum.TryParse(inputModel.Category, out category) || auctionHouseFromDb == null)
            {
                throw new ArgumentNullException(nameof(auctionHouseFromDb));
            }

            var owner = await this.userService.GetById(ownerId);

            var item = new Item
            {
                Name          = inputModel.Name,
                Category      = category,
                StartingPrice = inputModel.StartingPrice,
                BuyOutPrice   = inputModel.BuyOutPrice,
                Picture       = pictureUrl,
                StartTime     = startTime,
                EndTime       = endTime,
                AuctionHouse  = auctionHouseFromDb,
                Description   = inputModel.Description,
            };

            item.AuctionUser = owner;
            item.Status      = ItemStatus.InAction;
            this.context.Items.Add(item);

            int result = await this.context
                         .SaveChangesAsync();

            return(result > 0);
        }
예제 #8
0
        public async Task <ItemIndexViewModel> CreateAsync(ItemCreateInputModel itemFromView)
        {
            var checkForName = this.itemRepository.All().FirstOrDefault(x => x.Name == itemFromView.Name);

            // If item exists return existing view model
            if (checkForName != null)
            {
                throw new ArgumentException(string.Format(ItemConstants.ArgumentExceptionItemName, itemFromView.Name), nameof(itemFromView.Name));
            }

            var itemToDb = itemFromView.To <Item>();

            await this.itemRepository.AddAsync(itemToDb);

            await this.itemRepository.SaveChangesAsync();

            return(itemToDb.To <ItemIndexViewModel>());
        }
예제 #9
0
        public async Task GetByIdAsyncReturnsItem()
        {
            var addItem = new ItemCreateInputModel
            {
                Name                     = SecondItemName,
                OrdinaryPrice            = SecondtemOrdinaryPrice,
                ExpressAddOnPrice        = SecondItemExpressAddOnPrice,
                FlavorAddOnPrice         = SecondItemFlavorAddOnPrice,
                VacuumCleaningAddOnPrice = SecondItemVacuumCleaningAddOnPrice,
            };

            await this.ItemsServiceMock.CreateAsync(addItem);

            var id = 1;

            var result = await this.ItemsServiceMock.GetByIdAsync <ItemDetailsViewModel>(id);

            Assert.Equal(addItem.OrdinaryPrice, result.OrdinaryPrice);
        }
예제 #10
0
        public async Task <IActionResult> Create(ItemCreateInputModel itemCreate)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(AutoMapper.Mapper.Map <ItemIndexViewModel>(itemCreate)));
            }

            var isNameExist = await this.itemsService.GetAllItemsAsync <ItemIndexViewModel>().AnyAsync(x => x.Name == itemCreate.Name);

            if (isNameExist)
            {
                this.ModelState.AddModelError(nameof(itemCreate.Name), string.Format(ItemConstants.ArgumentExceptionItemName, itemCreate.Name));
                return(this.View(AutoMapper.Mapper.Map <ItemIndexViewModel>(itemCreate)));
            }

            var result = await this.itemsService.CreateAsync(itemCreate);

            return(this.RedirectToAction(nameof(this.Index)));
        }
예제 #11
0
        public async Task <IActionResult> Create(ItemCreateInputModel inputModel)
        {
            if (!ModelState.IsValid || inputModel.StartingPrice > inputModel.BuyOutPrice)
            {
                var auctionHouses = await auctionHouseService.GetAllAuctionHouses().ToListAsync();

                this.ViewData["auctionHouses"] = auctionHouses.Select(auctionHouse =>
                                                                      new ItemCreateAuctionHouseViewModel
                {
                    Name = auctionHouse.Name
                })
                                                 .ToList();

                return(this.View());
            }

            var ownerId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            await this.itemService.Create(inputModel, ownerId);

            return(this.RedirectToHome());
        }