diff --git a/app/repositories/orient_db/learning_object_repository.rb b/app/repositories/orient_db/learning_object_repository.rb index 50ee87a2dc3b572a77823e202f7b723c88915b28..4cad3b310d2856765697a6b19715d7ef89af8188 100644 --- a/app/repositories/orient_db/learning_object_repository.rb +++ b/app/repositories/orient_db/learning_object_repository.rb @@ -3,6 +3,12 @@ module OrientDb include OrientDb::Methods::EdgeMethods include RepositoriesProxy + ## + # Call this methods for increment the views number of a +learning_object+ + # You are creating the 'views' relation between an +user+ and +learning_object+ + # This relation is created only once. + # + # Nothing is returned def increment_views(user, learning_object) if !edge_exists? "Views", user.rid, learning_object.id create_edge "Views", user.rid, learning_object.id @@ -10,59 +16,78 @@ module OrientDb end end + ## + # Call this method checks if the +user+ has a like relation with +learning_object+ + # This methods returns a boolean def liked?(user, learning_object) edge_exists? 'Likes', user.rid, learning_object.id end + ## + # Call this method to create a 'like' relation between an user and a learning object. + # If the user already has the like relation, then nothing happens. + # + # If you want raise an exeception, use the LearningObjectRepository::like! method. # # Usage: # repository.for(:learning_objects).like current_user, @learning_object - # def like(user, learning_object) like_edge_class = 'Likes' + if !liked? + create_edge like_edge_class, user.rid, learning_object.id + learning_object.likes = learning_object.likes + 1 + end + end + + ## + # Call this method to create a 'like' relation between an user and a learning object. + # If the +user+ already liked the +learning_object+ throw an exception. + def like!(user, learning_object) if liked? user, learning_object raise 'The user already likes this object.' end - - create_edge like_edge_class, user.rid, learning_object.id - learning_object.likes = learning_object.likes + 1 + like user, learning_object end + ## + # This methods destroy a 'like' relation between +user+ and +learning_object+ + # If the +user+ hasn't liked the +learning_object+ nothing happens + # If you want raise an exeception, use the LearningObjectRepository::dislike! method. def dislike(user, learning_object) like_edge_class = 'Likes' - if liked? user, learning_object destroy_edge like_edge_class, user.rid, learning_object.id learning_object.likes = learning_object.likes - 1 - - return true + true end + end + ## + # This methods destroy a 'like' relation between +user+ and +learning_object+ + # If the +user+ hasn't liked the +learning_object+ throw an exception. + def dislike!(user, learning_object) + if liked? user, learning_object + dislike user, learning_object + end raise 'The user hasn`t likes this object.' end - # Example: - # list = repository.for(:learning_objects).all - # list.each do |learning_object| - # learning_object.inspect <LearningObject model> - # end - # def all - # learning_objects_hash = connection.query "SELECT FROM LearningObject" - # build_objects(learning_objects_hash) || [] - # end - - # Usage: - # learning_object = repository.for(:learning_objects).get_by_dspace_id 123 - # def get_by_dspace_id(id_dspace) result = select_by_property(odb_class, "id_dspace", id_dspace) build_object result.first end + ## + # This method return all learning objects of +user+ scope. + # + # No users have objects. + # Learning Object submission was not implemented yet. + # For now, an empty array is returned. def all(user) - result = select_by_property(odb_class, "p_id", user.id) - build_objects result + #result = select_by_property(odb_class, "p_id", user.id) + #build_objects result + [] end def get_subjects(learning_object) @@ -108,17 +133,17 @@ module OrientDb # TODO: fix search metadata from dspace unless order.nil? order = case order - when "author" - "metadata.key['dc.contributor.author']" - when "publicationasc" - "metadata.key['dc.date.submitted']" - when "publicationdesc" - "metadata.key['dc.date.submitted'] DESC" - when "title" - "name ASC" - else - nil - end + when "author" + "metadata.key['dc.contributor.author']" + when "publicationasc" + "metadata.key['dc.date.submitted']" + when "publicationdesc" + "metadata.key['dc.date.submitted'] DESC" + when "title" + "name ASC" + else + nil + end end query = "SELECT EXPAND(rid) FROM index:learningobject_search WHERE key LUCENE '#{qry}'" @@ -129,7 +154,7 @@ module OrientDb # return only rids with their modification time learning_objects_hash.map do |e| - {'@rid': e['rid'], 'last_modified': e['last_modified']} + {'@rid' : e['rid'], 'last_modified' : e['last_modified']} end end @@ -137,8 +162,8 @@ module OrientDb LearningObjectBuilder.build_from_orientdb args end - def build_hash - hash = super + def build_hash(object) + hash = super(object) hash.delete("likes") hash.delete("views") hash.delete("downloads")