Browse Source

Change model style.

pull/6/head
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', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
contest_id: {
type: Sequelize.INTEGER,
references: {
model: 'contest',
key: 'id'
}
}
contest_id: { type: Sequelize.INTEGER }
}, {
timestamps: false,
tableName: 'rating_calculation',
@ -45,13 +39,8 @@ let model = db.define('rating_calculation', {
let Model = require('./common');
class RatingCalculation extends Model {
static async create(contest_id, newRatingList) {
const newItem = await 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;
static async create(contest_id) {
return RatingCalculation.fromRecord(RatingCalculation.model.create({ contest_id: contest_id }));
}
async loadRelationships() {

26
models/rating_history.js

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

Loading…
Cancel
Save