Skip to content

Commit

Permalink
Working in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardobrito committed Oct 9, 2023
1 parent 5ca04ae commit 640fa47
Show file tree
Hide file tree
Showing 30 changed files with 28,977 additions and 14 deletions.
Binary file added .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ GEM
json (2.6.3)
json-schema (3.0.0)
addressable (>= 2.8)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
kwalify (0.7.2)
language_server-protocol (3.17.0.3)
loofah (2.21.3)
Expand Down Expand Up @@ -439,6 +451,7 @@ DEPENDENCIES
factory_bot_rails
importmap-rails
jbuilder
kaminari
pg
pry (~> 0.14.2)
puma (~> 5.0)
Expand Down
19 changes: 19 additions & 0 deletions app/chewy/songs_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class SongsIndex < Chewy::Index
index_scope Song

settings analysis: {
analyzer: {
email: {
tokenizer: "keyword",
filter: ["lowercase"]
}
}
}

field :title, type: :text
field :artist, type: :text
field :genre, type: :keyword
field :lyrics, type: :text
end
76 changes: 76 additions & 0 deletions app/controllers/songs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class SongsController < ApplicationController
before_action :set_song, only: %i[ show edit update destroy ]

# GET /songs or /songs.json
def index
# @songs = Song.all
query = params["query"] || ""
genre = params["genre"] || ""
filters = { keywords: query, genre: genre }

@songs = Songs::Search.new(filters: filters).search
@songs
end

# GET /songs/1 or /songs/1.json
def show
end

# GET /songs/new
def new
@song = Song.new
end

# GET /songs/1/edit
def edit
end

# POST /songs or /songs.json
def create
@song = Song.new(song_params)

respond_to do |format|
if @song.save
format.html { redirect_to song_url(@song), notice: "Song was successfully created." }
format.json { render :show, status: :created, location: @song }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /songs/1 or /songs/1.json
def update
respond_to do |format|
if @song.update(song_params)
format.html { redirect_to song_url(@song), notice: "Song was successfully updated." }
format.json { render :show, status: :ok, location: @song }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end

# DELETE /songs/1 or /songs/1.json
def destroy
@song.destroy

respond_to do |format|
format.html { redirect_to songs_url, notice: "Song was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_song
@song = Song.find(params[:id])
end

# Only allow a list of trusted parameters through.
def song_params
params.require(:song).permit(:title, :artist, :genre, :lyrics)
end
end
2 changes: 2 additions & 0 deletions app/helpers/songs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SongsHelper
end
18 changes: 18 additions & 0 deletions app/models/song.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'csv'

class Song < ApplicationRecord
update_index("songs") { self }

def self.import_csv!
filepath = "tcc_ceds_music.csv"
res = CSV.parse(File.read(filepath), headers: true)
res.each_with_index do |s, ind|
Song.create!(
artist: s["artist_name"],
title: s["track_name"],
genre: s["genre"],
lyrics: s["lyrics"]
)
end
end
end
39 changes: 39 additions & 0 deletions app/models/songs/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Songs
class Search
def initialize(filters:, scope: SongsIndex.all, page: 1, per_page: 5)
@scope = scope
@keywords = filters.fetch(:keywords, "")
@genre = filters.fetch(:genre)
@page = page
@per_page = per_page
end

def search
apply_filters.objects
end

private

attr_reader :keywords, :genre, :scope, :page, :per_page

def apply_filters
[
genre_filter,
query_multi_match
].compact.reduce(:merge)
end

def query_multi_match
scope.query(multi_match: {
query: keywords,
fields: [ :title, :artist, :lyrics ],
operator: :and,
type: :cross_fields
}) if keywords.present?
end

def genre_filter
scope.query(term: { genre: genre }) if genre.present?
end
end
end
37 changes: 37 additions & 0 deletions app/views/songs/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= form_with(model: song) do |form| %>
<% if song.errors.any? %>
<div style="color: red">
<h2><%= pluralize(song.errors.count, "error") %> prohibited this song from being saved:</h2>

<ul>
<% song.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :artist, style: "display: block" %>
<%= form.text_field :artist %>
</div>

<div>
<%= form.label :genre, style: "display: block" %>
<%= form.text_field :genre %>
</div>

<div>
<%= form.label :lyrics, style: "display: block" %>
<%= form.text_area :lyrics %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
22 changes: 22 additions & 0 deletions app/views/songs/_song.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div id="<%= dom_id song %>">
<p>
<strong>Title:</strong>
<%= song.title %>
</p>

<p>
<strong>Artist:</strong>
<%= song.artist %>
</p>

<p>
<strong>Genre:</strong>
<%= song.genre %>
</p>

<p>
<strong>Lyrics:</strong>
<%= song.lyrics %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/songs/_song.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! song, :id, :title, :artist, :genre, :lyrics, :created_at, :updated_at
json.url song_url(song, format: :json)
10 changes: 10 additions & 0 deletions app/views/songs/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing song</h1>

<%= render "form", song: @song %>

<br>

<div>
<%= link_to "Show this song", @song %> |
<%= link_to "Back to songs", songs_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/songs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Songs</h1>

<div id="songs">
<% @songs.each do |song| %>
<%= render song %>
<p>
<%= link_to "Show this song", song %>
</p>
<% end %>
</div>

<%= link_to "New song", new_song_path %>
1 change: 1 addition & 0 deletions app/views/songs/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @songs, partial: "songs/song", as: :song
9 changes: 9 additions & 0 deletions app/views/songs/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New song</h1>

<%= render "form", song: @song %>

<br>

<div>
<%= link_to "Back to songs", songs_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/songs/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @song %>

<div>
<%= link_to "Edit this song", edit_song_path(@song) %> |
<%= link_to "Back to songs", songs_path %>

<%= button_to "Destroy this song", @song, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/songs/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "songs/song", song: @song
13 changes: 0 additions & 13 deletions config/initializers/chewy.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
# frozen_string_literal: true

Chewy.strategy(:atomic)
Chewy.request_strategy = :sidekiq
Chewy.logger = Logger.new($stdout)
Chewy.logger.level = Logger::INFO
Chewy.settings = {
strategy_config: {
delayed_sidekiq: {
latency: 3,
margin: 2,
ttl: 60 * 60 * 24,
reindex_wrapper: lambda { |&reindex|
ActiveRecord::Base.connected_to(role: :reading) { reindex.call }
}
}
}
}
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "sidekiq/web"

Rails.application.routes.draw do
resources :songs
mount Sidekiq::Web => "/sidekiq"
mount Rswag::Ui::Engine => "/api-docs"
mount Rswag::Api::Engine => "/api-docs"
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20231006010408_create_songs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateSongs < ActiveRecord::Migration[7.0]
def change
create_table :songs do |t|
t.string :title
t.string :artist
t.string :genre
t.text :lyrics

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/factories/songs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :song do
title { "MyString" }
artist { "MyString" }
genre { "MyString" }
lyrics { "MyText" }
end
end
15 changes: 15 additions & 0 deletions spec/helpers/songs_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the SongsHelper. For example:
#
# describe SongsHelper 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 SongsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/song_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Song, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading

0 comments on commit 640fa47

Please sign in to comment.