Ejemplo n.º 1
0
        public IActionResult Add()
        {
            var viewModel = new AddTrophyInputModel
            {
                LakesItems = this.lakesService.GetAllAsKeyValuePairs(),
                RigsItems  = this.rigsService.GetAllAsKeyValuePairs(),
            };

            return(this.View(viewModel));
        }
Ejemplo n.º 2
0
        public async Task AddAsyncShouldWorkCorrect()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);
            var trophyRepository = new EfDeletableEntityRepository <Trophy>(db);

            var clodinaryMock = new Mock <ICloudinaryService>();

            var service = new TrophiesService(clodinaryMock.Object, trophyRepository);

            // Arrange
            var fileMock = new Mock <IFormFile>();

            // Setup mock file using a memory stream
            var content  = "Hello World from a Fake File";
            var fileName = "test.jpg";
            var ms       = new MemoryStream();

            using var writer = new StreamWriter(ms);
            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.Length).Returns(ms.Length);

            var file = fileMock.Object;

            var input = new AddTrophyInputModel
            {
                LakeId          = 1,
                BaitDescription = "corn",
                RigId           = null,
                Weight          = 12.500,
                MainImage       = file,
                OtherImages     = new List <IFormFile> {
                    file
                },
            };

            Assert.Equal(0, service.GetCount());

            await service.AddAsync(input, Guid.NewGuid().ToString());

            await trophyRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }
Ejemplo n.º 3
0
        public async Task AddAsync(AddTrophyInputModel input, string userId)
        {
            var trophy = new Trophy()
            {
                Weight          = input.Weight,
                BaitDescription = input.BaitDescription,
                LakeId          = input.LakeId,
                RigId           = input.RigId,
                OwnerId         = userId,
            };

            var mainImgUrl = await this.cloudinaryService.UploadAsync(input.MainImage, input.MainImage.FileName);

            var mainImg = new Image()
            {
                OwnerId = userId,
                Url     = mainImgUrl,
            };

            trophy.TrophyImages.Add(new TrophyImage
            {
                Image  = mainImg,
                IsMain = true,
            });

            if (input.OtherImages != null)
            {
                foreach (var img in input.OtherImages)
                {
                    var currImgUrl = await this.cloudinaryService.UploadAsync(img, img.FileName);

                    var currImg = new Image()
                    {
                        OwnerId = userId,
                        Url     = currImgUrl,
                    };

                    trophy.TrophyImages.Add(new TrophyImage()
                    {
                        Image = currImg,
                    });
                }
            }

            await this.trophiesRepository.AddAsync(trophy);

            await this.trophiesRepository.SaveChangesAsync();
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Add(AddTrophyInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.LakesItems = this.lakesService.GetAllAsKeyValuePairs();
                input.RigsItems  = this.rigsService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

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

            try
            {
                await this.trophiesService.AddAsync(input, userId);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }

            return(this.RedirectToAction("All"));
        }