public async Task <IActionResult> Create( CreateLyricViewModel viewModel) { string userId = User .GetUserId() .ToString(); viewModel.UserId = userId; LyricCreateResultViewModel result = await _lyricsService .AddLyricAsync(viewModel); return(RedirectToAction("Like", "Lyric", new { artistSlug = result.ArtistSlug, lyricSlug = result.LyricSlug, lyricId = result.LyricId })); }
public async Task <LyricCreateResultViewModel> AddLyricAsync( CreateLyricViewModel viewModel) { LyricCreateResultViewModel result = new LyricCreateResultViewModel(); string connectionString = _databaseOptions.ConnectionString; string sqlStatement = "insert into lyrics (title, body, user_id, created_at, is_deleted, is_approved, artist_id, is_verified) values (@title, @body, @user_id, @created_at, @is_deleted, @is_approved, @artist_id, @is_verified) returning id;"; string title = viewModel.Title.ToLower().FirstCharToUpper(); string body = viewModel.Body; string userId = viewModel.UserId; DateTime createdAt = DateTime.UtcNow; bool isDeleted = false; bool isApproved = false; int artistId = viewModel.Artist.Id; bool isVerified = false; using (NpgsqlConnection connection = new NpgsqlConnection(connectionString)) { NpgsqlCommand command = new NpgsqlCommand(sqlStatement, connection); command.Parameters.AddWithValue("@title", title); command.Parameters.AddWithValue("@body", body); command.Parameters.AddWithValue("@user_id", userId); command.Parameters.AddWithValue("@created_at", createdAt); command.Parameters.AddWithValue("@is_deleted", isDeleted); command.Parameters.AddWithValue("@is_approved", isApproved); command.Parameters.AddWithValue("@artist_id", artistId); command.Parameters.AddWithValue("@is_verified", isVerified); await connection.OpenAsync(); object lyricIdentity = command.ExecuteScalar(); int lyricId = (int)lyricIdentity; CreateLyricSlugViewModel createLyricSlugModel = new CreateLyricSlugViewModel(); createLyricSlugModel.Name = title.NormalizeStringForUrl(); createLyricSlugModel.LyricId = lyricId; LyricSlugCreateResultViewModel slugCreationResult = await AddLyricSlugAsync(createLyricSlugModel); result.LyricId = lyricId; result.ArtistSlug = viewModel.Artist.PrimarySlug; result.LyricSlug = slugCreationResult.LyricPrimarySlug; } return(result); }