Exemplo n.º 1
0
        public async Task <IActionResult> Create(CreateHikeInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.CategoriesItems = this.categoriesService.GetAllHikeCategotiesAsKeyValuePairs();
                input.RegionsItems    = this.regionsService.GetAllAsKeyValuePairs();
                input.MountainsItems  = this.mountainsService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.hikesService.CreateAsync(input, user.Id, $"{this.hostEnvironment.WebRootPath}/images");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);

                input.CategoriesItems = this.categoriesService.GetAllHikeCategotiesAsKeyValuePairs();
                input.RegionsItems    = this.regionsService.GetAllAsKeyValuePairs();
                input.MountainsItems  = this.mountainsService.GetAllAsKeyValuePairs();

                return(this.View(input));
            }

            return(this.Redirect("/"));
        }
Exemplo n.º 2
0
        public IActionResult Create()
        {
            var viewModel = new CreateHikeInputModel
            {
                CategoriesItems = this.categoriesService.GetAllHikeCategotiesAsKeyValuePairs(),
                RegionsItems    = this.regionsService.GetAllAsKeyValuePairs(),
                MountainsItems  = this.mountainsService.GetAllAsKeyValuePairs(),

                // HikeStartPoint = new HikeStartPointsService,
            };

            return(this.View(viewModel));
        }
Exemplo n.º 3
0
        public async Task CreateAsync(CreateHikeInputModel input, string userId, string imagePath)
        {
            var startPoint = this.hikeStartPointsRepository.All()
                             .FirstOrDefault(x => x.Name == input.HikeStartPoint.Name);

            if (startPoint == null)
            {
                startPoint = new HikeStartPoint
                {
                    Name      = input.HikeStartPoint.Name,
                    Altitude  = input.HikeStartPoint.Altitude,
                    Latitude  = input.HikeStartPoint.Longitute,
                    Longitute = input.HikeStartPoint.Longitute,
                };

                await this.hikeStartPointsRepository.AddAsync(startPoint);

                await this.hikeStartPointsRepository.SaveChangesAsync();
            }

            var endPoint = this.hikeEndPointsRepository.All()
                           .FirstOrDefault(x => x.Name == input.HikeEndPoint.Name);

            if (endPoint == null)
            {
                endPoint = new HikeEndPoint
                {
                    Name      = input.HikeEndPoint.Name,
                    Altitude  = input.HikeEndPoint.Altitude,
                    Latitude  = input.HikeEndPoint.Latitude,
                    Longitute = input.HikeEndPoint.Longitute,
                };

                await this.hikeEndPointsRepository.AddAsync(endPoint);

                await this.hikeEndPointsRepository.SaveChangesAsync();
            }

            var hike = new Hike
            {
                Name             = input.Name,
                Length           = input.Length,
                Duration         = TimeSpan.FromMinutes(input.Duration),
                Description      = input.Description,
                Marking          = input.Marking,
                Difficulty       = input.Difficulty,
                CategoryId       = input.CategoryId,
                RegionId         = input.RegionId,
                MountainId       = input.RegionId,
                HikeStartPointId = startPoint.Id,
                HikeEndPointId   = endPoint.Id,
                Denivelation     = startPoint.Altitude - endPoint.Altitude,
                UserId           = userId,
            };

            Directory.CreateDirectory($"{imagePath}/hikes/");
            foreach (var image in input.HikeImages)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');

                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extension}");
                }

                var dbImage = new HikeImage
                {
                    UserId    = userId,
                    Extension = extension,
                };
                var    physicalPath = $"{imagePath}/hikes/{dbImage.Id}.{extension}";
                string localImgUrl  = physicalPath.Split("wwwroot")[1];
                dbImage.RemoteImageUrl = localImgUrl;
                hike.HikeImages.Add(dbImage);
                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.hikesRepository.AddAsync(hike);

            await this.hikesRepository.SaveChangesAsync();
        }