Beispiel #1
0
        public IActionResult Add()
        {
            var viewModel = new AddLakeInputModel
            {
                CountriesItems = this.countriesService.GetAllAsKeyValuePairs(),
            };

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

            using var db = new ApplicationDbContext(options);
            var lakeRepository = new EfDeletableEntityRepository <Lake>(db);

            var clodinaryMock = new Mock <ICloudinaryService>();

            var service = new LakesService(clodinaryMock.Object, lakeRepository);

            // 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 AddLakeInputModel
            {
                Location = new LocationLakeInputModel
                {
                    Latitude  = 12.22,
                    Longitude = 13.33,
                },
                CountryId   = 2,
                Name        = "Test2",
                WebsiteUrl  = "www.test2.com",
                Area        = 22.22,
                IsFree      = true,
                MainImage   = file,
                OtherImages = new List <IFormFile> {
                    file
                },
            };

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

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

            await lakeRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }
Beispiel #3
0
        public async Task AddAsync(AddLakeInputModel input, string userId)
        {
            var lake = new Lake()
            {
                Name       = input.Name,
                OwnerId    = userId,
                CountryId  = input.CountryId,
                Area       = input.Area,
                IsFree     = input.IsFree,
                WebsiteUrl = input.WebsiteUrl,
            };

            lake.Location = new Location
            {
                Latitude  = input.Location.Latitude,
                Longitude = input.Location.Longitude,
            };

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

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

            lake.LakeImages.Add(new LakeImage()
            {
                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,
                    };

                    lake.LakeImages.Add(new LakeImage()
                    {
                        Image = currImg,
                    });
                }
            }

            await this.lakeRepository.AddAsync(lake);

            await this.lakeRepository.SaveChangesAsync();
        }
Beispiel #4
0
        public async Task <IActionResult> Add(AddLakeInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.CountriesItems = this.countriesService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

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

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

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