示例#1
0
        public async Task <IActionResult> Create(CreateLandmarkInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                this.categoriesService.GetAllLandmarkCategotiesAsKeyValuePairs();
                this.regionsService.GetAllAsKeyValuePairs();
                this.townsService.GetAllAsKeyValuePairs();
                this.mountainsService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

            // Get userId by cookie
            // var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            // Get userId by UserManager
            var user = await this.userManager.GetUserAsync(this.User);

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

                this.categoriesService.GetAllLandmarkCategotiesAsKeyValuePairs();
                this.regionsService.GetAllAsKeyValuePairs();
                this.townsService.GetAllAsKeyValuePairs();
                this.mountainsService.GetAllAsKeyValuePairs();
                return(this.View(input));
            }

            return(this.Redirect("/"));
        }
示例#2
0
        public IActionResult Create()
        {
            var viewModel = new CreateLandmarkInputModel
            {
                CategoriesItems = this.categoriesService.GetAllLandmarkCategotiesAsKeyValuePairs(),
                RegionsItems    = this.regionsService.GetAllAsKeyValuePairs(),
                TownsItems      = this.townsService.GetAllAsKeyValuePairs(),
                MountainsItems  = this.mountainsService.GetAllAsKeyValuePairs(),
            };

            return(this.View(viewModel));
        }
示例#3
0
        public async Task CreateAsync(CreateLandmarkInputModel input, string userId, string imagePath)
        {
            var landmark = new Landmark
            {
                Name        = input.Name,
                CategoryId  = input.CategoryId,
                RegionId    = input.RegionId,
                TownId      = input.TownId,
                MountainId  = input.MountainId,
                Description = input.Description,
                Latitude    = input.Latitude,
                Longitute   = input.Longitute,
                Websate     = input.Websate,
                Address     = input.Address,
                PhoneNumber = input.PhoneNumber,
                WorkTime    = input.WorkTime,
                DayOff      = input.DayOff,
                EntranceFee = input.EntranceFee,
                Difficulty  = input.Difficulty,
                UserId      = userId,
            };

            // landmark.Town.IsTown = true;
            Directory.CreateDirectory($"{imagePath}/landmarks/");
            foreach (var image in input.LandmarkImages)
            {
                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 LandmarkImage
                {
                    UserId    = userId,
                    Extension = extension,
                };

                var physicalPath = $"{imagePath}/landmarks/{dbImage.Id}.{extension}";

                string localImgUrl = physicalPath.Split("wwwroot")[1];
                dbImage.RemoteImageUrl = localImgUrl;
                landmark.LandmarkImages.Add(dbImage);

                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.landmarksRepository.AddAsync(landmark);

            await this.landmarksRepository.SaveChangesAsync();
        }