public void AddLandmark(AddEditLandmarkBindingModel model, List <string> imagesPaths)
        {
            var landmark = new Landmark
            {
                Name        = model.Name,
                CategoryId  = model.CategoryId,
                Description = model.Description,
                RegionId    = model.RegionId,
                CreatorId   = model.CreatorId,
            };

            this.DbContext.Landmarks.Add(landmark);
            this.DbContext.SaveChanges();

            if (!imagesPaths.Any())
            {
                return;
            }

            var imagesToDb = new List <Image>();

            foreach (var imagesPath in imagesPaths)
            {
                imagesToDb.Add(new Image {
                    LandmarkId = landmark.Id, Path = imagesPath
                });
            }

            this.DbContext.Images.AddRange(imagesToDb);
            this.DbContext.SaveChanges();

            landmark.Images = imagesToDb;

            this.DbContext.SaveChanges();
        }
        public void SaveEntity(AddEditLandmarkBindingModel model, List <string> imagesPaths)
        {
            var landmark = this.DbContext.Landmarks.Include(l => l.Images).FirstOrDefault(l => l.Id == model.Id);

            //remove old picture from database and server
            RemoveImagesFromServer(landmark);
            var imagesForDelete = this.DbContext.Images.Where(i => i.LandmarkId == model.Id);

            this.DbContext.Images.RemoveRange(imagesForDelete);
            this.DbContext.SaveChanges();

            var imagesToDb = new List <Image>();

            if (imagesPaths.Any())
            {
                foreach (var imagesPath in imagesPaths)
                {
                    imagesToDb.Add(new Image {
                        LandmarkId = model.Id, Path = imagesPath
                    });
                }

                this.DbContext.Images.AddRange(imagesToDb);
                this.DbContext.SaveChanges();
            }

            landmark.Name        = model.Name;
            landmark.CategoryId  = model.CategoryId;
            landmark.Description = model.Description;
            landmark.RegionId    = model.RegionId;
            landmark.Images      = imagesToDb;

            this.DbContext.Attach(landmark).State = EntityState.Modified;
            this.DbContext.SaveChanges();
        }
Exemple #3
0
        public IActionResult OnGet(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var landmark = this._service.GetLandmark(id.Value);

            if (landmark == null)
            {
                return(NotFound());
            }

            var userId = this.User.GetUserId();

            if (landmark.CreatorId != userId)
            {
                return(RedirectToPage(RedirectURL.ToLandmarkList, new { Area = NamesConstants.OperatorArea }));
            }

            this.EditLandmarkBindingModel = new AddEditLandmarkBindingModel
            {
                Name = landmark.Name,
                ImagesPathToDisplay = landmark.Images.Select(i => i.Path).ToList(),
                CategoryId          = landmark.CategoryId,
                Description         = landmark.Description,
                RegionId            = landmark.RegionId
            };

            this._service.FillDropDownItems(this.EditLandmarkBindingModel);

            return(this.Page());
        }
        public void SaveEntity(AddEditLandmarkBindingModel editLandmarkBindingModel)
        {
            var landmark = this.Mapper.Map <Landmarks.Models.Landmark>(editLandmarkBindingModel);

            this.DbContext.Attach(landmark).State = EntityState.Modified;
            this.DbContext.SaveChanges();
        }
        public void CreateLandmark(AddEditLandmarkBindingModel addRegionBindingModel)
        {
            var landmark = this.Mapper.Map <AddEditLandmarkBindingModel, Landmark>(addRegionBindingModel);

            this.DbContext.Landmarks.Add(landmark);
            this.DbContext.SaveChanges();
        }
        public IActionResult OnGet(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var landmark = this._service.GetLandmark(id.Value);

            if (landmark == null)
            {
                return(NotFound());
            }

            this.EditLandmarkBindingModel = this._mapper.Map <Landmarks.Models.Landmark, AddEditLandmarkBindingModel>(landmark);
            this._service.FillDropDownItems(this.EditLandmarkBindingModel);

            return(this.Page());
        }
        public void FillDropDownItems(AddEditLandmarkBindingModel model)
        {
            //get categories and region for drop down list
            var categories = this.DbContext.Categories.ToList();
            var regions    = this.DbContext.Regions.ToList();

            model.Categories = new List <SelectListItem>();
            categories.ForEach(c => model.Categories.Add(new SelectListItem
            {
                Text  = c.Name,
                Value = c.Id.ToString()
            }));

            model.Regions = new List <SelectListItem>();
            regions.ForEach(r => model.Regions.Add(new SelectListItem
            {
                Text  = r.Name,
                Value = r.Id.ToString()
            }));
        }