コード例 #1
0
        public async Task <IActionResult> AddSongTheme(int songId, string theme)
        {
            //Make sure the song exists, if not redirect to ItemNotFound page
            if (songId < 1)
            {
                return(new BadRequestResult());
            }

            //Make sure the user actually submitted text. If not, do nothing
            if (!string.IsNullOrWhiteSpace(theme))
            {
                //Check to see if this theme already exists
                var existingThemeId = await _themeService.CheckIfThemeExistsAsync(theme);

                if (existingThemeId == null)
                {
                    //if we're here the theme deosn't exist, create new theme
                    var themeId = await _themeService.AddThemeAsync(theme, GetUserId());

                    //Now create a new SongTheme using the ThemeId from the theme that was just created
                    await _themeService.AddSongThemeAsync(songId, themeId, GetUserId());
                }
                else
                {
                    //if we're here, the theme already exists. Check to see if a SongTheme already exists.
                    var existingSongThemeId = await _themeService.CheckIfSongThemeExistsAsync(existingThemeId.GetValueOrDefault(), songId);

                    if (existingSongThemeId == null)
                    {
                        //if we're here, the SongTheme doesn't exist, create a new one (will also create a SongThemeUser for the current user)
                        await _themeService.AddSongThemeAsync(songId, existingThemeId.GetValueOrDefault(), GetUserId());
                    }
                    else
                    {
                        //if we're here, the SongTheme already exists. Create a SongThemeUser for the current user.
                        await _themeService.AddSongThemeUserAsync(existingSongThemeId.GetValueOrDefault(), GetUserId());
                    }
                }
            }

            return(PartialView("SongThemes", new SongThemesViewModel {
                SongThemes = await _themeService.GetSongThemesBySongIdAsync(songId), UserId = GetUserId(), SongId = songId
            }));
        }