将entities重构

This commit is contained in:
guange 2014-12-09 16:36:43 +08:00
parent 41d9af1ab9
commit 4a6c26d499
5 changed files with 116 additions and 94 deletions

View File

@ -1,4 +1,12 @@
module Mobile
module Entities
class Auth < Grape::Entity
expose :token
expose :user, using: User
end
end
module Apis
class Auth < Grape::API
resource :auth do
@ -12,8 +20,9 @@ module Mobile
if user
::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id)
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
{status: 0, data: data}
api_user = UserService.new.show_user(user.id)
present :data, {token: key, user: api_user}, using: Mobile:Entities::Auth
present :status, 0
else
{status: 1, message: 'Unauthorized.'}
end

View File

@ -1,46 +1,5 @@
module Mobile
module Apis
module Entities
class Course < Grape::Entity
def self.course_expose(field)
expose field do |f,opt|
f[:course][field] || f[:course].__send__(field)
end
end
expose :img_url
course_expose :attachmenttype
course_expose :class_period
course_expose :code
course_expose :created_at
course_expose :description
course_expose :endup_time
course_expose :extra
course_expose :id
course_expose :inherit_members
course_expose :is_public
course_expose :lft
course_expose :location
course_expose :name
course_expose :open_student
# course_expose :password
course_expose :rgt
course_expose :school_id
course_expose :setup_time
course_expose :state
course_expose :status
course_expose :string
course_expose :tea_id
course_expose :term
course_expose :time
course_expose :updated_at
course_expose :teacher
end
end
class Courses < Grape::API
resource :courses do
desc "get all courses"
@ -52,7 +11,7 @@ module Mobile
get do
cs = CoursesService.new
courses = cs.course_list(params)
present :data, courses, with: Entities::Course
present :data, courses, with: Mobile::Entities::Course
present :status, 0
end

View File

@ -19,17 +19,13 @@ module Mobile
user = us.register params.merge(:password_confirmation => params[:password],
:should_confirmation_password => true)
raise "register failed #{user.errors.full_messages}" if user.new_record?
{status: 0, data: user}
present :data, user, with: Mobile::Entities::User
present :status, 0
rescue => e
{status: 1, message: e.message}
end
end
desc "test upload"
post "upload" do
puts "haha11"
end
end
end
end

View File

@ -0,0 +1,38 @@
module Mobile
module Entities
class Course < Grape::Entity
def self.course_expose(field)
expose field do |f,opt|
f[:course][field] || f[:course].__send__(field)
end
end
expose :img_url
course_expose :attachmenttype
course_expose :class_period
course_expose :code
course_expose :created_at
course_expose :description
course_expose :endup_time
course_expose :extra
course_expose :id
course_expose :inherit_members
course_expose :is_public
course_expose :lft
course_expose :location
course_expose :name
course_expose :open_student
# course_expose :password
course_expose :rgt
course_expose :school_id
course_expose :setup_time
course_expose :state
course_expose :status
course_expose :string
course_expose :tea_id
course_expose :term
course_expose :time
course_expose :updated_at
course_expose :teacher
end
end
end

View File

@ -0,0 +1,20 @@
module Mobile
module Entities
class User < Grape::Entity
#头像
#昵称
expose :login
#性别
expose :gender do |user, opt|
user.user_extentions.gender
end
#我的二维码
#工作单位
#邮箱地址
expose :mail
#地区
#签名
end
end
end