CSC/ECE 517 Spring 2024 - E2424. Reimplement the Bookmarks Controller: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 127: Line 127:
* You cannot read a particular bookmark as it not a feature, you can only list the bookmarks of a topic id
* You cannot read a particular bookmark as it not a feature, you can only list the bookmarks of a topic id
[[Image:List Bookmarks.png| 500px]]
[[Image:List Bookmarks.png| 500px]]
* Update Bookmark
[[Image:Updated bookmark.png | 500px]]
* Delete Bookmark. If you get 204, that means bookmark got deleted
[[Image:Delete bookmark.png | 500px]]
* Save rating for a bookmark
[[Image:Save bookmark rating.png | 500px]]


==Github==
==Github==

Revision as of 22:56, 27 March 2024

This page provides a description of the Expertiza based OSS project based on reimplementing the bookmarks controller.



About Expertiza

Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.

Problem Statement

The Expertiza application requires the reimplementation of its backend functionality for the BookmarksController. Currently, the system relies on traditional Rails architecture, but the project aims to transition to a more streamlined approach using Rails API. This transition is essential for scalability and flexibility, allowing for separate frontend and backend applications.

The BookmarksController serves as the backbone for bookmark management and user interactions within the Expertiza platform. It facilitates actions such as listing bookmarks associated with specific topics, creating new bookmarks, editing existing ones, and deleting bookmarks. Furthermore, it incorporates authorization rules to ensure users possess appropriate roles and permissions for each action.

The reimplemented BookmarksController will adhere to the Rails API structure, residing in the controller/api/v1 directory. It will maintain the existing functionality, including methods for retrieving bookmark ratings, calculating average scores for specific and total ratings, and managing strong parameters through private methods.

Implementation

Current Implementation

The current implementation of the bookmarks crud queries is as follows:

 def create
   params[:url] = params[:url].gsub!(%r{http://}, ) if params[:url].start_with?('http://')
   params[:url] = params[:url].gsub!(%r{https://}, ) if params[:url].start_with?('https://')
   begin
     Bookmark.create(url: create_bookmark_params[:url], title: create_bookmark_params[:title], description: create_bookmark_params[:description], user_id: session[:user].id, topic_id: create_bookmark_params[:topic_id])
     ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully created!', request)
     flash[:success] = 'Your bookmark has been successfully created!'
   rescue StandardError
     ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request)
     flash[:error] = $ERROR_INFO
   end
   redirect_to action: 'list', id: params[:topic_id]
 end
 def edit
   @bookmark = Bookmark.find(params[:id])
 end
 def update
   @bookmark = Bookmark.find(params[:id])
   @bookmark.update_attributes(url: update_bookmark_params[:bookmark][:url], title: update_bookmark_params[:bookmark][:title], description: update_bookmark_params[:bookmark][:description])
   ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully updated!', request)
   flash[:success] = 'Your bookmark has been successfully updated!'
   redirect_to action: 'list', id: @bookmark.topic_id
 end
 def destroy
   @bookmark = Bookmark.find(params[:id])
   @bookmark.destroy
   ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully deleted!', request)
   flash[:success] = 'Your bookmark has been successfully deleted!'
   redirect_to action: 'list', id: @bookmark.topic_id
 end

The problems with the current implementation, which is resolved in the new implementation, is:

1. The new implementation is designed for API use with JSON responses, improving the interaction with API clients by clearly communicating error states and messages.

2. The new controller utilizes structured error handling.

3. Introduction of before_action :set_bookmark in the new controller eliminates redundancy and enhances code reusability by centralizing bookmark fetching for relevant actions.

New Implementation

The new implementation for the CRUD operations is shown below. The new functions return a JSON object instead of rendering an HTML page.

create

  • Handles URL normalization to ensure consistency in bookmark data.
  • Utilizes structured error handling for better response communication.
  • Implements before_action `:set_bookmark` to centralize bookmark fetching for relevant actions.
def create
   begin
     create_bookmark_params[:url] = create_bookmark_params[:url].gsub!(%r{http://}, ) if create_bookmark_params[:url].present? && create_bookmark_params[:url].start_with?('http://')
     create_bookmark_params[:url] = create_bookmark_params[:url].gsub!(%r{https://}, ) if create_bookmark_params[:url].present? && create_bookmark_params[:url].start_with?('https://')
     @bookmark = Bookmark.new(create_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

  • Incorporates URL normalization for consistency in bookmark data.
  • Returns JSON response with updated bookmark information for improved client-side interactions.
  • Provides detailed error messages in case of update failure for better troubleshooting.
 def update
   update_bookmark_params[:url] = update_bookmark_params[:url].gsub!(%r{http://}, ) if update_bookmark_params[:url].start_with?('http://')
   update_bookmark_params[:url] = update_bookmark_params[:url].gsub!(%r{https://}, ) if update_bookmark_params[:url].start_with?('https://')
   if @bookmark.update(update_bookmark_params)
     render json: @bookmark, status: :ok
   else
     render json: @bookmark.errors.full_messages, status: :unprocessable_entity
   end
 end

destroy

  • Utilizes structured error handling to provide informative error responses.
  • Implements proper exception handling to gracefully manage record deletion errors.
  • Provides clear feedback to users upon successful deletion of a bookmark.
 def destroy
   @bookmark = Bookmark.find(params[:id])
   @bookmark.destroy
   rescue ActiveRecord::RecordNotFound
       render json: $ERROR_INFO.to_s, status: :not_found
 end

Testing on Postman

Postman was used to manually test the additional method in impersonate_controller.rb, as well as the actions and routes of the corresponding controllers. Before testing any of these methods with Postman, submit a request to /login using the user_name and password fields, which will send an authentication token. This token must be added to Postman's 'Authorization' tab as a 'Bearer token' before any further requests can be made.

  • Login with provided credentials and copy the token

  • Copy token to Authorization section of postman and paste as Bearer token

  • Create a bookmark

  • You cannot read a particular bookmark as it not a feature, you can only list the bookmarks of a topic id

  • Update Bookmark

  • Delete Bookmark. If you get 204, that means bookmark got deleted

  • Save rating for a bookmark

Github

Repository: https://github.com/akshat22/reimplementation-back-end

Pull Request: https://github.com/expertiza/reimplementation-back-end/pull/76

Team

Mentor

Mohammed Ali Qureshi <mquresh@ncsu.edu>

Contributors

Akshat Nitin Savla <asavla@ncsu.edu>
Mitanshu Reshamwala <mresham@ncsu.edu>
Tanay Gandhi <tgandhi@ncsu.edu>

References