Skip to content

Commit

Permalink
E2424 (expertiza#120)
Browse files Browse the repository at this point in the history
* bookmark scaffold files created

* Create bookmarks_controller

* crud ops

* add score related functions for bookmark

* Create bookmarks_controller_spec.rb

* Adding setup.sh to automate the setup inside containers (expertiza#84)

* Adding files to setup

* Updating setup script with working seed file

* Adding a setup script to try to automate setup

* Adding setup.sh as entrypoint

* add db creation initilization script

---------

Co-authored-by: Muhammad Ali Qureshi <[email protected]>
Co-authored-by: ameyagv <[email protected]>

* deleted unnecessary files

* modified sign_up_topic

* removed gemfile.lock and schema.rb

* added gemfile.lock and schema.rb

* changes bookmarks controller

* added bookmark rating functions

* added bookmark controller tests

* schema update

* swagger config update

* resovle sign_up_topic migration

* update schema.rb

---------

Co-authored-by: Akshat Nitin Savla <[email protected]>
Co-authored-by: Mitanshu Reshamwala <[email protected]>
Co-authored-by: Mitanshu Reshamwala <[email protected]>
Co-authored-by: vyshnavi-adusumelli <[email protected]>
Co-authored-by: Ali Qureshi <[email protected]>
Co-authored-by: Muhammad Ali Qureshi <[email protected]>
  • Loading branch information
7 people authored Oct 13, 2024
1 parent 2e68f30 commit fdf2a81
Show file tree
Hide file tree
Showing 14 changed files with 1,861 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ GEM
racc (~> 1.4)
nokogiri (1.15.2-x64-mingw-ucrt)
racc (~> 1.4)
nokogiri (1.15.2-x86_64-linux)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand Down Expand Up @@ -242,6 +244,7 @@ GEM
PLATFORMS
aarch64-linux
x64-mingw-ucrt
x86_64-linux

DEPENDENCIES
bcrypt (~> 3.1.7)
Expand Down
92 changes: 92 additions & 0 deletions app/controllers/api/v1/bookmarks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class Api::V1::BookmarksController < ApplicationController

# Index method returns the list of JSON objects of the bookmark
# GET on /bookmarks
def index
@bookmarks = Bookmark.order(:id)
render json: @bookmarks, status: :ok and return
end

# Show method returns the JSON object of bookmark with id = {:id}
# GET on /bookmarks/:id
def show
begin
@bookmark = Bookmark.find(params[:id])
render json: @bookmark, status: :ok and return
rescue ActiveRecord::RecordNotFound
render json: $ERROR_INFO.to_s, status: :not_found and return
end
end

# Create method creates a bookmark and returns the JSON object of the created bookmark
# POST on /bookmarks
def create
begin
# params[:user_id] = @current_user.id
@bookmark = Bookmark.new(bookmark_params)
@bookmark.user_id = @current_user.id
@bookmark.save!
render json: @bookmark, status: :created and return
rescue ActiveRecord::RecordInvalid
render json: $ERROR_INFO.to_s, status: :unprocessable_entity
end
end

# Update method updates the bookmark object with id - {:id} and returns the updated bookmark JSON object
# PUT on /bookmarks/:id
def update
@bookmark = Bookmark.find(params[:id])
if @bookmark.update(update_bookmark_params)
render json: @bookmark, status: :ok
else
render json: @bookmark.errors.full_messages, status: :unprocessable_entity
end
end

# Destroy method deletes the bookmark object with id- {:id}
# DELETE on /bookmarks/:id
def destroy
begin
@bookmark = Bookmark.find(params[:id])
@bookmark.delete
rescue ActiveRecord::RecordNotFound
render json: $ERROR_INFO.to_s, status: :not_found and return
end
end

# get_bookmark_rating_score method gets the bookmark rating of the bookmark object with id- {:id}
# GET on /bookmarks/:id/bookmarkratings
def get_bookmark_rating_score
begin
@bookmark = Bookmark.find(params[:id])
@bookmark_rating = BookmarkRating.where(bookmark_id: @bookmark.id, user_id: @current_user.id).first
render json: @bookmark_rating, status: :ok and return
rescue ActiveRecord::RecordNotFound
render json: $ERROR_INFO.to_s, status: :not_found and return
end
end

# save_bookmark_rating_score method creates or updates the bookmark rating of the bookmark object with id- {:id}
# POST on /bookmarks/:id/bookmarkratings
def save_bookmark_rating_score
@bookmark = Bookmark.find(params[:id])
@bookmark_rating = BookmarkRating.where(bookmark_id: @bookmark.id, user_id: @current_user.id).first
if @bookmark_rating.blank?
@bookmark_rating = BookmarkRating.create(bookmark_id: @bookmark.id, user_id: @current_user.id, rating: params[:rating])
else
@bookmark_rating.update({'rating': params[:rating].to_i})
end
render json: {"bookmark": @bookmark, "rating": @bookmark_rating}, status: :ok
end

private

def bookmark_params
params.require(:bookmark).permit(:url, :title, :description, :topic_id, :rating, :id)
end

def update_bookmark_params
params.require(:bookmark).permit(:url, :title, :description)
end

end
8 changes: 8 additions & 0 deletions app/models/bookmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Bookmark < ApplicationRecord
belongs_to :user
# belongs_to :topic, class_name: "SignUpTopic"
has_many :bookmark_ratings
validates :url, presence: true
validates :title, presence: true
validates :description, presence: true
end
4 changes: 4 additions & 0 deletions app/models/bookmark_rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BookmarkRating < ApplicationRecord
belongs_to :bookmark
belongs_to :user
end
4 changes: 3 additions & 1 deletion app/models/sign_up_topic.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class SignUpTopic < ApplicationRecord
has_many :signed_up_teams, foreign_key: 'sign_up_topic_id', dependent: :destroy
has_many :signed_up_teams, foreign_key: 'topic_id', dependent: :destroy
has_many :teams, through: :signed_up_teams # list all teams choose this topic, no matter in waitlist or not
has_many :assignment_questionnaires, class_name: 'AssignmentQuestionnaire', foreign_key: 'topic_id', dependent: :destroy
belongs_to :assignment
end
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
end
end

resources :bookmarks, except: [:new, :edit] do
member do
get 'bookmarkratings', to: 'bookmarks#get_bookmark_rating_score'
post 'bookmarkratings', to: 'bookmarks#save_bookmark_rating_score'
end
end

resources :courses do
collection do
get ':id/add_ta/:ta_id', action: :add_ta
Expand Down
10 changes: 5 additions & 5 deletions db/migrate/20231129050431_create_sign_up_topics.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class CreateSignUpTopics < ActiveRecord::Migration[7.0]
def change
create_table :sign_up_topics do |t|
t.text :topic_name
t.text :topic_name, null: false
t.references :assignment, null: false, foreign_key: true
t.integer :max_choosers
t.integer :max_choosers, default: 0, null: false
t.text :category
t.string :topic_identifier
t.integer :micropayment
t.string :topic_identifier, limit: 10
t.integer :micropayment, default: 0
t.integer :private_to
t.text :description
t.string :link

t.index ["assignment_id"], name: "fk_sign_up_categories_sign_up_topics"
t.timestamps
end
end
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240318205124_create_bookmarks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateBookmarks < ActiveRecord::Migration[7.0]
def change
create_table :bookmarks do |t|
t.text :url
t.text :title
t.text :description
t.integer :user_id
t.integer :topic_id

t.timestamps
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240324000112_create_bookmark_ratings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateBookmarkRatings < ActiveRecord::Migration[7.0]
def up
create_table "bookmark_ratings" do |t|
t.integer "bookmark_id"
t.integer "user_id"
t.integer "rating"
t.timestamps
end
end
def down
drop_table "bookmark_ratings"
end
end
107 changes: 103 additions & 4 deletions db/schema.rb

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

10 changes: 9 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FactoryBot.define do

factory :join_team_request do

end

factory :bookmark do
url { "MyText" }
title { "MyText" }
description { "MyText" }
user_id { 1 }
topic_id { 1 }
end

factory :user do
Expand Down
Loading

0 comments on commit fdf2a81

Please sign in to comment.