Skip to content

Commit

Permalink
event comments(events): New comments model
Browse files Browse the repository at this point in the history
Permits comments to be attached to Events
  • Loading branch information
mrmod committed Mar 16, 2020
1 parent 45276eb commit 9e99874
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rails g model Social name:string url:text handle:string business:references --fo
rails g model Event name:string theme:text description:text start_time:timestamp end_time:timestamp parent_id:bigint business:references --force
rails g model Post topic:text text:text private:boolean employee:references --force
rails g model Comment text:text post:references comment_id:bigint employee:references --force
rails g model EventComment text:text event:references event_comment_id:bigint employee:references --force
rails g model BusinessGroup name:string description:text private:boolean --force
rails g model BusinessGroupMember business_group:references business:references --force
rails g model PostMember post:references employee:references role:integer --force
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/api/v1/event_comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::V1::EventCommentsController < ApplicationController
def index
@event = Event.find_by_id params[:event_id]
if !@event
return 404
end
respond_to do |format|
format.json { render json: @event.comments }
end
end
end
13 changes: 13 additions & 0 deletions app/javascript/services/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {authorizedJSONHeaders, railsErrorHandler} from './requests'

const allEventsURL = '/api/events'
const oneEventUrl = id => `${allEventsURL}/${id}`
const eventCommentsUrl = id => `${allEventsURL}/${id}/comments`


export const getAllEvents = () => axios.get(allEventsURL, authorizedJSONHeaders)
.then(r => r.data)
Expand Down Expand Up @@ -35,6 +37,17 @@ export const getOneEvent = id => axios.get(oneEventUrl(id), authorizedJSONHeader
error: null,
}))
.catch(railsErrorHandler)

export const getEventComments = id => axios.get(
eventCommentsUrl(id),
authorizedJSONHeaders
).then(r => r.data)
.then(data => ({
data: data,
error: null,
}))
.catch(railsErrorHandler)

export const updateEvent = (id, event) => axios.put(
oneEventUrl(id),
{event},
Expand Down
9 changes: 9 additions & 0 deletions app/models/event_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class EventComment < ApplicationRecord
belongs_to :event
belongs_to :employee
belongs_to :comment, optional: true
has_many :tags, as: :taggable
has_many :media, as: :mediumable

validates :text, presence: true
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
resources :media, controller: 'v1/media'
end
resources :events, controller: 'v1/events' do
resources :comments, controller: 'v1/event_comments'
resources :media, controller: 'v1/event_media'
resources :tags, controller: 'v1/tags'
resources :locations, controller: 'v1/locations'
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20200316004611_create_event_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateEventComments < ActiveRecord::Migration[6.0]
def change
create_table :event_comments do |t|
t.text :text
t.references :event, null: false, foreign_key: true
t.bigint :event_comment_id
t.references :employee, null: false, foreign_key: true

t.timestamps
end
end
end
13 changes: 13 additions & 0 deletions test/fixtures/event_comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
text: MyText
event: one
event_comment_id:
employee: one

two:
text: MyText
event: two
event_comment_id:
employee: two
7 changes: 7 additions & 0 deletions test/models/event_comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class EventCommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 9e99874

Please sign in to comment.