forked from hasslesstech/edu-dis-labs
5.7 KiB
5.7 KiB
Реалізація інформаційного та програмного забезпечення
SQL-скрипт для створення та початкового наповнення бази даних
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Account`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Account` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` CHAR(255) NOT NULL,
`password` CHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) VISIBLE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Survey`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Survey` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`isPaused` TINYINT UNSIGNED NOT NULL,
`isNamed` TINYINT UNSIGNED NOT NULL,
`name` CHAR(255) NULL,
`duration` CHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
CONSTRAINT `fk_Survey_Account1`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`Account` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Question` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Text` CHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
CONSTRAINT `fk_Question_Survey1`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`Survey` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Response`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Response` (
`id` INT UNSIGNED NULL AUTO_INCREMENT,
`Value` VARCHAR(16384) NULL,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
PRIMARY KEY (`id`),
CONSTRAINT `fk_Response_Question`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`Question` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Response_Account1`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`Account` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Link`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Link` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`uses` INT NOT NULL,
`responces` INT NOT NULL,
`usageLimit` INT NULL,
`responceLimit` INT NULL,
`path` CHAR(32) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_Link_Survey1`
FOREIGN KEY (`id`)
REFERENCES `mydb`.`Survey` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
RESTfull-сервіс для керування опитуваннями
Код серверу (Python, Flask) для таблиці "survey":
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:1234@localhost/odb'
db = SQLAlchemy(app)
class Survey(db.Model):
id = db.Column(db.Integer, primary_key=True)
isPaused = db.Column(db.Boolean)
isNamed = db.Column(db.Boolean)
name = db.Column(db.String(255))
duration = db.Column(db.Integer)
account_id = db.Column(db.Integer)
def __repr__(self):
return f"<Survey {self.name}>"
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
@app.route("/api/survey", methods=["GET", "POST"])
def api_survey():
if request.method == "GET":
surveys = Survey.query.all()
return jsonify([survey.as_dict() for survey in surveys])
elif request.method == "POST":
data = request.json
new_survey = Survey(isPaused=data['isPaused'], isNamed=data['isNamed'], name=data['name'],
duration=data['duration'], account_id=data['account_id'])
db.session.add(new_survey)
db.session.commit()
return jsonify({'success': True}), 201
@app.route("/api/survey/<int:survey_id>", methods=["GET", "PUT", "DELETE"])
def api_survey_numbered(survey_id):
survey = Survey.query.get_or_404(survey_id)
if request.method == "GET":
return jsonify(survey.as_dict())
elif request.method == "PUT":
data = request.json
survey.isPaused = data['isPaused']
survey.isNamed = data['isNamed']
survey.name = data['name']
survey.duration = data['duration']
survey.account_id = data['account_id']
db.session.commit()
return jsonify({'success': True})
elif request.method == "DELETE":
db.session.delete(survey)
db.session.commit()
return jsonify({'success': True})
if __name__ == '__main__':
app.run(debug=True)
'''