// GET: /BackOffice/Collection/Create
        public async Task<ActionResult> Create(int authorId = 0)
        {
            var model = new CollectionEditViewModel();

            var c = new Collection();
            c.Translations.Add(new CollectionTranslation
            {
                LanguageCode = LanguageDefinitions.DefaultLanguage
            });

            if (authorId != 0)
            {
                c.Authors = await db.Set<Author>().Where(a => a.Id == authorId).ToListAsync();
            }

            return View(GenerateViewModel(c));
        }
        public async Task<ActionResult> Create(CollectionEditViewModel model)
        {
            if (DoesCodeAlreadyExist(model.Collection))
            {
                ModelState.AddModelError("Collection.CatalogCode", CollectionStrings.Validation_CodeAlreadyExists);
            }

            if (ModelState.IsValid)
            {
                if (model.Logo != null)
                {
                    var newName = Guid.NewGuid().ToString() + ".jpg";

                    FileUploadHelper.SaveImage(model.Logo.InputStream,
                        400, 400,
                        Server.MapPath("~/Public/Collections/") + newName,
                        FitMode.Crop);

                    model.Collection.LogoLocation = newName;
                }

                var authors = db.Set<Author>()
                                .Where(a => model.AuthorIds.Contains(a.Id));

                model.Collection.Authors = authors.ToList();

                db.Add(model.Collection);
                await db.SaveChangesAsync();

                return RedirectToAction("Index");
            }

            model.AvailableAuthors = db.Set<Author>()
                .Select(a => new SelectListItem
                {
                    Value = a.Id.ToString(),
                    Text = a.LastName + ", " + a.FirstName,
                    Selected = model.AuthorIds.Contains(a.Id)
                })
                .ToList();

            return View(model);
        }
        private CollectionEditViewModel GenerateViewModel(Collection c)
        {
            var vm = new CollectionEditViewModel();

            vm.Collection = c;

            var authorIds = c.Authors.Select(a => a.Id).ToList();

            vm.AvailableAuthors = db.Set<Author>()
                .Select(a => new SelectListItem
                {
                    Value = a.Id.ToString(),
                    Text = a.LastName + ", " + a.FirstName,
                    Selected = authorIds.Contains(a.Id)
                })
                .ToList();

            return vm;
        }
        public async Task<ActionResult> Edit(CollectionEditViewModel model)
        {
            if (DoesCodeAlreadyExist(model.Collection))
            {
                ModelState.AddModelError("Collection.CatalogCode", CollectionStrings.Validation_CodeAlreadyExists);
            }

            if (ModelState.IsValid)
            {
                // Force-update the collection's author list.
                await db.ForceLoadAsync(model.Collection, c => c.Authors);

                model.Collection.Authors = db.Set<Author>()
                     .Where(a => model.AuthorIds.Contains(a.Id)).ToList();

                foreach (var t in model.Collection.Translations)
                {
                    db.UpdateTranslation(t);
                }

                // Update the logo if a new one is supplied. Don't allow property value changes if
                // the logo doesn't exist.
                if (model.Logo != null)
                {
                    var logo = db.GetValueFromDb(model.Collection, c => c.LogoLocation);

                    if (logo == null)
                    {
                        model.Collection.LogoLocation =
                            Guid.NewGuid().ToString() + ".jpg";

                        logo = model.Collection.LogoLocation;
                    }

                    FileUploadHelper.SaveImage(model.Logo.InputStream,
                        400, 400,
                        Server.MapPath("~/Public/Collections/") + logo,
                        FitMode.Crop);
                }
                else
                {
                    db.ExcludeFromUpdate(model.Collection, c => new { c.LogoLocation });
                }

                db.Update(model.Collection);

                await db.SaveChangesAsync();

                return RedirectToAction("Index");
            }

            model.AvailableAuthors = db.Set<Author>()
                .Select(a => new SelectListItem
                {
                    Value = a.Id.ToString(),
                    Text = a.LastName + ", " + a.FirstName,
                    Selected = model.AuthorIds.Contains(a.Id)
                })
                .ToList();

            return View(model);
        }