Commit 9c00589b authored by 李尚科's avatar 李尚科

fix

parent 3130f337
Pipeline #13891 passed with stage
in 16 seconds
...@@ -12,7 +12,7 @@ class InstitutionController extends Controller { ...@@ -12,7 +12,7 @@ class InstitutionController extends Controller {
const { ctx } = this; const { ctx } = this;
const input_params = ctx.request.body; const input_params = ctx.request.body;
const results = await ctx.service.course.institution.getInstitutions(input_params); const results = await ctx.service.course.institution.getInstitutions(input_params);
ctx.success({ results }); ctx.success({ results });
} }
...@@ -47,6 +47,9 @@ class InstitutionController extends Controller { ...@@ -47,6 +47,9 @@ class InstitutionController extends Controller {
const { ctx } = this; const { ctx } = this;
const class_id = ctx.params.class_id; const class_id = ctx.params.class_id;
if (!class_id) {
ctx.failed('error class_id');
}
let ret = await ctx.service.course.institution.getClass(class_id); let ret = await ctx.service.course.institution.getClass(class_id);
ctx.success(ret); ctx.success(ret);
...@@ -71,6 +74,9 @@ class InstitutionController extends Controller { ...@@ -71,6 +74,9 @@ class InstitutionController extends Controller {
const { ctx } = this; const { ctx } = this;
const teacher_id = ctx.params.teacher_id; const teacher_id = ctx.params.teacher_id;
if (!teacher_id) {
ctx.failed('error teacher_id');
}
let ret = await ctx.service.course.institution.getTeacher(teacher_id); let ret = await ctx.service.course.institution.getTeacher(teacher_id);
ctx.success(ret); ctx.success(ret);
......
'use strict';
const Controller = require('egg').Controller;
class UserController extends Controller {
/**
*
*/
async getBabyInfo() {
const { ctx } = this;
const result = await ctx.service.course.user.getBabyInfo();
ctx.success({ result });
}
async saveBabyInfo() {
const { ctx } = this;
const input_params = ctx.request.body;
const result = await ctx.service.course.user.saveBabyInfo(input_params);
ctx.success({ result });
}
async delBabyInfo() {
const { ctx } = this;
const result = await ctx.service.course.user.delBabyInfo();
ctx.success({ result });
}
async getCollectInstitutions() {
const { ctx } = this;
const input_params = ctx.request.body;
const result = await ctx.service.course.user.getCollectInstitutions(input_params);
ctx.success({ result });
}
async collectInstitution() {
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.user.collectInstitution(institution_id);
ctx.success({ result });
}
}
module.exports = UserController;
'use strict';
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseUserBaby = app.prometheusModel.define('course_user_baby', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
user_id: STRING,
app_user_id: STRING,
app_id: STRING,
app_type_id: STRING,
gender: ENUM('boy', 'girl'),
birth: STRING,
address: STRING,
is_deleted: INTEGER,
created_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_user_baby',
});
CourseUserBaby.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseUserBaby.findOne({
attributes: attributes,
where: where,
});
}
CourseUserBaby.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseUserBaby.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseUserBaby.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await CourseUserBaby.findAndCountAll(condition);
return { page, count, rows };
}
CourseUserBaby.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseUserBaby.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
CourseUserBaby.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseUserBaby.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return CourseUserBaby;
};
\ No newline at end of file
'use strict';
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseUserCollection = app.prometheusModel.define('course_user_collection', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
user_id: STRING,
app_user_id: STRING,
app_id: STRING,
app_type_id: STRING,
institution_id: INTEGER,
is_deleted: INTEGER,
created_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_user_collection',
});
CourseUserCollection.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseUserCollection.findOne({
attributes: attributes,
where: where,
});
}
CourseUserCollection.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseUserCollection.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseUserCollection.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await CourseUserCollection.findAndCountAll(condition);
return { page, count, rows };
}
CourseUserCollection.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseUserCollection.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
CourseUserCollection.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseUserCollection.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return CourseUserCollection;
};
\ No newline at end of file
...@@ -15,4 +15,11 @@ module.exports = app => { ...@@ -15,4 +15,11 @@ module.exports = app => {
router.get('/teachers', 'course.institution.teacherList'); router.get('/teachers', 'course.institution.teacherList');
router.get('/teacher/:teacher_id', 'course.institution.teacherInfo'); router.get('/teacher/:teacher_id', 'course.institution.teacherInfo');
router.get('/user/baby', 'course.user.getBabyInfo');
router.post('/user/baby', 'course.user.saveBabyInfo');
router.delete('/user/baby', 'course.user.delBabyInfo');
router.get('/user/collection/institution', 'course.user.getCollectInstitutions');
router.post('/user/collection/institution', 'course.user.collectInstitution');
}; };
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const R = require('ramda');
const GENDER = {
boy: '小王子',
girl: '小公举'
}
class UserService extends Service {
async getBabyInfo() {
const { ctx } = this;
const user_id = 1;
const app_type_id = 1;
const app_user_id = 1;
const app_id = 1;
const where = { user_id, is_deleted: 0 };
const baby_info = await ctx.prometheusModel.CourseUserBaby.one({ where, });
let ret = {};
if (baby_info && baby_info.id) {
ret.id = baby_info.id;
ret.gender = baby_info.gender;
ret.birth = baby_info.birth;
ret.address = baby_info.address;
ret.gender_text = GENDER[ret.gender];
ret.age = moment().format('YYYY') - baby_info.birth.substr(0, 4);
}
return ret;
}
async saveBabyInfo(input) {
const { ctx } = this;
const user_id = 1;
const app_type_id = 1;
const app_user_id = 1;
const app_id = 1;
const { gender, birth, address } = input;
const baby_info = await this.getBabyInfo();
const where = { user_id, is_deleted: 0 };
if (baby_info && baby_info.id) {
await ctx.prometheusModel.CourseUserBaby.edit({ params: { gender, birth, address }, where });
} else {
await ctx.prometheusModel.CourseUserBaby.add({ user_id, app_type_id, app_user_id, app_id, gender, birth, address });
}
return true;
}
async delBabyInfo() {
const { ctx } = this;
const user_id = 1;
const app_type_id = 1;
const app_user_id = 1;
const app_id = 1;
await ctx.prometheusModel.CourseUserBaby.edit({ params: { is_deleted: 1, }, where: { user_id } });
return true;
}
async collectInstitution(institution_id) {
const { ctx } = this;
const user_id = 1;
const app_type_id = 1;
const app_user_id = 1;
const app_id = 1;
const where = { user_id, is_deleted: 0 };
let ret = await ctx.prometheusModel.CourseUserCollection.one({ where, });
if (ret && ret.id) {
return ret.id;
}
ret = await await ctx.prometheusModel.CourseUserCollection.add({ user_id, app_type_id, app_user_id, app_id, institution_id });
return ret;
}
async getCollectInstitutions(input) {
const { ctx } = this;
const user_id = 1;
const app_type_id = 1;
const app_user_id = 1;
const app_id = 1;
const { page, limit } = input;
const where = { user_id, is_deleted: 0 };
const collect_institution_rows = await ctx.prometheusModel.CourseUserCollection.list({ page, limit, where });
const institution_ids = R.pluck('institution_id', collect_institution_rows.rows);
if (institution_ids.length === 0) {
return { page: collect_institution_rows.page, count: collect_institution_rows.count, rows: [] };
}
const institutions = await ctx.prometheusModel.CourseInstitution.all({ where: { id: { $in: institution_ids }, status: 1, is_deleted: 0 } });
const ret = await ctx.service.course.institution.formatInstitutions(institutions);
return { page: collect_institution_rows.page, count: collect_institution_rows.count, rows: ret };
}
}
module.exports = UserService;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment