<commit_before>require "../models/*"

class PostController < Base::Controller
  property :posts, :post, :authorized

  actions :index, :new, :create, :read, :edit, :update, :delete

  layout "default", "#{__DIR__}/../views/layouts"
  
  view "index", "#{__DIR__}/../views/post"
  view "read", "#{__DIR__}/../views/post"
  view "new", "#{__DIR__}/../views/post"
  view "edit", "#{__DIR__}/../views/post"

  def initialize
    @authorized = false
    @posts = [] of Post
    @post = Post.new
    super
  end
 
  def index
    @authorized = session.has_key? :authorized 
    @posts = Post.all
    respond_to do |format|
      format.html { render_with_layout "index", "default" }
    end
  end

  def new
    @authorized = session.has_key? :authorized 
    respond_to do |format|
      if @authorized
        format.html { render_with_layout "new", "default" }
      else
        format.html { redirect_to "/posts" }
      end
    end
  end

  def create
    @authorized = session.has_key? :authorized 
    if @authorized
      post = Post.new
      if post
        post.name = request.parameters["name"]
        post.body = request.parameters["body"]
        post.save
      end
    end
    
    respond_to do |format|
      format.html { redirect_to "/posts" }
    end
  end

  def read
    @authorized = session.has_key? :authorized 
    id = request.parameters["id"]
   
    @post = Post.find(id) 
    respond_to do |format|
      format.html { render_with_layout "read", "default" }
    end
  end

  def edit
    @authorized = session.has_key? :authorized 
    id = request.parameters["id"]
    
    @post = Post.find(id) 
    respond_to do |format|
      if @authorized
        format.html { render_with_layout "edit", "default" }
      else
        format.html { redirect_to "/posts" }
      end
    end
  end

  def update
    @authorized = session.has_key? :authorized 
    id = request.parameters["id"]
    if @authorized
      post = Post.find(id) 
      if post
        post.name = request.parameters["name"]
        post.body = request.parameters["body"]
        post.save
      end
    end
    
    respond_to do |format|
      format.html { redirect_to "/posts/#{id}" }
    end
  end

  def delete
    @authorized = session.has_key? :authorized 
    id = request.parameters["id"]
    if @authorized
      post = Post.find(id) 
      if post
        post.destroy
      end
    end

    respond_to do |format|
      format.html { redirect_to "/posts" }
    end
  end

end<commit_msg>change this to use a before_action<commit_after>require "../models/*"

class PostController < Base::Controller
  property :posts, :post, :authorized

  actions :index, :new, :create, :read, :edit, :update, :delete
  before_action :authorize

  layout "default", "#{__DIR__}/../views/layouts"
  
  view "index", "#{__DIR__}/../views/post"
  view "read", "#{__DIR__}/../views/post"
  view "new", "#{__DIR__}/../views/post"
  view "edit", "#{__DIR__}/../views/post"

  def initialize
    @authorized = false
    @posts = [] of Post
    @post = Post.new
    super
  end
 
  def index
    @posts = Post.all
    respond_to do |format|
      format.html { render_with_layout "index", "default" }
    end
  end

  def new
    respond_to do |format|
      if @authorized
        format.html { render_with_layout "new", "default" }
      else
        format.html { redirect_to "/posts" }
      end
    end
  end

  def create
    if @authorized
      post = Post.new
      if post
        post.name = request.parameters["name"]
        post.body = request.parameters["body"]
        post.save
      end
    end
    
    respond_to do |format|
      format.html { redirect_to "/posts" }
    end
  end

  def read
    id = request.parameters["id"]
   
    @post = Post.find(id) 
    respond_to do |format|
      format.html { render_with_layout "read", "default" }
    end
  end

  def edit
    id = request.parameters["id"]
    
    @post = Post.find(id) 
    respond_to do |format|
      if @authorized
        format.html { render_with_layout "edit", "default" }
      else
        format.html { redirect_to "/posts" }
      end
    end
  end

  def update
    id = request.parameters["id"]
    if @authorized
      post = Post.find(id) 
      if post
        post.name = request.parameters["name"]
        post.body = request.parameters["body"]
        post.save
      end
    end
    
    respond_to do |format|
      format.html { redirect_to "/posts/#{id}" }
    end
  end

  def delete
    id = request.parameters["id"]
    if @authorized
      post = Post.find(id) 
      if post
        post.destroy
      end
    end

    respond_to do |format|
      format.html { redirect_to "/posts" }
    end
  end

  def authorize
    puts "DRU: AUTHORIZE CALLED"
    @authorized = session.has_key? :authorized 
  end

end
{'fraction_non_alphanumeric': 0.12078300708038317, 'fraction_numerical': 0.0, 'mean_word_length': 2.1850132625994694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '227559', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1513, 'n_words': 399}