def like(self, status): """ user: UserProfile - User liking the status Returns True if the action was successful """ # Check if the user already liked it if Like.get_or_none(user=self.user, status=status): return False # First we create the like object Like.create( user=self.user, status=status, ) # Update the counter in the object Status.update({ Status.favourites_count: Status.favourites_count + 1 }).where(Status.id == status.id) # Create a redis notification like_status(status, self.user) return True
def new_like(): user_id = request.json.get("user_id", None) existing_user = User.get_or_none(User.id == user_id) chef_id = request.json.get("chef_id", None) existing_chef = Chef.get_or_none(Chef.id == chef_id) existing_like = Like.get_or_none(Like.user_id == user_id, Like.chef_id == chef_id) if existing_user == None: return jsonify({ "message": "User does not exist", "status": "failed" }), 400 if existing_chef == None: return jsonify({ "message": "Chef does not exist", "status": "failed" }), 400 if existing_user and existing_chef: if existing_like: return jsonify({ "message": "User already liked this chef", "status": "failed" }), 400 else: new_like = Like(user=user_id, chef=chef_id) if new_like.save(): return jsonify({ "message": "Liked successfully", "status": "Success" })
def dislike(self, status): # Check if the like exists like = Like.get_or_none(user=self.user, status=status) if not like: return False like.delete().execute() Status.update({ Status.favourites_count: Status.favourites_count - 1 }).where(Status.id == status.id)
def delete(like_id): existing_like = Like.get_or_none(Like.id == like_id) if existing_like: if existing_like.delete_instance(): return jsonify({ "message": "successfully deleted", "status": "success" }), 200 else: return jsonify({ "message": "this 'like' no longer exist or you might deleted twice", "status": "failed" }), 400