Skip to content

Commit

Permalink
Merge pull request #1 from makayi/refactor-suggestions
Browse files Browse the repository at this point in the history
refactor: added some code examples to make it more clean
  • Loading branch information
Mulubwa17 committed May 4, 2022
2 parents 33a7057 + 252e8a0 commit 942e6ae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/config/db.ts
Expand Up @@ -2,9 +2,9 @@ import dotenv from 'dotenv';
import mongoose from "mongoose";
dotenv.config();

const uriA: string = 'mongodb://localhost:27017/admin_control';
const MONGO_DB_URI: string = 'mongodb://localhost:27017/admin_control';

export const adminDB = () => {
export const adminDb = () => {
mongoose.connect(uriA, (err: any) => {
if (err) {
console.log(err.message);
Expand All @@ -17,12 +17,16 @@ mongoose.connect(uriA, (err: any) => {
const uriC: string = 'mongodb://localhost:27017/customer';

export const customerDB = () => {
mongoose.connect(uriC, (err: any) => {
if (err) {
mongoose.connect(uriC, (error: any) => {
if (error) {
console.log(err.message);
} else {
console.log("Customer DB Successfully Connected!");
}

if(error) return console.log()

console.log(sucesss)
});
}

Expand Down
@@ -1,27 +1,27 @@
import { Request, Response, NextFunction } from "express";
import { nextTick } from "process";
import { Admin } from "../models/admin";
import { Admin, getAdmins, getAdminsByEmail, insertAdmin } from "../../models/admin";
import { NewAdminParams, NewAdminResponse, StatusCodes } from "./types";

export const createAdmin = async (req: Request, res: Response) => {

export const createAdmin = async (newAdaminParams: NewAdminParams ):Promise<NewAdminResponse> => {
try {
const { firstName, lastName, email, contact } = req.body;
const admin = await Admin.findOne({ email });
if (admin) {
return res.status(404).json({ error: "Admin already exists!" });
const {email} = newAdaminParams
const admins = await getAdminsByEmail(email)
if (admins) {
return { message:'Not found', status:StatusCodes.ERROR}
}
const newAdmin = new Admin({ firstName, lastName, email, contact });

await newAdmin.save();
res.status(200).json({ data: newAdmin, message: "Admin created" });
const newAdmin = await insertAdmin(newAdaminParams)
return { message:'Created', status:StatusCodes.SUCESSS, data:newAdmin}
console.log(newAdmin);
} catch (error) {
console.log(error);
res.status(500).json({ error: "something went wrong" });
return { message:'Something went wrong ', status:StatusCodes.ERROR}
}
};

export const getAdmins = async (req: Request, res: Response) => {
const admins = await Admin.find({})
const admins = await getAdminsByEmail()

res.status(200).send(admins);
}
Expand Down
16 changes: 16 additions & 0 deletions src/controllers/admin/types.ts
@@ -0,0 +1,16 @@
export interface NewAdminParams {
firstName:string;
email:string,
lastName:string,
contact:string
}

export enum StatusCodes{
SUCESSS='SUCCESS',
ERROR='ERROR'
}
export interface NewAdminResponse{
status: StatusCodes,
message: string,
data?: any
}
10 changes: 10 additions & 0 deletions src/models/admin.ts
@@ -1,5 +1,6 @@
// admin.model.ts
import { Schema, model } from "mongoose";
import { NewAdminParams } from "../controllers/admin/types";

// Create the interface
interface IAdmin {
Expand Down Expand Up @@ -49,3 +50,12 @@ const AdminSchema = new Schema<IAdmin>(

// Create and export admin model
export const Admin = model<IAdmin>("Admin", AdminSchema);

export const getAdminsByEmail= async(email:string):Promise<IAdmin>=>{
return Admin.findOne({email})
}

export const insertAdmin =async (params:NewAdminParams) => {
const newAdmin = new Admin({ ...params});
return newAdmin.save();
}
13 changes: 10 additions & 3 deletions src/routes/admin.ts
@@ -1,10 +1,17 @@
import express from 'express';
import { createAdmin, deleteAdmin, getAdmin, getAdmins, updateAdmin } from '../controllers/adminController';
import express,{ Request, Response} from 'express';
import { createAdmin, deleteAdmin, getAdmin, getAdmins, updateAdmin } from '../controllers/admin/adminController';
// import { isCached, isCached1 } from '../middlewares/cache';

const router = express.Router();

router.post('/new_admin', createAdmin)
router.post('/new_admin', async (request:Request, response: Response)=>{
try {
const { firstName, lastName, contact, email}= request.body
const response= await createAdmin({firstName,lastName,contact,email})
} catch (error) {

}
})

router.get('/view_admins', getAdmins)

Expand Down

0 comments on commit 942e6ae

Please sign in to comment.