User registration completed

This commit is contained in:
2024-09-10 20:28:24 +02:00
parent 628d1e7bee
commit ee07a5a5af
14 changed files with 203 additions and 56 deletions

View File

@@ -4,7 +4,10 @@
"id": 0,
"username": "hagemeister93",
"password": "Xjt3qb5t",
"address": "Laportestraße 22, 30449 Hannover",
"street": "Laportestraße",
"houseNumber": 22,
"postalCode": 30449,
"city": "Hannover",
"firstName": "Laurin",
"lastName": "Hagemeister"
},
@@ -12,7 +15,10 @@
"id": 1,
"username": "katjaStoiber",
"password": "target123",
"address": "Gustav-Adolf-Straße 30, 30167 Hannover",
"street": "Gustav-Adolf-Straße",
"houseNumber": 30,
"postalCode": 30167,
"city": "Hannover",
"firstName": "Katja",
"lastName": "Stoiber"
},
@@ -20,7 +26,10 @@
"id": 2,
"username": "oetkerohnek",
"password": "iloveyou",
"address": "Eckermannstraße 1, 30625 Hannover",
"street": "Eckermannstraße",
"houseNumber": 1,
"postalCode": 30625,
"city": "Hannover",
"firstName": "Luna",
"lastName": "Oeter"
},
@@ -28,7 +37,10 @@
"id": 3,
"username": "duranduran",
"password": "H4nn0ver",
"address": "Schlägerstraße 36, 30171 Hannover",
"street": "Schlägerstraße",
"houseNumber": 36,
"postalCode": 30171,
"city": "Hannover",
"firstName": "Jürgen",
"lastName": "Durand"
},
@@ -36,7 +48,10 @@
"id": 4,
"username": "guitarhero",
"password": "gwerty123",
"address": "Steinmetzstraße 12, 30163 Hannover",
"street": "Steinmetzstraße",
"houseNumber": 12,
"postalCode": 30163,
"city": "Hannover",
"firstName": "Frederik",
"lastName": "Furtwängler"
},
@@ -44,7 +59,10 @@
"id": 5,
"username": "herbstMareike",
"password": "qhsrbpgrs",
"address": "Allerweg 33, 30851 Langenhagen",
"street": "Allerweg",
"houseNumber": 33,
"postalCode": 30851,
"city": "Langenhagen",
"firstName": "Mareike",
"lastName": "Herbst"
},
@@ -52,7 +70,10 @@
"id": 6,
"username": "seibertmitb",
"password": "{jkz+WvQe",
"address": "Marktstraße 26, 30880 Laatzen",
"street": "Marktstraße",
"houseNumber": 26,
"postalCode": 30880,
"city": "Laatzen",
"firstName": "Janna",
"lastName": "Seibert"
}

View File

@@ -23,7 +23,7 @@ export const sequelize = new Sequelize({
export function startDatabase() {
// Create database and tables
sequelize.sync({ force: false })
sequelize.sync({ force: true })
.then(() => {
console.log(`Database & tables created!`)
})

View File

@@ -1,23 +1,33 @@
import { Table, Column, Model, HasMany } from 'sequelize-typescript';
import { Table, Column, Model, HasMany, Unique } from 'sequelize-typescript';
import { Order } from './order.model';
@Table
export class Account extends Model {
@Unique
@Column
username: string
@Column
password: string
@Column
address: string
@Column
firstName: string
@Column
lastName: string
@Column
street: string
@Column
houseNumber: number
@Column
postalCode: number
@Column
city: string
// Relations
@HasMany(() => Order)
orders: Order[]

View File

@@ -1,12 +1,36 @@
import { Router, Request, Response, NextFunction } from "express";
import { Account } from "../models/account.model";
import { validateString } from "../scripts/validateHelper";
export const account = Router()
account.get("/", (req: Request, res: Response, next: NextFunction)=> {
// Request all user from the database
account.get("/", (req: Request, res: Response, next: NextFunction) => {
Account.findAll()
.then(accounts => {
res.json(accounts)
})
.catch(next)
})
// Creating a new user
account.post("/", (req: Request, res: Response, next: NextFunction) => {
if (!validateString(req.body.username, 4))
{
res.status(400).send({ error: "Username too short!" })
}
else if (!validateString(req.body.password, 8))
{
res.status(400).send({ error: "Password too short!" })
}
else
{
Account.create(req.body)
.then(account => {
res.json(account)
res.status(200).send()
}).catch(reason => {
res.status(400).send({ error: reason })
})
}
})

View File

@@ -13,7 +13,6 @@ category.get("/", (req: Request, res: Response, next: NextFunction) => {
category.post("/", (req: Request, res: Response, next: NextFunction) => {
try {
console.log(req.body)
const category = Category.create(req.body)
res.status(201).json(category)
} catch (e) {

View File

@@ -1,5 +0,0 @@
{
"id": 1,
"title": "Hello World",
"completed": false
}

View File

@@ -0,0 +1,16 @@
/**
* Validates a string for undefined and length
*
* @param string String to prove
* @param length Minimal length of string
*
* @returns True if valid
*/
export function validateString(string: string, length: number = 0) : boolean {
if (string != undefined) {
if (string.length >= length)
return true
}
return false
}