public async Task <ArtistCreationResult> AddArtistAsync( CreateArtistViewModel viewModel) { ArtistCreationResult result = new ArtistCreationResult(); string connectionString = _databaseOptions.ConnectionString; string sqlStatement = "insert into artists (first_name, last_name, full_name, is_approved, user_id, created_at, is_deleted, has_image) values (@first_name, @last_name, @full_name, @is_approved, @user_id, @created_at, @is_deleted, @has_image) returning id"; int artistId = 0; string firstName = viewModel.FirstName.ToLower(); string lastName = viewModel.LastName.ToLower(); string fullName = string.IsNullOrEmpty(lastName) ? firstName : $"{firstName} {lastName}"; bool isApproved = false; DateTime createdAt = DateTime.UtcNow; string userId = viewModel.UserId; bool isDeleted = false; bool hasImage = false; using (NpgsqlConnection connection = new NpgsqlConnection(connectionString)) { NpgsqlCommand command = new NpgsqlCommand(sqlStatement, connection); command.Parameters.AddWithValue("@first_name", firstName); command.Parameters.AddWithValue("@last_name", lastName); command.Parameters.AddWithValue("@full_name", fullName); command.Parameters.AddWithValue("@is_approved", isApproved); command.Parameters.AddWithValue("@user_id", userId); command.Parameters.AddWithValue("@created_at", createdAt); command.Parameters.AddWithValue("@is_deleted", isDeleted); command.Parameters.AddWithValue("@has_image", hasImage); try { await connection.OpenAsync(); object artistIdentity = command.ExecuteScalar(); artistId = (int)artistIdentity; ArtistSlugCreateViewModel artistSlug = new ArtistSlugCreateViewModel(); artistSlug.Name = fullName.NormalizeStringForUrl(); artistSlug.IsPrimary = true; artistSlug.CreatedAt = createdAt; artistSlug.ArtistId = artistId; await _artistSlugsService.AddArtistSlugAsync(artistSlug); result.PrimarySlug = artistSlug.Name; } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(result); }
public async Task <IActionResult> Create( CreateArtistViewModel viewModel) { try { viewModel.UserId = User .GetUserId() .ToString(); ArtistCreationResult result = await _artistsService .AddArtistAsync(viewModel); return(RedirectToAction("ArtistLyrics", "Lyric", new { artistSlug = result.PrimarySlug })); } catch { return(View(viewModel)); } }