将entities重构
This commit is contained in:
parent
41d9af1ab9
commit
4a6c26d499
|
@ -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
|
||||
|
|
|
@ -1,74 +1,33 @@
|
|||
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)
|
||||
class Courses < Grape::API
|
||||
resource :courses do
|
||||
desc "get all courses"
|
||||
params do
|
||||
optional :school_id, type: Integer, desc: 'school number'
|
||||
requires :per_page_count, type: Integer
|
||||
requires :page, type: Integer, desc: 'current page no'
|
||||
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"
|
||||
params do
|
||||
optional :school_id, type: Integer, desc: 'school number'
|
||||
requires :per_page_count, type: Integer
|
||||
requires :page, type: Integer, desc: 'current page no'
|
||||
end
|
||||
get do
|
||||
cs = CoursesService.new
|
||||
courses = cs.course_list(params)
|
||||
present :data, courses, with: Entities::Course
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc "Return a course"
|
||||
params do
|
||||
requires :id, type: Integer
|
||||
end
|
||||
route_param :id do
|
||||
get do
|
||||
course = Course.find(params[:id])
|
||||
{status: 0, data: course}
|
||||
cs = CoursesService.new
|
||||
courses = cs.course_list(params)
|
||||
present :data, courses, with: Mobile::Entities::Course
|
||||
present :status, 0
|
||||
end
|
||||
end
|
||||
|
||||
desc "Return a course"
|
||||
params do
|
||||
requires :id, type: Integer
|
||||
end
|
||||
route_param :id do
|
||||
get do
|
||||
course = Course.find(params[:id])
|
||||
{status: 0, data: course}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,38 +1,34 @@
|
|||
module Mobile
|
||||
module Apis
|
||||
class Users < Grape::API
|
||||
resource :users do
|
||||
desc "get all users"
|
||||
get do
|
||||
['hello']
|
||||
end
|
||||
|
||||
desc "add a user"
|
||||
params do
|
||||
requires :login, type: String, desc: 'username'
|
||||
requires :mail, type: String, desc: 'mail'
|
||||
requires :password, type: String, desc: 'password'
|
||||
end
|
||||
post do
|
||||
us = UsersService.new
|
||||
begin
|
||||
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}
|
||||
rescue => e
|
||||
{status: 1, message: e.message}
|
||||
class Users < Grape::API
|
||||
resource :users do
|
||||
desc "get all users"
|
||||
get do
|
||||
['hello']
|
||||
end
|
||||
end
|
||||
|
||||
desc "test upload"
|
||||
post "upload" do
|
||||
puts "haha11"
|
||||
end
|
||||
desc "add a user"
|
||||
params do
|
||||
requires :login, type: String, desc: 'username'
|
||||
requires :mail, type: String, desc: 'mail'
|
||||
requires :password, type: String, desc: 'password'
|
||||
end
|
||||
post do
|
||||
us = UsersService.new
|
||||
begin
|
||||
user = us.register params.merge(:password_confirmation => params[:password],
|
||||
:should_confirmation_password => true)
|
||||
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
rescue => e
|
||||
{status: 1, message: e.message}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue