waaa
This commit is contained in:
parent
da817cb96a
commit
b1974ce50e
1
.gitignore
vendored
1
.gitignore
vendored
@ -33,3 +33,4 @@ yarn-error.log*
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
db.sqlite
|
||||
|
||||
27
docker-compose.yml
Normal file
27
docker-compose.yml
Normal file
@ -0,0 +1,27 @@
|
||||
version: "3.0"
|
||||
services:
|
||||
db:
|
||||
image: mariadb
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: rootpassword
|
||||
MYSQL_DATABASE: mydatabase
|
||||
MYSQL_USER: user
|
||||
MYSQL_PASSWORD: password
|
||||
volumes:
|
||||
- dbdata:/var/lib/mysql
|
||||
ports:
|
||||
- "3306:3306"
|
||||
phpmyadmin:
|
||||
image: phpmyadmin/phpmyadmin
|
||||
container_name: pma
|
||||
links:
|
||||
- db
|
||||
environment:
|
||||
PMA_HOST: db
|
||||
PMA_PORT: 3306
|
||||
PMA_ARBITRARY: 1
|
||||
restart: always
|
||||
ports:
|
||||
- 8081:80
|
||||
volumes:
|
||||
dbdata:
|
||||
1016
package-lock.json
generated
1016
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -24,6 +24,9 @@
|
||||
"react": "18.2.0",
|
||||
"react-bootstrap": "^2.7.4",
|
||||
"react-dom": "18.2.0",
|
||||
"sequelize": "^6.32.0",
|
||||
"sqlite": "^4.2.1",
|
||||
"sqlite3": "^5.1.6",
|
||||
"tailwindcss": "3.3.2",
|
||||
"typescript": "5.0.4"
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ export interface IPost extends RowDataPacket {
|
||||
export async function getPost(id:Number): Promise<IPost[]> {
|
||||
// let [rows]:Array<IPost> = await conn.execute("SELECT * FROM `post`", []);
|
||||
return new Promise((resolve, reject) => {
|
||||
let res = getConnection().then((conn)=>{conn.query<IPost[]>(`SELECT * FROM \`post\` WHERE \`id\` = ${id}`, (err:QueryError, res) => {
|
||||
let res = getConnection().then((conn)=>{conn.query<IPost[]>(`SELECT * FROM post WHERE id = ${id}`, (err:QueryError, res) => {
|
||||
if (err) reject(err)
|
||||
else resolve(res)
|
||||
});
|
||||
|
||||
@ -3,9 +3,9 @@ import mysql2, { Connection } from "mysql2";
|
||||
|
||||
const connectionInfo:mysql.ConnectionOptions = {
|
||||
host: '127.0.0.1',
|
||||
port: 40000,
|
||||
user: 'root',
|
||||
password: 'rootpassword',
|
||||
port: 3306,
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
database: 'portfolio',
|
||||
|
||||
};
|
||||
|
||||
@ -1,13 +1,42 @@
|
||||
import Gens from "@/gens";
|
||||
import { Sequelize, DataTypes } from 'sequelize';
|
||||
import { Sequelize, DataTypes, Association } from 'sequelize';
|
||||
|
||||
const sequelize = new Sequelize('sqlite::memory:');
|
||||
const MPost = sequelize.define('Post', {
|
||||
id: DataTypes.INTEGER,
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: 'db.sqlite'
|
||||
});
|
||||
export const MPost = sequelize.define('Post', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true
|
||||
},
|
||||
title: DataTypes.STRING,
|
||||
content: DataTypes.STRING,
|
||||
date: DataTypes.DATE,
|
||||
// date: DataTypes.DATE,
|
||||
});
|
||||
export const MAuth = sequelize.define('Auth', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true
|
||||
},
|
||||
token: DataTypes.STRING,
|
||||
user_id: {
|
||||
type: DataTypes.INTEGER,
|
||||
}
|
||||
// date: DataTypes.DATE,
|
||||
});
|
||||
export const MUser = sequelize.define('User', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true
|
||||
},
|
||||
username: DataTypes.STRING,
|
||||
password: DataTypes.STRING,
|
||||
// date: DataTypes.DATE,
|
||||
});
|
||||
|
||||
MAuth.hasOne(MUser)
|
||||
|
||||
|
||||
export type Post = {
|
||||
id: Number,
|
||||
|
||||
52
src/pages/api/auth/index.ts
Normal file
52
src/pages/api/auth/index.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import mysql2, { Connection, RowDataPacket, OkPacket, QueryError } from "mysql2";
|
||||
import { getConnection } from "@/db";
|
||||
import { Post, postPlaceholder } from "@/model/Models";
|
||||
import { getPosts, IPost } from "@/controller/Post";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { MPost, MUser, MAuth } from "@/model/Models"
|
||||
import { Sequelize } from "sequelize";
|
||||
import { Elsie_Swash_Caps } from "next/font/google";
|
||||
|
||||
export default async function handler(req:NextApiRequest, res:NextApiResponse) {
|
||||
await MUser.sync();
|
||||
await MAuth.sync();
|
||||
switch (req.method) {
|
||||
case 'POST':
|
||||
case 'GET':
|
||||
const users = await MUser.findAll();
|
||||
// res.status(200).json(posts);
|
||||
let username = req.body.username;
|
||||
let password = req.body.password;
|
||||
console.log(req.body );
|
||||
if(users.length == 0){
|
||||
MUser.create({
|
||||
username: "admin",
|
||||
password: "changeme"
|
||||
})
|
||||
}
|
||||
users.forEach(user => async {
|
||||
if(user.username == username && user.password == password){
|
||||
try{
|
||||
const authtoken = await MAuth.findOne({where : {user_id: user.id}});
|
||||
if(authtoken != null){
|
||||
res.status(200).json({"status":"correct"});
|
||||
console.log(authtoken);
|
||||
}
|
||||
else{
|
||||
res.status(200).json({"status":"no such auth token"});
|
||||
}
|
||||
} catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
else{
|
||||
console.log(user.password);
|
||||
res.status(200).json({"status":"incorrect"});
|
||||
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
33
src/pages/api/brown/index.ts
Normal file
33
src/pages/api/brown/index.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import mysql2, { Connection, RowDataPacket, OkPacket, QueryError } from "mysql2";
|
||||
import { getConnection } from "@/db";
|
||||
import { Post, postPlaceholder } from "@/model/Models";
|
||||
import { getPosts, IPost } from "@/controller/Post";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { MPost } from "@/model/Models"
|
||||
import { Sequelize } from "sequelize";
|
||||
|
||||
export default async function handler(req:NextApiRequest, res:NextApiResponse) {
|
||||
await MPost.sync();
|
||||
switch (req.method) {
|
||||
case 'GET':
|
||||
const posts = await MPost.findAll();
|
||||
res.status(200).json(posts);
|
||||
break;
|
||||
case 'POST':
|
||||
try {
|
||||
const post = await MPost.create({
|
||||
title: req.body.title,
|
||||
content: req.body.content,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},{ fields: ['title','content'] });
|
||||
const posts2 = await MPost.findAll();
|
||||
res.status(200).json(posts2);
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
11
src/pages/api/migrate/index.ts
Normal file
11
src/pages/api/migrate/index.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { MPost } from "@/model/Models"
|
||||
import { JSON, Sequelize } from "sequelize";
|
||||
|
||||
export default async function handler(req:NextApiRequest, res:NextApiResponse) {
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: 'db.sqlite'
|
||||
});
|
||||
await MPost.sync();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user