Skip to content

Commit

Permalink
Merge pull request #9 from Kaduh15/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Kaduh15 authored Jul 30, 2023
2 parents d47c1a6 + 39f30c7 commit f250f00
Show file tree
Hide file tree
Showing 40 changed files with 719 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prisma.fileWatcher": true
}
2 changes: 1 addition & 1 deletion app/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"db:migrate": "prisma migrate dev",
"db:studio": "prisma studio",
"db:seed": "prisma db seed",
"test": "vitest",
"test": "vitest dev",
"test:ui": "vitest --ui",
"coverage": "vitest run --coverage"
},
Expand Down
7 changes: 4 additions & 3 deletions app/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import express from 'express'
import 'express-async-errors'

import errorMiddleware from './middlewares/error.middleware'
import { usersRouter } from './routes'
import { authRouter } from './routes/auth-router'
import { authRouter, openRouter, queueRouter, userRouter } from './routes'

class App {
public app: express.Express
Expand Down Expand Up @@ -36,8 +35,10 @@ class App {
res.status(200).json({ Hello: 'world!' })
})

this.app.use('/user', usersRouter)
this.app.use('/user', userRouter)
this.app.use('/login', authRouter)
this.app.use('/open', openRouter)
this.app.use('/queue', queueRouter)

this.app.use(errorMiddleware)
}
Expand Down
12 changes: 3 additions & 9 deletions app/backend/src/entities/base.entity.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { Id } from '@/repositories/base.repository'

class IBaseEntity {
interface IBaseEntity {
id: Id
createdAt?: Date
updatedAt?: Date

constructor(data: BaseEntity) {
this.id = data.id
this.createdAt = data.createdAt
this.updatedAt = data.updatedAt
}
}

export class BaseEntity implements IBaseEntity {
id: Id
createdAt?: Date
updatedAt?: Date
createdAt: Date
updatedAt: Date

constructor(data: BaseEntity) {
this.id = data.id
Expand Down
17 changes: 17 additions & 0 deletions app/backend/src/entities/open.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BaseEntity } from './base.entity'

export type Status = 'WAITING' | 'ABSENT' | 'DONE'

export default class Open extends BaseEntity {
isOpen: boolean

constructor({ id, createdAt, updatedAt, isOpen }: Open) {
super({
id,
createdAt,
updatedAt,
})

this.isOpen = isOpen
}
}
20 changes: 20 additions & 0 deletions app/backend/src/entities/queue.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BaseEntity } from './base.entity'

export type Status = 'WAITING' | 'ABSENT' | 'DONE'

export default class Queue extends BaseEntity {
name: string
phoneNumber: string
status: Status

constructor({ id, name, createdAt, updatedAt, phoneNumber, status }: Queue) {
super({
id,
createdAt,
updatedAt,
})
this.name = name
this.phoneNumber = phoneNumber
this.status = status
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- CreateTable
CREATE TABLE "queue" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"phone" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "queue_pkey" PRIMARY KEY ("id")
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `phone` on the `queue` table. All the data in the column will be lost.
- Added the required column `phoneNumber` to the `queue` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "queue" DROP COLUMN "phone",
ADD COLUMN "phoneNumber" TEXT NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- CreateEnum
CREATE TYPE "Status" AS ENUM ('WAITING', 'ABSENT', 'DONE');

-- AlterTable
ALTER TABLE "queue" ADD COLUMN "status" "Status" NOT NULL DEFAULT 'WAITING';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- Added the required column `updatedAt` to the `Open` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Open" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;
21 changes: 19 additions & 2 deletions app/backend/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,25 @@ model User {
}

model Open {
id Int @id @default(autoincrement())
isOpen Boolean @default(false)
id Int @id @default(autoincrement())
isOpen Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model queue {
id Int @id @default(autoincrement())
name String
phoneNumber String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status Status @default(WAITING)
}

enum Status {
WAITING
ABSENT
DONE
}

enum Role {
Expand Down
2 changes: 1 addition & 1 deletion app/backend/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function main() {
create: {
email: 'admin@admin.com',
name: 'Admin',
password: await Encrypt.hash('admin'),
password: await Encrypt.hash('@admin'),
role: 'ADMIN',
},
})
Expand Down
2 changes: 1 addition & 1 deletion app/backend/src/repositories/base.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseEntity } from '@/entities/base.entity'
export type Id = number

export interface BaseRepository<T = object & BaseEntity> {
create(data: Omit<T, 'id'>): Promise<T>
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'>): Promise<T>
getAll(): Promise<T[]>
getById(id: Id): Promise<T | undefined>
update(id: Id, data: Partial<T>): Promise<T>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Open from '@/entities/open.entity'
import { NotFoundError } from '@/utils/http-errors'

import { OpenRepository } from './open.repository'

export class OpenRepositoryInMemory implements OpenRepository {
opens: Open[] = []
index = 0

create({
isOpen = false,
}: Omit<Open, 'id' | 'createdAt' | 'updatedAt'>): Promise<Open> {
const date = new Date()

const open = new Open({
id: this.index + 1,
isOpen,
createdAt: date,
updatedAt: date,
})

this.opens.push(open)
this.index++

return Promise.resolve(open)
}

getAll(): Promise<Open[]> {
return Promise.resolve(this.opens)
}

getById(id: number): Promise<Open | undefined> {
const open = this.opens.find((open) => open.id === id)

return Promise.resolve(open)
}

update(id: number, data: Partial<Open>): Promise<Open> {
const open = this.opens.find((open) => open.id === id)

if (!open) {
return Promise.reject(NotFoundError)
}
open.isOpen = data.isOpen ?? open.isOpen

open.updatedAt = new Date()

return Promise.resolve(new Open(open))
}

delete(id: number): Promise<void> {
const index = this.opens.findIndex((open) => open.id === id)

if (!index) {
return Promise.reject(NotFoundError)
}

this.opens.splice(index, 1)

return Promise.resolve()
}

deleteAll(): Promise<void> {
this.opens = []

return Promise.resolve()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Open from '@/entities/open.entity'
import prisma from '@/prisma/prisma-client'

import { OpenRepository } from './open.repository'

export class OpenPrismaRepository implements OpenRepository {
async create(
data: Omit<Open, 'id' | 'createdAt' | 'updatedAt'>,
): Promise<Open> {
const open = await prisma.open.create({
data,
})

return new Open(open)
}

getAll(): Promise<Open[]> {
throw new Error('Method not implemented.')
}

async getById(id: number): Promise<Open | undefined> {
const open = await prisma.open.findUnique({
where: { id },
})

if (!open) return undefined

return new Open(open)
}

async update(id: number, data: Partial<Open>): Promise<Open> {
const openUpdated = await prisma.open.update({
where: { id },
data,
})

return new Open(openUpdated)
}

delete(_id: number): Promise<void> {
throw new Error('Method not implemented.')
}

async deleteAll(): Promise<void> {
await prisma.open.deleteMany()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Open from '@/entities/open.entity'

import { BaseRepository } from '../base.repository'

export type OpenRepository = BaseRepository<Open>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Queue from '@/entities/queue.entity'

import { QueueRepository } from './queue.repository'

export class QueueRepositoryInMemory implements QueueRepository {
private queues: Queue[] = []
private index = 1

getToday(): Promise<Queue[]> {
const queueToday = this.queues.filter((queue) => {
const today = new Date()
const queueDate = new Date(queue.createdAt)

return (
queueDate.getDate() === today.getDate() &&
queueDate.getMonth() === today.getMonth() &&
queueDate.getFullYear() === today.getFullYear()
)
})

return Promise.resolve(queueToday)
}

create(data: Omit<Queue, 'id'>): Promise<Queue> {
const nowDate = new Date()

const newQueue = new Queue({
...data,
id: this.index,
createdAt: nowDate,
updatedAt: nowDate,
status: 'WAITING',
})

this.queues.push(newQueue)

this.index++

return Promise.resolve(newQueue)
}

getAll(): Promise<Queue[]> {
throw new Error('Method not implemented.')
}

getById(_id: number): Promise<Queue | undefined> {
throw new Error('Method not implemented.')
}

update(_id: number, _data: Partial<Queue>): Promise<Queue> {
throw new Error('Method not implemented.')
}

delete(_id: number): Promise<void> {
throw new Error('Method not implemented.')
}

deleteAll(): Promise<void> {
this.queues = []

return Promise.resolve()
}
}
Loading

0 comments on commit f250f00

Please sign in to comment.