ADD some apis

This commit is contained in:
Jasder 2019-10-21 15:07:26 +08:00
parent 6aac0d0926
commit 857b888f7c
13 changed files with 211 additions and 9 deletions

72
README.md Normal file
View File

@ -0,0 +1,72 @@
# API文档
---
## 基本介绍
trustie平台同步其他平台数据的相关接口如Educode、Openl在获得许可权限后如下接口可在其他任何平台调用。
#### token认证
token在trustie控制台下通过Setting.api_token获取获取后可放环境变量中或者某个配置文件中
#### 同步用户数量、开源项目数量、实训项目数量
```
PUT homes/sync_count
```
*示例*
```
curl -X PUT http://localhost:3001/api/v1/homes/sync_count \
-d "token=34c82f51e0b699d9d16d70fd6497c9b1e4821d6ea3e872558a6537a091076b8e" \
-d "type=project" | jq
```
请求参数说明:
token(requires): String权限验证
type(requires): String同步的类型取值范围: {user(用户) | project(开源项目) | practical_training_project(实训项目)}
number(optional): Integer同步数量, 该参数可选不传时后台默认值为1
返回值
```
{
"status": 1,
"message": "token is invalid!"
}
```
---
#### 同步动态新闻
```
PUT homes/sync_news
```
*示例*
```
curl -X PUT http://localhost:3001/api/v1/homes/sync_news \
-d "token=34c82f51e0b699d9d16d70fd6497c9b1e4821d6ea3e872558a6537a091076b8e" \
-d "title=“基于OpenI海参的视频编码大赛”海口启动" \
-d "simple_intruduce=2019年8月31日下午“AVS工作组第70次会议暨AI标准工作组第8次会议”闭幕式在海口召开会上启动了“基于OpenI海参的视频编码大赛”哈尔滨工业大学范晓鹏教授介绍了大赛背景、选题及参赛流程等信息正式开启大赛参赛团队招募。" \
-d "remote_url=https://www.openi.org.cn/html/2019/dongtai_0903/255.html" | jq
```
请求参数说明:
cover_remote_url(optional): String, 封面访问地址
category_id(optional): Integer, 分类ID
content(optional): String, 新闻内容
title(requires): String, 新闻标题
simple_intruduce(requires): String, 新闻简介
remote_url(requires): String, 新闻访问地址
返回值
```
{
"status": 1,
"message": "token is invalid!"
}
```
---

View File

@ -1,5 +0,0 @@
= Redmine
Redmine is a flexible project management web application written using Ruby on Rails framework.
More details can be found in the doc directory or on the official website http://www.redmine.org

View File

@ -21,6 +21,7 @@ module Mobile
require_relative 'apis/resources'
require_relative 'apis/syllabuses'
require_relative 'apis/projects'
require_relative 'apis/homes'
class API < Grape::API
version 'v1', using: :path
@ -56,6 +57,11 @@ module Mobile
nil
end
# 使用固定token来简单验证用户是否有调用某个api权限
def authenticate_token!
raise 'token is invalid!' unless params[:token].strip == Setting.api_token
end
end
mount Apis::Auth
@ -77,10 +83,9 @@ module Mobile
mount Apis::Resources
mount Apis::Syllabuses
mount Apis::Projects
mount Apis::Homes
add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development?
end
end

View File

@ -0,0 +1,69 @@
#coding=utf-8
module Mobile
module Apis
class Homes< Grape::API
resources :homes do
desc "Current user's information in access token's scope<span class='accstr'>*</span>", {
headers: {
"Authorization" => {
description: "Valdates your identity",
required: true
}
}
}
desc "同步用户数量、开源项目数量、实训项目数量"
params do
optional :number, type: Integer, default: 1, desc: "同步的数量"
requires :token, type: String, desc: "token is validate user identity"
requires :type, type: String, values: ['user', 'project', 'practical_training_project'], desc: "type requires."
end
put 'sync_count' do
authenticate_token!
begin
HomesService.new.sync_count(params[:type].strip, params[:number].to_i)
present :status, 0
present :message, "sync success."
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
desc "同步社区动态新闻"
params do
optional :cover_remote_url, type: String, regexp: Home::REG_URL_FORMAT
optional :category_id, type: Integer
optional :content, type: String
requires :title, type: String
requires :simple_intruduce, type: String
requires :remote_url, type: String, regexp: Home::REG_URL_FORMAT
end
put 'sync_news' do
authenticate_token!
news_params = {
title: params[:title],
simple_intruduce: params[:simple_intruduce],
remote_url: params[:remote_url],
category_id: params[:category_id],
content: params[:content],
cover_remote_url: params[:cover_remote_url]
}
begin
DynamicNewsService.new.sync(news_params)
present :status, 0
present :message, 'sync success'
rescue => e
logger.info e
present :status, -1
present :message, e.message
end
end
end
end
end
end

3
app/models/category.rb Normal file
View File

@ -0,0 +1,3 @@
class Category < ActiveRecord::Base
has_many :dynamic_news
end

View File

@ -1,4 +1,7 @@
class DynamicNew < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :simple_intruduce, :category_id
scope :desc, -> { order('id desc') }

View File

@ -1,5 +1,6 @@
class Home < ActiveRecord::Base
SHOW_CONTENT_LENGTH = 245
REG_URL_FORMAT = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/
has_one :cover, as: :coverable, dependent: :destroy
has_many :banners, dependent: :destroy

View File

@ -0,0 +1,9 @@
class DynamicNewsService
def sync(news_params)
if news_params["category_id"].blank?
default_category = Category.find_by_name "社区动态"
news_params = news_params.merge({category_id: default_category&.id})
end
DynamicNew.create!(news_params)
end
end

View File

@ -0,0 +1,13 @@
class HomesService
def sync_count(type, sync_count)
home = Home.first
case type
when "user"
home.update_attributes!(users_count: home.users_count + sync_count)
when "project"
home.update_attributes!(projects_count: home.projects_count + sync_count)
when "practical_training_project"
home.update_attributes!(practical_training_projects_count: home.practical_training_projects_count + sync_count)
end
end
end

View File

@ -208,7 +208,7 @@ default_projects_modules:
# - dts
default_projects_tracker_ids:
serialized: true
default:
default:
# Role given to a non-admin user who creates a project
new_project_user_role_id:
format: int
@ -290,3 +290,6 @@ plugin_redmine_ckeditor:
toolbar: Source,ShowBlocks,--,Undo,Redo,-,Find,Replace,--,Bold,Italic,Underline,Strike,-,Subscript,Superscript,-,NumberedList,BulletedList,-,Outdent,Indent,Blockquote,-,JustifyLeft,JustifyCenter,JustifyRight,JustifyBlock,-,Link,Unlink,-,richImage,Table,HorizontalRule,/,Styles,Format,Font,FontSize,-,TextColor,BGColor
at_enabled:
default: 1
api_token:
default: 34c82f51e0b699d9d16d70fd6497c9b1e4821d6ea3e872558a6537a091076b8e

View File

@ -0,0 +1,15 @@
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
add_index :categories, :name
names = %w(社区动态 行业资讯 成员动态 开源项目 成果展示 技术分享)
names.each do |name|
Category.where(name: name).first_or_create
end
end
end

View File

@ -0,0 +1,5 @@
class AddCategoryRefToDynamicNews < ActiveRecord::Migration
def change
add_column :dynamic_news, :category_id, :integer, :null => false
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20191017102232) do
ActiveRecord::Schema.define(:version => 20191021063319) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -352,6 +352,14 @@ ActiveRecord::Schema.define(:version => 20191017102232) do
t.datetime "updated_at", :null => false
end
create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "categories", ["name"], :name => "index_categories_on_name"
create_table "challenge_samples", :force => true do |t|
t.string "input"
t.string "output"
@ -949,6 +957,7 @@ ActiveRecord::Schema.define(:version => 20191017102232) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "simple_intruduce"
t.integer "category_id", :null => false
end
add_index "dynamic_news", ["title"], :name => "index_dynamic_news_on_title"