diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index b542d50..b1e4610 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -6,4 +6,23 @@ def index def show @artist = Artist.find(params[:id]) end + + def new + @artist = Artist.new + end + + def create + @artist = Artist.new(artist_params) + if @artist.save + redirect_to @artist, notice: "Artist Created" + else + render :new, status: :unprocessable_entity + end + end + + private + + def artist_params + params.require(:artist).permit(:name) + end end diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index b1529a3..77d3e67 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -6,4 +6,42 @@ def index def show @track = Track.find(params[:id]) end + + def new + @track = Track.new + end + + def create + @track = Track.new(track_params) + @track.user = User.find_by(username: "Twhite") + if @track.save + redirect_to @track, notice: "Track Created!" + else + render :new, status: :unprocessable_entity + end + end + + def edit + @track = Track.find(params[:id]) + end + + def update + @track = Track.find(params[:id]) + @track.user = User.find_by(username: "Twhite") + if @track.update(track_params) + redirect_to @track, notice: "Track Updated" + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @track = Track.find(params[:id]) + @track.destroy + redirect_to dashboard_path, notice: "Track Deleted" + end + + def track_params + params.require(:track).permit(:title, :artist_id, :year, :bpm, :key, :notes) + end end diff --git a/app/helpers/tracks_helper.rb b/app/helpers/tracks_helper.rb new file mode 100644 index 0000000..4769c0f --- /dev/null +++ b/app/helpers/tracks_helper.rb @@ -0,0 +1,2 @@ +module TracksHelper +end diff --git a/app/models/track.rb b/app/models/track.rb index 07954bc..a617471 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -1,5 +1,5 @@ class Track < ApplicationRecord - belongs_to :user + belongs_to :user, optional: true belongs_to :artist has_many :comments, dependent: :destroy diff --git a/app/views/artists/_form.html.erb b/app/views/artists/_form.html.erb new file mode 100644 index 0000000..19f755a --- /dev/null +++ b/app/views/artists/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_with model: @artist do |form| %> + <% if @artist.errors.any? %> +
+ +
+ <% end %> + +
+ <%= form.label :name %>
+ <%= form.text_field :name %> +
+ +
+ <%= form.submit %> +
+ + <% end %> \ No newline at end of file diff --git a/app/views/artists/new.html.erb b/app/views/artists/new.html.erb new file mode 100644 index 0000000..711a9e3 --- /dev/null +++ b/app/views/artists/new.html.erb @@ -0,0 +1,2 @@ +

New Artist

+<%= render "form" %> \ No newline at end of file diff --git a/app/views/artists/show.html.erb b/app/views/artists/show.html.erb index 35d09fc..d35816a 100644 --- a/app/views/artists/show.html.erb +++ b/app/views/artists/show.html.erb @@ -1,4 +1,4 @@ -

<%= @artist.name %>

+

Artist: <%= @artist.name %>

Tracks

\ No newline at end of file + + +

+ <%= link_to "Add New Track", new_track_path %> +

\ No newline at end of file diff --git a/app/views/tracks/_form.html.erb b/app/views/tracks/_form.html.erb new file mode 100644 index 0000000..29ac095 --- /dev/null +++ b/app/views/tracks/_form.html.erb @@ -0,0 +1,48 @@ +<%= form_with model: @track do |form| %> + <% if @track.errors.any? %> +
+

<%= pluralize(@track.errors.count, "error") %> prohibited this track from being saved:

+ +
+ <% end %> + +
+ <%= form.label :title %>
+ <%= form.text_field :title %> +
+ +
+ <%= form.label :artist_id, "Artist" %>
+ <%= form.collection_select :artist_id, Artist.all, :id, :name, prompt: "Choose an artist" %> +
+

+ <%= link_to "Add New Artist", new_artist_path %> +

+
+ <%= form.label :year %>
+ <%= form.number_field :year %> +
+ +
+ <%= form.label :bpm %>
+ <%= form.number_field :bpm %> +
+ +
+ <%= form.label :key %>
+ <%= form.text_field :key %> +
+ +
+ <%= form.label :notes %>
+ <%= form.text_area :notes %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/tracks/edit.html.erb b/app/views/tracks/edit.html.erb new file mode 100644 index 0000000..ba907f3 --- /dev/null +++ b/app/views/tracks/edit.html.erb @@ -0,0 +1,2 @@ +

Edit Track

+<%= render "form" %> \ No newline at end of file diff --git a/app/views/tracks/new.html.erb b/app/views/tracks/new.html.erb new file mode 100644 index 0000000..6852bd1 --- /dev/null +++ b/app/views/tracks/new.html.erb @@ -0,0 +1,2 @@ +

New Track

+<%= render "form" %> \ No newline at end of file diff --git a/app/views/tracks/show.html.erb b/app/views/tracks/show.html.erb index 52201b4..534c482 100644 --- a/app/views/tracks/show.html.erb +++ b/app/views/tracks/show.html.erb @@ -1,4 +1,4 @@ -

<%= @track.title %>

+

Track: <%= @track.title %>

Artist: <%= @track.artist.name %>
Year: <%= @track.year %>
@@ -6,22 +6,40 @@

Samples Used

- + +<% if @track.sampled_tracks.any? %> + +<% else %> +

None

+<% end %>

Sampled By

- + +<% if @track.sampled_by_tracks.any? %> + +<% else %> +

None

+<% end %>

Comments

\ No newline at end of file + + +

+ <%= link_to "Edit Track", edit_track_path(@track) %> | + <%= link_to "Delete Track", track_path(@track), + data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to delete this track?" } %> +

\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index bdd92c0..b9c90f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - resources :artists, only: [ :index, :show ] - resources :tracks, only: [ :index, :show ] + resources :artists, only: [ :index, :show, :new, :create ] + resources :tracks, only: [ :index, :show, :new, :create, :edit, :update, :destroy ] get "/dashboard", to: "dashboard#index" diff --git a/spec/helpers/tracks_helper_spec.rb b/spec/helpers/tracks_helper_spec.rb new file mode 100644 index 0000000..02eafde --- /dev/null +++ b/spec/helpers/tracks_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the TracksHelper. For example: +# +# describe TracksHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe TracksHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/tracks_spec.rb b/spec/requests/tracks_spec.rb new file mode 100644 index 0000000..8ebc971 --- /dev/null +++ b/spec/requests/tracks_spec.rb @@ -0,0 +1,44 @@ +require 'rails_helper' + +RSpec.describe "Tracks" do + let!(:user) { User.create!(email: "twhite@example.com", username: "Twhite") } + let!(:artist) { Artist.create!(name: "Test Artist") } + + describe "POST /tracks" do + it "creates a new track" do + expect { + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + }.to change(Track, :count).by(1) + end + + it "redirects to the new track page" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(response).to redirect_to(track_path(Track.last)) + end + + it "saves the correct title" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(Track.last.title).to eq("New Song") + end + + it "saves the correct artist" do + post tracks_path, params: { track: { title: "New Song", year: 2024, artist_id: artist.id } } + expect(Track.last.artist).to eq(artist) + end + end + + describe "PATCH /tracks/:id" do + let!(:track) { Track.create!(title: "Old Song", year: 2020, user: user, artist: artist) } + + it "redirects after update" do + patch track_path(track), params: { track: { key: "C minor" } } + expect(response).to redirect_to(track_path(track)) + end + + it "updates the key" do + patch track_path(track), params: { track: { key: "C minor" } } + track.reload + expect(track.key).to eq("C minor") + end + end +end