Skip to content

Commit

Permalink
Folder / File - Service, Controller, Route, Test files (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
oroth8 authored Apr 13, 2023
1 parent 4086b2c commit f58b714
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 220 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# Maps tcp port 5432 on service container to the host
- 5432:5432
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/veritext_box
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/veritext_box_test
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
3 changes: 2 additions & 1 deletion .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"**/*.{js,jsx,scss,json,md}": "prettier --write",
"**/*.{js,jsx}": "eslint --fix --max-warnings=0"
"fe/**/*.{js,jsx}": "eslint --fix --max-warnings=0",
"be/**/*.{js,jsx}": "eslint --fix --max-warnings=0"
}
5 changes: 3 additions & 2 deletions be/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import { App } from './app.js';
import { HealthRoute } from './src/routes/health.routes.js';
import { UserRoute } from './src/routes/user.routes.js';
import { FolderRoute } from './src/routes/folder.routes.js';
import { FileRoute } from './src/routes/file.routes.js';

// ValidateEnv();

const app = new App([new HealthRoute(), new UserRoute()]);
const app = new App([new HealthRoute(), new FolderRoute(), new FileRoute()]);

app.listen();
62 changes: 62 additions & 0 deletions be/src/controllers/files.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { FileService } from '../services/file.service.js'

export class FilesController {
get file() {
return new FileService()
}

getFiles = async (req, res, next) => {
try {
const findAllFilesData = await this.file.findAllFile()

res.status(200).json({ data: findAllFilesData, message: 'findAll' })
} catch (error) {
next(error)
}
}

getFileById = async (req, res, next) => {
try {
const fileId = Number(req.params.id)
const findOneFileData = await this.file.findFileById(fileId)

res.status(200).json({ data: findOneFileData, message: 'findOne' })
} catch (error) {
next(error)
}
}

createFile = async (req, res, next) => {
try {
const fileData = req.body
const createFileData = await this.file.createFile(fileData)

res.status(201).json({ data: createFileData, message: 'created' })
} catch (error) {
next(error)
}
}

updateFile = async (req, res, next) => {
try {
const fileId = Number(req.params.id)
const fileData = req.body
const updateFileData = await this.file.updateFile(fileId, fileData)

res.status(200).json({ data: updateFileData, message: 'updated' })
} catch (error) {
next(error)
}
}

deleteFile = async (req, res, next) => {
try {
const fileId = Number(req.params.id)
const deleteFileData = await this.file.deleteFile(fileId)

res.status(200).json({ data: deleteFileData, message: 'deleted' })
} catch (error) {
next(error)
}
}
}
65 changes: 65 additions & 0 deletions be/src/controllers/folders.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { FolderService } from '../services/folder.service.js'

export class FoldersController {
get folder() {
return new FolderService()
}

getFolders = async (req, res, next) => {
try {
const findAllFoldersData = await this.folder.findAllFolder()

res.status(200).json({ data: findAllFoldersData, message: 'findAll' })
} catch (error) {
next(error)
}
}

getFolderById = async (req, res, next) => {
try {
const folderId = Number(req.params.id)
const findOneFolderData = await this.folder.findFolderById(folderId)

res.status(200).json({ data: findOneFolderData, message: 'findOne' })
} catch (error) {
next(error)
}
}

createFolder = async (req, res, next) => {
try {
const folderData = req.body
const createFolderData = await this.folder.createFolder(folderData)

res.status(201).json({ data: createFolderData, message: 'created' })
} catch (error) {
next(error)
}
}

updateFolder = async (req, res, next) => {
try {
const folderId = Number(req.params.id)
const folderData = req.body
const updateFolderData = await this.folder.updateFolder(
folderId,
folderData
)

res.status(200).json({ data: updateFolderData, message: 'updated' })
} catch (error) {
next(error)
}
}

deleteFolder = async (req, res, next) => {
try {
const folderId = Number(req.params.id)
const deleteFolderData = await this.folder.deleteFolder(folderId)

res.status(200).json({ data: deleteFolderData, message: 'deleted' })
} catch (error) {
next(error)
}
}
}
62 changes: 0 additions & 62 deletions be/src/controllers/users.controller.js

This file was deleted.

29 changes: 29 additions & 0 deletions be/src/routes/file.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Router } from 'express'
import { FilesController } from '../controllers/files.controller.js'

const router = Router()

export class FileRoute {
get path() {
return '/files'
}
get router() {
return router
}

get file() {
return new FilesController()
}

constructor() {
this.initializeRoutes()
}

async initializeRoutes() {
this.router.get(`${this.path}`, await this.file.getFiles)
this.router.get(`${this.path}/:id(\\d+)`, this.file.getFileById)
this.router.post(`${this.path}`, this.file.createFile)
this.router.put(`${this.path}/:id(\\d+)`, this.file.updateFile)
this.router.delete(`${this.path}/:id(\\d+)`, this.file.deleteFile)
}
}
29 changes: 29 additions & 0 deletions be/src/routes/folder.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Router } from 'express'
import { FoldersController } from '../controllers/folders.controller.js'

const router = Router()

export class FolderRoute {
get path() {
return '/folders'
}
get router() {
return router
}

get folder() {
return new FoldersController()
}

constructor() {
this.initializeRoutes()
}

async initializeRoutes() {
this.router.get(`${this.path}`, await this.folder.getFolders)
this.router.get(`${this.path}/:id(\\d+)`, this.folder.getFolderById)
this.router.post(`${this.path}`, this.folder.createFolder)
this.router.put(`${this.path}/:id(\\d+)`, this.folder.updateFolder)
this.router.delete(`${this.path}/:id(\\d+)`, this.folder.deleteFolder)
}
}
91 changes: 0 additions & 91 deletions be/src/routes/user.routes.js

This file was deleted.

Loading

0 comments on commit f58b714

Please sign in to comment.