コード例 #1
0
        public async Task <IActionResult> Create([Bind("Title,MediaUrl,IsFavorite,MediaAlbumId,MediaName")] MediaAlbumItemViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = await GetUserIdAsync();

                var media = await TryCreateFileAsync("MediaUrl", true, userId);

                if (media != null)
                {
                    Database.Add(media);

                    if (!string.IsNullOrWhiteSpace(model.MediaName))
                    {
                        media.FileName = SanitizeInput(model.MediaName, true);
                    }

                    // save in order to retrieve the media id from db
                    await Database.SaveChangesAsync();

                    // var album = await GetAlbumAsync(model.MediaAlbumId, userId);

                    var song = new MediaAlbumItem
                    {
                        // MediaAlbum = album,
                        MediaAlbumId = model.MediaAlbumId,
                        IsFavorite   = model.IsFavorite,
                        MediaType    = MediaType.Audio,
                        Title        = model.Title,
                        UserId       = userId,
                        MediaFileId  = media.Id,
                        MediaUrl     = media.GetMediaUrl()
                    };

                    Database.Add(song);
                    await Database.SaveChangesAsync();

                    return(RedirectToLocal(Request.Form["ReturnUrl"], true));
                }
                else
                {
                    ModelState.AddModelError("", "Couldn't save the audio file. Please try again.");
                }
            }
            await RetrieveAlbumsAsync();

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(long id, [Bind("Id,Title,IsFavorite,MediaUrl,MediaAlbumId,MediaName")] MediaAlbumItemViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var entity = await GetSongAsync(id, await GetUserIdAsync());

                    if (entity != null)
                    {
                        entity.Title = model.Title;
                        // entity.MediaUrl = model.MediaUrl;
                        entity.IsFavorite   = model.IsFavorite;
                        entity.MediaAlbumId = model.MediaAlbumId;

                        Database.Update(entity);
                        await Database.SaveChangesAsync();

                        return(RedirectToAction(nameof(Details), new { id }));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SongExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            await RetrieveAlbumsAsync(model.MediaAlbumId);

            return(View(model));
        }