public async Task <LyricSlugCreateResultViewModel> AddLyricSlugAsync( CreateLyricSlugViewModel viewModel) { LyricSlugCreateResultViewModel result = new LyricSlugCreateResultViewModel(); string connectionString = _databaseOptions.ConnectionString; string sqlStatement = "insert into lyric_slugs (name, is_primary, created_at, is_deleted, lyric_id) values (@name, @is_primary, @created_at, @is_deleted, @lyric_id) returning name"; string name = viewModel.Name.NormalizeStringForUrl(); bool isPrimary = true; DateTime createdAt = DateTime.UtcNow; bool isDeleted = false; int lyricId = viewModel.LyricId; using (NpgsqlConnection connection = new NpgsqlConnection(connectionString)) { NpgsqlCommand command = new NpgsqlCommand(sqlStatement, connection); command.Parameters.AddWithValue("@name", name); command.Parameters.AddWithValue("@is_primary", isPrimary); command.Parameters.AddWithValue("@created_at", createdAt); command.Parameters.AddWithValue("@is_deleted", isDeleted); command.Parameters.AddWithValue("@lyric_id", lyricId); await connection.OpenAsync(); object artistSlug = command.ExecuteScalar(); result.LyricPrimarySlug = (string)artistSlug; } return(result); }
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); }