Browse Source

Change model style.

master
t123yh 7 years ago
parent
commit
6efc449e06
  1. 17
      models/rating_calculation.js
  2. 26
      models/rating_history.js

17
models/rating_calculation.js

@ -26,13 +26,7 @@ const Contest = syzoj.model('contest');
let model = db.define('rating_calculation', { let model = db.define('rating_calculation', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
contest_id: { contest_id: { type: Sequelize.INTEGER }
type: Sequelize.INTEGER,
references: {
model: 'contest',
key: 'id'
}
}
}, { }, {
timestamps: false, timestamps: false,
tableName: 'rating_calculation', tableName: 'rating_calculation',
@ -45,13 +39,8 @@ let model = db.define('rating_calculation', {
let Model = require('./common'); let Model = require('./common');
class RatingCalculation extends Model { class RatingCalculation extends Model {
static async create(contest_id, newRatingList) { static async create(contest_id) {
const newItem = await RatingCalculation.model.create({ contest_id: contest_id }); return RatingCalculation.fromRecord(RatingCalculation.model.create({ contest_id: contest_id }));
const RatingHistory = syzoj.model('rating_history');
for (const val of newRatingList) {
await RatingHistory.create(newItem.id, val.user_id, val.rating);
}
return newItem;
} }
async loadRelationships() { async loadRelationships() {

26
models/rating_history.js

@ -24,22 +24,8 @@ const User = syzoj.model('user');
let db = syzoj.db; let db = syzoj.db;
let model = db.define('rating_history', { let model = db.define('rating_history', {
rating_calculation_id: { rating_calculation_id: { type: Sequelize.INTEGER, primaryKey: true },
type: Sequelize.INTEGER, user_id: { type: Sequelize.INTEGER, primaryKey: true },
primaryKey: true,
references: {
model: 'rating_calculation',
key: 'id'
}
},
user_id: {
type: Sequelize.INTEGER,
primaryKey: true,
references: {
model: 'user',
key: 'id'
}
},
rating_after: { type: Sequelize.INTEGER }, rating_after: { type: Sequelize.INTEGER },
}, { }, {
timestamps: false, timestamps: false,
@ -57,15 +43,11 @@ let model = db.define('rating_history', {
let Model = require('./common'); let Model = require('./common');
class RatingHistory extends Model { class RatingHistory extends Model {
static async create(rating_calculation_id, user_id, rating) { static async create(rating_calculation_id, user_id, rating) {
const newRecord = await RatingHistory.model.create({ return RatingHistory.fromRecord(RatingHistory.model.build({
rating_calculation_id: rating_calculation_id, rating_calculation_id: rating_calculation_id,
user_id: user_id, user_id: user_id,
rating_after: rating rating_after: rating
}); }));
await newRecord.loadRelationShips();
newRecord.user.rating = rating;
await newRecord.user.save();
return newRecord;
} }
async loadRelationships() { async loadRelationships() {

Loading…
Cancel
Save