Ejemplo n.º 1
0
        public async Task <IActionResult> ExperienceCreate(ExperienceCreateUpdateModel model, int[] selectedCategoryIds, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                var user = await _UserManager.FindByNameAsync(User.Identity.Name);

                var entity = new Experience()
                {
                    Name            = model.Name,
                    Title           = model.Title,
                    Author          = model.Author,
                    Body            = model.Body,
                    LocationId      = model.LocationOfExperience.LocationId,
                    BeenThereUserId = user.Id,
                    ImageUrl        = "defaultExperiencePic.jpg"
                };
                var locationOfExperience = _LocationService.GetById(model.LocationOfExperience.LocationId);

                await UpdateCountryCodeList(user.Id.ToString(), locationOfExperience.CountryCode, null);

                if (file != null)
                {
                    var extention  = Path.GetExtension(file.FileName);
                    var randomName = string.Format($"{Guid.NewGuid()}{extention}");
                    entity.ImageUrl = randomName;
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot//img", randomName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                }


                _ExperienceService.Create(entity);

                _ExperienceService.AddCategoriesToExperience(entity, selectedCategoryIds);

                if (model.LocationOfExperience.LocationId > 0)
                {
                    var locationToBeEdited = _LocationService.GetById(model.LocationOfExperience.LocationId);

                    _LocationService.AttachCategoriesToLocation(locationToBeEdited, selectedCategoryIds);
                }

                return(Redirect("/social/experiencedetails/" + entity.ExperienceId));
            }
            return(View(model));
        }