Create OrdersPage, load orders from backend, move NavDrawer items to Component
This commit is contained in:
@@ -1,8 +1,24 @@
|
||||
{
|
||||
"data": [
|
||||
{ "id": 0, "icon": "mdi-chip", "name": "Electronic" },
|
||||
{ "id": 1, "icon": "mdi-soccer", "name": "Sports" },
|
||||
{ "id": 2, "icon": "mdi-tshirt-crew", "name": "Clothes" },
|
||||
{ "id": 3, "icon": "mdi-bookshelf", "name": "Books" }
|
||||
{
|
||||
"id": 0,
|
||||
"icon": "mdi-chip",
|
||||
"name": "Electronic"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"icon": "mdi-soccer",
|
||||
"name": "Sports"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"icon": "mdi-tshirt-crew",
|
||||
"name": "Clothes"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"icon": "mdi-bookshelf",
|
||||
"name": "Books"
|
||||
}
|
||||
]
|
||||
}
|
||||
28
software/backend/data/orderItems.json
Normal file
28
software/backend/data/orderItems.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"id": 0,
|
||||
"orderId": 0,
|
||||
"productId": 0,
|
||||
"quantity": 2
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"orderId": 1,
|
||||
"productId": 6,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"orderId": 2,
|
||||
"productId": 3,
|
||||
"quantity": 3
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"orderId": 2,
|
||||
"productId": 2,
|
||||
"quantity": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"orderId": 0,
|
||||
"productId": 4,
|
||||
"quantity": 2,
|
||||
"totalPrice": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,16 +1,19 @@
|
||||
{
|
||||
"data": [
|
||||
{ "id": 0, "accountId": 0, "totalPrice": 0 },
|
||||
{ "id": 1, "accountId": 1, "totalPrice": 0 },
|
||||
{ "id": 2, "accountId": 1, "totalPrice": 0 },
|
||||
{ "id": 3, "accountId": 2, "totalPrice": 0 },
|
||||
{ "id": 4, "accountId": 2, "totalPrice": 0 },
|
||||
{ "id": 5, "accountId": 3, "totalPrice": 0 },
|
||||
{ "id": 6, "accountId": 3, "totalPrice": 0 },
|
||||
{ "id": 7, "accountId": 3, "totalPrice": 0 },
|
||||
{ "id": 8, "accountId": 4, "totalPrice": 0 },
|
||||
{ "id": 9, "accountId": 5, "totalPrice": 0 },
|
||||
{ "id": 10, "accountId": 6, "totalPrice": 0 },
|
||||
{ "id": 11, "accountId": 6, "totalPrice": 0 }
|
||||
]
|
||||
{
|
||||
"id": 0,
|
||||
"accountId": 0,
|
||||
"totalPrice": 0
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"accountId": 3,
|
||||
"totalPrice": 0
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"accountId": 3,
|
||||
"totalPrice": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -2,8 +2,8 @@ import { Sequelize } from "sequelize-typescript"
|
||||
|
||||
// Models
|
||||
import { Category } from "./models/category.model"
|
||||
import { OrderedItem } from "./models/orderedItem.model"
|
||||
import { Order } from "./models/order.model"
|
||||
import { OrderItem } from "./models/orderItem.model"
|
||||
import { Product } from "./models/product.model"
|
||||
import { Account } from "./models/account.model"
|
||||
|
||||
@@ -18,7 +18,7 @@ export const sequelize = new Sequelize({
|
||||
username: dbUser,
|
||||
password: dbPassword,
|
||||
storage: "database.sqlite",
|
||||
models: [ Category, Product, Account, Order, OrderedItem ]
|
||||
models: [ Category, Product, Account, Order, OrderItem ]
|
||||
})
|
||||
|
||||
export function startDatabase() {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany, BelongsToMany } from 'sequelize-typescript';
|
||||
import { Account } from './account.model';
|
||||
import { OrderedItem } from './orderedItem.model';
|
||||
import { Product } from './product.model';
|
||||
import { OrderItem } from './orderItem.model';
|
||||
|
||||
@Table
|
||||
export class Order extends Model {
|
||||
@@ -12,10 +11,11 @@ export class Order extends Model {
|
||||
@Column
|
||||
totalPrice: number
|
||||
|
||||
|
||||
// Relations
|
||||
@BelongsTo(() => Account)
|
||||
user: Account
|
||||
account: Account
|
||||
|
||||
@BelongsToMany(() => Product, () => OrderedItem)
|
||||
orderedItems: OrderedItem
|
||||
}
|
||||
@HasMany(() => OrderItem)
|
||||
orderItem: OrderItem[]
|
||||
}
|
||||
|
||||
25
software/backend/models/orderItem.model.ts
Normal file
25
software/backend/models/orderItem.model.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Model, BelongsTo, Column, ForeignKey, HasMany, HasOne, Table } from "sequelize-typescript";
|
||||
import { Product } from "./product.model";
|
||||
import { Order } from "./order.model";
|
||||
|
||||
@Table
|
||||
export class OrderItem extends Model {
|
||||
@Column
|
||||
@ForeignKey(() => Order)
|
||||
orderId: number
|
||||
|
||||
@Column
|
||||
quantity: number
|
||||
|
||||
@Column
|
||||
@ForeignKey(() => Product)
|
||||
productId: number
|
||||
|
||||
|
||||
// Relations
|
||||
@BelongsTo(() => Order)
|
||||
order: Order
|
||||
|
||||
@BelongsTo(() => Product)
|
||||
product: Product
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany } from 'sequelize-typescript';
|
||||
import { Order } from './order.model';
|
||||
import { Product } from './product.model';
|
||||
|
||||
@Table
|
||||
export class OrderedItem extends Model {
|
||||
@Column
|
||||
@ForeignKey(() => Order)
|
||||
orderId: number
|
||||
|
||||
@Column
|
||||
@ForeignKey(() => Product)
|
||||
productId: number
|
||||
|
||||
@Column
|
||||
quantity: number
|
||||
|
||||
@Column
|
||||
totalPrice: number
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Table, Column, Model, ForeignKey, BelongsTo, BelongsToMany } from 'sequelize-typescript';
|
||||
import { Table, Column, Model, ForeignKey, BelongsTo, BelongsToMany, HasMany } from 'sequelize-typescript';
|
||||
import { Category } from './category.model';
|
||||
import { OrderedItem } from './orderedItem.model';
|
||||
import { Order } from './order.model';
|
||||
import { OrderItem } from './orderItem.model';
|
||||
|
||||
@Table
|
||||
export class Product extends Model {
|
||||
@@ -34,6 +33,6 @@ export class Product extends Model {
|
||||
@BelongsTo(() => Category)
|
||||
category: Category
|
||||
|
||||
@BelongsToMany(() => Order,() => OrderedItem)
|
||||
orderedItem: OrderedItem
|
||||
@HasMany(() => OrderItem)
|
||||
order: OrderItem
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
import { Order } from "../models/order.model";
|
||||
import { Product } from "../models/product.model";
|
||||
import { OrderItem } from "../models/orderItem.model";
|
||||
|
||||
export const order = Router()
|
||||
|
||||
order.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
Order.findAll()
|
||||
Order.findAll({
|
||||
where: { accountId: req.query.accountId },
|
||||
include: [
|
||||
{ model: OrderItem, include: [ Product ] }
|
||||
]
|
||||
})
|
||||
.then(orders => {
|
||||
res.json(orders)
|
||||
res.send(orders)
|
||||
})
|
||||
.catch(next)
|
||||
})
|
||||
6
software/backend/routes/orderItem.routes.ts
Normal file
6
software/backend/routes/orderItem.routes.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Product } from "../models/product.model";
|
||||
import { OrderItem } from "../models/orderItem.model";
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
|
||||
export const orderItem = Router()
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
import { OrderedItem } from "../models/orderedItem.model";
|
||||
|
||||
export const orderedItem = Router()
|
||||
|
||||
orderedItem.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
OrderedItem.findAll()
|
||||
.then(orderedItems => {
|
||||
res.json(orderedItems)
|
||||
})
|
||||
.catch(next)
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Category } from '../models/category.model'
|
||||
import { OrderedItem } from '../models/orderedItem.model'
|
||||
import { Order } from '../models/order.model'
|
||||
import { OrderItem } from '../models/orderItem.model'
|
||||
import { Product } from '../models/product.model'
|
||||
import { Account } from '../models/account.model'
|
||||
|
||||
@@ -8,15 +8,15 @@ import categories from "./../data/categories.json"
|
||||
import products from "./../data/products.json"
|
||||
import accounts from "./../data/accounts.json"
|
||||
import orders from "./../data/orders.json"
|
||||
import orderedItems from "./../data/orderedItems.json"
|
||||
import orderItems from "./../data/orderItems.json"
|
||||
|
||||
/**
|
||||
* Delete all datasets in every database table
|
||||
*/
|
||||
export function deleteAllTables() {
|
||||
Category.destroy({ truncate: true })
|
||||
OrderedItem.destroy({ truncate: true })
|
||||
Order.destroy({ truncate: true })
|
||||
OrderItem.destroy({truncate: true })
|
||||
Product.destroy({ truncate: true })
|
||||
Account.destroy({ truncate: true })
|
||||
}
|
||||
@@ -29,5 +29,5 @@ export function prepopulateDatabase() {
|
||||
Product.bulkCreate(products.data)
|
||||
Account.bulkCreate(accounts.data)
|
||||
Order.bulkCreate(orders.data)
|
||||
OrderedItem.bulkCreate(orderedItems.data)
|
||||
OrderItem.bulkCreate(orderItems.data)
|
||||
}
|
||||
@@ -6,8 +6,8 @@ import { startDatabase } from './database'
|
||||
import { category } from './routes/category.routes'
|
||||
import { product } from './routes/product.routes'
|
||||
import { order } from './routes/order.routes'
|
||||
import { orderedItem } from './routes/orderedItem.routes'
|
||||
import { account } from './routes/account.routes'
|
||||
import { orderItem } from './routes/orderItem.routes'
|
||||
|
||||
const app = express()
|
||||
const port = 3000
|
||||
@@ -26,8 +26,8 @@ app.use("/api", api)
|
||||
app.use("/categories", category)
|
||||
app.use("/products", product)
|
||||
app.use("/orders", order)
|
||||
app.use("/ordereditems", orderedItem)
|
||||
app.use("/accounts", account)
|
||||
app.use("/orderItems", orderItem)
|
||||
|
||||
// Static files
|
||||
const path = require('path')
|
||||
|
||||
Reference in New Issue
Block a user