From 4a6c26d499844c6cc4cdb086c01c1fa032745be6 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Tue, 9 Dec 2014 16:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86entities=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/auth.rb | 13 ++++- app/api/mobile/apis/courses.rb | 87 ++++++++----------------------- app/api/mobile/apis/users.rb | 52 +++++++++--------- app/api/mobile/entities/course.rb | 38 ++++++++++++++ app/api/mobile/entities/user.rb | 20 +++++++ 5 files changed, 116 insertions(+), 94 deletions(-) create mode 100644 app/api/mobile/entities/course.rb create mode 100644 app/api/mobile/entities/user.rb diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index 8281c4aa9..33eff26ad 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -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 diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 2dca9c220..1084e8cbc 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -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 diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index e4544a09e..df17782f2 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -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 diff --git a/app/api/mobile/entities/course.rb b/app/api/mobile/entities/course.rb new file mode 100644 index 000000000..b9823d9ac --- /dev/null +++ b/app/api/mobile/entities/course.rb @@ -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 diff --git a/app/api/mobile/entities/user.rb b/app/api/mobile/entities/user.rb new file mode 100644 index 000000000..33ae3f049 --- /dev/null +++ b/app/api/mobile/entities/user.rb @@ -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