public static async Task <Album> Create(DBContext context, AlbumPostData post_data) { Album newAlbum = null; try { // Check and make sure there's not an existing album with that name if (context.Albums.FirstOrDefault <Album>(a => a.Name == post_data.Name) != null) { return(newAlbum); } newAlbum = new Album(); newAlbum.Name = post_data.Name; newAlbum.Description = post_data.Description; newAlbum.Created = DateTime.Now; newAlbum.Updated = DateTime.Now; context.Albums.Add(newAlbum); await context.SaveChangesAsync(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(newAlbum); }
public async Task <IActionResult> UpdateAlbum(AlbumPostData post_data) { dynamic retVal = new ExpandoObject(); retVal.album = await Album.Update(_context, this, post_data); retVal.success = retVal.album != null; return(Json(retVal)); }
public async Task <IActionResult> DeleteAlbum(AlbumPostData post_data) { dynamic retVal = new ExpandoObject(); retVal.success = await Album.Delete(_context, post_data.ID); retVal.album_id = post_data.ID; if (!retVal.success) { retVal.response = string.Format("Unable to Delete Album ID [{0}].", post_data.ID); } return(Json(retVal)); }
public static async Task <Album> Update(DBContext context, Controller controller, AlbumPostData post_data) { Album update_album = null; try { // Make sure there's not an album with this name already if (context.Albums.FirstOrDefault <Album>(a => a.Name == post_data.Name && a.ID != post_data.ID) != null) { return(update_album); } update_album = await context.Albums.SingleOrDefaultAsync(A => A.ID == post_data.ID); update_album.Name = post_data.Name; update_album.Description = post_data.Description; update_album.Updated = DateTime.Now; if (await controller.TryUpdateModelAsync <Album>(update_album, "", a => a.Name, a => a.Description, a => a.Updated)) { try { await context.SaveChangesAsync(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(update_album); }