# -*- encoding : utf-8 -*-

class TinyUrlsController < ApplicationController
  layout "admin"
  before_filter :authenticate_admin!, except: [:show]
  # GET /tiny_urls
  # GET /tiny_urls.json
  def index
    @tiny_urls = TinyUrl.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @tiny_urls }
    end
  end

  # GET /tiny_urls/1
  # GET /tiny_urls/1.json
  def show
    @tiny_url = TinyUrl.find(params[:id])
@tiny_url.nbr_views = @tiny_url.nbr_views.to_i + 1
@tiny_url.save
    redirect_to @tiny_url.url, :status => 301
  end

  # GET /tiny_urls/new
  # GET /tiny_urls/new.json
  def new
    @tiny_url = TinyUrl.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @tiny_url }
    end
  end

  # GET /tiny_urls/1/edit
  def edit
    @tiny_url = TinyUrl.find(params[:id])
  end

  # POST /tiny_urls
  # POST /tiny_urls.json
  def create
    @tiny_url = TinyUrl.new(params[:tiny_url])

    respond_to do |format|
      if @tiny_url.save
        format.html { redirect_to tiny_urls_url, notice: 'Tiny url was successfully created.' }
        
      else
        format.html { render action: "new" }
   
      end
    end
  end

  # PUT /tiny_urls/1
  # PUT /tiny_urls/1.json
  def update
    @tiny_url = TinyUrl.find(params[:id])

    respond_to do |format|
      if @tiny_url.update_attributes(params[:tiny_url])
        format.html { redirect_to tiny_urls_url, notice: 'Tiny url was successfully updated.' }
       
      else
        format.html { render action: "edit" }
        
      end
    end
  end

  # DELETE /tiny_urls/1
  # DELETE /tiny_urls/1.json
  def destroy
    @tiny_url = TinyUrl.find(params[:id])
    @tiny_url.destroy

    respond_to do |format|
      format.html { redirect_to tiny_urls_url }
      format.json { head :no_content }
    end
  end
end
