Skip to content

Commit

Permalink
Add image (#58)
Browse files Browse the repository at this point in the history
* Answer insert function

* Comments added

* Merge conflict push 11/12/23
Tags changes

* Tags Changes

* Tags filter

* list
view
create
update
delete

for Events

* About cms update

* Abou Gallery add image
  • Loading branch information
Xaalf104 authored Dec 27, 2023
1 parent 583c2e7 commit aea4b66
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion draft_sql/cms.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ CREATE TABLE about_gallery(
imagesrc TEXT,
createdAt timestamp DEFAULT CURRENT_TIMESTAMP,
updatedAt timestamp DEFAULT CURRENT_TIMESTAMP,
cms_id INT,
cms_id INT NOT NULL,
FOREIGN KEY (cms_id) REFERENCES about_cms(id) ON DELETE CASCADE
)
12 changes: 12 additions & 0 deletions src/modules/About/AboutController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ export async function updateAbout(req: SessionRequest, res: Response) {
errorHandler(res, error)
}
}

export async function addImage(req: SessionRequest, res: Response) {
try {
const { body } = await zParse(Schema.AddImage, req)
const file = req.file

const addImage = await Interactor.addImage(body, file)
res.status(200).json({ message: 'Image successfully added', addImage })
} catch (error) {
errorHandler(res, error)
}
}
29 changes: 28 additions & 1 deletion src/modules/About/AboutInteractor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import HttpError from '../../utils/HttpError'
import { UpdateAbout } from '../../types/DBTypes'
import { AddImage, UpdateAbout } from '../../types/DBTypes'
import * as Service from './AboutService'
import fs from 'fs'
import { readFileAsStream } from '../../utils/file'
import {
deleteFile,
getObjectUrl,
uploadFile,
} from '../AWS-Bucket/UploadService'
import dbErrorHandler from '../../utils/dbErrorHandler'
import { object } from 'zod'

export async function updateAbout(body: UpdateAbout) {
const update = await Service.updateAboutPage(body)

if (!update) throw new HttpError('Missing or not found, Check syntax', 400)
return update
}

export async function addImage(body: AddImage, image: Express.Multer.File) {
try {
const fileKey = image.filename
const stream: fs.ReadStream = await readFileAsStream(image.path)
await uploadFile(stream, fileKey, image.mimetype)
const data = { ...body, imagesrc: image.filename }
const addImage = await Service.addImage(data)

deleteFile(image.filename)

if (!addImage) throw new HttpError('Failed: Empty or not found', 400)
return { ...addImage, imagesrc: getObjectUrl(fileKey) }
} catch (error) {
deleteFile(image.filename)
dbErrorHandler(error)
}
}
6 changes: 6 additions & 0 deletions src/modules/About/AboutRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as AboutController from './AboutController'
import express from 'express'
import { UserGuard } from '../AuthGuard/UserGuard'
import upload from '../../config/multer'

export const AboutRouter = express.Router()

AboutRouter.put('/', AboutController.updateAbout)
AboutRouter.post(
'/about_gallery',
upload.single('imagesrc'),
AboutController.addImage
)
11 changes: 9 additions & 2 deletions src/modules/About/AboutService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { UpdateAbout } from '../../types/DBTypes'
import { AddImage, Gallery, UpdateAbout } from '../../types/DBTypes'
import { db } from '../../config/database'

export async function updateAboutPage(update: UpdateAbout) {
console.log(update)
return await db
.updateTable('about_cms')
.set(update)
.returningAll()
.executeTakeFirst()
}

export async function addImage(addImage: Gallery): Promise<Gallery> {
return await db
.insertInto('about_gallery')
.values(addImage)
.returningAll()
.executeTakeFirst()
}
9 changes: 9 additions & 0 deletions src/schema/AboutSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ export const UpdateAbout = z.object({
president_message: z.string().optional(),
}),
})

export const AddImage = z.object({
body: z.object({
cms_id: z.string({ required_error: 'id required' }),
}),
file: z.object({
filename: z.string({ required_error: 'filname required' }),
}),
})
3 changes: 3 additions & 0 deletions src/types/DBTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Crops,
CropReports,
AnswerVotes,
AboutGallery,
} from 'kysely-codegen'

export type User = Selectable<Users>
Expand All @@ -38,6 +39,8 @@ export type UserTag = Insertable<UserTags>

//About
export type UpdateAbout = Updateable<AboutCms>
export type AddImage = Insertable<AboutCms>
export type Gallery = Insertable<AboutGallery>

//events
export type Events = Selectable<CommunityEvents>
Expand Down

0 comments on commit aea4b66

Please sign in to comment.