Тикет-система поддержки

Материал из Национальной библиотеки им. Н. Э. Баумана
Последнее изменение этой страницы: 07:05, 23 января 2016.

Требования

  • Angular 1.2 < * <= 1.4.8
  • ngResource
  • Ruby On Rails >= 4.0

Код

ticket.js

angular.module('app').factory('Comment', ['$resource',  function ($resource) {
        return $resource('http://localhost:3000/api/v1/tickets/:ticket_id/:action',
            {
                ticket_id: '@id'
            },
            {
                list:       { method: 'GET',    isArray: true },
                show:       { method: 'GET',    isArray: false},
                create:     { method: 'POST' },
            });
    }]);


comment.js

angular.module('app').factory('Comment', ['$resource',  function ($resource) {
        return $resource('http://localhost:3000/api/v1/comments/:ticket_id',
            {
                ticket_id: '@id'
            },
            {
                create: { method: 'POST' }
            });
    }]);


ticket_service.js

angular.module('app').service('TicketService', ['$stateParams', 'Ticket', 'Comment',
        function($stateParams, Ticket, Comment) {
            var self = this;

            self.loadTickets = function()
            {
                return Ticket.list().$promise;
            };

            self.loadTicket = function()
            {
                return Ticket.show({ticket_id: $stateParams.ticket_id}).$promise;
            };

            self.createComment = function(commentForm)
            {
                return Comment.create({id: $stateParams.ticket_id, comment: commentForm}).$promise;
            };

            self.createTicket = function(ticketForm)
            {
                return Ticket.create({ticket: ticketForm}).$promise;
            };
        }]);


tickets_controller.rb

class Api::V1::TicketsController < Api::ApplicationController
  before_filter :check_permissions,
                :only => [:show]


  def index
    @tickets = current_user.tickets.includes(:comments, :comments => [:user]).newest

    render json: @tickets
  end

  def create
    @ticket = current_user.tickets.new(ticket_params)

    if @ticket.save
      render json: {ticket_id: @ticket.id}
    else
      _bad_request(@ticket.errors.full_messages)
    end
  end

  def show
     render json: @ticket
  end

  private

  def check_permissions
    @ticket = Ticket.includes(:comments, :comments => [:user]).find(params[:id])

    unless @ticket.user == current_user
      _not_allowed
    end
  end

  def ticket_params
    params.require(:ticket).permit(:title, :description)
  end
end


comments_controller.rb

class Api::V1::CommentsController < Api::ApplicationController
  def create
    ticket = Ticket.find(params[:ticket_id])

    comment = ticket.comments.new(comment_params)
    comment.user = current_user

    if comment.save
      render json: {comment_id: comment.id}
    else
      _bad_request(comment.errors.full_messages)
    end

  end

  private
    def comment_params
      params.require(:comment).permit(:contents)
    end
end

Скриншоты

Список тикетов и форма создания нового тикета:

Скриншот 1


Созданный тикет с ответами:

Скриншот 2