public async Task <IActionResult> Add(CreateAlbumViewModel model) { AlbumViewModelMapper mapper = new AlbumViewModelMapper(); PublishStatus flags = PublishStatus.PUBLISHED | PublishStatus.UNPUBLISHED; var artistNames = await _artistRepo.ListNamesAsync(flags); model.ArtistOptions = artistNames .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = x.Id == model.ArtistId }).ToList(); List <CheckBoxListItem> groupOptions = (await _albumGroupRepo.ListAsync()) .Select(x => new CheckBoxListItem { Label = x.Name, Value = x.Key, IsSelected = model.Groups.Any(grp => grp.IsSelected && grp.Value == x.Key) }).ToList(); model.Groups = groupOptions; if (ModelState.IsValid) { int?createdImageId = null; if (model.CoverImage != null && model.CoverImage.Length > 0) { using (MemoryStream ms = new MemoryStream()) { await model.CoverImage.CopyToAsync(ms); ImageReferenceDetail imageRef = await _imageRepo.AddAsync(new ImageReferenceDetail { Data = ms.ToArray(), FileName = model.CoverImage.FileName, MimeType = model.CoverImage.ContentType }); if (imageRef != null) { createdImageId = imageRef.Id; } } } var result = await _albumRepo.AddAsync(mapper.Map(model, createdImageId)); await _albumGroupRepo.SetGroupsForAlbum(result.Id, model.Groups.Where(g => g.IsSelected).Select(g => g.Value).ToList()); this.SetBootstrapPageAlert("Success", "Album added", BootstrapAlertType.success); return(RedirectToAction(nameof(Index))); } else { return(View(model)); } }
public async Task <IActionResult> Edit(EditArtistViewModel model) { ArtistDetail artist = await _artistRepo.GetAsync(model.Id); if (artist != null) { // set non postback properties model.BioImageId = artist.BioImageId; model.Created = artist.CreatedUtc; model.Updated = artist.UpdatedUtc; model.PublishedStatus = artist.PublishedStatus; if (ModelState.IsValid) { int?createdBioImageId = null; if (model.BioImage != null && model.BioImage.Length > 0) { using (MemoryStream ms = new MemoryStream()) { await model.BioImage.CopyToAsync(ms); ImageReferenceDetail imageRef = await _imageRepo.AddAsync(new ImageReferenceDetail { Data = ms.ToArray(), FileName = model.BioImage.FileName, MimeType = model.BioImage.ContentType }); if (imageRef != null) { createdBioImageId = imageRef.Id; } } } artist.Name = model.Name; artist.BioText = model.BioText; artist.UpdatedUtc = DateTime.UtcNow; artist.Genres = model.Genres.Where(g => g.IsSelected).Select(g => g.Name).ToList(); if (createdBioImageId.HasValue) { artist.BioImageId = createdBioImageId.Value; } await _artistRepo.UpdateAsync(artist.Id, artist); this.SetBootstrapPageAlert("Success", "Artist updated", BootstrapAlertType.success); return(RedirectToAction(nameof(Index))); } else { return(View(model)); } } else { // todo: show error message return(RedirectToAction(nameof(Index))); } }
public async Task <ImageReferenceDetail> AddAsync(ImageReferenceDetail image) { var dbImage = new DbImageResource() { CreatedUtc = DateTime.Now, UpdatedUtc = DateTime.Now, Filename = image.FileName, MimeType = image.MimeType, Data = image.Data }; this._context.ImageResources.Add(dbImage); await this._context.SaveChangesAsync(); image.Id = dbImage.Id; return(image); }
public async Task <IActionResult> Add(CreateArtistViewModel model) { if (ModelState.IsValid) { int?createdBioImageId = null; if (model.BioImage != null && model.BioImage.Length > 0) { using (MemoryStream ms = new MemoryStream()) { await model.BioImage.CopyToAsync(ms); ImageReferenceDetail imageRef = await _imageRepo.AddAsync(new ImageReferenceDetail { Data = ms.ToArray(), FileName = model.BioImage.FileName, MimeType = model.BioImage.ContentType }); if (imageRef != null) { createdBioImageId = imageRef.Id; } } } var result = await _artistRepo.AddAsync(new Artist { PublishedStatus = PublishStatus.UNPUBLISHED, Name = model.Name, BioText = model.BioText, Genres = model.Genres.Where(g => g.IsSelected).Select(g => g.Name).ToList(), BioImageId = createdBioImageId }); this.SetBootstrapPageAlert("Success", "Artist added", BootstrapAlertType.success); return(RedirectToAction(nameof(Index))); } else { return(View(model)); } }