Support ticket system
From Bauman National Library
This page was last modified on 23 January 2016, at 04:06.
This page was last modified on 23 January 2016, at 04:06.
Requirements
- Angular 1.2 < * <= 1.4.8
- ngResource
- Ruby On Rails >= 4.0
The code
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
Screenshots
Tickets list and ticket creation:
The ticket:
Присоединяйся к команде
ISSN:
Следуй за Полисом
Оставайся в курсе последних событий
License
Except as otherwise noted, the content of this page is licensed under the Creative Commons Creative Commons «Attribution-NonCommercial-NoDerivatives» 4.0 License, and code samples are licensed under the Apache 2.0 License. See Terms of Use for details.