public ActionResult Upload(AlbumUploadViewModel model) { if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0) { ModelState.AddModelError("Photo", "Provide a photo"); } if (!ModelState.IsValid) { model.Albums = this.repository.GetAlbumsByOwner(User.Identity.Name.ToLowerInvariant()); return(View(model)); } var photo = Request.Files[0]; // generate a unique name string id = SlugHelper.GetSlug(model.Title); string owner = User.Identity.Name.ToLowerInvariant(); string albumName = model.Album; try { this.repository.Add( new Photo() { AlbumId = albumName, Description = model.Description, Owner = owner, PhotoId = id, Title = model.Title, ThumbnailUrl = "/Content/images/processing.png", Url = "/Content/images/processing.png", RawTags = string.IsNullOrEmpty(model.Tags) ? "uncategorized" : model.Tags }, photo.InputStream, photo.ContentType, photo.FileName); } catch (PhotoNameAlreadyInUseException) { ModelState.AddModelError("Title", "A picture with the same name already exists on this album"); model.Albums = this.repository.GetAlbumsByOwner(User.Identity.Name.ToLowerInvariant()); return(View(model)); } return(RedirectToAction("Index", "Photo", new { owner = owner, album = model.Album, photoId = id })); }
public ActionResult Create(AlbumCreateViewModel album) { if (!ModelState.IsValid) { return(View(album)); } var owner = User.Identity.Name.ToLowerInvariant(); if (this.repository.GetAlbumsByOwner(owner).FirstOrDefault(o => o.AlbumId == SlugHelper.GetSlug(album.Title)) != null) { ModelState.AddModelError("Title", "An album with the same name already exists."); return(View(album)); } this.repository.CreateAlbum(album.Title, owner); return(RedirectToAction("Index")); }