parent
0a4f39be58
commit
6a40cdd6bf
@ -0,0 +1,28 @@ |
||||
module Projects |
||||
class ProjectMapper < Yaks::Mapper |
||||
link :self, '/api/v3/projects/{id}' |
||||
link :root, '/api/v3' |
||||
link :createChildren, '/api/v3/projects?parent_id={id}', method: :post |
||||
link :update, '/api/v3/projects/{id}', method: :patch |
||||
link :delete, '/api/v3/projects/{id}', method: :delete |
||||
link :version, '/api/v3/versions?filter[project_id]={id}' |
||||
link :responsible, '/api/v3/users/{responsible_id}' |
||||
link :members, '/api/v3/projects/{id}/members' |
||||
link :workPackages, '/api/v3/work_packages?filter[project_id]={id}' |
||||
link :createWorkPackages, '/api/v3/work_packages', method: :post |
||||
link :possibleResponsibles, '/api/v3/projects/{id}/possible_responsibles' |
||||
|
||||
attributes :id, :name, :description, :createdAt, :updatedAt, :summary |
||||
|
||||
has_one :responsible, mapper: Users::UserMapper |
||||
has_many :users, mapper: Users::UserMapper, as: :members |
||||
|
||||
def createdAt |
||||
object.created_on.strftime("at %I:%M%p") |
||||
end |
||||
|
||||
def updatedAt |
||||
object.updated_on.strftime("at %I:%M%p") |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,52 @@ |
||||
module Projects |
||||
class API < Grape::API |
||||
|
||||
resources :projects do |
||||
get do |
||||
projects = Project.all |
||||
projects_array = [] |
||||
projects.each do |project| |
||||
resource = ProjectMapper.new(project).to_resource |
||||
projects_array << Yaks::HalSerializer.new(resource).serialize |
||||
end |
||||
json = { |
||||
_collection: projects_array, |
||||
_type: "Project", |
||||
_links: { |
||||
root: { href: '/api/v3' }, |
||||
self: { href: '/api/v3/projects' }, |
||||
next: { href: '/api/v3/projects/page=3' }, |
||||
previous: { href: '/api/v3/projects/page=1' }, |
||||
first: { href: '/api/v3/projects/page=1' }, |
||||
last: { href: '/api/v3/projects/page=102' }, |
||||
create: { href: '/api/v3/projects,', method: 'post' }, |
||||
batchUpdate: { href: '/api/v3/projects', method: 'delete' }, |
||||
batchDelete: { href: '/api/v3/projects?{ids}', method: 'delete' }, |
||||
|
||||
}, |
||||
_count: projects_array.count, |
||||
_total: Project.count |
||||
}.to_json |
||||
end |
||||
|
||||
get ':id' do |
||||
project = Project.find(params[:id]) |
||||
resource = ProjectMapper.new(project).to_resource |
||||
Yaks::HalSerializer.new(resource).serialize.to_json |
||||
end |
||||
|
||||
patch ':id' do |
||||
end |
||||
|
||||
delete :':id' do |
||||
end |
||||
|
||||
patch do |
||||
end |
||||
|
||||
delete do |
||||
end |
||||
|
||||
end |
||||
end |
||||
end |
@ -0,0 +1,6 @@ |
||||
module Users |
||||
class UserMapper < Yaks::Mapper |
||||
|
||||
attributes :id, :login, :firstname, :lastname, :mail |
||||
end |
||||
end |
@ -0,0 +1,24 @@ |
||||
require 'roar/decorator' |
||||
require 'roar/representer/json' |
||||
require 'roar/representer/json/hal' |
||||
|
||||
module Versions |
||||
class VersionRepresenter < Roar::Decorator |
||||
include Roar::Representer::JSON |
||||
include Roar::Representer::Feature::Hypermedia |
||||
include Roar::Representer::JSON::HAL |
||||
|
||||
property :id |
||||
property :name |
||||
property :description |
||||
property :effective_date, getter: lambda { |args| effective_date.to_time.to_i } |
||||
property :created_on, getter: lambda { |args| created_on.to_time.to_i } |
||||
property :updated_on, getter: lambda { |args| updated_on.to_time.to_i } |
||||
property :woki_page_title |
||||
property :status |
||||
|
||||
link :self do |
||||
"/versions/#{represented.id}" |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,59 @@ |
||||
module WorkPackages |
||||
class WorkPackageMapper < Yaks::Mapper |
||||
link :self, '/api/v3/work_packages/{id}' |
||||
|
||||
attributes :id, :subject, :description, :type, :dueDate, :status, :priority, :percentageDone, |
||||
:estimatedTime, :startDate, :createdAt, :updatedAt, :customFields, :_type |
||||
|
||||
# has_one :responsible, mapper: Users::UserMapper |
||||
# has_many :users, mapper: Users::UserMapper, as: :members |
||||
|
||||
def type |
||||
object.type.name |
||||
end |
||||
|
||||
def dueDate |
||||
object.due_date.to_s |
||||
end |
||||
|
||||
def status |
||||
object.status.name |
||||
end |
||||
|
||||
def priority |
||||
object.priority.name |
||||
end |
||||
|
||||
def percentageDone |
||||
object.done_ratio |
||||
end |
||||
|
||||
def estimatedTime |
||||
{ unit: 'hours', value: object.estimated_hours } |
||||
end |
||||
|
||||
def startDate |
||||
object.start_date.to_s |
||||
end |
||||
|
||||
def createdAt |
||||
object.created_at.to_s |
||||
end |
||||
|
||||
def updatedAt |
||||
object.updated_at.to_s |
||||
end |
||||
|
||||
def customFields |
||||
fields = [ ] |
||||
object.custom_field_values.each do |custom_value| |
||||
fields << { name: custom_value.custom_field.name, format: custom_value.custom_field.field_format, value: custom_value.value } |
||||
end |
||||
fields |
||||
end |
||||
|
||||
def _type |
||||
"WorkPackage" |
||||
end |
||||
end |
||||
end |
@ -1,52 +0,0 @@ |
||||
require 'roar/decorator' |
||||
require 'roar/representer/json' |
||||
require 'roar/representer/json/hal' |
||||
|
||||
module WorkPackages |
||||
class WorkPackageRepresenter < Roar::Decorator |
||||
include Roar::Representer::JSON |
||||
include Roar::Representer::Feature::Hypermedia |
||||
include Roar::Representer::JSON::HAL |
||||
|
||||
property :id |
||||
property :subject |
||||
property :description |
||||
property :type, getter: lambda { |arg| type.try(:name) } |
||||
property :due_date |
||||
property :category, getter: lambda { |arg| category.try(:name) } |
||||
property :status, getter: lambda { |arg| status.try(:name) } |
||||
property :priority, getter: lambda { |arg| priority.try(:name) } |
||||
property :fixed_version, getter: lambda { |arg| fixed_version.try(:name) } |
||||
property :lock_version |
||||
property :done_ratio |
||||
property :estimated_hours |
||||
property :start_date |
||||
property :created_at |
||||
property :updated_at |
||||
|
||||
property :_links, exec_context: :decorator |
||||
|
||||
def _links |
||||
{ |
||||
root: { href: "/", title: 'OpenProject API entry point' }, |
||||
self: { href: "/work_packages/#{represented.id}", title: "represented.subject" }, |
||||
work_packages: { href: "/work_packages", title: "Work packages" }, |
||||
project_work_packages: { href: "/projects/#{represented.project.identifier}/work_packages", title: "#{represented.project.name} - Work packages" }, |
||||
descendants: { href: "/projects/#{represented.project.identifier}/work_packages?filter[ancestor_id]=#{represented.id}", title: "#{represented.subject} - Descendant work packages" }, |
||||
children: { href: "/projects/#{represented.project.identifier}/work_packages?filter[parent_id]=#{represented.id}", title: "#{represented.subject} - Children work packages" }, |
||||
create: { href: "/projects/#{represented.project.identifier}/work_packages", method: :post, title: "#{represented.project.name} - Create new work package" }, |
||||
update: { href: "/work_packages/#{represented.id}", method: :patch, title: "Update #{represented.subject}" }, |
||||
delete: { href: "/work_packages/#{represented.id}", method: :delete, title: "Delete #{represented.subject}" }, |
||||
project: { href: "/projects/#{represented.project.identifier}", title: "#{represented.project}" }, |
||||
author: { href: "/users/#{represented.author_id}", title: "?" }, |
||||
assignee: { href: "/users/#{represented.assigned_to_id}", title: "?" }, |
||||
responsible: { href: "/users/#{represented.responsible_id}", title: "?" } |
||||
} |
||||
end |
||||
|
||||
property :project, class: Project, decorator: Projects::ProjectRepresenter, embedded: true |
||||
property :author, class: User, decorator: Users::UserRepresenter, embedded: true |
||||
property :assigned_to, as: :assignee, class: User, decorator: Users::UserRepresenter, embedded: true |
||||
property :responsible, class: User, decorator: Users::UserRepresenter, embedded: true |
||||
end |
||||
end |
@ -0,0 +1,7 @@ |
||||
class ApplicationPolicy |
||||
def initialize(user, record) |
||||
raise Pundit::NotAuthorizedError, "must be logged in" unless user |
||||
@user = user |
||||
@record = record |
||||
end |
||||
end |
Loading…
Reference in new issue