// GET: BackOffice/ShowcasePhotoes/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ShowcasePhoto showcasePhoto = await db.GetByIdAsync(id);

            if (showcasePhoto == null)
            {
                return(HttpNotFound());
            }
            return(View(GenerateViewModel(showcasePhoto)));
        }
        public async Task <ActionResult> Index()
        {// Get the newest one.
            ShowcasePhoto showcasephoto = await db.Entities
                                          .Include(sp => sp.Image)
                                          .Where(sp => sp.VisibleSince <= DateTime.Now)
                                          .OrderByDescending(sp => sp.VisibleSince)
                                          .FirstOrDefaultAsync(sp => sp.HideAt == null || sp.HideAt.Value > DateTime.Now);

            if (showcasephoto == null)
            {
                return(HttpNotFound());
            }

            return(View("Article", new TranslatedViewModel <ShowcasePhoto, ShowcasePhotoTranslation>(showcasephoto)));
        }
        // GET: BackOffice/ShowcasePhotoes/Delete/5
        public async Task <ActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ShowcasePhoto showcasePhoto = await db.GetByIdAsync(id);

            if (showcasePhoto == null)
            {
                return(HttpNotFound());
            }

            showcasePhoto.Translations = showcasePhoto.Translations.ToList();

            return(View(showcasePhoto));
        }
        public async Task <ActionResult> Edit(ShowcasePhoto showcasePhoto)
        {
            if (ModelState.IsValid)
            {
                db.Update(showcasePhoto);

                foreach (var item in showcasePhoto.Translations)
                {
                    db.UpdateTranslation(item);
                }

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(GenerateViewModel(showcasePhoto)));
        }
        public async Task <ActionResult> Create(ShowcasePhoto showcasePhoto)
        {
            var image = db.Set <Image>().FirstOrDefault(i => i.Id == showcasePhoto.ImageId);

            if (!image.IsVisible)
            {
                ModelState.AddModelError("ImageId", ShowcasePhotoStrings.ValidationError_ImageHidden);
            }

            if (ModelState.IsValid)
            {
                db.Add(showcasePhoto);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(GenerateViewModel(showcasePhoto)));
        }
        // GET: BackOffice/ShowcasePhotoes/Create
        public async Task <ActionResult> Create(int?imageId)
        {
            var photo = new ShowcasePhoto();

            if (imageId != null && await db.Set <Image>()
                .AnyAsync(i =>
                          i.Id == imageId &&
                          i.ImageUrl != null && i.IsVisible && i.Document.Collection.IsVisible &&
                          i.ShowImage))
            {
                photo.ImageId = imageId.Value;
            }

            photo.Translations.Add(new ShowcasePhotoTranslation
            {
                LanguageCode = LanguageDefinitions.DefaultLanguage
            });

            return(View(GenerateViewModel(photo)));
        }
        private ShowcasePhotoEditViewModel GenerateViewModel(ShowcasePhoto photo)
        {
            var model = new ShowcasePhotoEditViewModel
            {
                ShowcasePhoto = photo
            };

            model.AvailableImages = db.Set <Image>()
                                    .Where(i =>
                                           i.IsVisible &&
                                           i.Document.Collection.IsVisible &&
                                           i.ImageUrl != null &&
                                           i.ShowImage)
                                    .ToList()
                                    .Select(i => new TranslatedViewModel <Image, ImageTranslation>(i))
                                    .Select(i => new SelectListItem
            {
                Value = i.Entity.Id.ToString(),
                Text  = i.Translation.Title
            });

            return(model);
        }