forked from hngprojects/hng_boilerplate_expressjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hngprojects#333 from Ibrahim4Grace/feat/contact-us
feat: contact us
- Loading branch information
Showing
14 changed files
with
423 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { Request, Response } from "express"; | ||
import { ContactService } from "../services/contactService"; | ||
import { validateContact } from "../utils/contactValidator"; | ||
|
||
const contactService = new ContactService(); | ||
|
||
/** | ||
* @swagger | ||
* /api/contact: | ||
* post: | ||
* summary: Submit a contact form | ||
* description: Allows users to submit their contact details and message. | ||
* tags: | ||
* - Contact | ||
* requestBody: | ||
* description: Contact details and message | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* name: | ||
* type: string | ||
* example: John Doe | ||
* email: | ||
* type: string | ||
* format: email | ||
* example: [email protected] | ||
* phoneNumber: | ||
* type: string | ||
* example: 1234567890 | ||
* message: | ||
* type: string | ||
* example: I would like to inquire about your services. | ||
* required: | ||
* - name | ||
* - phoneNumber | ||
* - message | ||
* responses: | ||
* 201: | ||
* description: Contact submitted successfully | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* message: | ||
* type: string | ||
* example: Contact submitted successfully | ||
* contact: | ||
* type: object | ||
* properties: | ||
* id: | ||
* type: integer | ||
* example: 1 | ||
* name: | ||
* type: string | ||
* example: John Doe | ||
* email: | ||
* type: string | ||
* example: [email protected] | ||
* phoneNumber: | ||
* type: string | ||
* example: 1234567890 | ||
* message: | ||
* type: string | ||
* example: I would like to inquire about your services. | ||
* 400: | ||
* description: Bad request, validation failed | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* errors: | ||
* type: array | ||
* items: | ||
* type: string | ||
* example: [ "Please enter a valid email address." ] | ||
* 500: | ||
* description: Internal server error | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* error: | ||
* type: string | ||
* example: Internal server error | ||
*/ | ||
|
||
export class ContactController { | ||
async createContact(req: Request, res: Response): Promise<void> { | ||
const { name, email, phoneNumber, message } = req.body; | ||
|
||
const validationErrors = validateContact({ | ||
name, | ||
email, | ||
phoneNumber, | ||
message, | ||
}); | ||
if (validationErrors.length > 0) { | ||
res.status(400).json({ errors: validationErrors }); | ||
return; | ||
} | ||
|
||
try { | ||
const contact = await contactService.createContact({ | ||
name, | ||
email, | ||
phoneNumber, | ||
message, | ||
}); | ||
res | ||
.status(201) | ||
.json({ message: "Message submitted successfully", contact }); | ||
} catch (error) { | ||
res.status(500).json({ error: "Internal server error" }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"; | ||
|
||
@Entity() | ||
export class Contact { | ||
@PrimaryGeneratedColumn() | ||
id!: number; | ||
|
||
@Column({ type: "varchar", length: 100 }) | ||
name!: string; | ||
|
||
@Column({ type: "varchar", length: 100 }) | ||
email!: string; | ||
|
||
@Column({ type: "varchar", length: 20 }) | ||
phoneNumber!: number; | ||
|
||
@Column({ type: "text" }) | ||
message!: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Router } from "express"; | ||
import { ContactController } from "../controllers/contactController"; | ||
|
||
const contactRouter = Router(); | ||
const contactController = new ContactController(); | ||
|
||
contactRouter.post( | ||
"/contact-us", | ||
contactController.createContact.bind(contactController), | ||
); | ||
|
||
export { contactRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import AppDataSource from "../data-source"; | ||
import { Contact } from "../models/contact-us"; | ||
|
||
export class ContactService { | ||
private contactRepository = AppDataSource.getRepository(Contact); | ||
|
||
async createContact(contactData: Partial<Contact>): Promise<Contact> { | ||
const contact = this.contactRepository.create(contactData); | ||
return this.contactRepository.save(contact); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.