diff --git a/.gitignore b/.gitignore index bb6354a..115118e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ *.swp /config/key.yml .rvmrc +.DS_Store diff --git a/Gemfile b/Gemfile index 92c8da2..6b83616 100644 --- a/Gemfile +++ b/Gemfile @@ -59,3 +59,5 @@ gem 'rails_admin', '>= 1.0.0.rc' gem 'annotate' gem 'omniauth-facebook' gem 'httparty' +gem 'simple_token_authentication', '~> 1.0' +gem 'bitly' diff --git a/Gemfile.lock b/Gemfile.lock index 306c695..26a9144 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -49,6 +49,10 @@ GEM rake (>= 10.4, < 12.0) arel (7.1.1) bcrypt (3.1.11) + bitly (0.10.4) + httparty (>= 0.7.6) + multi_json (~> 1.3) + oauth2 (>= 0.5.0, < 2.0) builder (3.2.2) byebug (9.0.5) carrierwave (1.0.0.beta) @@ -192,6 +196,10 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + simple_token_authentication (1.14.0) + actionmailer (>= 3.2.6, < 6) + actionpack (>= 3.2.6, < 6) + devise (>= 3.2, < 6) spring (1.7.2) spring-watcher-listen (2.0.0) listen (>= 2.7, < 4.0) @@ -227,6 +235,7 @@ PLATFORMS DEPENDENCIES annotate + bitly byebug carrierwave (>= 1.0.0.beta, < 2.0) coffee-rails (~> 4.2) @@ -242,6 +251,7 @@ DEPENDENCIES rails_admin (>= 1.0.0.rc) remotipart! sass-rails (~> 5.0) + simple_token_authentication (~> 1.0) spring spring-watcher-listen (~> 2.0.0) sqlite3 diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 0ebd7fe..f984b83 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,17 @@ *= require_tree . *= require_self */ + +body { + min-height: 2000px; + padding-top: 70px; +} + +.row.courses { + text-align: center; +} + +.nopadding { + padding: 0 !important; + margin: 0 !important; +} diff --git a/app/assets/stylesheets/courses.scss b/app/assets/stylesheets/courses.scss index a54ff0f..f45e0d5 100644 --- a/app/assets/stylesheets/courses.scss +++ b/app/assets/stylesheets/courses.scss @@ -4,8 +4,8 @@ #course-title { min-height: 50px; - position: fixed; - top: 0; + //position: fixed; + //top: 0; background-color: #f8f8f8; border-color: #e7e7e7; border: 1px solid transparent; @@ -88,4 +88,8 @@ a#btn-new-post { position: fixed; bottom: 0; border-radius: 0; -} \ No newline at end of file +} + +.users { + text-align: left; +} diff --git a/app/assets/stylesheets/posts.scss b/app/assets/stylesheets/posts.scss index 4507eee..32c7698 100644 --- a/app/assets/stylesheets/posts.scss +++ b/app/assets/stylesheets/posts.scss @@ -1,7 +1,6 @@ // Place all the styles related to the posts controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ - #post-title { min-height: 50px; position: fixed; @@ -31,4 +30,4 @@ max-width: 100%; height: auto; } -} \ No newline at end of file +} diff --git a/app/controllers/api/comments_controller.rb b/app/controllers/api/comments_controller.rb new file mode 100644 index 0000000..c71e91e --- /dev/null +++ b/app/controllers/api/comments_controller.rb @@ -0,0 +1,66 @@ +class Api::CommentsController < ApplicationController + # TODO pagination + def index + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + comments = post.comments + + render :json => { + comments: comments + } + else + render :json => { + comments: [] + }, :status => 401 + end + end + + def create + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + comment = Comment.new(comment_params) + comment.user = current_user + comment.course = post.course + comment.save + + render :json => { + comment: comment + } + else + render :json => { + comment: {} + }, :status => 401 + end + end + + def update + comment = Comment.find(params[:id]) + + if comment.user_id == current_user.id + comment.update(comment_params) + + render :json => { + comment: comment + } + else + render :json => {}, :status => 401 + end + end + + def delete + comment = Comment.find(params[:id]) + + if comment.user_id == current_user.id + comment.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def comment_params + params.require(:comment).permit(:content) + end +end diff --git a/app/controllers/api/courses_controller.rb b/app/controllers/api/courses_controller.rb new file mode 100644 index 0000000..a250afc --- /dev/null +++ b/app/controllers/api/courses_controller.rb @@ -0,0 +1,94 @@ +class Api::CoursesController < ApplicationController + # TODO pagintation + def index + courses = Course.all + my_courses = current_user.courses + + render :json => { + courses: courses, + my_courses: my_courses + } + end + + def create + course = Course.new(course_params) + course.manager = current_user + course.save + + course.generate_short_link + course.join(current_user) + + render :json => { + course: course + } + end + + def join + course = Course.find(params[:id]) + course.join(current_user) + + render :json => { + course: course + } + end + + def leave + course = Course.find(params[:id]) + course.leave(current_user) + + render :json => { + } + end + + def show + respond_to do |format| + format.html { + @course = Course.find(params[:id]) + } + format.json { + course = Course.find(params[:id]) + + if current_user.enrolled?(course.id) + render :json => { + course: course + } + else + render :json => { + course: {} + }, :status => 401 + end + } + end + end + + def update + course = Course.find(params[:id]) + + if course.manager_id == current_user.id + course.update(course_params) + + render :json => { + course: course + } + else + render :json => {}, :status => 401 + end + end + + def delete + course = Course.find(params[:id]) + + if course.manager_id == current_user.id + # TODO need to add condition + course.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def course_params + #params.require(:course).permit(:name) + params.require(:course).permit(:name, :attachment, :date) + end +end diff --git a/app/controllers/api/photos_controller.rb b/app/controllers/api/photos_controller.rb new file mode 100644 index 0000000..c44c7c8 --- /dev/null +++ b/app/controllers/api/photos_controller.rb @@ -0,0 +1,127 @@ +class Api::PhotosController < ApplicationController + def index + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + photos = post.photos + + render :json => { + photos: photos + } + else + render :json => { + photos: [] + }, :status => 401 + end + end + + def create + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + photo = Photo.new(photo_params) + photo.user = current_user + photo.post = post + photo.save + + render :json => { + photo: photo + } + else + render :json => { + photo: {} + }, :status => 401 + end + end + + def show + photo = Photo.find(params[:id]) + + if current_user.enrolled?(params[:course_id]) + render :json => { + photo: photo.attachment, + date: photo.created_at + } + else + render :json => { + photo: {}, + date: nil + }, :status => 401 + end + end + + def update + photo = Photo.find(params[:id]) + + if photo.user_id == current_user.id + photo.update(photo_params) + + render :json => { + photo: photo + } + else + render :json => {}, :status => 401 + end + end + + def delete + photo = Photo.find(params[:id]) + + if photo.user_id == current_user.id + photo.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def add_tag + course = Course.find(params[:course_id]) + + # TODO remove duplicated tag on post_id, user_id + if current_user.enrolled?(course.id) + tag = TaggedUser.new(tag_params) + tag.user = current_user + tag.photo_id = params[:photo_id] + tag.save + + render :json => { + tag: tag + } + else + render :json => {}, :status => 401 + end + end + + def edit_tag + course = Course.find(params[:course_id]) + tag = TaggedUser.find(params[:id]) + + if tag.user_id == current_user.id || course.manager_id == current_user.id + tag.update(tag_params) + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def remove_tag + course = Course.find(params[:course_id]) + tag = TaggedUser.find(params[:id]) + + if tag.user_id == current_user.id || course.manager_id == current_user.id + tag.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def photo_params + params.require(:photo).permit(:attachment, :post_id) + end + + def tag_params + params.require(:tag).permit(:x, :y, :width, :height) + end +end diff --git a/app/controllers/api/posts_controller.rb b/app/controllers/api/posts_controller.rb new file mode 100644 index 0000000..e2abe84 --- /dev/null +++ b/app/controllers/api/posts_controller.rb @@ -0,0 +1,105 @@ +class Api::PostsController < ApplicationController + # TODO pagination + def index + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + posts = course.posts + + render :json => { + posts: posts + } + else + render :json => { + posts: [] + }, :status => 401 + end + end + + def create + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + post = Post.new(post_params) + post.user = current_user + post.course = course + post.save + + respond_to do |format| + format.html { + photo = Photo.new(photo_params) + photo.user = current_user + photo.save + + redirect_to(:back) + } + format.json { + render :json => { + post: post + } + } + end + else + render :json => { + post: {} + }, :status => 401 + end + end + + def show + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + post = Post.find(params[:id]) + + respond_to do |format| + format.html { + @course_name = course.name + @post = course.posts.find(params[:id]) + } + format.json { + render :json => { + post: post + } + } + end + else + render :json => { + post: {} + }, :status => 401 + end + end + + def update + post = Post.find(params[:id]) + + if post.user_id == current_user.id + post.update(post_params) + + render :json => { + post: post + } + else + render :json => {}, :status => 401 + end + end + + def delete + post = Post.find(params[:id]) + + if post.user_id == current_user.id + post.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def post_params + params.require(:post).permit(:content) + end + + def photo_params + params.require(:post).permit(:attachment, :post_id) + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/api/users_controller.rb similarity index 77% rename from app/controllers/users_controller.rb rename to app/controllers/api/users_controller.rb index 4669f33..5f43f6b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/api/users_controller.rb @@ -1,4 +1,6 @@ -class UsersController < ApplicationController +class Api::UsersController < ApplicationController + acts_as_token_authentication_handler_for User, except: [:create] + def create token = params[:token] @@ -6,8 +8,6 @@ def create access_token: token }).parsed_response - logger.info facebook_data - # You need to implement the method below in your model (e.g. app/models/user.rb) @user = User.find_for_facebook_oauth(facebook_data) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b6f240a..fc2662d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ class ApplicationController < ActionController::Base - #protect_from_forgery with: :exception + #protect_from_forgery with: :exception + + acts_as_token_authentication_handler_for User end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7669955..78209ac 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,2 +1,66 @@ class CommentsController < ApplicationController + # TODO pagination + def index + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + comments = post.comments + + render :json => { + comments: comments + } + else + render :json => { + comments: [] + }, :status => 401 + end + end + + def create + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + comment = Comment.new(comment_params) + comment.user = current_user + comment.course = post.course + comment.save + + render :json => { + comment: comment + } + else + render :json => { + comment: {} + }, :status => 401 + end + end + + def update + comment = Comment.find(params[:id]) + + if comment.user_id == current_user.id + comment.update(comment_params) + + render :json => { + comment: comment + } + else + render :json => {}, :status => 401 + end + end + + def delete + comment = Comment.find(params[:id]) + + if comment.user_id == current_user.id + comment.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def comment_params + params.require(:comment).permit(:content) + end end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 485a184..39819d6 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1,22 +1,67 @@ class CoursesController < ApplicationController + # TODO pagintation def index - @course = Course.all - render :json => {courses: @course} + @courses = Course.all + @my_courses = current_user.courses end + def new + @course = Course.new + end + + def edit + @course = Course.find(params[:id]) + end + + def create + course = Course.new(course_params) + course.manager = current_user + course.save + + course.generate_short_link + course.join(current_user) + + redirect_to course_path(course) + end + + def join + @course = Course.find(params[:id]) + render :layout => false + end + + def leave + course = Course.find(params[:id]) + course.leave(current_user) + + redirect_to courses_path + end + def show - @course = Course.find(params[:id]) - render :layout => true + @course = Course.find(params[:id]) end def update - @course = Course.find(params[:id]) - @posts = @course.posts - @photos = @posts.first - redirect_to(:back) - end + course = Course.find(params[:id]) - def course_params - params.require(:course).permit(:name, :attachment, :date, :short_link, :manager_id) + if course.manager_id == current_user.id + course.update(course_params) + end + + redirect_to course_path(course) end -end \ No newline at end of file + + def delete + course = Course.find(params[:id]) + + if course.manager_id == current_user.id + # TODO need to add condition + course.delete + end + + redirect_to courses_path + end + + def course_params + params.require(:course).permit(:name, :attachment, :date) + end +end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 4c906d9..b81ac36 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -1,25 +1,126 @@ class PhotosController < ApplicationController - def index - @course = Course.find(params[:course_id]) - post = @course.posts.find(params[:post_id]) - photos = post.photos - render :json => {photos: photos} + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + @photos = post.photos + end end + + # TODO + def create + post = Post.find(params[:post_id]) - def show - @course = Course.find(params[:course_id]) - post = @course.posts.find(params[:post_id]) - photo = post.photos.find(params[:id]) + if current_user.enrolled?(post.course_id) + photo = Photo.new(photo_params) + photo.user = current_user + photo.post = post + photo.save - render :json => {photo: photo.attachment, date: photo.created_at} + render :json => { + photo: photo + } + else + render :json => { + photo: {} + }, :status => 401 + end end - def create - render :json => {photo: nil} + # TODO + def show + photo = Photo.find(params[:id]) + + if current_user.enrolled?(params[:course_id]) + render :json => { + photo: photo.attachment, + date: photo.created_at + } + else + render :json => { + photo: {}, + date: nil + }, :status => 401 + end end + # TODO + def update + photo = Photo.find(params[:id]) + + if photo.user_id == current_user.id + photo.update(photo_params) + + render :json => { + photo: photo + } + else + render :json => {}, :status => 401 + end + end + + # TODO + def delete + photo = Photo.find(params[:id]) + + if photo.user_id == current_user.id + photo.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + # TODO + def add_tag + course = Course.find(params[:course_id]) + + # TODO remove duplicated tag on post_id, user_id + if current_user.enrolled?(course.id) + tag = TaggedUser.new(tag_params) + tag.user = current_user + tag.photo_id = params[:photo_id] + tag.save + + render :json => { + tag: tag + } + else + render :json => {}, :status => 401 + end + end + + # TODO + def edit_tag + course = Course.find(params[:course_id]) + tag = TaggedUser.find(params[:id]) + + if tag.user_id == current_user.id || course.manager_id == current_user.id + tag.update(tag_params) + render :json => {} + else + render :json => {}, :status => 401 + end + end + + # TODO + def remove_tag + course = Course.find(params[:course_id]) + tag = TaggedUser.find(params[:id]) + + if tag.user_id == current_user.id || course.manager_id == current_user.id + tag.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + def photo_params params.require(:photo).permit(:attachment, :post_id) end + + def tag_params + params.require(:tag).permit(:x, :y, :width, :height) + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a7c1c84..2112ee0 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,39 +1,80 @@ class PostsController < ApplicationController - def index - @course = Course.find(params[:course_id]) - posts = @course.posts - render :json => {posts: posts} - end + # TODO pagination + # TODO + def index + course = Course.find(params[:course_id]) - def show - course = Course.find(params[:course_id]) - @course_name = course.name - @post = course.posts.find(params[:id]) - render :layout => true - end - - def create - @course = Course.find(params[:course_id]) - post = @course.posts.create(post_params) - photo = post.photos.create(photo_params) - - if post.save - redirect_to(:back) - else - logger.info photo.errors.to_s - render :json => {post: nil} - end - end + if current_user.enrolled?(course.id) + posts = course.posts - def photo_params - params.require(:post).permit(:attachment, :post_id) - end + render :json => { + posts: posts + } + else + render :json => { + posts: [] + }, :status => 401 + end + end - def post_params - params.require(:post).permit(:course_id) - end + def create + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + post = Post.new(post_params) + post.user = current_user + post.course = course + post.save + + photo = Photo.new(photo_params) + photo.user = current_user + photo.save + end + + redirect_to(:back) + end + + def show + course = Course.find(params[:course_id]) - def course_params - params.require(:post).permit(:id) + if current_user.enrolled?(course.id) + @course_name = course.name + @post = course.posts.find(params[:id]) + end + end + + # TODO + def update + post = Post.find(params[:id]) + + if post.user_id == current_user.id + post.update(post_params) + + render :json => { + post: post + } + else + render :json => {}, :status => 401 + end + end + + # TODO + def delete + post = Post.find(params[:id]) + + if post.user_id == current_user.id + post.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def post_params + params.require(:post).permit(:content) + end + + def photo_params + params.require(:post).permit(:attachment, :post_id) end end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 47fd9b5..e4f4bed 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,2 +1,9 @@ class WelcomeController < ApplicationController -end \ No newline at end of file + acts_as_token_authentication_handler_for User, except: [:index] + + def index + @msg = "Hello DeepCheck!" + + redirect_to courses_path if user_signed_in? + end +end diff --git a/app/models/admin.rb b/app/models/admin.rb index 4cae570..6d53dce 100644 --- a/app/models/admin.rb +++ b/app/models/admin.rb @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: admins +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# + class Admin < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable diff --git a/app/models/comment.rb b/app/models/comment.rb index 4a019df..095dd62 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: comments +# +# id :integer not null, primary key +# post_id :integer +# user_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + class Comment < ApplicationRecord belongs_to :post belongs_to :user diff --git a/app/models/course.rb b/app/models/course.rb index 3a13a8d..1f64ce0 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,54 @@ +# == Schema Information +# +# Table name: courses +# +# id :integer not null, primary key +# name :string +# short_link :string +# manager_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + class Course < ApplicationRecord + include Rails.application.routes.url_helpers has_many :posts has_many :course_users has_many :users, :through => :course_users - #belongs_to :manager, :class_name => "User", :foreign_key => :manager_id + belongs_to :manager, :class_name => "User", :foreign_key => :manager_id + + after_create :generate_short_link + + def join(user) + cu = CourseUser.where(:course_id => self.id, :user_id => user.id).first + if cu.nil? + cu = CourseUser.new + cu.user = user + cu.course = self + cu.save + end + cu + end + + def leave(user) + cu = CourseUser.where(:course_id => self.id, :user_id => user.id).first + cu.delete if !cu.nil? + end + + def ios_link_course_url + url_for(controller: 'courses', + action: 'join', + id: self.id, + only_path: false) + end + + def generate_short_link + link = self.ios_link_course_url + bitly = Bitly.new("deepcheck", KEYS['bitly']) + + shorten = bitly.shorten(link) + + self.short_link = shorten.short_url + self.save + end end diff --git a/app/models/course_user.rb b/app/models/course_user.rb index 078e6e7..84bcbbb 100644 --- a/app/models/course_user.rb +++ b/app/models/course_user.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: course_users +# +# id :integer not null, primary key +# course_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + class CourseUser < ApplicationRecord belongs_to :course belongs_to :user diff --git a/app/models/photo.rb b/app/models/photo.rb index 3550c33..59fc43d 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,7 +1,20 @@ +# == Schema Information +# +# Table name: photos +# +# id :integer not null, primary key +# post_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# attachment :string +# user_id :integer +# + class Photo < ApplicationRecord mount_uploader :attachment, AttachmentUploader belongs_to :post has_many :tagged_users + belongs_to :user has_many :users, :through => :tagged_users end diff --git a/app/models/post.rb b/app/models/post.rb index 34582cb..87583fc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,17 @@ +# == Schema Information +# +# Table name: posts +# +# id :integer not null, primary key +# course_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# user_id :integer +# + class Post < ApplicationRecord + belongs_to :user belongs_to :course has_many :photos @@ -7,4 +20,12 @@ class Post < ApplicationRecord def self.latest self.order(:created_at => :desc) end + + def tagged_users + users = [] + self.photos.each do |p| + users += p.users + end + users.uniq + end end diff --git a/app/models/tagged_user.rb b/app/models/tagged_user.rb index b052ea7..af347da 100644 --- a/app/models/tagged_user.rb +++ b/app/models/tagged_user.rb @@ -1,3 +1,18 @@ +# == Schema Information +# +# Table name: tagged_users +# +# id :integer not null, primary key +# photo_id :integer +# user_id :integer +# x :integer +# y :integer +# width :integer +# height :integer +# created_at :datetime not null +# updated_at :datetime not null +# + class TaggedUser < ApplicationRecord belongs_to :photo belongs_to :user diff --git a/app/models/user.rb b/app/models/user.rb index 1948111..b40d5e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,31 @@ +# == Schema Information +# +# Table name: users +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# provider :string +# uid :string +# authentication_token :string(30) +# + class User < ApplicationRecord + acts_as_token_authenticatable # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable - #devise :database_authenticatable, :registerable, - # :recoverable, :rememberable, :trackable, :validatable - - devise :omniauthable, :omniauth_providers => [:facebook] + devise :database_authenticatable,# :registerable, + :recoverable, :rememberable, :trackable, :validatable has_many :comments has_many :posts @@ -12,7 +33,12 @@ class User < ApplicationRecord has_many :photos, :through => :tagged_users has_many :course_users has_many :courses, :through => :course_users - has_many :managed_courses, :class_name => "Course", :inverse_of => :manager + has_many :managed_courses, :class_name => "Course", :foreign_key => :manager + + def enrolled?(course_id) + cu = CourseUser.where(:course_id => course_id, :user_id => self.id).first + !cu.nil? + end def self.find_for_facebook_oauth(data, provider = "facebook") uid = data['id'] @@ -21,7 +47,7 @@ def self.find_for_facebook_oauth(data, provider = "facebook") else user = User.new(:provider => "facebook", :uid => uid) user.email = "#{uid}@facebook.com" - #user.password = Digest::MD5.hexdigest(uid) + user.password = Digest::MD5.hexdigest(uid) user.save user end diff --git a/app/views/courses/_form.html.erb b/app/views/courses/_form.html.erb new file mode 100644 index 0000000..742ad39 --- /dev/null +++ b/app/views/courses/_form.html.erb @@ -0,0 +1,13 @@ +<%= form_for(@course, :html => {:class => "form-horizontal"}) do |f| %> +
+ <%= f.label :name, :class => "col-sm-2 control-label" %> +
+ <%= f.text_field :name, :class => "form-control", :placeholder => "Email" %> +
+
+
+
+ <%= f.submit :class => "btn btn-default" %> +
+
+<% end %> diff --git a/app/views/courses/edit.html.erb b/app/views/courses/edit.html.erb new file mode 100644 index 0000000..6ddee45 --- /dev/null +++ b/app/views/courses/edit.html.erb @@ -0,0 +1,3 @@ +

Edit Course

+ +<%= render 'form' %> diff --git a/app/views/courses/index.html.erb b/app/views/courses/index.html.erb new file mode 100644 index 0000000..22b6dbe --- /dev/null +++ b/app/views/courses/index.html.erb @@ -0,0 +1,29 @@ +

My Courses <%= link_to "Create", new_course_path %>

+ +
+ <% @my_courses.each do |course| %> +
+ Generic placeholder image +

<%= course.name %>

+

+ <%= link_to "View details »", course_path(course), :class => "btn btn-default" %> +

+
+ <% end %> +
+ +
+ +

All Courses

+ +
+ <% @courses.each do |course| %> +
+ Generic placeholder image +

<%= course.name %>

+

+ <%= link_to "View details »", course_path(course), :class => "btn btn-default" %> +

+
+ <% end %> +
diff --git a/app/views/courses/join.html.erb b/app/views/courses/join.html.erb new file mode 100644 index 0000000..61ebd62 --- /dev/null +++ b/app/views/courses/join.html.erb @@ -0,0 +1,3 @@ + diff --git a/app/views/courses/new.html.erb b/app/views/courses/new.html.erb new file mode 100644 index 0000000..5211bfb --- /dev/null +++ b/app/views/courses/new.html.erb @@ -0,0 +1,3 @@ +

New Course

+ +<%= render 'form' %> diff --git a/app/views/courses/show.html.erb b/app/views/courses/show.html.erb index 697c697..a626629 100644 --- a/app/views/courses/show.html.erb +++ b/app/views/courses/show.html.erb @@ -2,11 +2,12 @@

<%= @course.name %> + <%= link_to "Edit", edit_course_path(@course) %>

-
+
create
@@ -21,6 +22,15 @@

<%= post.created_at.strftime("%Y-%m-%d") %>

+ +
+ <% post.tagged_users.each do |u| %> + <%= u.name %> + <% end %> + <% (@course.users - post.tagged_users).each do |u| %> + <%= u.name %> + <% end %> +
<% end %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a5cf02f..0d77dd1 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,23 +1,64 @@ - - DeepcheckBackend - <%= csrf_meta_tags %> + + DeepcheckBackend + + <%= csrf_meta_tags %> - <%= stylesheet_link_tag 'application', media: 'all' %> - <%= javascript_include_tag 'application' %> + <%= javascript_include_tag 'application' %> - - + + - - - + + + - - + <%= stylesheet_link_tag 'application', media: 'all' %> + - - <%= yield %> - + + + + +
+ <%= yield %> +
+ diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb index 99d696f..1a7767e 100644 --- a/app/views/welcome/index.html.erb +++ b/app/views/welcome/index.html.erb @@ -1 +1 @@ -

Hello DeepCheck!

\ No newline at end of file +

<%= @msg %>

diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f71970..6e76336 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -51,4 +51,6 @@ # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + + Rails.application.routes.default_url_options[:host] = 'localhost:3000' end diff --git a/config/environments/production.rb b/config/environments/production.rb index ee5d650..2224404 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -83,4 +83,6 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + Rails.application.routes.default_url_options[:host] = 'deepcheck.herokuapp.com' end diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb index 4a994e1..6733967 100644 --- a/config/initializers/filter_parameter_logging.rb +++ b/config/initializers/filter_parameter_logging.rb @@ -1,4 +1,4 @@ # Be sure to restart your server when you modify this file. # Configure sensitive parameters which will be filtered from the log file. -Rails.application.config.filter_parameters += [:password] +Rails.application.config.filter_parameters += [:password, :token] diff --git a/config/initializers/simple_token_authentication.rb b/config/initializers/simple_token_authentication.rb new file mode 100644 index 0000000..69e51cd --- /dev/null +++ b/config/initializers/simple_token_authentication.rb @@ -0,0 +1,2 @@ +SimpleTokenAuthentication.configure do |config| +end diff --git a/config/key.yml.example b/config/key.yml.example index 4ccd448..ac86cfd 100644 --- a/config/key.yml.example +++ b/config/key.yml.example @@ -1,6 +1,7 @@ defaults: &defaults facebook_app_id: 'a1b2c3' facebook_app_secret: 'a1b2c3' + bitly: 'a1b2c3' development: <<: *defaults diff --git a/config/routes.rb b/config/routes.rb index e44db0c..ed87100 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,18 +1,40 @@ Rails.application.routes.draw do - devise_for :admins - mount RailsAdmin::Engine => '/admin', as: 'rails_admin' - #devise_for :users + # common + devise_for :admins + mount RailsAdmin::Engine => '/admin', as: 'rails_admin' - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - root "welcome#index" + # for webview + root "welcome#index" + devise_for :users + resources :courses do + get "/join" => "courses#join", :on => :member + resources :posts, :only => [:index, :create, :show, :update, :delete] do + resources :photos, :only => [:index, :create, :update, :delete] + resources :comments, :only => [:index, :create, :update, :delete] + end + end - - resources :courses, :only => [:index, :show, :update] do - resources :posts, :only => [:index, :show, :create] do - resources :photos, :only => [:index, :show, :create] - end - end + # for react-native + namespace :api do + authenticated :user do + resources :courses, :only => [:index, :create, :show, :update, :delete] do + post "/join" => "courses#join", :on => :member + delete "/leave" => "courses#leave", :on => :member - resources :users - resources :comments + resources :posts, :only => [:index, :create, :show, :update, :delete] do + resources :photos, :only => [:index, :create, :update, :delete] do + post '/tag' => "photos#add_tag", :on => :member + put '/tag/:tag_id' => "photos#edit_tag", :on => :member + delete '/tag/:tag_id' => "photos#remove_tag", :on => :member + end + resources :comments, :only => [:index, :create, :update, :delete] + end + end + resources :users + end + + unauthenticated :user do + resources :users, :only => [:create] + end + end end diff --git a/db/migrate/20160910140016_create_course_users.rb b/db/migrate/20160910140016_create_course_users.rb index 8f29d8a..89d01ad 100644 --- a/db/migrate/20160910140016_create_course_users.rb +++ b/db/migrate/20160910140016_create_course_users.rb @@ -7,7 +7,6 @@ def change t.timestamps end - add_index :course_users, :course_id - add_index :course_users, :user_id + add_index :course_users, [:course_id, :user_id], :unique => true end end diff --git a/db/migrate/20160913120422_add_user_id_to_posts.rb b/db/migrate/20160913120422_add_user_id_to_posts.rb new file mode 100644 index 0000000..346426c --- /dev/null +++ b/db/migrate/20160913120422_add_user_id_to_posts.rb @@ -0,0 +1,6 @@ +class AddUserIdToPosts < ActiveRecord::Migration[5.0] + def change + add_column :posts, :user_id, :integer + add_index :posts, :user_id + end +end diff --git a/db/migrate/20160913121942_add_authentication_token_to_users.rb b/db/migrate/20160913121942_add_authentication_token_to_users.rb new file mode 100644 index 0000000..0a1b45c --- /dev/null +++ b/db/migrate/20160913121942_add_authentication_token_to_users.rb @@ -0,0 +1,6 @@ +class AddAuthenticationTokenToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :authentication_token, :string, limit: 30 + add_index :users, :authentication_token, unique: true + end +end diff --git a/db/migrate/20160914052903_add_user_id_to_photos.rb b/db/migrate/20160914052903_add_user_id_to_photos.rb new file mode 100644 index 0000000..6392495 --- /dev/null +++ b/db/migrate/20160914052903_add_user_id_to_photos.rb @@ -0,0 +1,6 @@ +class AddUserIdToPhotos < ActiveRecord::Migration[5.0] + def change + add_column :photos, :user_id, :integer + add_index :photos, :user_id + end +end diff --git a/db/migrate/20160922042833_add_name_to_users.rb b/db/migrate/20160922042833_add_name_to_users.rb new file mode 100644 index 0000000..f3e327b --- /dev/null +++ b/db/migrate/20160922042833_add_name_to_users.rb @@ -0,0 +1,5 @@ +class AddNameToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index fff3d25..4d5dee9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160910140016) do +ActiveRecord::Schema.define(version: 20160922042833) do create_table "admins", force: :cascade do |t| t.string "email", default: "", null: false @@ -62,6 +62,8 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "attachment" + t.integer "user_id" + t.index ["user_id"], name: "index_photos_on_user_id" end create_table "posts", force: :cascade do |t| @@ -69,7 +71,9 @@ t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id" t.index ["course_id"], name: "index_posts_on_course_id" + t.index ["user_id"], name: "index_posts_on_user_id" end create_table "tagged_users", force: :cascade do |t| @@ -86,20 +90,23 @@ end create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "provider" t.string "uid" + t.string "authentication_token", limit: 30 + t.string "name" + t.index ["authentication_token"], name: "index_users_on_authentication_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/public/.DS_Store b/public/.DS_Store deleted file mode 100644 index d249b91..0000000 Binary files a/public/.DS_Store and /dev/null differ diff --git a/test/fixtures/admins.yml b/test/fixtures/admins.yml index 80aed36..4c4dff9 100644 --- a/test/fixtures/admins.yml +++ b/test/fixtures/admins.yml @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: admins +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index 6c4a6ea..48f0f9d 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: comments +# +# id :integer not null, primary key +# post_id :integer +# user_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/course_users.yml b/test/fixtures/course_users.yml index dd9b8da..2867874 100644 --- a/test/fixtures/course_users.yml +++ b/test/fixtures/course_users.yml @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: course_users +# +# id :integer not null, primary key +# course_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/courses.yml b/test/fixtures/courses.yml index 80aed36..3007fc8 100644 --- a/test/fixtures/courses.yml +++ b/test/fixtures/courses.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: courses +# +# id :integer not null, primary key +# name :string +# short_link :string +# manager_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml index b703e75..845ad3f 100644 --- a/test/fixtures/photos.yml +++ b/test/fixtures/photos.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: photos +# +# id :integer not null, primary key +# post_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# attachment :string +# user_id :integer +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml index b96e1bf..dfbe198 100644 --- a/test/fixtures/posts.yml +++ b/test/fixtures/posts.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: posts +# +# id :integer not null, primary key +# course_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# user_id :integer +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/tagged_users.yml b/test/fixtures/tagged_users.yml index ab5377b..06bd246 100644 --- a/test/fixtures/tagged_users.yml +++ b/test/fixtures/tagged_users.yml @@ -1,3 +1,18 @@ +# == Schema Information +# +# Table name: tagged_users +# +# id :integer not null, primary key +# photo_id :integer +# user_id :integer +# x :integer +# y :integer +# width :integer +# height :integer +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 80aed36..650f27e 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,3 +1,25 @@ +# == Schema Information +# +# Table name: users +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# provider :string +# uid :string +# authentication_token :string(30) +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the diff --git a/test/models/admin_test.rb b/test/models/admin_test.rb index ab20b8c..6fcb76f 100644 --- a/test/models/admin_test.rb +++ b/test/models/admin_test.rb @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: admins +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class AdminTest < ActiveSupport::TestCase diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb index b6d6131..d4e34cd 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: comments +# +# id :integer not null, primary key +# post_id :integer +# user_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class CommentTest < ActiveSupport::TestCase diff --git a/test/models/course_test.rb b/test/models/course_test.rb index 4afb5cd..fe00724 100644 --- a/test/models/course_test.rb +++ b/test/models/course_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: courses +# +# id :integer not null, primary key +# name :string +# short_link :string +# manager_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class CourseTest < ActiveSupport::TestCase diff --git a/test/models/course_user_test.rb b/test/models/course_user_test.rb index 09b0ec4..c2041a7 100644 --- a/test/models/course_user_test.rb +++ b/test/models/course_user_test.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: course_users +# +# id :integer not null, primary key +# course_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class CourseUserTest < ActiveSupport::TestCase diff --git a/test/models/photo_test.rb b/test/models/photo_test.rb index e2ec03a..c8df96d 100644 --- a/test/models/photo_test.rb +++ b/test/models/photo_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: photos +# +# id :integer not null, primary key +# post_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# attachment :string +# user_id :integer +# + require 'test_helper' class PhotoTest < ActiveSupport::TestCase diff --git a/test/models/post_test.rb b/test/models/post_test.rb index 6d9d463..e111e36 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: posts +# +# id :integer not null, primary key +# course_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# user_id :integer +# + require 'test_helper' class PostTest < ActiveSupport::TestCase diff --git a/test/models/tagged_user_test.rb b/test/models/tagged_user_test.rb index 22b7454..21b60a1 100644 --- a/test/models/tagged_user_test.rb +++ b/test/models/tagged_user_test.rb @@ -1,3 +1,18 @@ +# == Schema Information +# +# Table name: tagged_users +# +# id :integer not null, primary key +# photo_id :integer +# user_id :integer +# x :integer +# y :integer +# width :integer +# height :integer +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class TaggedUserTest < ActiveSupport::TestCase diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 82f61e0..dcfaa0c 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,3 +1,25 @@ +# == Schema Information +# +# Table name: users +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# provider :string +# uid :string +# authentication_token :string(30) +# + require 'test_helper' class UserTest < ActiveSupport::TestCase